diff --git a/README.md b/README.md index 699f08b..94f7502 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,9 @@ Options: ``` Usage: brew cu [CASK] [options] - -a, --all Force upgrade outdated apps including those marked as latest and those that auto-update + -a, --all Include apps that auto-update in the upgrade --cleanup Cleans up cached downloads and tracker symlinks after updating + -f --force Include apps that are marked as latest (i.e. force-reinstall them) -y, --yes Update all outdated apps; answer yes to updating packages ``` diff --git a/cmd/brew-cu.rb b/cmd/brew-cu.rb index 70231b8..197e514 100755 --- a/cmd/brew-cu.rb +++ b/cmd/brew-cu.rb @@ -8,12 +8,15 @@ #: Upgrade a specific app. #: #:OPTIONS: -#: If `--all` or `-a` is passed, force upgrade outdated apps including those -#: marked as latest and those that auto-update. +#: If `--all` or `-a` is passed, include apps that auto-update in the +#: upgrade. #: #: If `--cleanup` is passed, clean up cached downloads and tracker symlinks #: after updating. #: +#: If `--force` or `-f` is passed, include apps that are marked as latest +#: (i.e. force-reinstall them). +#: #: If `--yes` or `-y` is passed, update all outdated apps; answer yes to #: updating packages. diff --git a/lib/bcu.rb b/lib/bcu.rb index 8ae1d67..274c5b4 100644 --- a/lib/bcu.rb +++ b/lib/bcu.rb @@ -9,10 +9,12 @@ module Bcu def self.process(args) parse!(args) - outdated = find_outdated_apps + outdated, state_info = find_outdated_apps return if outdated.empty? - print_outdated_app(outdated) + puts + ohai "Found outdated apps" + print_app_table(outdated, state_info) if options.dry_run printf "\nDo you want to upgrade %d app%s [y/N]? ", outdated.length, outdated.length > 1 ? "s" : "" @@ -47,6 +49,7 @@ def self.process(args) def self.find_outdated_apps outdated = [] + state_info = Hash.new("") ohai "Finding outdated apps" installed = Hbc.installed_apps @@ -59,58 +62,48 @@ def self.find_outdated_apps end end - table = [["No.", "Name", "Cask", "Current", "Latest", "Auto-Update", "State"]] - installed.each_with_index do |app, i| - row = [] - row << "#{i+1}/#{installed.length}" - row << app[:name].to_s - row << app[:token] - row << app[:current].join(", ") - row << app[:version] - row << (app[:auto_updates] ? "Y" : "") - if options.all && app[:version] == "latest" - row << "forced to upgrade" + installed.each do |app| + version_latest = (app[:version] == "latest") + + if options.force && options.all && version_latest && app[:auto_updates] + outdated.push app + state_info[app] = "forced to reinstall" + elsif options.force && version_latest && !app[:auto_updates] outdated.push app - elsif options.all && app[:auto_updates] && app[:outdated?] - row << "forced to upgrade" + state_info[app] = "forced to reinstall" + elsif options.all && !version_latest && app[:auto_updates] && app[:outdated?] outdated.push app - elsif !options.all && app[:auto_updates] - row << "ignored" - elsif app[:outdated?] - row << "outdated" + state_info[app] = "forced to upgrade" + elsif !version_latest && !app[:auto_updates] && app[:outdated?] outdated.push app + state_info[app] = "outdated" + elsif version_latest || app[:outdated?] + state_info[app] = "ignored" elsif app[:cask].nil? - row << "no cask available" + state_info[app] = "no cask available" end - table << row end - puts Formatter.table(table) - outdated + print_app_table(installed, state_info) + + [outdated, state_info] end - def self.print_outdated_app(outdated) + def self.print_app_table(apps, state_info) table = [["No.", "Name", "Cask", "Current", "Latest", "Auto-Update", "State"]] - outdated.each_with_index do |app, i| + + apps.each_with_index do |app, i| row = [] - row << "#{i+1}/#{outdated.length}" + row << "#{i+1}/#{apps.length}" row << app[:name].to_s row << app[:token] row << app[:current].join(", ") row << app[:version] row << (app[:auto_updates] ? "Y" : "") - if options.all && app[:version] == "latest" - row << "forced to upgrade" - elsif options.all && app[:auto_updates] && app[:outdated?] - row << "forced to upgrade" - elsif app[:outdated?] - row << "outdated" - end + row << state_info[app] table << row end - puts - ohai "Found outdated apps" puts Formatter.table(table) end end diff --git a/lib/bcu/options.rb b/lib/bcu/options.rb index 919ba27..9ea3e5d 100644 --- a/lib/bcu/options.rb +++ b/lib/bcu/options.rb @@ -7,6 +7,7 @@ class << self; attr_accessor :options; end def self.parse!(args) options = OpenStruct.new options.all = false + options.force = false options.cask = nil options.cleanup = false options.dry_run = true @@ -14,7 +15,7 @@ def self.parse!(args) parser = OptionParser.new do |opts| opts.banner = "Usage: brew cu [CASK] [options]" - opts.on("-a", "--all", "Force upgrade outdated apps including those marked as latest and those that auto-update") do + opts.on("-a", "--all", "Include apps that auto-update in the upgrade") do options.all = true end @@ -22,6 +23,10 @@ def self.parse!(args) options.cleanup = true end + opts.on("-f", "--force", "Include apps that are marked as latest (i.e. force-reinstall them)") do + options.force = true + end + opts.on("-y", "--yes", "Update all outdated apps; answer yes to updating packages") do options.dry_run = false end