From 33a97a1a5fa4ea09795cdc68d99ca00ae71d7c9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=B5=20Anh=20Duy?= Date: Sun, 16 Feb 2014 05:10:46 +0700 Subject: [PATCH] CLI - Fetch Add `fetch` command to cli like suggestion at the end of #2528 --- USAGE.md | 1 + doc/src/brew-cask.1.md | 8 +++++ lib/cask/cli.rb | 1 + lib/cask/cli/fetch.rb | 18 ++++++++++ lib/cask/download.rb | 3 +- test/cask/cli/fetch_test.rb | 71 +++++++++++++++++++++++++++++++++++++ 6 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 lib/cask/cli/fetch.rb create mode 100644 test/cask/cli/fetch_test.rb diff --git a/USAGE.md b/USAGE.md index f80011678913..da8a7dd4ccdd 100644 --- a/USAGE.md +++ b/USAGE.md @@ -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 diff --git a/doc/src/brew-cask.1.md b/doc/src/brew-cask.1.md index 50ba71b1e867..2ce48fa900fd 100644 --- a/doc/src/brew-cask.1.md +++ b/doc/src/brew-cask.1.md @@ -60,7 +60,15 @@ names, and other aspects of this manual are still subject to change. * `edit` : Open the given Caskfile for editing. +<<<<<<< HEAD * `home` or `homepage`: +======= + * `fetch` [--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 in a browser. * `info` or `abv` : diff --git a/lib/cask/cli.rb b/lib/cask/cli.rb index 251f03cecb17..f0f223773473 100644 --- a/lib/cask/cli.rb +++ b/lib/cask/cli.rb @@ -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' diff --git a/lib/cask/cli/fetch.rb b/lib/cask/cli/fetch.rb new file mode 100644 index 000000000000..9ee325049280 --- /dev/null +++ b/lib/cask/cli/fetch.rb @@ -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 diff --git a/lib/cask/download.rb b/lib/cask/download.rb index 79c8e6582878..b918566aee1f 100644 --- a/lib/cask/download.rb +++ b/lib/cask/download.rb @@ -5,7 +5,7 @@ def initialize(cask) @cask = cask end - def perform + def perform(force=false) require 'software_spec' cask = @cask if cask.url.using == :svn @@ -13,6 +13,7 @@ def perform 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 diff --git a/test/cask/cli/fetch_test.rb b/test/cask/cli/fetch_test.rb new file mode 100644 index 000000000000..ef2b52f99b3f --- /dev/null +++ b/test/cask/cli/fetch_test.rb @@ -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