Skip to content

Commit

Permalink
CLI - Fetch
Browse files Browse the repository at this point in the history
Add `fetch` command to cli like suggestion at the end of Homebrew#2528
  • Loading branch information
Võ Anh Duy committed Feb 25, 2014
1 parent 297cd63 commit 33a97a1
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 1 deletion.
1 change: 1 addition & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ This will both uninstall the Cask and remove symlinks which were created in

* `info` -- displays information about the given Cask
* `list` -- with no args, lists installed Casks; given installed Casks, lists installed files
* `fetch` -- downloads Cask resources to local cache (with `--force`, re-download even if already cached)
* `doctor` -- checks for configuration issues
* `cleanup` -- cleans up cached downloads (with `--outdated`, only cleans old downloads)
* `home` -- opens the homepage of the given Cask; or with no arguments, the homebrew-cask project page
Expand Down
8 changes: 8 additions & 0 deletions doc/src/brew-cask.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ names, and other aspects of this manual are still subject to change.
* `edit` <Cask>:
Open the given Caskfile for editing.

<<<<<<< HEAD
* `home` or `homepage`:
=======
* `fetch` <Cask> [--force]:
Fetch remote resources for the given Cask to the local cache. With
`--force`, force re-download even if the resources are already cached.

* `home`:
>>>>>>> CLI - Fetch
Open the project page <http://caskroom.io> in a browser.
* `info` or `abv` <Cask>:
Expand Down
1 change: 1 addition & 0 deletions lib/cask/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Cask::CLI; end
require 'cask/cli/create'
require 'cask/cli/doctor'
require 'cask/cli/edit'
require 'cask/cli/fetch'
require 'cask/cli/home'
require 'cask/cli/info'
require 'cask/cli/install'
Expand Down
18 changes: 18 additions & 0 deletions lib/cask/cli/fetch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Cask::CLI::Fetch
def self.run(*args)
raise CaskUnspecifiedError if args.empty?
cask_names = args.reject { |a| a.chars.first == '-' }
force = args.include? '--force'

cask_names.each do |cask_name|
ohai "Fetching resources for Cask #{cask_name}"
cask = Cask.load(cask_name)
@downloaded_path = Cask::Download.new(cask).perform force
ohai "Success! Downloaded to -> #{@downloaded_path}"
end
end

def self.help
"downloads Cask resources to local cache"
end
end
3 changes: 2 additions & 1 deletion lib/cask/download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ def initialize(cask)
@cask = cask
end

def perform
def perform(force=false)
require 'software_spec'
cask = @cask
if cask.url.using == :svn
downloader = Cask::SubversionDownloadStrategy.new(cask)
else
downloader = Cask::CurlDownloadStrategy.new(cask)
end
downloader.clear_cache if force
downloaded_path = downloader.fetch
begin
# this symlink helps track which downloads are ours
Expand Down
71 changes: 71 additions & 0 deletions test/cask/cli/fetch_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require 'test_helper'

describe Cask::CLI::Fetch do
let(:local_transmission) do
Cask.load('local-transmission')
end

let(:local_caffeine) do
Cask.load('local-caffeine')
end

it "allows download the installer of a cask" do
shutup do
Cask::CLI::Fetch.run('local-transmission', 'local-caffeine')
end
Cask::CurlDownloadStrategy.new(local_transmission).cached_location.must_be :exist?
Cask::CurlDownloadStrategy.new(local_caffeine).cached_location.must_be :exist?
end

it "prevents double fetch (without nuking existing installation)" do
download_stategy = Cask::CurlDownloadStrategy.new(local_transmission)

shutup do
Cask::Download.new(local_transmission).perform
end
old_ctime = File.stat(download_stategy.cached_location).ctime

shutup do
Cask::CLI::Fetch.run('local-transmission')
end
new_ctime = File.stat(download_stategy.cached_location).ctime

old_ctime.to_i.must_equal new_ctime.to_i

end

it "allows double fetch with --force" do
shutup do
Cask::Download.new(local_transmission).perform
end

download_stategy = Cask::CurlDownloadStrategy.new(local_transmission)
old_ctime = File.stat(download_stategy.cached_location).ctime
sleep(1)

shutup do
Cask::CLI::Fetch.run('local-transmission','--force')
end
download_stategy = Cask::CurlDownloadStrategy.new(local_transmission)
new_ctime = File.stat(download_stategy.cached_location).ctime

# new_ctime.to_i.must_be :>, old_ctime.to_i
new_ctime.to_i.must_be :>, old_ctime.to_i
end

it "properly handles casks that are not present" do
lambda {
shutup do
Cask::CLI::Fetch.run('notacask')
end
}.must_raise CaskUnavailableError
end

it "raises an exception when no cask is specified" do
lambda {
shutup do
Cask::CLI::Fetch.run
end
}.must_raise CaskUnspecifiedError
end
end

0 comments on commit 33a97a1

Please sign in to comment.