Created
January 22, 2012 14:10
-
-
Save lastobelus/1657177 to your computer and use it in GitHub Desktop.
install a gem from github
This file contains hidden or 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 -KU | |
require "rubygems" | |
require "thor" | |
require "tmpdir" | |
class GemFromGit < Thor | |
include Thor::Actions | |
desc "install GIT-USER/REPO", "install a gem from github source." | |
method_option :tag, :default => "master", :banner => "TAG_OR_BRANCH", :aliases => '-t' | |
def install(repo) | |
tag = options[:tag] | |
# http://help.rubygems.org/discussions/problems/501-broken-utf-8-handling-in-newest-rubygems-when-environment-locales-are-not-set | |
ENV['LC_ALL'] = "en_US.UTF-8" | |
ENV['LANG'] = "en_US.UTF-8" | |
user, repo_name = repo.split('/') | |
Dir.mktmpdir do |tmpdir| | |
Dir.chdir tmpdir | |
destination_root = tmpdir | |
# get git_url | |
run "wget -q #{git_url(repo,tag)}" | |
run "unzip -qq #{tag}" | |
gdir = Dir.glob("#{user}-#{repo_name}*").first | |
Dir.chdir gdir | |
gemspec = Dir.glob("*.gemspec").first | |
existing_gemfiles = Dir.glob("*.gem") | |
if gemspec | |
if File.read(gemspec) =~ %r{\bgit\b} | |
run "git init -q" | |
run "git add --all" | |
run "git ci -q -m 'import for gemspec'" | |
end | |
run "gem build #{gemspec}" | |
gem_file = Dir.glob("*.gem") - existing_gemfiles | |
raise Thor::Error, "no gem file was created by gem build" if gem_file.empty? | |
gem_file = gem_file.first | |
run "rvm current" | |
run "cp #{gem_file} /tmp/" | |
run "gem install #{gem_file}" | |
else | |
run "rake build" | |
run "rake install" | |
end | |
end | |
end | |
private | |
def git_url(repo, tag) | |
raise Thor::Error, "supply repo in form 'thorsk/repo_name'" unless | |
repo.split('/').length == 2 | |
return "https://github.com/#{repo}/zipball/#{tag}" | |
end | |
end | |
GemFromGit.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment