Skip to content

Commit

Permalink
Merge pull request #29 from sportngin/homebrew-ruby
Browse files Browse the repository at this point in the history
Allow brew-gem to accept a '--homebrew-ruby' flag that uses /usr/local/bin/ruby instead of /usr/bin/ruby
  • Loading branch information
Andy Fleener committed Aug 23, 2016
2 parents 317098c + 1a1eb0d commit fdb78e0
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 11 deletions.
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
brew-gem
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in brew-gem.gemspec
gem 'rake', '~>11.0'
gemspec
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,19 @@ Usage
To install a specific version:

brew gem install heroku 3.8.3


To install using a brew installed ruby(/usr/local/bin/ruby):

brew gem install heroku --homebrew-ruby

And with a specific version:

brew gem install heroku 3.8.3 --homebrew-ruby

To upgrade:

brew gem upgrade heroku

To uninstall:

brew gem uninstall heroku
Expand Down
19 changes: 13 additions & 6 deletions lib/brew/gem/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ def process_args(args)
exit 0
end

args[0..2]
args[0..3]
end

def expand_formula(name, version)
def expand_formula(name, version, use_homebrew_ruby=false)
klass = 'Gem' + name.capitalize.gsub(/[-_.\s]([a-zA-Z0-9])/) { $1.upcase }.gsub('+', 'x')
user_gemrc = "#{ENV['HOME']}/.gemrc"
template_file = File.expand_path('../formula.rb.erb', __FILE__)
template = ERB.new(File.read(template_file))
template.result(binding)
end

def with_temp_formula(name, version)
def with_temp_formula(name, version, use_homebrew_ruby)
filename = File.join Dir.tmpdir, "gem-#{name}.rb"

open(filename, 'w') do |f|
f.puts expand_formula(name, version)
f.puts expand_formula(name, version, use_homebrew_ruby)
end

yield filename
Expand All @@ -62,11 +62,18 @@ def with_temp_formula(name, version)
end

def run(args = ARGV)
command, name, supplied_version = process_args(args)
command, name, supplied_version, homebrew_ruby = process_args(args)
homebrew_ruby_flag = "--homebrew-ruby"
if supplied_version == homebrew_ruby_flag
supplied_version = nil
homebrew_ruby = homebrew_ruby_flag
end

use_homebrew_ruby = homebrew_ruby == homebrew_ruby_flag

version = fetch_version(name, supplied_version)

with_temp_formula(name, version) do |filename|
with_temp_formula(name, version, use_homebrew_ruby) do |filename|
system "brew #{command} #{filename}"
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/brew/gem/formula.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ class <%= klass %> < Formula
# they might not be there if, say, we change to a different rvm gemset
ENV['GEM_HOME']="#{prefix}"
ENV['GEM_PATH']="#{prefix}"
system "gem", "install", cached_download,
rubybindir = '<%= use_homebrew_ruby ? "/usr/local/bin" : "/usr/bin" %>'
gem_path = "#{rubybindir}/gem"
ruby_path = "#{rubybindir}/ruby"
system gem_path, "install", cached_download,
"--no-ri",
"--no-rdoc",
"--no-wrapper",
Expand Down Expand Up @@ -67,7 +71,7 @@ class <%= klass %> < Formula
file = Pathname.new("#{brew_gem_prefix}/#{gemspec.bindir}/#{exe}")
(bin+file.basename).open('w') do |f|
f << <<-RUBY
#!/usr/bin/ruby
#!#{ruby_path}
ENV['GEM_HOME']="#{prefix}"
ENV['GEM_PATH']="#{prefix}"
$:.unshift(#{ruby_libs.map(&:inspect).join(",")})
Expand Down
9 changes: 8 additions & 1 deletion spec/brew/gem/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@

RSpec.describe Brew::Gem::CLI do
context "#expand_formula" do
subject(:formula) { described_class.expand_formula("foo-bar", "1.2.3") }
subject(:formula) { described_class.expand_formula("foo-bar", "1.2.3", false) }

it "generates valid Ruby" do
IO.popen("ruby -c -", "r+") { |f| f.puts formula }

expect($?).to be_success
end

it { is_expected.to match(/class GemFooBar < Formula/) }

it { is_expected.to match(/version "1\.2\.3"/) }
it { is_expected.to match("rubybindir = '/usr/bin'") }

context "homebrew-ruby" do
subject(:formula) { described_class.expand_formula("foo-bar", "1.2.3", true) }
it { is_expected.to match("rubybindir = '/usr/local/bin'") }
end
end

end

0 comments on commit fdb78e0

Please sign in to comment.