diff --git a/lib/autoupdate/core.rb b/lib/autoupdate/core.rb index 67470d9..8c5a90f 100644 --- a/lib/autoupdate/core.rb +++ b/lib/autoupdate/core.rb @@ -32,5 +32,22 @@ def tap_dir origin = Tap.names.join(" ").match(%r{(domt4|homebrew)/autoupdate})[1] Pathname.new(File.join(HOMEBREW_LIBRARY, "Taps", origin, "homebrew-autoupdate")) end + + def command_upgrade + " && #{Autoupdate::Core.brew} upgrade --formula -v" + end + + def command_cask(greedy) + greedy_argument = greedy ? " --greedy" : "" + " && #{Autoupdate::Core.brew} upgrade --cask -v#{greedy_argument}" + end + + def command_cleanup + " && #{Autoupdate::Core.brew} cleanup" + end + + def command_sudo + "export SUDO_ASKPASS='#{Autoupdate::Core.location/"brew_autoupdate_sudo_gui"}'" + end end end diff --git a/lib/autoupdate/start.rb b/lib/autoupdate/start.rb index 4a75e5c..2110322 100644 --- a/lib/autoupdate/start.rb +++ b/lib/autoupdate/start.rb @@ -16,7 +16,7 @@ def start(interval:, args:) auto_args = "update" # Spacing at start of lines is deliberate. Don't undo. if args.upgrade? - auto_args << " && #{Autoupdate::Core.brew} upgrade --formula -v" + auto_args << Autoupdate::Core.command_upgrade.to_s if (HOMEBREW_PREFIX/"Caskroom").exist? if ENV["SUDO_ASKPASS"].nil? && !args.sudo? @@ -28,11 +28,10 @@ def start(interval:, args:) EOS end - greedy = args.greedy? ? " --greedy" : "" - auto_args << " && #{Autoupdate::Core.brew} upgrade --cask -v#{greedy}" + auto_args << Autoupdate::Core.command_cask(args.greedy?).to_s end - auto_args << " && #{Autoupdate::Core.brew} cleanup" if args.cleanup? + auto_args << Autoupdate::Core.command_cleanup.to_s if args.cleanup? end # Enable the new AppleScript applet by default on Catalina and above. @@ -94,7 +93,7 @@ def start(interval:, args:) Please run `brew install pinentry-mac` and try again. EOS end - set_env << "\nexport SUDO_ASKPASS='#{Autoupdate::Core.location/"brew_autoupdate_sudo_gui"}'" + set_env << "\n#{Autoupdate::Core.command_sudo}" sudo_gui_script_contents = <<~EOS #!/bin/sh PATH='#{HOMEBREW_PREFIX}/bin' diff --git a/lib/autoupdate/status.rb b/lib/autoupdate/status.rb index e373085..2aed659 100644 --- a/lib/autoupdate/status.rb +++ b/lib/autoupdate/status.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "rexml/document" + module Autoupdate module_function @@ -21,11 +23,42 @@ def date_of_last_modification if File.exist?(Autoupdate::Core.location/"brew_autoupdate") birth = File.birthtime(Autoupdate::Core.location/"brew_autoupdate").to_s date = Date.parse(birth) - formatted_string = date.strftime("%D") + out = date.strftime("%D") + else + out = "Unable to determine date of command invocation. Please report this." + end + out + end + + def brew_update_options + brew_autoupdate = File.readlines(Autoupdate::Core.location/"brew_autoupdate") + out = "Options:" + + if brew_autoupdate + out += "\n--upgrade" if brew_autoupdate.last.include?(Autoupdate::Core.command_upgrade.to_s) + out += "\n--cleanup" if brew_autoupdate.last.include?(Autoupdate::Core.command_cleanup.to_s) + out += "\n--greedy" if brew_autoupdate.last.include?(Autoupdate::Core.command_cask(true).to_s) + out += "\n--sudo" if brew_autoupdate.any? { |line| line.chomp == Autoupdate::Core.command_sudo.to_s } + end + out + end + + def autoupdate_interval + plist = REXML::Document.new(File.read(Autoupdate::Core.plist)) + key = "StartInterval" + if (element = plist.elements["//key[text()='#{key}']"]) + value = element.next_element.text.to_i + out = "Interval: #{value}" else - formatted_string = "Unable to determine date of command invocation. Please report this." + out = "Interval: Not found, maybe using `StartCalendarInterval`" end - formatted_string + out + end + + def autoupdate_start_on_launch + plist = REXML::Document.new(File.read(Autoupdate::Core.plist)) + key = "RunAtLoad" + ("--immediate\n" if plist.elements["//key[text()='#{key}']"]) end def autoupdate_inadvisably_old_message @@ -46,15 +79,19 @@ def status puts <<~EOS Autoupdate is installed and running. + #{autoupdate_interval} + + #{brew_update_options} + #{autoupdate_start_on_launch} Autoupdate was initialised on #{date_of_last_modification}. - \n#{autoupdate_inadvisably_old_message} + #{autoupdate_inadvisably_old_message} EOS elsif autoupdate_installed_but_stopped? puts <<~EOS Autoupdate is installed but stopped. Autoupdate was initialised on #{date_of_last_modification}. - \n#{autoupdate_inadvisably_old_message} + #{autoupdate_inadvisably_old_message} EOS elsif autoupdate_not_configured? puts "Autoupdate is not configured. Use `brew autoupdate start` to begin."