Last active
April 7, 2020 12:25
-
-
Save dre-hh/88a119bc1e69892e45317eb04c2af9da to your computer and use it in GitHub Desktop.
asdf_latest
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 'optparse' | |
require 'pry' | |
class Latest | |
def initialize(lang, distros) | |
@lang, @distros = lang, distros | |
@distros = [@lang] unless distros | |
@versions = versions | |
end | |
def versions | |
all_versions = `asdf list-all #{@lang} 2> /dev/null`.split("\n") | |
versions = [] | |
if @distros.delete(@lang) | |
max = all_versions.reject { |v| v =~ /[a-zA-Z]/}.max_by { |v| Gem::Version.new(v) } | |
versions.unshift(max) | |
end | |
@distros.each do |distro| | |
max = all_versions.select { |v| v =~ /^#{distro}-/}.max_by { |v| Gem::Version.new(v.delete_prefix("#{distro}-")) } | |
versions.append(max) | |
end | |
versions | |
end | |
def install | |
@versions.each do |version| | |
`asdf install #{@lang} #{version}` | |
end | |
end | |
def install_only | |
installed_versions = `asdf list #{@lang} 2> /dev/null`.split("\n").map(&:strip) | |
old_versions = installed_versions - @versions | |
old_versions.each do |version| | |
`asdf uninstall #{@lang} #{version}` | |
end | |
install | |
end | |
def print | |
puts @versions | |
end | |
end | |
options = {} | |
OptionParser.new do |opts| | |
opts.banner = "Usage: [options]" | |
opts.on("-d", "--dist ruby, rbx, jruby", Array, "List of version distros") do |d| | |
options[:distros] = d | |
end | |
opts.on("-i", "--install", "Install the latest versions") do |i| | |
options[:install] = i | |
end | |
opts.on("--only", "Keep latest versions only") do |o| | |
options[:only] = o | |
end | |
opts.on("-h", "--help", "Prints this help") do | |
puts opts | |
exit | |
end | |
end.parse! | |
latest = Latest.new(ARGV[0], options[:distros]) | |
if options.key?(:install) | |
latest.install | |
elsif options.key?(:only) | |
latest.install_only | |
else | |
latest.print | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment