Skip to content

Commit

Permalink
Add path method to Caskroom and Cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Jun 9, 2018
1 parent 458a1e7 commit 3d423b0
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 73 deletions.
10 changes: 7 additions & 3 deletions Library/Homebrew/cask/lib/hbc/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ module Hbc
module Cache
module_function

def path
@path ||= HOMEBREW_CACHE.join("Cask")
end

def ensure_cache_exists
return if Hbc.cache.exist?
return if path.exist?

odebug "Creating Cache at #{Hbc.cache}"
Hbc.cache.mkpath
odebug "Creating Cache at #{path}"
path.mkpath
end
end
end
2 changes: 1 addition & 1 deletion Library/Homebrew/cask/lib/hbc/cask_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def self.can_load?(ref)

def initialize(url)
@url = URI(url)
super Hbc.cache/File.basename(@url.path)
super Cache.path/File.basename(@url.path)
end

def load
Expand Down
20 changes: 12 additions & 8 deletions Library/Homebrew/cask/lib/hbc/caskroom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@ module Hbc
module Caskroom
module_function

def path
@path ||= HOMEBREW_PREFIX.join("Caskroom")
end

def ensure_caskroom_exists
return if Hbc.caskroom.exist?
return if path.exist?

ohai "Creating Caskroom at #{Hbc.caskroom}" if $stdout.tty?
sudo = !Hbc.caskroom.parent.writable?
ohai "Creating Caskroom at #{path}" if $stdout.tty?
sudo = !path.parent.writable?

ohai "We'll set permissions properly so we won't need sudo in the future" if $stdout.tty? && sudo

SystemCommand.run("/bin/mkdir", args: ["-p", Hbc.caskroom], sudo: sudo)
SystemCommand.run("/bin/chmod", args: ["g+rwx", Hbc.caskroom], sudo: sudo)
SystemCommand.run("/usr/sbin/chown", args: [Utils.current_user, Hbc.caskroom], sudo: sudo)
SystemCommand.run("/usr/bin/chgrp", args: ["admin", Hbc.caskroom], sudo: sudo)
SystemCommand.run("/bin/mkdir", args: ["-p", path], sudo: sudo)
SystemCommand.run("/bin/chmod", args: ["g+rwx", path], sudo: sudo)
SystemCommand.run("/usr/sbin/chown", args: [Utils.current_user, path], sudo: sudo)
SystemCommand.run("/usr/bin/chgrp", args: ["admin", path], sudo: sudo)
end

def casks
Pathname.glob(Hbc.caskroom.join("*")).sort.select(&:directory?).map do |path|
Pathname.glob(path.join("*")).sort.select(&:directory?).map do |path|
token = path.basename.to_s

if tap_path = CaskLoader.tap_paths(token).first
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cask/lib/hbc/cli/cleanup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.needs_init?

attr_reader :cache_location

def initialize(*args, cache_location: Hbc.cache)
def initialize(*args, cache_location: Cache.path)
super(*args)
@cache_location = Pathname.new(cache_location)
end
Expand Down
6 changes: 3 additions & 3 deletions Library/Homebrew/cask/lib/hbc/cli/doctor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def check_install_location
def check_staging_location
ohai "Homebrew-Cask Staging Location"

path = Pathname.new(user_tilde(Hbc.caskroom.to_s))
path = Pathname.new(user_tilde(Caskroom.path.to_s))

if !path.exist?
add_error "The staging path #{path} does not exist."
Expand All @@ -77,7 +77,7 @@ def check_cached_downloads
cleanup = CLI::Cleanup.new
count = cleanup.cache_files.count
size = cleanup.disk_cleanup_size
msg = user_tilde(Hbc.cache.to_s)
msg = user_tilde(Cache.path.to_s)
msg << " (#{number_readable(count)} files, #{disk_usage_readable(size)})" unless count.zero?
puts msg
end
Expand Down Expand Up @@ -240,7 +240,7 @@ def self.render_cached_downloads
cleanup = CLI::Cleanup.new
count = cleanup.cache_files.count
size = cleanup.disk_cleanup_size
msg = user_tilde(Hbc.cache.to_s)
msg = user_tilde(Cache.path.to_s)
msg << " (#{number_readable(count)} files, #{disk_usage_readable(size)})" unless count.zero?
msg
end
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/cask/lib/hbc/download_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class HbVCSDownloadStrategy < AbstractDownloadStrategy
def initialize(*args, **options)
super(*args, **options)
@ref_type, @ref = extract_ref
@clone = Hbc.cache.join(cache_filename)
@clone = Cache.path.join(cache_filename)
end

def extract_ref
Expand Down Expand Up @@ -65,7 +65,7 @@ def clear_cache

class CurlDownloadStrategy < AbstractDownloadStrategy
def tarball_path
@tarball_path ||= Hbc.cache.join("#{name}--#{version}#{ext}")
@tarball_path ||= Cache.path.join("#{name}--#{version}#{ext}")
end

def temporary_path
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cask/lib/hbc/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def artifacts
end

def caskroom_path
@caskroom_path ||= Hbc.caskroom.join(token)
@caskroom_path ||= Caskroom.path.join(token)
end

def staged_path
Expand Down
8 changes: 0 additions & 8 deletions Library/Homebrew/cask/lib/hbc/locations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ def self.included(base)
end

module ClassMethods
def caskroom

This comment has been minimized.

Copy link
@agross

agross Jun 9, 2018

Removing this method caused this: buo/homebrew-cask-upgrade#95

@caskroom ||= HOMEBREW_PREFIX.join("Caskroom")
end

def cache
@cache ||= HOMEBREW_CACHE.join("Cask")
end

attr_writer :default_tap

def default_tap
Expand Down
44 changes: 22 additions & 22 deletions Library/Homebrew/compat/hbc/caskroom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,42 @@ module Caskroom
class << self
module Compat
def migrate_legacy_caskroom
return if Hbc.caskroom.exist?
return if path.exist?

legacy_caskroom = Pathname.new("/opt/homebrew-cask/Caskroom")
return if Hbc.caskroom == legacy_caskroom
return unless legacy_caskroom.exist?
return if legacy_caskroom.symlink?
legacy_caskroom_path = Pathname.new("/opt/homebrew-cask/Caskroom")
return if path == legacy_caskroom_path
return unless legacy_caskroom_path.exist?
return if legacy_caskroom_path.symlink?

ohai "Migrating Caskroom from #{legacy_caskroom} to #{Hbc.caskroom}."
if Hbc.caskroom.parent.writable?
FileUtils.mv legacy_caskroom, Hbc.caskroom
ohai "Migrating Caskroom from #{legacy_caskroom_path} to #{path}."
if path.parent.writable?
FileUtils.mv legacy_caskroom_path, path
else
opoo "#{Hbc.caskroom.parent} is not writable, sudo is needed to move the Caskroom."
SystemCommand.run("/bin/mv", args: [legacy_caskroom, Hbc.caskroom.parent], sudo: true)
opoo "#{path.parent} is not writable, sudo is needed to move the Caskroom."
SystemCommand.run("/bin/mv", args: [legacy_caskroom_path, path.parent], sudo: true)
end

ohai "Creating symlink from #{Hbc.caskroom} to #{legacy_caskroom}."
if legacy_caskroom.parent.writable?
FileUtils.ln_s Hbc.caskroom, legacy_caskroom
ohai "Creating symlink from #{path} to #{legacy_caskroom_path}."
if legacy_caskroom_path.parent.writable?
FileUtils.ln_s path, legacy_caskroom_path
else
opoo "#{legacy_caskroom.parent} is not writable, sudo is needed to link the Caskroom."
SystemCommand.run("/bin/ln", args: ["-s", Hbc.caskroom, legacy_caskroom], sudo: true)
opoo "#{legacy_caskroom_path.parent} is not writable, sudo is needed to link the Caskroom."
SystemCommand.run("/bin/ln", args: ["-s", path, legacy_caskroom_path], sudo: true)
end
end

def migrate_caskroom_from_repo_to_prefix
repo_caskroom = HOMEBREW_REPOSITORY.join("Caskroom")
return if Hbc.caskroom.exist?
return unless repo_caskroom.directory?
repo_caskroom_path = HOMEBREW_REPOSITORY.join("Caskroom")
return if path.exist?
return unless repo_caskroom_path.directory?

ohai "Moving Caskroom from HOMEBREW_REPOSITORY to HOMEBREW_PREFIX"

if Hbc.caskroom.parent.writable?
FileUtils.mv repo_caskroom, Hbc.caskroom
if path.parent.writable?
FileUtils.mv repo_caskroom_path, path
else
opoo "#{Hbc.caskroom.parent} is not writable, sudo is needed to move the Caskroom."
SystemCommand.run("/bin/mv", args: [repo_caskroom, Hbc.caskroom.parent], sudo: true)
opoo "#{path.parent} is not writable, sudo is needed to move the Caskroom."
SystemCommand.run("/bin/mv", args: [repo_caskroom_path, path.parent], sudo: true)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/test/cask/cask_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
it "proposes a versioned metadata directory name for each instance" do
cask_token = "local-caffeine"
c = Hbc::CaskLoader.load(cask_token)
metadata_timestamped_path = Hbc.caskroom.join(cask_token, ".metadata", c.version)
metadata_timestamped_path = Hbc::Caskroom.path.join(cask_token, ".metadata", c.version)
expect(c.metadata_versioned_path.to_s).to eq(metadata_timestamped_path.to_s)
end
end
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/test/cask/cli/uninstall_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
[last_installed_version, "456000"],
]
}
let(:caskroom_path) { Hbc.caskroom.join(token).tap(&:mkpath) }
let(:caskroom_path) { Hbc::Caskroom.path.join(token).tap(&:mkpath) }

before do
timestamped_versions.each do |timestamped_version|
Expand Down Expand Up @@ -143,7 +143,7 @@

describe "when Casks in Taps have been renamed or removed" do
let(:app) { Hbc::Config.global.appdir.join("ive-been-renamed.app") }
let(:caskroom_path) { Hbc.caskroom.join("ive-been-renamed").tap(&:mkpath) }
let(:caskroom_path) { Hbc::Caskroom.path.join("ive-been-renamed").tap(&:mkpath) }
let(:saved_caskfile) { caskroom_path.join(".metadata", "latest", "timestamp", "Casks").join("ive-been-renamed.rb") }

before do
Expand Down
38 changes: 19 additions & 19 deletions Library/Homebrew/test/cask/installer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

Hbc::Installer.new(caffeine).install

expect(Hbc.caskroom.join("local-caffeine", caffeine.version)).to be_a_directory
expect(Hbc::Caskroom.path.join("local-caffeine", caffeine.version)).to be_a_directory
expect(Hbc::Config.global.appdir.join("Caffeine.app")).to be_a_directory
end

Expand All @@ -18,7 +18,7 @@

Hbc::Installer.new(asset).install

expect(Hbc.caskroom.join("container-dmg", asset.version)).to be_a_directory
expect(Hbc::Caskroom.path.join("container-dmg", asset.version)).to be_a_directory
expect(Hbc::Config.global.appdir.join("container")).to be_a_file
end

Expand All @@ -27,7 +27,7 @@

Hbc::Installer.new(asset).install

expect(Hbc.caskroom.join("container-tar-gz", asset.version)).to be_a_directory
expect(Hbc::Caskroom.path.join("container-tar-gz", asset.version)).to be_a_directory
expect(Hbc::Config.global.appdir.join("container")).to be_a_file
end

Expand All @@ -36,7 +36,7 @@

Hbc::Installer.new(asset).install

expect(Hbc.caskroom.join("container-xar", asset.version)).to be_a_directory
expect(Hbc::Caskroom.path.join("container-xar", asset.version)).to be_a_directory
expect(Hbc::Config.global.appdir.join("container")).to be_a_file
end

Expand All @@ -45,7 +45,7 @@

Hbc::Installer.new(asset).install

expect(Hbc.caskroom.join("container-bzip2", asset.version)).to be_a_directory
expect(Hbc::Caskroom.path.join("container-bzip2", asset.version)).to be_a_directory
expect(Hbc::Config.global.appdir.join("container-bzip2--#{asset.version}")).to be_a_file
end

Expand All @@ -54,7 +54,7 @@

Hbc::Installer.new(asset).install

expect(Hbc.caskroom.join("container-gzip", asset.version)).to be_a_directory
expect(Hbc::Caskroom.path.join("container-gzip", asset.version)).to be_a_directory
expect(Hbc::Config.global.appdir.join("container")).to be_a_file
end

Expand Down Expand Up @@ -167,15 +167,15 @@

Hbc::Installer.new(naked_pkg).install

expect(Hbc.caskroom.join("container-pkg", naked_pkg.version, "container.pkg")).to be_a_file
expect(Hbc::Caskroom.path.join("container-pkg", naked_pkg.version, "container.pkg")).to be_a_file
end

it "works properly with an overridden container :type" do
naked_executable = Hbc::CaskLoader.load(cask_path("naked-executable"))

Hbc::Installer.new(naked_executable).install

expect(Hbc.caskroom.join("naked-executable", naked_executable.version, "naked_executable")).to be_a_file
expect(Hbc::Caskroom.path.join("naked-executable", naked_executable.version, "naked_executable")).to be_a_file
end

it "works fine with a nested container" do
Expand Down Expand Up @@ -214,9 +214,9 @@
installer.install
installer.uninstall

expect(Hbc.caskroom.join("local-caffeine", caffeine.version, "Caffeine.app")).not_to be_a_directory
expect(Hbc.caskroom.join("local-caffeine", caffeine.version)).not_to be_a_directory
expect(Hbc.caskroom.join("local-caffeine")).not_to be_a_directory
expect(Hbc::Caskroom.path.join("local-caffeine", caffeine.version, "Caffeine.app")).not_to be_a_directory
expect(Hbc::Caskroom.path.join("local-caffeine", caffeine.version)).not_to be_a_directory
expect(Hbc::Caskroom.path.join("local-caffeine")).not_to be_a_directory
end

it "uninstalls all versions if force is set" do
Expand All @@ -225,17 +225,17 @@

Hbc::Installer.new(caffeine).install

expect(Hbc.caskroom.join("local-caffeine", caffeine.version)).to be_a_directory
expect(Hbc.caskroom.join("local-caffeine", mutated_version)).not_to be_a_directory
FileUtils.mv(Hbc.caskroom.join("local-caffeine", caffeine.version), Hbc.caskroom.join("local-caffeine", mutated_version))
expect(Hbc.caskroom.join("local-caffeine", caffeine.version)).not_to be_a_directory
expect(Hbc.caskroom.join("local-caffeine", mutated_version)).to be_a_directory
expect(Hbc::Caskroom.path.join("local-caffeine", caffeine.version)).to be_a_directory
expect(Hbc::Caskroom.path.join("local-caffeine", mutated_version)).not_to be_a_directory
FileUtils.mv(Hbc::Caskroom.path.join("local-caffeine", caffeine.version), Hbc::Caskroom.path.join("local-caffeine", mutated_version))
expect(Hbc::Caskroom.path.join("local-caffeine", caffeine.version)).not_to be_a_directory
expect(Hbc::Caskroom.path.join("local-caffeine", mutated_version)).to be_a_directory

Hbc::Installer.new(caffeine, force: true).uninstall

expect(Hbc.caskroom.join("local-caffeine", caffeine.version)).not_to be_a_directory
expect(Hbc.caskroom.join("local-caffeine", mutated_version)).not_to be_a_directory
expect(Hbc.caskroom.join("local-caffeine")).not_to be_a_directory
expect(Hbc::Caskroom.path.join("local-caffeine", caffeine.version)).not_to be_a_directory
expect(Hbc::Caskroom.path.join("local-caffeine", mutated_version)).not_to be_a_directory
expect(Hbc::Caskroom.path.join("local-caffeine")).not_to be_a_directory
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
begin
HOMEBREW_CASK_DIRS.values.each(&:mkpath)

[Hbc::Config.global.binarydir, Hbc.caskroom, Hbc.cache].each(&:mkpath)
[Hbc::Config.global.binarydir, Hbc::Caskroom.path, Hbc::Cache.path].each(&:mkpath)

Hbc.default_tap = Tap.fetch("Homebrew", "cask-spec").tap do |tap|
FileUtils.mkdir_p tap.path.dirname
Expand All @@ -38,7 +38,7 @@
example.run
ensure
FileUtils.rm_rf HOMEBREW_CASK_DIRS.values
FileUtils.rm_rf [Hbc::Config.global.binarydir, Hbc.caskroom, Hbc.cache]
FileUtils.rm_rf [Hbc::Config.global.binarydir, Hbc::Caskroom.path, Hbc::Cache.path]
Hbc.default_tap.path.unlink
third_party_tap.path.unlink
FileUtils.rm_rf third_party_tap.path.parent
Expand Down

0 comments on commit 3d423b0

Please sign in to comment.