From 7794897843a4207d159c4719f1a190e49beb9952 Mon Sep 17 00:00:00 2001 From: Felix Dreissig Date: Sun, 26 Mar 2017 15:42:08 +0200 Subject: [PATCH 1/2] Refactor app table printing Remove duplicate code for outputting tables of apps and identification of an app's state. --- lib/bcu.rb | 50 +++++++++++++++++++------------------------------- 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/lib/bcu.rb b/lib/bcu.rb index 8ae1d67..faba016 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,43 @@ 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" : "") + installed.each do |app| if options.all && app[:version] == "latest" - row << "forced to upgrade" outdated.push app + state_info[app] = "forced to upgrade" elsif options.all && app[:auto_updates] && app[:outdated?] - row << "forced to upgrade" outdated.push app + state_info[app] = "forced to upgrade" elsif !options.all && app[:auto_updates] - row << "ignored" + state_info[app] = "ignored" elsif app[:outdated?] - row << "outdated" outdated.push app + state_info[app] = "outdated" 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 From aec085a9a2c16d3a8a4717f3ed1f64d646c235ce Mon Sep 17 00:00:00 2001 From: Felix Dreissig Date: Sun, 26 Mar 2017 17:28:22 +0200 Subject: [PATCH 2/2] Distinguish between "auto-update" and "latest" apps As a follow-up to 40baf90, this adds a separation between apps that auto-update and those that are marked as latest through new command line options: `--all` now includes apps that auto-update, while `--force` is (additionally or exclusively) required for apps marked as latest. The rationale behind this is to allow upgrades of apps that auto-update without always reinstalling such marked as latest. --- README.md | 3 ++- cmd/brew-cu.rb | 7 +++++-- lib/bcu.rb | 17 +++++++++++------ lib/bcu/options.rb | 7 ++++++- 4 files changed, 24 insertions(+), 10 deletions(-) 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 faba016..274c5b4 100644 --- a/lib/bcu.rb +++ b/lib/bcu.rb @@ -63,17 +63,22 @@ def self.find_outdated_apps end installed.each do |app| - if options.all && app[:version] == "latest" + version_latest = (app[:version] == "latest") + + if options.force && options.all && version_latest && app[:auto_updates] outdated.push app - state_info[app] = "forced to upgrade" - elsif options.all && app[:auto_updates] && app[:outdated?] + state_info[app] = "forced to reinstall" + elsif options.force && version_latest && !app[:auto_updates] + outdated.push app + state_info[app] = "forced to reinstall" + elsif options.all && !version_latest && app[:auto_updates] && app[:outdated?] outdated.push app state_info[app] = "forced to upgrade" - elsif !options.all && app[:auto_updates] - state_info[app] = "ignored" - elsif app[:outdated?] + 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? state_info[app] = "no cask available" 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