dotfiles

My personal shell configs and stuff
git clone git://git.alex.balgavy.eu/dotfiles.git
Log | Files | Refs | Submodules | README | LICENSE

commit b7118688151895d4dbd16c6656c939563c2dcd68
parent c1438594d8341c8e8076a9f2fcd713d740a1fb61
Author: Alex Balgavy <alex@balgavy.eu>
Date:   Wed, 17 Mar 2021 19:45:34 +0100

updatemaster: moved out into a separate repo

Diffstat:
Dscripts/updatemaster | 179-------------------------------------------------------------------------------
1 file changed, 0 insertions(+), 179 deletions(-)

diff --git a/scripts/updatemaster b/scripts/updatemaster @@ -1,179 +0,0 @@ -#!/usr/bin/env ruby -# frozen_string_literal: true - -def check_deps(deps) - deps.each do |dep| - unless system("command -v #{dep} >/dev/null 2>&1") - warn "error: command #{dep} not found" - exit 1 - end - end -end - -# The Homebrew package manager for macOS -class Homebrew - def initialize - check_deps ['brew'] - puts '-- HOMEBREW --' - 2.times { system 'brew update' } - @outdated_casks = `brew outdated --cask --greedy --verbose`.lines.map(&:chomp) - @cask_names = @outdated_casks.map { |c| c.split.first } - @outdated_formulae = `brew outdated --formula` - end - - def start_sudo_cache - puts 'Please enter the administrator password to install cask upgrades.' - system 'sudo -v' - @sudo_cache_pid = spawn 'while :; do sudo -n true; sleep 60; done 2>/dev/null' - end - - def prompt_user - return if @outdated_casks.empty? - - puts "\nOutdated casks:" - @outdated_casks.each_with_index { |c, i| puts "#{i} #{c}" } - print 'Numbers of casks to upgrade (space-separated): ' - begin - @to_upgrade = $stdin.gets.chomp.split.map { |casknum| @cask_names[Integer(casknum)] } - rescue ArgumentError - warn 'Only numbers allowed' - exit 1 - end - start_sudo_cache unless @to_upgrade.empty? - end - - def do_upgrade - if @outdated_formulae.empty? - puts "\nNo formulae to upgrade." - else - puts "\nUpgrading all formulae.\nTo upgrade:" - puts @outdated_formulae - system 'brew upgrade --formula' - end - - return if @outdated_casks.empty? - - if @to_upgrade.empty? - puts 'Not upgrading any casks.' - else - puts "\nUpgrading casks:" - puts @to_upgrade - - @to_upgrade.each { |cask| system "brew upgrade --cask --greedy --verbose #{cask}" } - puts 'Upgrade complete.' - end - end - - def cleanup - Process.kill('TERM', @sudo_cache_pid) if @sudo_cache_pid - system 'brew cleanup -s --prune-prefix' - system 'brew cleanup' - system 'brew doctor' - system 'brew missing' - - puts "Homebrew cache disk usage: #{`du -skh "$(brew --cache)" | cut -f1`}" - puts "Remove with:\trm -rf $(brew --cache)" - system 'brew bundle dump --force --file=~/.dotfiles/Brewfile;' - end -end - -# Pipx for Python -class Pipx - def initialize - check_deps ['pipx'] - puts '-- PIPX --' - end - - def do_upgrade - system 'pipx upgrade-all' - end -end - -# Gems for Ruby -class RubyGems - def initialize - check_deps ['gem'] - puts '-- GEM --' - puts 'To upgrade:' - system 'gem outdated' - end - - def do_upgrade - system 'gem update' - end - - def cleanup - system 'gem cleanup' - end -end - -# NPM for JS -class NPM - def initialize - check_deps ['npm'] - puts '-- NPM --' - puts 'To upgrade:' - system 'npm -g outdated' - end - - def do_upgrade - outdated = `npm outdated -g --parseable | sed 's!:.*!!g; s!.*/!!'`.split - outdated.each { |pkg| system "npm -g install #{pkg}@latest" } - system 'npm -g upgrade' - end -end - -def do_prompts(systems) - system_instances = systems.map(&:new) - system_instances.each(&:prompt_user) if (['-n', '--dry-run'] & ARGV).empty? - system_instances -end - -def split_systems_by_prompting(all_systems) - systems_with_prompt = all_systems.select { |s| s.method_defined? 'prompt_user' } - { prompting: systems_with_prompt, - no_prompting: all_systems - systems_with_prompt } -end - -def upgrade_cleanup(system_instance) - system_instance.do_upgrade if (['-n', '--dry-run'] & ARGV).empty? - system_instance.cleanup if system_instance.class.method_defined? 'cleanup' -end - -def main - package_systems = [Homebrew, Pipx, RubyGems, NPM] - - split_systems = split_systems_by_prompting package_systems - - # Do any prompts to get interactivity out of the way, then upgrade those first - do_prompts(split_systems[:prompting]).each { |s| upgrade_cleanup s } unless split_systems[:prompting].empty? - - # Finish up by upgrading systems that don't need user input - split_systems[:no_prompting].each do |nonprompting_sys| - pksys = nonprompting_sys.new - upgrade_cleanup pksys - end -end - -def handle_help - return if (['-h', '--help'] & ARGV).empty? - - puts <<~END_HELPTEXT - Usage: updatemaster [options] - - Options: - -h, --help display this helptext - -n, --dry-run don't upgrade anything, only update & cleanup - END_HELPTEXT - exit 0 -end - -handle_help -begin - caffeinate = spawn 'caffeinate -d' - main -rescue SystemExit, Interrupt - puts "\nInterrupted, exiting..." -end -Process.kill('SIGTERM', caffeinate) -Process.detach(caffeinate)