Skip to content

Instantly share code, notes, and snippets.

@vlad-shatskyi

vlad-shatskyi/gems.rb

Last active Dec 15, 2015
Embed
What would you like to do?
Installs all the required gems in the specified file or directory.
#!/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)
@vlad-shatskyi

This comment has been minimized.

Copy link
Owner Author

@vlad-shatskyi vlad-shatskyi commented Mar 31, 2013

I have a repo called gemper, which advances this idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment