brewgem 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6cba5c2b3a980afd508edd4e3e269a1f9c85937a
4
+ data.tar.gz: efc9edb5e8cf6b79b5e6d4d5e8240c0727957306
5
+ SHA512:
6
+ metadata.gz: a61f97a4b5e9cc7f651966e166dd352b0c0eaaca1ba942878ab14ba85ee699170fa1475395ca6f21f737a13741e5cea489acf4aa93059947c22bd86d2d341b60
7
+ data.tar.gz: 1c690c98415a125d7c268a99405261da25a1f3991e97a57b5fbac89f8ba0748744a0c3863b3800b861edd160033713a844ed63aaa155f82233d200946f4aba40
data/bin/brewgem ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ $: << File.expand_path('../../lib/', __FILE__)
3
+ require 'thor'
4
+ require 'brew_gem'
5
+
6
+ BrewGem::Cli.start
@@ -0,0 +1,85 @@
1
+ require 'formula'
2
+ require 'fileutils'
3
+
4
+ class RubyGemsDownloadStrategy < AbstractDownloadStrategy
5
+ def fetch
6
+ ohai "Fetching #{name} from gem source"
7
+ HOMEBREW_CACHE.cd do
8
+ <% if local_path %>
9
+ system "cp", "<%= local_path %>", "."
10
+ <% else %>
11
+ system "gem", "fetch", name, "--version", resource.version
12
+ <% end %>
13
+ end
14
+ end
15
+
16
+ def cached_location
17
+ Pathname.new("#{HOMEBREW_CACHE}/#{name}-#{resource.version}.gem")
18
+ end
19
+
20
+ def clear_cache
21
+ cached_location.unlink if cached_location.exist?
22
+ end
23
+ end
24
+
25
+ class <%= klass %> < Formula
26
+ url "<%= name %>", :using => RubyGemsDownloadStrategy
27
+ version "<%= version %>"
28
+
29
+ def install
30
+ # Copy user's RubyGems config to temporary build home.
31
+ buildpath_gemrc = "#{ENV['HOME']}/.gemrc"
32
+ if File.exists?('<%= user_gemrc %>') && !File.exists?(buildpath_gemrc)
33
+ FileUtils.cp('<%= user_gemrc %>', buildpath_gemrc)
34
+ end
35
+
36
+ # set GEM_HOME and GEM_PATH to make sure we package all the dependent gems
37
+ # together without accidently picking up other gems on the gem path since
38
+ # they might not be there if, say, we change to a different rvm gemset
39
+ ENV['GEM_HOME']="#{prefix}"
40
+ ENV['GEM_PATH']="#{prefix}"
41
+ system "gem", "install", "<%= local_path ? local_path : 'cached_download' %>",
42
+ "--no-ri",
43
+ "--no-rdoc",
44
+ "--no-wrapper",
45
+ "--no-user-install",
46
+ "--install-dir", prefix,
47
+ "--bindir", bin
48
+
49
+ bin.rmtree if bin.exist?
50
+ bin.mkpath
51
+
52
+ brew_gem_prefix = prefix+"gems/#{name}-#{version}"
53
+
54
+ completion_for_bash = Dir[
55
+ "#{brew_gem_prefix}/completion{s,}/#{name}.{bash,sh}",
56
+ "#{brew_gem_prefix}/**/#{name}_completion{s,}.{bash,sh}"
57
+ ].first
58
+ bash_completion.install completion_for_bash if completion_for_bash
59
+
60
+ completion_for_zsh = Dir[
61
+ "#{brew_gem_prefix}/completions/#{name}.zsh",
62
+ "#{brew_gem_prefix}/**/#{name}_completion{s,}.zsh"
63
+ ].first
64
+ zsh_completion.install completion_for_zsh if completion_for_zsh
65
+
66
+ ruby_libs = Dir.glob("#{prefix}/gems/*/lib")
67
+ Pathname.glob("#{brew_gem_prefix}/bin/*").each do |file|
68
+ (bin+file.basename).open('w') do |f|
69
+ f << <<-RUBY
70
+ #!/usr/bin/ruby
71
+ # brewgem-generated wrapper that locks the gemset of the installed gem
72
+ # ORIG_GEM_HOME and ORIG_GEM_PATH hold the values of the actual GEM_HOME and GEM_PATH
73
+ # at the moment of invocation of this script - if it wants to launch another ruby script
74
+ # in its current gemset, just copy them back to GEM_HOME and GEM_PATH before invoking.
75
+ ENV['ORIG_GEM_HOME'] = ENV['GEM_HOME']
76
+ ENV['ORIG_GEM_PATH'] = ENV['GEM_PATH']
77
+ ENV['GEM_HOME'] = "#{prefix}"
78
+ ENV['GEM_PATH'] = "#{prefix}"
79
+ $:.unshift(#{ruby_libs.map(&:inspect).join(",")})
80
+ load "#{file}"
81
+ RUBY
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,32 @@
1
+ require 'thor'
2
+
3
+ module BrewGem
4
+ class Cli < Thor
5
+ COMMANDS = {
6
+ install: 'Install a gem (from RubyGems, local dir, local .gem, or github)',
7
+ update: 'Update a gem installed from RubyGems',
8
+ uninstall: 'Uninstall a gem installed from RubyGemss'
9
+ }
10
+
11
+ desc 'install [<gem-name>] [<version>] [--local=<path-to-local-dir-or-.gem>] [--github=<git@github.com:project/repo.git]',
12
+ COMMANDS[:install]
13
+ method_option :local, type: 'string'
14
+ method_option :github, type: 'string'
15
+ method_option :verbose, :type => :boolean
16
+ def install(gem_name = nil, version = nil)
17
+ BrewGem.install(gem_name, version, options)
18
+ end
19
+
20
+ desc 'update <gem-name>', COMMANDS[:update]
21
+ method_option :verbose, :type => :boolean
22
+ def update(gem_name)
23
+ BrewGem.update(gem_name)
24
+ end
25
+
26
+ desc 'uninstall <gem-name>', COMMANDS[:uninstall]
27
+ method_option :verbose, :type => :boolean
28
+ def uninstall(gem_name)
29
+ BrewGem.uninstall(gem_name)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module BrewGem
2
+ VERSION = "0.0.1"
3
+ end
data/lib/brew_gem.rb ADDED
@@ -0,0 +1,102 @@
1
+ require 'erb'
2
+ require 'tempfile'
3
+
4
+ module BrewGem
5
+ class << self
6
+ def install(name, version, options = {})
7
+ perform(:install, name, version, options)
8
+ end
9
+
10
+ def update(name)
11
+ perform(:update, name)
12
+ end
13
+
14
+ def uninstall(name)
15
+ perform(:uninstall, name)
16
+ end
17
+
18
+ def require_all_under(dir, recursive: true)
19
+ glob_path = File.join("#{dir}", recursive ? '**' : '', '*')
20
+ Dir.glob(glob_path) do |file_path|
21
+ require file_path if File.file?(file_path)
22
+ end
23
+ end
24
+
25
+ private
26
+ # TODO: Refactor into several methods, and for that need to
27
+ # run ERB from hash bindings instead of local method bindings
28
+ def perform(command, name = nil, version = nil, options = {})
29
+ ENV['HOMEBREW_VERBOSE']='true' if options[:verbose]
30
+ if options[:local]
31
+ local_path = File.expand_path(options[:local])
32
+
33
+ if /\.gem$/ =~ local_path
34
+ # install from local gem
35
+ name, version = local_path.match(/.*\/([^-]+)-(.*)\.gem$/)[1..2]
36
+ else
37
+ # install from local dir
38
+ gemspec_name = `ls #{File.join(local_path, '*')}.gemspec`.chomp
39
+
40
+ # build local gem
41
+ Dir.chdir(local_path) do
42
+ run "gem build #{gemspec_name}"
43
+ end
44
+
45
+ gem_name = `ls #{File.join(local_path, '*')}.gem`.chomp
46
+
47
+ # recursively call self to install from local gem
48
+ run "#{$0} install --local=#{gem_name}", takeover: true
49
+ end
50
+ elsif options[:github]
51
+ # clone from github and build gem
52
+ github_gem_path = options[:github]
53
+ name = github_gem_path.match(/.*\/(.*)\.git$/)[1]
54
+ target_dir_name = File.join(Dir.tmpdir, "build-#{name}-#{rand(100000000)}")
55
+ run "git clone #{github_gem_path} #{target_dir_name}"
56
+ # recursively call self to install from local dir
57
+ run "#{$0} install --local=#{target_dir_name}", takeover: true
58
+ else
59
+ # install from rubygems
60
+ gems = `gem list --remote "^#{name}$"`.lines
61
+ unless gems.detect { |f| f =~ /^#{name} \(([^\s,]+).*\)/ }
62
+ abort "Could not find a valid gem '#{name}'"
63
+ end
64
+ end
65
+
66
+ klass = name.capitalize.gsub(/[-_.\s]([a-zA-Z0-9])/) { $1.upcase }.gsub('+', 'x')
67
+ user_gemrc = "#{ENV['HOME']}/.gemrc"
68
+
69
+ template = ERB.new(File.read(File.expand_path("../../config/templates/formula.rb.erb", __FILE__)))
70
+
71
+ filename = File.join Dir.tmpdir, "#{name}.rb"
72
+
73
+ begin
74
+ open(filename, 'w') do |f|
75
+ f.puts template.result(binding)
76
+ end
77
+
78
+ system "brew #{command} #{filename}"
79
+ ensure
80
+ File.unlink filename
81
+ end
82
+ end
83
+
84
+ def run(command, options={})
85
+ if command.is_a? Array
86
+ command.each do |single_command|
87
+ break unless run single_command
88
+ end
89
+ else
90
+ puts "Executing \"#{command}\"..."
91
+ if options[:takeover]
92
+ exec command # take over the process
93
+ else
94
+ system command
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ # require all files under lib
102
+ BrewGem.require_all_under(File.expand_path(File.dirname(__FILE__)))
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brewgem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Joshua Peek
8
+ - '...'
9
+ - Max Rozenoer
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2015-12-18 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thor
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.19'
22
+ - - '>='
23
+ - !ruby/object:Gem::Version
24
+ version: 0.15.0
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ~>
30
+ - !ruby/object:Gem::Version
31
+ version: '0.19'
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: 0.15.0
35
+ - !ruby/object:Gem::Dependency
36
+ name: bundler
37
+ requirement: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: '1.3'
42
+ type: :development
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ~>
47
+ - !ruby/object:Gem::Version
48
+ version: '1.3'
49
+ description: Install Systemwide Ruby Gems via Brew
50
+ email:
51
+ - maxr@gett.com
52
+ executables:
53
+ - brewgem
54
+ extensions: []
55
+ extra_rdoc_files: []
56
+ files:
57
+ - bin/brewgem
58
+ - config/templates/formula.rb.erb
59
+ - lib/brew_gem.rb
60
+ - lib/brew_gem/cli.rb
61
+ - lib/brew_gem/version.rb
62
+ homepage: https://github.com/gtmax/brewgem
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: 2.0.0
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - '>='
78
+ - !ruby/object:Gem::Version
79
+ version: 1.8.23
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.4.6
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Install a ruby gem with a locked gemset environemnt for systemwide use in
86
+ any rvm context
87
+ test_files: []