Skip to content

Instantly share code, notes, and snippets.

@thoran
Last active August 29, 2015 14:07
Show Gist options
  • Save thoran/12199e23ff3f5363ebcc to your computer and use it in GitHub Desktop.
Save thoran/12199e23ff3f5363ebcc to your computer and use it in GitHub Desktop.
brew-reinstall (Handy after an OSX upgrade!)
#!/usr/bin/env ruby
# brew-reinstall
# 20141003
# 0.4.0
# Notes:
# 1. If a reinstall fails, it should not block the reinstallation of other brews, but rather report that failure at the end.
# Changes since 0.3:
# 1. + failures().
# 2. + reinstall_brews().
# 3. + report_failures().
# 4. + main().
require 'Kernel/run'
def failures
@failures ||= []
end
def brews
if !ARGV.empty?
ARGV
else
`brew list`.split
end
end
def reinstall(brew)
run "brew uninstall #{brew}", show: true
run "brew install #{brew}", show: true
end
def reinstall_brews
brews.each do |brew|
begin
reinstall(brew)
rescue RuntimeError
failures << brew
end
end
end
def report_failures
if !failures.empty?
puts 'brew-reinstall failed to reinstall the following brews:'
failures.each do |failure|
puts failure
end
end
end
def main
reinstall_brews
report_failures
end
main
# Kernel/run.rb
# Kernel#run
# 20141002
# 0.1.4
# Changes:
# 1. Make use of a number of options, substituting ENV['DRY_RUN'] for options[:dry_run], and adding :show and :dont_raise.
# 0/1
# 2. When showing the output make it clear that it is a dry run.
# 1/2
# 3. If the :dry_run option is specified, then it makes no sense to test $? for success, so group all that code in the unless.
# 2/3
# 4. The logic was reversed for whether to raise or not.
# 3/4
# 5. Really fixed the raise logic finally, I think.
# Todo:
# 1. Even though this is a small method it might be good idea to add some automated tests.
require 'Array/extract_optionsX'
module Kernel
def run(*args)
options = args.extract_options!
command = args
if options[:show] && !options[:dry_run]
puts command.join(' ')
elsif options[:show] && options[:dry_run]
puts "DRY RUN *** #{command.join(' ')} *** DRY RUN"
end
unless options[:dry_run]
system(*command)
unless $?.success? && !options[:dont_raise]
raise "#{command.inspect} failed to exit cleanly."
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment