Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance output of status command #113

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/autoupdate/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 4 additions & 5 deletions lib/autoupdate/start.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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.
Expand Down Expand Up @@ -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'
Expand Down
47 changes: 42 additions & 5 deletions lib/autoupdate/status.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "rexml/document"

module Autoupdate
module_function

Expand All @@ -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
Expand All @@ -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."
Expand Down
Loading