forked from soupmatt/brew-gem
-
Notifications
You must be signed in to change notification settings - Fork 22
/
formula.rb.erb
114 lines (93 loc) · 3.53 KB
/
formula.rb.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- ruby -*-
require 'formula'
require 'fileutils'
USE_HOMEBREW_RUBY = <%= use_homebrew_ruby ? "true" : "false" %>
GEM_ARGUMENTS = <%= gem_arguments.inspect %>
module RubyBin
def ruby_bin
USE_HOMEBREW_RUBY ? Formula["ruby"].opt_bin: "/usr/bin"
end
end
class RubyGemsDownloadStrategy < AbstractDownloadStrategy
include RubyBin
def fetch(timeout: nil, **options)
ohai "Fetching <%= name %> from gem source"
cache.cd do
ENV['GEM_SPEC_CACHE'] = "#{cache}/gem_spec_cache"
system "#{ruby_bin}/gem", "fetch", "<%= name %>", "--version", gem_version
end
end
def cached_location
Pathname.new("#{cache}/<%= name %>-#{gem_version}.gem")
end
def cache
@cache ||= HOMEBREW_CACHE
end
def gem_version
return @version if defined?(@version) && @version
@version = @resource.version if defined?(@resource)
raise "Unable to determine version; did Homebrew change?" unless @version
@version
end
def clear_cache
cached_location.unlink if cached_location.exist?
end
end
class <%= klass %> < Formula
include RubyBin
url "<%= name %>", :using => RubyGemsDownloadStrategy
version "<%= version %>"
depends_on "ruby"
def install
# Copy user's RubyGems config to temporary build home.
buildpath_gemrc = "#{ENV['HOME']}/.gemrc"
if File.exist?('<%= user_gemrc %>') && !File.exist?(buildpath_gemrc)
FileUtils.cp('<%= user_gemrc %>', buildpath_gemrc)
end
# set GEM_HOME and GEM_PATH to make sure we package all the dependent gems
# together without accidently picking up other gems on the gem path since
# they might not be there if, say, we change to a different rvm gemset
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 "#{ruby_bin}/gem", "install", cached_download,
"--no-document",
"--no-wrapper",
"--no-user-install",
"--install-dir", prefix,
"--bindir", bin, *GEM_ARGUMENTS
raise "gem install '<%= name %>' failed with status #{$?.exitstatus}" unless $?.success?
bin.rmtree if bin.exist?
bin.mkpath
brew_gem_prefix = prefix+"gems/<%= name %>-#{version}"
completion_for_bash = Dir[
"#{brew_gem_prefix}/completion{s,}/<%= name %>.{bash,sh}",
"#{brew_gem_prefix}/**/<%= name %>{_,-}completion{s,}.{bash,sh}"
].first
bash_completion.install completion_for_bash if completion_for_bash
completion_for_zsh = Dir[
"#{brew_gem_prefix}/completions/<%= name %>.zsh",
"#{brew_gem_prefix}/**/<%= name %>{_,-}completion{s,}.zsh"
].first
zsh_completion.install completion_for_zsh if completion_for_zsh
gemspec = Gem::Specification::load("#{prefix}/specifications/<%= name %>-#{version}.gemspec")
ruby_libs = Dir.glob("#{prefix}/gems/*/lib")
gemspec.executables.each do |exe|
file = Pathname.new("#{brew_gem_prefix}/#{gemspec.bindir}/#{exe}")
(bin+file.basename).open('w') do |f|
f << <<-RUBY
#!#{ruby_bin}/ruby --disable-gems
ENV['GEM_HOME']="#{prefix}"
ENV['GEM_PATH']="#{prefix}"
require 'rubygems'
$:.unshift(#{ruby_libs.map(&:inspect).join(",")})
load "#{file}"
RUBY
end
end
end
end