-
Notifications
You must be signed in to change notification settings - Fork 160
/
ruby.go
333 lines (284 loc) · 7.68 KB
/
ruby.go
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package versions
import (
"encoding/json"
"fmt"
"github.com/Masterminds/semver"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/cloudfoundry/libbuildpack"
)
type Manifest interface {
AllDependencyVersions(string) []string
DefaultVersion(string) (libbuildpack.Dependency, error)
}
type Versions struct {
buildDir string
depDir string
manifest Manifest
cachedSpecs map[string]string
bundlerVersion string
}
func New(buildDir string, depDir string, manifest Manifest) *Versions {
bundlerVersions := manifest.AllDependencyVersions("bundler")
bundlerVersion := ""
if len(bundlerVersions) > 0 {
bundlerVersion = bundlerVersions[len(bundlerVersions)-1]
}
return &Versions{
buildDir: buildDir,
depDir: depDir,
manifest: manifest,
bundlerVersion: bundlerVersion,
}
}
type output struct {
Error string `json:"error"`
Output interface{} `json:"output"`
}
func (v *Versions) SetBundlerVersion(version string) {
v.bundlerVersion = version
}
func (v *Versions) GetBundlerVersion() string {
return v.bundlerVersion
}
func (v *Versions) CheckBundler2Compatibility() (bool, error) {
engine, err := v.Engine()
if err != nil {
return false, err
}
if engine == "jruby" {
return true, nil
}
gemfileRubyVersion, err := v.Version()
if err != nil {
return false, err
}
if gemfileRubyVersion == "" {
dep, err := v.manifest.DefaultVersion("ruby")
if err != nil {
return false, err
}
gemfileRubyVersion = dep.Version
}
rubyConstraint, err := semver.NewConstraint("<= 2.2.X")
if err != nil {
return false, err
}
rubySemver, err := semver.NewVersion(gemfileRubyVersion)
if err != nil {
return false, err
}
if engine == "ruby" && rubyConstraint.Check(rubySemver) {
return false, nil
}
return true, nil
}
func (v *Versions) Engine() (string, error) {
gemfile := v.Gemfile()
code := fmt.Sprintf(`
b = Bundler::Dsl.evaluate('%s', '%s.lock', {}).ruby_version if File.exists?('%s')
return 'ruby' if !b
b.engine
`, filepath.Base(gemfile), filepath.Base(gemfile), filepath.Base(gemfile))
data, err := v.run(filepath.Dir(gemfile), code, []string{})
if err != nil {
return "", err
}
return data.(string), nil
}
func (v *Versions) Version() (string, error) {
versions := v.manifest.AllDependencyVersions("ruby")
gemfile := v.Gemfile()
code := fmt.Sprintf(`
b = Bundler::Dsl.evaluate('%s', '%s.lock', {}).ruby_version
return '' if !b
r = Gem::Requirement.create(b.versions)
version = input.select { |v| r.satisfied_by? Gem::Version.new(v) }.sort.last
raise "No Matching versions, ruby #{r} not found in this buildpack" unless version
version
`, filepath.Base(gemfile), filepath.Base(gemfile))
data, err := v.run(filepath.Dir(gemfile), code, versions)
if err != nil {
return "", err
}
return data.(string), nil
}
func (v *Versions) JrubyVersion() (string, error) {
gemfile := v.Gemfile()
code := fmt.Sprintf(`
b = Bundler::Dsl.evaluate('%s', '%s.lock', {}).ruby_version
return '' if !b
"#{b.versions_string(b.engine_versions)}"
`, filepath.Base(gemfile), filepath.Base(gemfile))
data, err := v.run(filepath.Dir(gemfile), code, []string{})
if err != nil {
return "", err
}
return data.(string), nil
}
func (v *Versions) RubyEngineVersion() (string, error) {
code := `require 'rbconfig';RbConfig::CONFIG['ruby_version']`
data, err := v.run(v.buildDir, code, []string{})
if err != nil {
return "", err
}
return data.(string), nil
}
func (v *Versions) VersionConstraint(version string, constraints ...string) (bool, error) {
code := `
version = input.shift
Gem::Requirement.create(input).satisfied_by? Gem::Version.new(version)
`
data, err := v.run(v.buildDir, code, append([]string{version}, constraints...))
if err != nil {
return false, err
}
return data.(bool), nil
}
func (v *Versions) HasGemVersion(gem string, constraints ...string) (bool, error) {
specs, err := v.specs()
if err != nil {
return false, err
}
if specs[gem] == "" {
return false, nil
}
return v.VersionConstraint(specs[gem], constraints...)
}
func (v *Versions) HasGem(gem string) (bool, error) {
specs, err := v.specs()
if err != nil {
return false, err
}
if specs[gem] != "" {
return true, nil
}
return false, nil
}
func (v *Versions) GemMajorVersion(gem string) (int, error) {
specs, err := v.specs()
if err != nil {
return -1, err
}
if specs[gem] == "" {
return -1, nil
}
code := `Gem::Version.new(input.first).segments.first.to_s`
data, err := v.run(v.buildDir, code, []string{specs[gem]})
if err != nil {
return -1, err
}
if i, err := strconv.Atoi(data.(string)); err == nil {
return i, nil
} else {
return -1, err
}
}
//Should return true if either:
// (1) the only platform in the Gemfile.lock is windows (mingw/mswin)
// (there is no ruby or jruby platform)
// -or-
// (2) the Gemfile.lock line endings are /r/n, rather than just /n
func (v *Versions) HasWindowsGemfileLock() (bool, error) {
gemfileLockPath := v.Gemfile() + ".lock"
if good, err := libbuildpack.FileExists(gemfileLockPath); err != nil {
return false, err
} else if !good {
return false, nil
}
if bytes, err := ioutil.ReadFile(gemfileLockPath); err != nil {
return false, err
} else if strings.Contains(string(bytes), "\r\n") {
return true, nil
}
code := `
return false if !File.exists?(input["gemfilelock"])
parsed = Bundler::LockfileParser.new(File.read(input["gemfilelock"]))
parsed.platforms.detect do |platform|
if platform.is_a?(String)
/ruby/.match(platform)
elsif platform.is_a?(Gem::Platform)
/java/.match(platform.os)
end
end.nil?
`
data, err := v.run(filepath.Dir(v.Gemfile()),
code,
map[string]string{"gemfilelock": fmt.Sprintf("%s.lock", v.Gemfile())})
if err != nil {
return false, err
}
return data.(bool), nil
}
func (v *Versions) specs() (map[string]string, error) {
if len(v.cachedSpecs) > 0 {
return v.cachedSpecs, nil
}
code := `
parsed = Bundler::LockfileParser.new(File.read(input["gemfilelock"]))
Hash[*(parsed.specs.map{|spec| [spec.name, spec.version.to_s]}).flatten]
`
data, err := v.run(filepath.Dir(v.Gemfile()), code, map[string]string{"gemfilelock": fmt.Sprintf("%s.lock", v.Gemfile())})
if err != nil {
return nil, err
}
specs := make(map[string]string, 0)
for k, v := range data.(map[string]interface{}) {
specs[k] = v.(string)
}
v.cachedSpecs = specs
return v.cachedSpecs, nil
}
func (v *Versions) Gemfile() string {
gemfile := "Gemfile"
if os.Getenv("BUNDLE_GEMFILE") != "" {
gemfile = os.Getenv("BUNDLE_GEMFILE")
}
return filepath.Join(v.buildDir, gemfile)
}
func (v *Versions) run(dir, code string, in interface{}) (interface{}, error) {
data, err := json.Marshal(in)
if err != nil {
return "", err
}
code = fmt.Sprintf(`
stdout, $stdout = $stdout, $stderr
begin
def data(input)
%s
end
input = JSON.parse(STDIN.read)
out = data(input)
stdout.puts({error:nil, data:out}.to_json)
rescue => e
stdout.puts({error:e.to_s, data:nil}.to_json)
end
`, code)
args := []string{"-rjson", "-e", code}
if strings.Contains(code, "Bundler::") {
bundlerPath := filepath.Join(v.depDir, "bundler", "gems", fmt.Sprintf("bundler-%s", v.bundlerVersion), "lib")
args = append([]string{fmt.Sprintf("-I%s", bundlerPath), "-rbundler"}, args...)
}
cmd := exec.Command("ruby", args...)
cmd.Dir = dir
cmd.Stdin = strings.NewReader(string(data))
body, err := cmd.Output()
if err != nil {
return "", err
}
output := struct {
Error string `json:"error"`
Data interface{} `json:"data"`
}{}
if err := json.Unmarshal(body, &output); err != nil {
return "", err
}
if output.Error != "" {
return "", fmt.Errorf("Running ruby: %s", output.Error)
}
return output.Data, nil
}