/
lang.go
290 lines (254 loc) · 7.12 KB
/
lang.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
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package code
import (
"fmt"
"path"
"github.com/goodrain/rainbond/util"
)
func init() {
checkFuncList = append(checkFuncList, dockerfile)
checkFuncList = append(checkFuncList, javaJar)
checkFuncList = append(checkFuncList, javaWar)
checkFuncList = append(checkFuncList, javaMaven)
checkFuncList = append(checkFuncList, php)
checkFuncList = append(checkFuncList, python)
checkFuncList = append(checkFuncList, nodejs)
checkFuncList = append(checkFuncList, ruby)
checkFuncList = append(checkFuncList, static)
checkFuncList = append(checkFuncList, clojure)
checkFuncList = append(checkFuncList, golang)
checkFuncList = append(checkFuncList, gradle)
checkFuncList = append(checkFuncList, grails)
checkFuncList = append(checkFuncList, scala)
checkFuncList = append(checkFuncList, netcore)
}
//ErrCodeNotExist 代码为空错误
var ErrCodeNotExist = fmt.Errorf("code is not exist")
//ErrCodeDirNotExist 代码目录不存在
var ErrCodeDirNotExist = fmt.Errorf("code dir is not exist")
//ErrCodeUnableIdentify 代码无法识别语言
var ErrCodeUnableIdentify = fmt.Errorf("code lang unable to identify")
//ErrRainbondFileNotFound rainbond file not found
var ErrRainbondFileNotFound = fmt.Errorf("rainbond file not found")
//Lang 语言类型
type Lang string
//String return lang string
func (l Lang) String() string {
return string(l)
}
//NO 空语言类型
var NO Lang = "no"
//Dockerfile Lang
var Dockerfile Lang = "dockerfile"
//Docker Lang
var Docker Lang = "docker"
//Python Lang
var Python Lang = "Python"
//Ruby Lang
var Ruby Lang = "Ruby"
//PHP Lang
var PHP Lang = "PHP"
//JavaMaven Lang
var JavaMaven Lang = "Java-maven"
//JaveWar Lang
var JaveWar Lang = "Java-war"
//JavaJar Lang
var JavaJar Lang = "Java-jar"
//Nodejs Lang
var Nodejs Lang = "Node.js"
//Static Lang
var Static Lang = "static"
//Clojure Lang
var Clojure Lang = "Clojure"
//Golang Lang
var Golang Lang = "Go"
//Gradle Lang
var Gradle Lang = "Gradle"
//Grails Lang
var Grails Lang = "Grails"
//NetCore Lang
var NetCore Lang = ".NetCore"
//GetLangType check code lang
func GetLangType(homepath string) (Lang, error) {
if ok, _ := util.FileExists(homepath); !ok {
return NO, ErrCodeDirNotExist
}
//判断是否有代码
if ok := util.IsHaveFile(homepath); !ok {
return NO, ErrCodeNotExist
}
//获取确定的语言
for _, check := range checkFuncList {
if lang := check(homepath); lang != NO {
return lang, nil
}
}
//获取可能的语言
//无法识别
return NO, ErrCodeUnableIdentify
}
type langTypeFunc func(homepath string) Lang
var checkFuncList []langTypeFunc
func dockerfile(homepath string) Lang {
if ok, _ := util.FileExists(path.Join(homepath, "Dockerfile")); !ok {
return NO
}
return Dockerfile
}
func python(homepath string) Lang {
if ok, _ := util.FileExists(path.Join(homepath, "requirements.txt")); ok {
return Python
}
if ok, _ := util.FileExists(path.Join(homepath, "setup.py")); ok {
return Python
}
if ok, _ := util.FileExists(path.Join(homepath, "Pipfile")); ok {
return Python
}
return NO
}
func ruby(homepath string) Lang {
if ok, _ := util.FileExists(path.Join(homepath, "Gemfile")); ok {
return Ruby
}
return NO
}
func php(homepath string) Lang {
if ok, _ := util.FileExists(path.Join(homepath, "composer.json")); ok {
return PHP
}
if ok := util.SearchFile(homepath, "index.php", 2); ok {
return PHP
}
return NO
}
func javaMaven(homepath string) Lang {
if ok, _ := util.FileExists(path.Join(homepath, "pom.xml")); ok {
return JavaMaven
}
if ok, _ := util.FileExists(path.Join(homepath, "pom.atom")); ok {
return JavaMaven
}
if ok, _ := util.FileExists(path.Join(homepath, "pom.clj")); ok {
return JavaMaven
}
if ok, _ := util.FileExists(path.Join(homepath, "pom.groovy")); ok {
return JavaMaven
}
if ok, _ := util.FileExists(path.Join(homepath, "pom.rb")); ok {
return JavaMaven
}
if ok, _ := util.FileExists(path.Join(homepath, "pom.scala")); ok {
return JavaMaven
}
if ok, _ := util.FileExists(path.Join(homepath, "pom.yaml")); ok {
return JavaMaven
}
if ok, _ := util.FileExists(path.Join(homepath, "pom.yml")); ok {
return JavaMaven
}
return NO
}
func javaWar(homepath string) Lang {
if ok := util.FileExistsWithSuffix(homepath, ".war"); ok {
return JaveWar
}
return NO
}
//javaJar Procfile必须定义
func javaJar(homepath string) Lang {
if ok := util.FileExistsWithSuffix(homepath, ".jar"); ok {
return JavaJar
}
return NO
}
func nodejs(homepath string) Lang {
if ok, _ := util.FileExists(path.Join(homepath, "package.json")); ok {
return Nodejs
}
return NO
}
func static(homepath string) Lang {
if ok, _ := util.FileExists(path.Join(homepath, "index.html")); ok {
return Static
}
if ok, _ := util.FileExists(path.Join(homepath, "index.htm")); ok {
return Static
}
if ok, _ := util.FileExists(path.Join(homepath, "static.json")); ok {
return Static
}
return NO
}
func clojure(homepath string) Lang {
if ok, _ := util.FileExists(path.Join(homepath, "project.clj")); ok {
return Clojure
}
return NO
}
func golang(homepath string) Lang {
if ok, _ := util.FileExists(path.Join(homepath, "go.mod")); ok {
return Golang
}
if ok, _ := util.FileExists(path.Join(homepath, "Gopkg.lock")); ok {
return Golang
}
if ok, _ := util.FileExists(path.Join(homepath, "Godeps", "Godeps.json")); ok {
return Golang
}
if ok, _ := util.FileExists(path.Join(homepath, "vendor", "vendor.json")); ok {
return Golang
}
if ok, _ := util.FileExists(path.Join(homepath, "glide.yaml")); ok {
return Golang
}
if ok := util.FileExistsWithSuffix(path.Join(homepath, "src"), ".go"); ok {
return Golang
}
return NO
}
func gradle(homepath string) Lang {
if ok, _ := util.FileExists(path.Join(homepath, "build.gradle")); ok {
return Gradle
}
if ok, _ := util.FileExists(path.Join(homepath, "gradlew")); ok {
return Gradle
}
if ok, _ := util.FileExists(path.Join(homepath, "settings.gradle")); ok {
return Gradle
}
return NO
}
func grails(homepath string) Lang {
if ok, _ := util.FileExists(path.Join(homepath, "grails-app")); ok {
return Grails
}
return NO
}
//netcore
func netcore(homepath string) Lang {
if ok := util.FileExistsWithSuffix(homepath, ".sln"); ok {
return NetCore
}
if ok := util.FileExistsWithSuffix(homepath, ".csproj"); ok {
return NetCore
}
return NO
}
//暂时不支持
func scala(homepath string) Lang {
return NO
}