Skip to content

Instantly share code, notes, and snippets.

@nownabe
Created September 1, 2017 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nownabe/b09db191e34206882b66b2e6ae2c7658 to your computer and use it in GitHub Desktop.
Save nownabe/b09db191e34206882b66b2e6ae2c7658 to your computer and use it in GitHub Desktop.
Ruby Hack Challenge
# Usage:
# $ make
# $ make install
# $ ./ruby test_gem.rb test GEM_NAME
require "rbconfig"
require "rubygems"
require "rubygems/gem_runner"
require "rubygems/exceptions"
require "yaml"
class Config
class << self
def prefix
RbConfig::CONFIG["prefix"]
end
def gem
File.join(prefix, "bin", "gem")
end
def bundle
File.join(prefix, "bin", "bundle")
end
def rake
File.join(prefix, "bin", "rake")
end
def ruby
end
end
end
C = Config
def gem_install(gem, version = nil)
args = ["install", gem]
args += ["-v", version] if version
cmd = "#{C.gem} #{args.join(' ')}"
puts `#{cmd}`
end
def confirm_bundler
return if File.exist?(C.bundle)
puts "Installing bundler"
gem_install("bundler")
end
def confirm_rake
return if File.exist?(C.rake)
puts "Installing rake"
gem_install("rake")
end
def parse_repo(repo)
repo.sub(/\.git$/, "").split(/\/\/?/).values_at(1, 2, 3)
end
def fetch_gem(repo, dest)
unless File.directory?(dest)
cmd = "git clone #{repo} #{dest}"
`#{cmd}`
end
end
def bundle_install(dir)
cmd = <<~CMD
cd #{dir}
#{C.bundle} install
CMD
`#{cmd}`
end
subcommand = ARGV[0]
options = ARGV[1..-1]
gems_info = YAML.load(Object::DATA.read)
TEST_GEMS_DIR = "/home/nownabe/tmp/rubyhackchallenge/gems"
puts "Command: #{subcommand}"
puts "Options: #{options}"
case subcommand
when "install"
puts "install"
when "bundle"
puts "bundle"
when "test"
confirm_bundler
confirm_rake
gem = options[0]
version = options[1]
c = gems_info[gem]
root = c["root"]
deps = c["additional_dependencies"]
repo = c["github"] ? "https://github.com/#{c['github']}.git" : c["git"]
host, org, name = parse_repo(repo)
dest = File.join(TEST_GEMS_DIR, host, org, name)
fetch_gem(repo, dest)
base_dir = File.join(dest, root)
bundle_install(base_dir)
if c["additional_dependencies"]&.any?
c["additional_dependencies"].each { |g| gem_install(g) }
end
test_command = c.key?("rake") ? "bundle exec rake #{c['rake']}" : c["test"]
test_command = test_command.gsub("bundle", C.bundle)
test_command = test_command.gsub("rake", C.rake)
cmd = <<~CMD
PATH=#{C.prefix}/bin:$PATH
cd #{base_dir}
#{test_command}
CMD
puts `#{cmd}`
end
__END__
# Gems YAML
benchmark_driver:
git: https://github.com/k0kubun/benchmark_driver.git
root: /
rake:
thor:
github: erikhuda/thor
root: /
test: bundle exec thor spec
additional_dependencies:
- rspec
activesupport: # Not work
git: https://github.com/rails/rails.git
root: /activesupport
additional_dependencies:
- rake
example:
github: username/repo
git: https://github.com/username/repo.git
root: /app_path
additional_dependencies:
- rake
rake: test
test: rspec spec/**/*.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment