Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor cask metadata directory methods. #2514

Merged
merged 1 commit into from
Apr 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 4 additions & 48 deletions Library/Homebrew/cask/lib/hbc/cask.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require "forwardable"

require "hbc/dsl"
require "hbc/metadata"

module Hbc
class Cask
extend Forwardable
include Metadata

attr_reader :token, :sourcefile_path
def initialize(token, sourcefile_path: nil, &block)
Expand All @@ -20,55 +22,9 @@ def initialize(token, sourcefile_path: nil, &block)
define_method(method_name) { @dsl.send(method_name) }
end

METADATA_SUBDIR = ".metadata".freeze

def metadata_master_container_path
@metadata_master_container_path ||= caskroom_path.join(METADATA_SUBDIR)
end

def metadata_versioned_container_path
cask_version = version ? version : :unknown
metadata_master_container_path.join(cask_version.to_s)
end

def metadata_path(timestamp = :latest, create = false)
if create && timestamp == :latest
raise CaskError, "Cannot create metadata path when timestamp is :latest"
end
path = if timestamp == :latest
Pathname.glob(metadata_versioned_container_path.join("*")).sort.last
elsif timestamp == :now
Utils.nowstamp_metadata_path(metadata_versioned_container_path)
else
metadata_versioned_container_path.join(timestamp)
end
if create
odebug "Creating metadata directory #{path}"
FileUtils.mkdir_p path
end
path
end

def metadata_subdir(leaf, timestamp = :latest, create = false)
if create && timestamp == :latest
raise CaskError, "Cannot create metadata subdir when timestamp is :latest"
end
unless leaf.respond_to?(:length) && !leaf.empty?
raise CaskError, "Cannot create metadata subdir for empty leaf"
end
parent = metadata_path(timestamp, create)
return nil unless parent.respond_to?(:join)
subdir = parent.join(leaf)
if create
odebug "Creating metadata subdirectory #{subdir}"
FileUtils.mkdir_p subdir
end
subdir
end

def timestamped_versions
Pathname.glob(metadata_master_container_path.join("*", "*"))
.map { |p| p.relative_path_from(metadata_master_container_path) }
Pathname.glob(metadata_timestamped_path(version: "*", timestamp: "*"))
.map { |p| p.relative_path_from(p.parent.parent) }
.sort_by(&:basename) # sort by timestamp
.map { |p| p.split.map(&:to_s) }
end
Expand Down
10 changes: 2 additions & 8 deletions Library/Homebrew/cask/lib/hbc/cli/uninstall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,9 @@ def self.run(*args)

raise CaskNotInstalledError, cask unless cask.installed? || force

latest_installed_version = cask.timestamped_versions.last

unless latest_installed_version.nil?
latest_installed_cask_file = cask.metadata_master_container_path
.join(latest_installed_version.join(File::Separator),
"Casks", "#{cask_token}.rb")

if cask.installed? && !cask.installed_caskfile.nil?
# use the same cask file that was used for installation, if possible
cask = CaskLoader.load_from_file(latest_installed_cask_file) if latest_installed_cask_file.exist?
cask = CaskLoader.load_from_file(cask.installed_caskfile) if cask.installed_caskfile.exist?
end

Installer.new(cask, force: force).uninstall
Expand Down
16 changes: 7 additions & 9 deletions Library/Homebrew/cask/lib/hbc/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,13 @@ def disable_accessibility_access
end

def save_caskfile
unless (old_savedirs = Pathname.glob(@cask.metadata_path("*"))).empty?
old_savedirs.each(&:rmtree)
end
old_savedir = @cask.metadata_timestamped_path

return unless @cask.sourcefile_path

savedir = @cask.metadata_subdir("Casks", :now, true)
savedir.mkpath
savedir = @cask.metadata_subdir("Casks", timestamp: :now, create: true)
FileUtils.copy @cask.sourcefile_path, savedir
old_savedir.rmtree unless old_savedir.nil?
end

def uninstall
Expand Down Expand Up @@ -355,15 +353,15 @@ def purge_versioned_files
gain_permissions_remove(@cask.staged_path) if !@cask.staged_path.nil? && @cask.staged_path.exist?

# Homebrew-Cask metadata
if @cask.metadata_versioned_container_path.respond_to?(:children) &&
@cask.metadata_versioned_container_path.exist?
@cask.metadata_versioned_container_path.children.each do |subdir|
if @cask.metadata_versioned_path.respond_to?(:children) &&
@cask.metadata_versioned_path.exist?
@cask.metadata_versioned_path.children.each do |subdir|
unless PERSISTENT_METADATA_SUBDIRS.include?(subdir.basename)
gain_permissions_remove(subdir)
end
end
end
@cask.metadata_versioned_container_path.rmdir_if_possible
@cask.metadata_versioned_path.rmdir_if_possible
@cask.metadata_master_container_path.rmdir_if_possible

# toplevel staged distribution
Expand Down
73 changes: 73 additions & 0 deletions Library/Homebrew/cask/lib/hbc/metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
module Hbc
module Metadata
METADATA_SUBDIR = ".metadata".freeze

def metadata_master_container_path
@metadata_master_container_path ||= caskroom_path.join(METADATA_SUBDIR)
end

def metadata_versioned_path(version: self.version)
cask_version = (version || :unknown).to_s

if cask_version.empty?
raise CaskError, "Cannot create metadata path with empty version."
end

metadata_master_container_path.join(cask_version)
end

def metadata_timestamped_path(version: self.version, timestamp: :latest, create: false)
if create && timestamp == :latest
raise CaskError, "Cannot create metadata path when timestamp is :latest."
end

path = if timestamp == :latest
Pathname.glob(metadata_versioned_path(version: version).join("*")).sort.last
else
timestamp = new_timestamp if timestamp == :now
metadata_versioned_path(version: version).join(timestamp)
end

if create && !path.directory?
odebug "Creating metadata directory #{path}."
path.mkpath
end

path
end

def metadata_subdir(leaf, version: self.version, timestamp: :latest, create: false)
if create && timestamp == :latest
raise CaskError, "Cannot create metadata subdir when timestamp is :latest."
end

unless leaf.respond_to?(:empty?) && !leaf.empty?
raise CaskError, "Cannot create metadata subdir for empty leaf."
end

parent = metadata_timestamped_path(version: version, timestamp: timestamp, create: create)

return nil if parent.nil?

subdir = parent.join(leaf)

if create && !subdir.directory?
odebug "Creating metadata subdirectory #{subdir}."
subdir.mkpath
end

subdir
end

private

def new_timestamp(time = Time.now)
time = time.utc

timestamp = time.strftime("%Y%m%d%H%M%S")
fraction = format("%.3f", time.to_f - time.to_i)[1..-1]

timestamp.concat(fraction)
end
end
end
11 changes: 0 additions & 11 deletions Library/Homebrew/cask/lib/hbc/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,5 @@ def self.method_missing_message(method, token, section = nil)

opoo(poo.join(" ") + "\n" + error_message_with_suggestions)
end

def self.nowstamp_metadata_path(container_path)
@timenow ||= Time.now.gmtime
return unless container_path.respond_to?(:join)

precision = 3
timestamp = @timenow.strftime("%Y%m%d%H%M%S")
fraction = format("%.#{precision}f", @timenow.to_f - @timenow.to_i)[1..-1]
timestamp.concat(fraction)
container_path.join(timestamp)
end
end
end
4 changes: 2 additions & 2 deletions Library/Homebrew/test/cask/cask_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@
it "proposes a versioned metadata directory name for each instance" do
cask_token = "local-caffeine"
c = Hbc::CaskLoader.load(cask_token)
metadata_path = Hbc.caskroom.join(cask_token, ".metadata", c.version)
expect(c.metadata_versioned_container_path.to_s).to eq(metadata_path.to_s)
metadata_timestamped_path = Hbc.caskroom.join(cask_token, ".metadata", c.version)
expect(c.metadata_versioned_path.to_s).to eq(metadata_timestamped_path.to_s)
end
end

Expand Down
10 changes: 4 additions & 6 deletions Library/Homebrew/test/cask/installer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,8 @@
Hbc::Installer.new(caffeine).install
end

m_path = caffeine.metadata_path(:now, true)
expect(caffeine.metadata_path(:now, false)).to eq(m_path)
expect(caffeine.metadata_path(:latest)).to eq(m_path)
m_path = caffeine.metadata_timestamped_path(timestamp: :now, create: true)
expect(caffeine.metadata_timestamped_path(timestamp: :latest)).to eq(m_path)
end

it "generates and finds a metadata subdirectory for an installed Cask" do
Expand All @@ -349,9 +348,8 @@
end

subdir_name = "Casks"
m_subdir = caffeine.metadata_subdir(subdir_name, :now, true)
expect(caffeine.metadata_subdir(subdir_name, :now, false)).to eq(m_subdir)
expect(caffeine.metadata_subdir(subdir_name, :latest)).to eq(m_subdir)
m_subdir = caffeine.metadata_subdir(subdir_name, timestamp: :now, create: true)
expect(caffeine.metadata_subdir(subdir_name, timestamp: :latest)).to eq(m_subdir)
end
end

Expand Down