-
Notifications
You must be signed in to change notification settings - Fork 241
/
ruby.go
105 lines (79 loc) · 2.23 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
package scanner
import (
"os"
"os/exec"
"regexp"
"strings"
)
func configureRuby(sourceDir string, config *ScannerConfig) (*SourceInfo, error) {
if !checksPass(sourceDir, fileExists("Gemfile", "config.ru")) {
return nil, nil
}
s := &SourceInfo{
Family: "Ruby",
Port: 8080,
}
rubyVersion, err := extractRubyVersion("Gemfile.lock", "Gemfile", ".ruby_version")
if err != nil || rubyVersion == "" {
rubyVersion = "3.1.2"
out, err := exec.Command("ruby", "-v").Output()
if err == nil {
version := strings.TrimSpace(string(out))
re := regexp.MustCompile(`ruby (?P<version>[\d.]+)`)
m := re.FindStringSubmatch(version)
for i, name := range re.SubexpNames() {
if len(m) > 0 && name == "version" {
rubyVersion = m[i]
}
}
}
}
vars := make(map[string]interface{})
vars["rubyVersion"] = rubyVersion
s.Files = templatesExecute("templates/ruby", vars)
s.SkipDeploy = true
s.DeployDocs = `
Your Ruby app is prepared for deployment.
If you need custom packages installed, or have problems with your deployment
build, you may need to edit the Dockerfile for app-specific changes. If you
need help, please post on https://community.fly.io.
Now: run 'fly deploy' to deploy your Ruby app.
`
return s, nil
}
func extractRubyVersion(lockfilePath string, gemfilePath string, rubyVersionPath string) (string, error) {
var version string
lockfileContents, err := os.ReadFile(lockfilePath)
if err == nil {
re := regexp.MustCompile(`RUBY VERSION\s+ruby (?P<version>[\d.]+)`)
m := re.FindStringSubmatch(string(lockfileContents))
for i, name := range re.SubexpNames() {
if len(m) > 0 && name == "version" {
version = m[i]
}
}
}
if version == "" {
gemfileContents, err := os.ReadFile(gemfilePath)
if err != nil {
return "", err
}
re := regexp.MustCompile(`ruby \"(?P<version>[\d.]+)\"`)
m := re.FindStringSubmatch(string(gemfileContents))
for i, name := range re.SubexpNames() {
if len(m) > 0 && name == "version" {
version = m[i]
}
}
}
if version == "" {
if _, err := os.Stat(rubyVersionPath); err == nil {
versionString, err := os.ReadFile(rubyVersionPath)
if err != nil {
return "", err
}
version = string(versionString)
}
}
return version, nil
}