Skip to content

Instantly share code, notes, and snippets.

@vlad-shatskyi
Last active December 15, 2015 14:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vlad-shatskyi/5277946 to your computer and use it in GitHub Desktop.
Save vlad-shatskyi/5277946 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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