Skip to content

Commit

Permalink
Move rest of guts of brew-gem to lib/brew/gem/cli.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksieger committed May 16, 2016
1 parent 571c275 commit 3cb8945
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 37 deletions.
37 changes: 3 additions & 34 deletions bin/brew-gem
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,6 @@

BREW_GEM_HOME = File.expand_path('../..', __FILE__)

if !ARGV[0] || ARGV[0] == 'help'
abort "Please specify a gem name (e.g. brew gem command <name>)
install - Install a brew gem, accepts an optional version argument (e.g. brew gem install <name> <version>)
upgrade - Upgrade to the latest version of a brew gem
uninstall - Uninstall a brew gem"
end

command = ARGV[0]
name = ARGV[1]
gems = `gem list --remote "^#{name}$"`.lines
unless gems.detect { |f| f =~ /^#{name} \(([^\s,]+).*\)/ }
abort "Could not find a valid gem '#{name}'"
end
version = ARGV[2] || $1

klass = 'Gem' + name.capitalize.gsub(/[-_.\s]([a-zA-Z0-9])/) { $1.upcase }.gsub('+', 'x')
user_gemrc = "#{ENV['HOME']}/.gemrc"

require 'erb'
template_file = File.join(BREW_GEM_HOME, 'lib/brew/gem/template.rb.erb')
template = ERB.new(File.read(template_file))

require 'tempfile'
filename = File.join Dir.tmpdir, "gem-#{name}.rb"

begin
open(filename, 'w') do |f|
f.puts template.result(binding)
end

system "brew #{command} #{filename}"
ensure
File.unlink filename
end
$LOAD_PATH.unshift(File.join(BREW_GEM_HOME, 'lib'))
require 'brew/gem'
Brew::Gem::CLI.run(ARGV)
6 changes: 3 additions & 3 deletions lib/brew/gem.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "brew/gem/version"

module Brew
module Gem
# Your code goes here...
end
end

require "brew/gem/cli"
require "brew/gem/version"
43 changes: 43 additions & 0 deletions lib/brew/gem/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'erb'
require 'tempfile'

module Brew::Gem::CLI
HELP_MSG = <<-STR
Please specify a gem name (e.g. brew gem command <name>)
install - Install a brew gem, accepts an optional version argument (e.g. brew gem install <name> <version>)
upgrade - Upgrade to the latest version of a brew gem
uninstall - Uninstall a brew gem
STR

def self.run(args = ARGV)
if !args[0] || args[0] == 'help'
abort HELP_MSG
end

command = args[0]
name = args[1]
gems = `gem list --remote "^#{name}$"`.lines

unless gems.detect { |f| f =~ /^#{name} \(([^\s,]+).*\)/ }
abort "Could not find a valid gem '#{name}'"
end

version = args[2] || $1

klass = 'Gem' + name.capitalize.gsub(/[-_.\s]([a-zA-Z0-9])/) { $1.upcase }.gsub('+', 'x')
user_gemrc = "#{ENV['HOME']}/.gemrc"
template_file = File.expand_path('../template.rb.erb', __FILE__)
template = ERB.new(File.read(template_file))
filename = File.join Dir.tmpdir, "gem-#{name}.rb"

begin
open(filename, 'w') do |f|
f.puts template.result(binding)
end

system "brew #{command} #{filename}"
ensure
File.unlink filename
end
end
end

1 comment on commit 3cb8945

@xubom1
Copy link

@xubom1 xubom1 commented on 3cb8945 May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.