lib/vendorificator/cli.rb in vendorificator-0.1.1 vs lib/vendorificator/cli.rb in vendorificator-0.2.0

- old
+ new

@@ -2,30 +2,43 @@ require 'vendorificator' module Vendorificator class CLI < Thor - include Vendorificator + attr_reader :environment check_unknown_options! :except => [:git, :diff, :log] stop_on_unknown_option! :git, :diff, :log default_task :help - class_option :file, :aliases => '-f', :type => :string, :banner => 'PATH' - class_option :debug, :aliases => '-d', :type => :boolean, :default => false - class_option :quiet, :aliases => ['-q'], :default => false, :type => :boolean - class_option :modules, :type => :string, :default => '', + class_option :file, :aliases => '-f', :type => :string, + :banner => 'PATH' + class_option :debug, :aliases => '-d', :type => :boolean, :default => false + class_option :quiet, :aliases => '-q', :type => :boolean, :default => false + class_option :modules, :aliases => '-m', :type => :string, :default => '', :banner => 'mod1,mod2,...,modN', :desc => 'Run only for specified modules (name or path, comma separated)' + class_option :version, :type => :boolean + class_option :help, :aliases => '-h', :type => :boolean - def initialize(*args) + def initialize(args=[], options={}, config={}) super - Grit.debug = true if options[:debug] - Vendorificator::Config.from_file(find_vendorfile) - Vendorificator::Config[:shell] = shell + if self.options[:version] + say "Vendorificator #{Vendorificator::VERSION}" + exit + end + + if self.options[:help] && config[:current_task].name != 'help' + invoke :help, [ config[:current_task].name ] + exit + end + + @environment = Vendorificator::Environment.new(self.options[:file]) + environment.shell = shell + class << shell # Make say_status always say it. def quiet? false end @@ -33,13 +46,13 @@ end desc :sync, "Download new or updated vendor files" method_option :update, :type => :boolean, :default => false def sync - ensure_clean_repo! - conf[:use_upstream_version] = options[:update] - Vendorificator::Config.each_module(*modules) do |mod| + ensure_clean! + environment.config[:use_upstream_version] = options[:update] + Vendorificator::Vendor.each(*modules) do |mod| say_status :module, mod.name begin shell.padding += 1 mod.run! ensure @@ -49,13 +62,13 @@ end desc "status", "List known vendor modules and their status" method_option :update, :type => :boolean, :default => false def status - say_status 'WARNING', 'Git repository is not clean', :red unless repo.clean? - conf[:use_upstream_version] = options[:update] - Vendorificator::Config.each_module(*modules) do |mod| + say_status 'WARNING', 'Git repository is not clean', :red unless environment.clean? + environment.config[:use_upstream_version] = options[:update] + Vendorificator::Vendor.each(*modules) do |mod| status_line = mod.to_s updatable = mod.updatable? if updatable if updatable == true @@ -72,15 +85,15 @@ desc :pull, "Pull upstream branches from a remote repository" method_option :remote, :aliases => ['-r'], :default => nil method_option :dry_run, :aliases => ['-n'], :default => false, :type => :boolean def pull - ensure_clean_repo! - remotes = options[:remote] ? options[:remote].split(',') : conf[:remotes] + ensure_clean! + remotes = options[:remote] ? options[:remote].split(',') : environment.config[:remotes] remotes.each do |remote| indent 'remote', remote do - repo.pull(remote, options) + environment.pull(remote, options) end end end desc "git GIT_COMMAND [GIT_ARGS [...]]", "Run a git command for specified modules" @@ -94,13 +107,12 @@ Examples: vendor git log @MERGED@..HEAD -- @PATH@ # basic 'vendor log' vendor git diff --stat @MERGED@ -- @PATH@ # 'vendor diff', as diffstat EOF - method_option :only_changed, :default => false, :type => :boolean def git(command, *args) - Vendorificator::Config.each_module(*modules) do |mod| + Vendorificator::Vendor.each(*modules) do |mod| unless mod.merged say_status 'unmerged', mod.to_s, :red unless options[:only_changed] next end @@ -108,30 +120,23 @@ arg. gsub('@MERGED@', mod.merged). gsub('@PATH@', mod.work_dir) end - output = repo.git.native(command, {}, *actual_args) - if output.empty? - say_status 'unchanged', mod.to_s, :green unless options[:only_changed] - else - say_status 'changed', mod.to_s, :yellow - end - puts output unless options[:quiet] || output.empty? + say_status command, mod.to_s + output = environment.git.git(command, *actual_args) end end desc "diff [OPTIONS] [GIT OPTIONS]", "Show differences between work tree and upstream module(s)" - method_option :only_changed, :default => false, :type => :boolean def diff(*args) invoke :git, %w'diff' + args + %w'@MERGED@ -- @PATH@' end desc "log [OPTIONS] [GIT OPTIONS]", "Show git log of commits added to upstream module(s)" - method_option :only_changed, :default => false, :type => :boolean def log(*args) invoke :git, %w'log' + args + %w'@MERGED@..HEAD -- @PATH@' end desc :pry, 'Pry into the binding', :hide => true @@ -173,18 +178,10 @@ def modules options[:modules].split(',').map(&:strip) end - def conf - Vendorificator::Config - end - - def repo - Vendorificator::Config.repo - end - def fail!(message, exception_message='I give up.') say_status('FATAL', message, :red) raise Thor::Error, 'I give up.' end @@ -194,33 +191,11 @@ yield ensure shell.padding -= 1 end - # Find proper Vendorfile - def find_vendorfile - given = options.file || ENV['VENDORFILE'] - return Pathname.new(given).expand_path if given && !given.empty? - - Pathname.pwd.ascend do |dir| - vf = dir.join('Vendorfile') - return vf if vf.exist? - - vf = dir.join('config/vendor.rb') - return vf if vf.exist? - - # avoid stepping above the tmp directory when testing - if ENV['VENDORIFICATOR_SPEC_RUN'] && - dir.join('vendorificator.gemspec').exist? - raise RuntimeError, "Vendorfile not found" - end - end - - raise RuntimeError, "Vendorfile not found" - end - - def ensure_clean_repo! - unless repo.clean? + def ensure_clean! + unless environment.clean? fail!('Repository is not clean.') end end end end