-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathconfig-inferrer.ts
227 lines (211 loc) · 7.95 KB
/
config-inferrer.ts
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
/**
* Copyright (c) 2021 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/
import { WorkspaceConfig } from "@gitpod/gitpod-protocol";
export interface Context {
config: WorkspaceConfig;
excludeVsCodeConfig: boolean;
exists(path: string): Promise<boolean>;
read(path: string): Promise<string | undefined>;
}
export class ConfigInferrer {
protected contributions: ((ctx: Context) => Promise<void>)[] = [
this.checkNode.bind(this),
this.checkJava.bind(this),
this.checkPython.bind(this),
this.checkGo.bind(this),
this.checkRust.bind(this),
this.checkMake.bind(this),
this.checkNuget.bind(this),
this.checkRuby.bind(this),
];
async getConfig(ctx: Context): Promise<WorkspaceConfig> {
for (const contrib of this.contributions) {
try {
await contrib(ctx);
} catch (e) {
console.log(e);
}
}
return ctx.config;
}
protected async checkNode(ctx: Context): Promise<void> {
const pckjsonContent = await ctx.read("package.json");
if (!pckjsonContent) {
return;
}
let pckjson;
try {
pckjson = JSON.parse(pckjsonContent);
} catch (e) {
console.log(e, pckjsonContent);
}
let command: "yarn" | "npm" | "pnpm" = "npm";
if (await ctx.exists("yarn.lock")) {
command = "yarn";
}
if ((await ctx.exists("pnpm-lock.yaml")) || pckjson?.packageManager?.startsWith("pnpm")) {
command = "pnpm";
}
this.addCommand(ctx.config, command + " install", "init");
if (pckjson.scripts) {
if (pckjson.scripts.build) {
this.addCommand(ctx.config, command + " run build", "init");
} else if (pckjson.scripts.compile) {
this.addCommand(ctx.config, command + " run compile", "init");
}
if (pckjson.scripts.start) {
this.addCommand(ctx.config, command + " run start", "command");
} else if (pckjson.scripts.dev) {
this.addCommand(ctx.config, command + " run dev", "command");
} else if (pckjson.scripts.watch) {
this.addCommand(ctx.config, command + " run watch", "command");
}
}
this.addExtension(ctx, "dbaeumer.vscode-eslint");
}
protected async checkJava(ctx: Context): Promise<void> {
if (await ctx.exists("build.gradle")) {
let cmd = "gradle";
if (await ctx.exists("gradlew")) {
cmd = "./gradlew";
}
this.addCommand(ctx.config, cmd + " build", "init");
this.addExtension(ctx, "redhat.java");
this.addExtension(ctx, "vscjava.vscode-java-debug");
return;
}
if (await ctx.exists("pom.xml")) {
let cmd = "mvn";
if (await ctx.exists("mvnw")) {
cmd = "./mvnw";
}
this.addCommand(ctx.config, cmd + " install -DskipTests=false", "init");
this.addExtension(ctx, "redhat.java");
this.addExtension(ctx, "vscjava.vscode-java-debug");
this.addExtension(ctx, "vscjava.vscode-maven");
return;
}
}
protected addExtension(ctx: Context, extensionName: string) {
if (ctx.excludeVsCodeConfig) {
return;
}
if (!ctx.config.vscode || !ctx.config.vscode.extensions) {
ctx.config.vscode = {
extensions: [],
};
}
if (ctx.config.vscode.extensions?.indexOf(extensionName) === -1)
ctx.config.vscode.extensions!.push(extensionName);
}
protected async isMake(ctx: Context) {
return (await ctx.exists("Makefile")) || (await ctx.exists("makefile"));
}
protected async checkMake(ctx: Context) {
if (await ctx.exists("CMakeLists.txt")) {
this.addCommand(ctx.config, "cmake .", "init");
} else if (await this.isMake(ctx)) {
this.addCommand(ctx.config, "make", "init");
}
}
protected async checkPython(ctx: Context) {
if (await this.isMake(ctx)) {
// https://docs.python-guide.org/writing/structure/#makefile
return;
}
if (await ctx.exists("requirements.txt")) {
this.addCommand(ctx.config, "pip install -r requirements.txt", "init");
this.addExtension(ctx, "ms-python.python");
} else if (await ctx.exists("setup.py")) {
this.addCommand(ctx.config, "pip install .", "init");
this.addExtension(ctx, "ms-python.python");
}
if (await ctx.exists("main.py")) {
this.addCommand(ctx.config, "python main.py", "command");
this.addExtension(ctx, "ms-python.python");
} else if (await ctx.exists("app.py")) {
this.addCommand(ctx.config, "python app.py", "command");
this.addExtension(ctx, "ms-python.python");
} else if (await ctx.exists("runserver.py")) {
this.addCommand(ctx.config, "python runserver.py", "command");
this.addExtension(ctx, "ms-python.python");
}
}
protected async checkGo(ctx: Context) {
if (await ctx.exists("go.mod")) {
this.addCommand(ctx.config, "go get", "init");
this.addCommand(ctx.config, "go build ./...", "init");
this.addCommand(ctx.config, "go test ./...", "init");
this.addCommand(ctx.config, "go run .", "command");
this.addExtension(ctx, "golang.go");
}
}
protected async checkRust(ctx: Context) {
if (await ctx.exists("Cargo.toml")) {
this.addCommand(ctx.config, "cargo build", "init");
this.addCommand(ctx.config, "cargo watch -x run", "command");
this.addExtension(ctx, "matklad.rust-analyzer");
}
}
protected async checkNuget(ctx: Context) {
if (await ctx.exists("packages.config")) {
this.addCommand(ctx.config, "nuget install", "init");
}
}
protected async checkRuby(ctx: Context) {
if (await ctx.exists("bin/setup")) {
this.addCommand(ctx.config, "bin/setup", "init");
} else if (await ctx.exists("Gemfile")) {
this.addCommand(ctx.config, "bundle install", "init");
}
if (await ctx.exists("bin/startup")) {
this.addCommand(ctx.config, "bin/startup", "command");
} else if (await ctx.exists("bin/rails")) {
this.addCommand(ctx.config, "bin/rails server", "command");
}
}
protected addCommand(
config: WorkspaceConfig,
command: string,
phase: "before" | "init" | "command",
unless?: string,
): void {
if (!config.tasks) {
config.tasks = [];
}
if (!config.tasks[0]) {
config.tasks.push({});
}
const existing = config.tasks[0][phase];
if (unless && existing && existing.indexOf(unless) !== -1) {
// skip
return;
}
config.tasks[0][phase] = (existing ? existing + " && " : "") + command;
}
toYaml(config: WorkspaceConfig): string {
const i = " ";
let tasks = "";
if (config.tasks) {
tasks = `tasks:\n${i}- ${config.tasks
.map((task) =>
Object.entries(task)
.map(([phase, command]) => `${phase}: ${command}`)
.join("\n "),
)
.join("\n - ")}`;
}
let vscode = "";
if (config.vscode?.extensions) {
vscode = `vscode:\n${i}extensions:\n${config.vscode.extensions
.map((extension) => `${i + i}- ${extension}`)
.join("\n")}`;
}
return `${tasks}
${vscode}
`;
}
}