Skip to content

Instantly share code, notes, and snippets.

@indirect
Created February 1, 2017 08:30
Show Gist options
  • Save indirect/be8dff8e339f63c617e3fff7896cb117 to your computer and use it in GitHub Desktop.
Save indirect/be8dff8e339f63c617e3fff7896cb117 to your computer and use it in GitHub Desktop.
A script to easily install new packages and add them to boxen
#!/usr/bin/env ruby
if ARGV.empty?
abort "USAGE: boxen-add [brew|cask|mas] NAME"
end
require 'yaml'
def hiera_path
"/opt/boxen/repo/hiera/users/indirect.yaml"
end
def config
@config ||= YAML.load_file(hiera_path)
end
def insert_config(key, value)
config[key].push(package)
config[key].sort!
config[key].uniq!
end
def find_app_id(package)
options = `mas search "#{package}"`.lines.select{|l| l.include?(package) }
abort("Couldn't find an app matching '#{package}'!") if options.size.zero?
abort("Found too many options!\n#{options.join}") if options.size > 1
app_id = options.first.split(" ").first
end
cmd = ARGV[0]
package = ARGV[1]
case cmd
when "brew"
if system("brew install #{package}")
insert_config("boxen::personal::homebrew_packages", package)
else
abort "Installing #{package} failed!"
end
when "cask"
if system("brew cask install #{package}")
insert_config("boxen::personal::osx_apps", package)
else
abort "Installing #{package} failed!"
end
when "mas"
app_id = ARGV[2] || find_app_id(package)
if system("mas install #{app_id}")
config["mas::apps"][package] = app_id
end
else
abort "Command must be one of brew, cask, or mas."
end
config_yaml = YAML.dump(config).gsub(/^- /, " - ")
File.write(hiera_path, config_yaml)
puts "Added #{package} to boxen!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment