Created
February 17, 2020 16:47
-
-
Save tsutsu/270e09c68690ec85c51dbd054e22b9ef to your computer and use it in GitHub Desktop.
Update all available package ecosystems
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'pathname' | |
class << ENV | |
def path | |
@path ||= self['PATH'].split(':').map{ |d| Pathname.new(d) } | |
end | |
def which(cmd) | |
cmd = cmd.to_s | |
self.path.lazy.map{ |d| d + cmd }.find{ |e| e.file? && e.executable? } | |
end | |
end | |
COLORS = { | |
green: "\e[32m", | |
yellow: "\e[33m", | |
reset: "\e[0m" | |
} | |
def colorize(str, color) | |
COLORS[color] + str + COLORS[:reset] | |
end | |
def run_cmd(cmd) | |
puts("=> " + colorize(cmd.join(' '), :green)) | |
system(*cmd) | |
end | |
def read_from_cmd(cmd) | |
puts("** " + colorize(cmd.join(' '), :yellow)) | |
IO.popen(cmd){ |f| f.read.split("\n").map{ |ln| ln.strip } } | |
end | |
if ENV.which('apt-get') | |
apt_commands = [ | |
['update'], | |
['dist-upgrade'], | |
['autoremove', '--purge'], | |
['clean'] | |
] | |
apt_commands.each do |cmd| | |
cmd = ['sudo', 'apt-get'] + cmd | |
run_cmd(cmd) | |
end | |
end | |
if ENV.which('brew') | |
brew_config_lns = read_from_cmd(['brew', 'config']) | |
brew_config = brew_config_lns.map{ |ln| ln.split(': ', 2) }.to_h | |
brew_prefix = Pathname.new(brew_config['HOMEBREW_PREFIX']) | |
cask_prefix = brew_prefix + 'Caskroom' | |
brew_commands = [ | |
['update'], | |
['upgrade'], | |
(['cask', 'upgrade'] if cask_prefix.directory?), | |
['cleanup'], | |
] | |
brew_commands.each do |cmd| | |
next unless cmd | |
cmd = ['brew'] + cmd | |
run_cmd(cmd) | |
end | |
end | |
['pip', 'pip3'].each do |pip_cmd| | |
next unless ENV.which(pip_cmd) | |
wheel_lns = read_from_cmd([pip_cmd, 'list', '--outdated'])[2..-1] || [] | |
wheels = wheel_lns.map{ |ln| ln.split(' ').first } | |
pip_blacklist = ['pip', 'setuptools'] | |
wheels -= pip_blacklist | |
unless wheels.empty? | |
run_cmd([pip_cmd, 'install', '--upgrade'] + wheels) | |
end | |
end | |
if ENV.which('gem') | |
run_cmd(['gem', 'update']) | |
run_cmd(['gem', 'cleanup']) | |
end | |
if ENV.which('npm') | |
run_cmd(['npm', 'update', '-g']) | |
end | |
if ENV.which('rustup') | |
run_cmd(['rustup', 'update']) | |
end | |
if ENV.which('gcloud') | |
run_cmd(['gcloud', 'components', 'update', '--quiet']) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment