Skip to content

Instantly share code, notes, and snippets.

@adonaldson
Last active December 30, 2015 11:29
Show Gist options
  • Save adonaldson/7823076 to your computer and use it in GitHub Desktop.
Save adonaldson/7823076 to your computer and use it in GitHub Desktop.
Quick, ugly script to grep rails projects for rails versions, as suggested by @Govan - https://twitter.com/gavinmontague/status/408252762330517506
#!/usr/bin/env ruby
TERMINAL_RED = 31
TERMINAL_GREEN = 32
TERMINAL_DEFAULT = 0
LATEST_RAILS_VERSIONS = {
'2.3' => '2.3.18',
'3.0' => '3.0.20',
'3.1' => '3.1.12',
'3.2' => '3.2.16',
'4.0' => '4.0.2'
}
if ARGV[0] == nil
STDERR.puts 'Usage: rails_versions <Folder containing all rails projects> # e.g. rails_versions ~/work/'
abort
end
root = File.expand_path(ARGV[0])
root_for_grep = File.join(root, '**', 'Gemfile.lock')
rails_regex = "^ rails "
raw_results = `grep -nrs "#{rails_regex}" #{root_for_grep}`
results = raw_results.split($/).map do |r|
split = r.split(' ')
version = split.last.scan(/\((.+)\)/).flatten.first
latest_rails_version = LATEST_RAILS_VERSIONS[version.scan(/\d+\.\d+/).flatten.first]
path_split = split.first.split('/')
path_split.pop
app_name = path_split.pop
[app_name, { current: version, latest: latest_rails_version }]
end
app_versions = Hash[*results.flatten]
if app_versions.empty?
abort
end
STDERR.puts "App name \tCurrent\tLatest\tOut of date"
app_versions.each do |app_name, versions|
output = []
up_to_date = versions[:current] == versions[:latest]
colour = up_to_date ? TERMINAL_GREEN : TERMINAL_RED
output << "\e[#{colour}m#{app_name.ljust(20)}"
output << "#{versions[:current]}"
output << "\e[0m#{versions[:latest]}"
output << "*" unless up_to_date
STDOUT.puts output.join("\t")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment