-
-
Notifications
You must be signed in to change notification settings - Fork 378
/
runtimes.go
224 lines (193 loc) · 5.01 KB
/
runtimes.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
package config
import (
"os"
"github.com/apex/up/internal/util"
"github.com/pkg/errors"
)
// Runtime is an app runtime.
type Runtime string
// Runtimes available.
const (
RuntimeUnknown Runtime = "unknown"
RuntimeGo = "go"
RuntimeNode = "node"
RuntimeClojure = "clojure"
RuntimeCrystal = "crystal"
RuntimePython = "python"
RuntimeStatic = "static"
RuntimeJavaMaven = "java maven"
RuntimeJavaGradle = "java gradle"
)
// inferRuntime returns the runtime based on files present in the CWD.
func inferRuntime() Runtime {
switch {
case util.Exists("main.go"):
return RuntimeGo
case util.Exists("main.cr"):
return RuntimeCrystal
case util.Exists("package.json"):
return RuntimeNode
case util.Exists("app.js"):
return RuntimeNode
case util.Exists("project.clj"):
return RuntimeClojure
case util.Exists("pom.xml"):
return RuntimeJavaMaven
case util.Exists("build.gradle"):
return RuntimeJavaGradle
case util.Exists("app.py"):
return RuntimePython
case util.Exists("index.html"):
return RuntimeStatic
default:
return RuntimeUnknown
}
}
// runtimeConfig performs config inferences based on what Up thinks the runtime is.
func runtimeConfig(runtime Runtime, c *Config) error {
switch runtime {
case RuntimeGo:
golang(c)
case RuntimeClojure:
clojureLein(c)
case RuntimeJavaMaven:
javaMaven(c)
case RuntimeJavaGradle:
javaGradle(c)
case RuntimeCrystal:
crystal(c)
case RuntimePython:
python(c)
case RuntimeStatic:
c.Type = "static"
case RuntimeNode:
if err := nodejs(c); err != nil {
return err
}
}
return nil
}
// golang config.
func golang(c *Config) {
if c.Hooks.Build.IsEmpty() {
c.Hooks.Build = Hook{`GOOS=linux GOARCH=amd64 go build -o server *.go`}
}
if c.Hooks.Clean.IsEmpty() {
c.Hooks.Clean = Hook{`rm server`}
}
if s := c.Stages.GetByName("development"); s != nil {
if s.Proxy.Command == "" {
s.Proxy.Command = "go run *.go"
}
}
}
// java gradle config.
func javaGradle(c *Config) {
if c.Proxy.Command == "" {
c.Proxy.Command = "java -jar server.jar"
}
if c.Hooks.Build.IsEmpty() {
// assumes build results in a shaded jar named server.jar
if util.Exists("gradlew") {
c.Hooks.Build = Hook{`./gradlew clean build && cp build/libs/server.jar .`}
} else {
c.Hooks.Build = Hook{`gradle clean build && cp build/libs/server.jar .`}
}
}
if c.Hooks.Clean.IsEmpty() {
c.Hooks.Clean = Hook{`rm server.jar && gradle clean`}
}
}
// java maven config.
func javaMaven(c *Config) {
if c.Proxy.Command == "" {
c.Proxy.Command = "java -jar server.jar"
}
if c.Hooks.Build.IsEmpty() {
// assumes package results in a shaded jar named server.jar
if util.Exists("mvnw") {
c.Hooks.Build = Hook{`./mvnw clean package && cp target/server.jar .`}
} else {
c.Hooks.Build = Hook{`mvn clean package && cp target/server.jar .`}
}
}
if c.Hooks.Clean.IsEmpty() {
c.Hooks.Clean = Hook{`rm server.jar && mvn clean`}
}
}
// clojure lein config.
func clojureLein(c *Config) {
if c.Proxy.Command == "" {
c.Proxy.Command = "java -jar server.jar"
}
if c.Hooks.Build.IsEmpty() {
// assumes package results in a shaded jar named server.jar
c.Hooks.Build = Hook{`lein uberjar && cp target/*-standalone.jar server.jar`}
}
if c.Hooks.Clean.IsEmpty() {
c.Hooks.Clean = Hook{`lein clean && rm server.jar`}
}
}
// crystal config.
func crystal(c *Config) {
if c.Hooks.Build.IsEmpty() {
c.Hooks.Build = Hook{`docker run --rm -v $(pwd):/src -w /src crystallang/crystal crystal build -o server main.cr --release --static`}
}
if c.Hooks.Clean.IsEmpty() {
c.Hooks.Clean = Hook{`rm server`}
}
if s := c.Stages.GetByName("development"); s != nil {
if s.Proxy.Command == "" {
s.Proxy.Command = "crystal run main.cr"
}
}
}
// nodejs config.
func nodejs(c *Config) error {
var pkg struct {
Scripts struct {
Start string `json:"start"`
Build string `json:"build"`
} `json:"scripts"`
}
// read package.json
if err := util.ReadFileJSON("package.json", &pkg); err != nil && !os.IsNotExist(errors.Cause(err)) {
return err
}
// use "start" script unless explicitly defined in up.json
if c.Proxy.Command == "" {
if s := pkg.Scripts.Start; s == "" {
c.Proxy.Command = `node app.js`
} else {
c.Proxy.Command = s
}
}
// use "build" script unless explicitly defined in up.json
if c.Hooks.Build.IsEmpty() {
c.Hooks.Build = Hook{pkg.Scripts.Build}
}
return nil
}
// python config.
func python(c *Config) {
if c.Proxy.Command == "" {
c.Proxy.Command = "python app.py"
}
// Only add build & clean hooks if a requirements.txt exists
if !util.Exists("requirements.txt") {
return
}
// Set PYTHONPATH env
if c.Environment == nil {
c.Environment = Environment{}
}
c.Environment["PYTHONPATH"] = ".pypath/"
// Copy libraries into .pypath/
if c.Hooks.Build.IsEmpty() {
c.Hooks.Build = Hook{`mkdir -p .pypath/ && pip install -r requirements.txt -t .pypath/`}
}
// Clean .pypath/
if c.Hooks.Clean.IsEmpty() {
c.Hooks.Clean = Hook{`rm -r .pypath/`}
}
}