Last active
December 15, 2015 14:58
-
-
Save vlad-shatskyi/5277946 to your computer and use it in GitHub Desktop.
Installs all the required gems in the specified file or directory.
This file contains 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 | |
def installed?(gem_name) | |
require gem_name | |
true | |
rescue LoadError | |
false | |
end | |
def required_gems(file_name) | |
File.readlines(file_name).map { |line| | |
line.match(/^require.*(?=['"](.+)['"])(?:"\1"|'\1')/) | |
}.compact.map { |e| e[1] }.uniq | |
end | |
def gems_in_path(path, recursive = false) | |
if File.file? path | |
required_gems path | |
elsif File.directory? path | |
Dir.chdir path | |
Dir["#{recursive ? '**/' : ''}*.{rb,ru}"].map{ |file_name| required_gems(file_name) }.flatten.uniq | |
else | |
abort "\e[31mFile not found: #{path}\e[0m" | |
end.reject { |e| installed? e } | |
end | |
def install(gems) | |
if gems.size == 0 | |
puts 'All the gems are already installed.' | |
else | |
puts "Following gem" + (gems.size > 1 ? 's' : '') + ' need to be installed:' | |
gems.each { |e| puts e } | |
print 'Install? [Y/n]: ' | |
install = ['', 'y', 'yes'].include? STDIN.gets.chomp.downcase | |
`gem install #{gems.join(' ')}` if install | |
end | |
end | |
recursive = ARGV.delete('-r') | |
abort 'Usage: gemper [-r] file|directory' if ARGV.size == 0 | |
install gems_in_path(ARGV[0], recursive) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have a repo called gemper, which advances this idea.