Skip to content

Commit

Permalink
Merge pull request #53 from origin/default-homebrew-ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksieger committed Feb 9, 2018
2 parents 2321ae4 + 1718e5b commit bc1aecd
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 21 deletions.
71 changes: 52 additions & 19 deletions lib/brew/gem/cli.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'erb'
require 'tempfile'
require 'shellwords'

module Brew::Gem::CLI
module_function
Expand All @@ -14,6 +15,36 @@ module Brew::Gem::CLI
"help" => "This message"
}

HOMEBREW_RUBY_FLAG = "--homebrew-ruby"
SYSTEM_RUBY_FLAG = "--system-ruby"
RUBY_FLAGS = [HOMEBREW_RUBY_FLAG, SYSTEM_RUBY_FLAG]

class Arguments
attr_reader :ruby_flag

def initialize(args)
@ruby_flag = args.select {|a| RUBY_FLAGS.include?(a) }.last
@args = args.reject {|a| RUBY_FLAGS.include?(a) }
@args_without_flags = @args.reject {|a| a.start_with?('-') }
end

def command
@args_without_flags[0]
end

def gem
@args_without_flags[1]
end

def supplied_version
@args_without_flags[2]
end

def to_args
@args.reject {|a| a == gem || a == supplied_version }
end
end

def help_msg
(["Please specify a gem name (e.g. brew gem command <name>)"] +
COMMANDS.map {|name, desc| " #{name} - #{desc}"}).join("\n")
Expand All @@ -31,21 +62,26 @@ def fetch_version(name, version = nil)
end

def process_args(args)
abort help_msg unless args[0]
abort "unknown command: #{args[0]}\n#{help_msg}" unless COMMANDS.keys.include?(args[0])
arguments = Arguments.new(args)
command = arguments.command
abort help_msg unless command
abort "unknown command: #{command}\n#{help_msg}" unless COMMANDS.keys.include?(command)

if args[0] == 'help'
if command == 'help'
STDERR.puts help_msg
exit 0
end

args[0..3]
arguments
end

def homebrew_prefix
ENV['HOMEBREW_PREFIX'] || `brew --prefix`.chomp
end

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"
homebrew_prefix = ENV['HOMEBREW_PREFIX'] || `brew --prefix`.chomp
template_file = File.expand_path('../formula.rb.erb', __FILE__)
template = ERB.new(File.read(template_file))
template.result(binding)
Expand All @@ -63,24 +99,21 @@ def with_temp_formula(name, version, use_homebrew_ruby)
File.unlink filename
end

def run(args = ARGV)
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)
def homebrew_ruby?(ruby_flag)
File.exist?("#{homebrew_prefix}/opt/ruby") &&
ruby_flag.nil? || ruby_flag == HOMEBREW_RUBY_FLAG
end

with_temp_formula(name, version, use_homebrew_ruby) do |filename|
case command
def run(args = ARGV)
arguments = process_args(args)
name = arguments.gem
version = fetch_version(name, arguments.supplied_version)
with_temp_formula(name, version, homebrew_ruby?(arguments.ruby_flag)) do |filename|
case arguments.command
when "formula"
$stdout.puts File.read(filename)
else
system "brew #{command} #{filename}"
system "brew #{arguments.to_args.shelljoin} #{filename}"
exit $?.exitstatus unless $?.success?
end
end
Expand Down
6 changes: 6 additions & 0 deletions lib/brew/gem/formula.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class <%= klass %> < Formula
ENV['GEM_HOME']="#{prefix}"
ENV['GEM_PATH']="#{prefix}"
# Use /usr/local/bin at the front of the path instead of Homebrew shims,
# which mess with Ruby's own compiler config when building native extensions
if defined?(HOMEBREW_SHIMS_PATH)
ENV['PATH'] = ENV['PATH'].sub(HOMEBREW_SHIMS_PATH.to_s, '/usr/local/bin')
end
system BREWGEM_GEM_PATH, "install", cached_download,
"--no-ri",
"--no-rdoc",
Expand Down
72 changes: 70 additions & 2 deletions spec/brew/gem/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

RSpec.describe Brew::Gem::CLI do
before { ENV['HOMEBREW_PREFIX'] = '/usr/local' }
let(:cli) { described_class }

context "#expand_formula" do
subject(:formula) { described_class.expand_formula("foo-bar", "1.2.3", false) }
subject(:formula) { cli.expand_formula("foo-bar", "1.2.3", false) }

it "generates valid Ruby" do
IO.popen("ruby -c -", "r+") { |f| f.puts formula }
Expand All @@ -18,9 +19,76 @@
it { is_expected.to match("BREWGEM_RUBYBINDIR = '/usr/bin'") }

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

context "#run" do
let(:gem) { 'dummygem' }
let(:version) { '1.0.1.0' }
let(:formula) { 'temp-formula.rb' }
let(:command) { '' }
let(:opt_ruby_exists) { true }

before do
allow(cli).to receive(:exit)
allow(cli).to receive(:system) {|x| command << x }
allow(cli).to receive(:with_temp_formula).and_yield(formula)
allow(cli).to receive(:fetch_version) {|n,v| v || version }
allow(cli).to receive(:abort) {|msg| raise msg }
allow(File).to receive(:exist?).with('/usr/local/opt/ruby').and_return opt_ruby_exists
end

it 'runs brew on a formula file' do
cli.run ['install', gem]
expect(command.split).to eql(['brew', 'install', formula])
end

context 'with a homebrew ruby installed' do
it 'installs with homebrew ruby by default' do
cli.run ['install', gem]
expect(cli).to have_received(:with_temp_formula).with(gem, version, true)
end
end

context 'with a homebrew ruby not installed' do
let(:opt_ruby_exists) { false }

it 'installs with system ruby by default' do
cli.run ['install', gem]
expect(cli).to have_received(:with_temp_formula).with(gem, version, false)
end
end

it 'accepts an optional requested version' do
cli.run ['install', gem, '2.2.2']
expect(command.split).to eql(['brew', 'install', formula])
expect(cli).to have_received(:with_temp_formula).with(gem, '2.2.2', true)
end

it 'accepts a --homebrew-ruby flag' do
cli.run ['install', gem, '--homebrew-ruby']
expect(command.split).to eql(['brew', 'install', formula])
expect(cli).to have_received(:with_temp_formula).with(gem, version, true)
end

it 'accepts a --homebrew-ruby flag anywhere' do
cli.run ['install', '--homebrew-ruby', gem]
expect(command.split).to eql(['brew', 'install', formula])
expect(cli).to have_received(:with_temp_formula).with(gem, version, true)
end

it 'accepts a --system-ruby flag' do
cli.run ['install', gem, '--system-ruby']
expect(command.split).to eql(['brew', 'install', formula])
expect(cli).to have_received(:with_temp_formula).with(gem, version, false)
end

it 'accepts other flags and keeps the order' do
cli.run ['-v', 'uninstall', '--force', gem, '2.1.2']
expect(command.split).to eql(['brew', '-v', 'uninstall', '--force', formula])
expect(cli).to have_received(:with_temp_formula).with(gem, '2.1.2', true)
end
end
end

0 comments on commit bc1aecd

Please sign in to comment.