-
Notifications
You must be signed in to change notification settings - Fork 455
/
constructs-maker.ts
367 lines (303 loc) · 11.1 KB
/
constructs-maker.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import * as fs from 'fs-extra';
import * as path from 'path';
import { CodeMaker } from 'codemaker';
import { mkdtemp } from '../util';
import * as srcmak from 'jsii-srcmak';
import { TerraformModuleConstraint, TerraformDependencyConstraint } from '../config'
import { ProviderSchema, readSchema } from './generator/provider-schema';
import { TerraformProviderGenerator } from './generator/provider-generator';
import { ModuleGenerator } from './generator/module-generator';
import { ModuleSchema } from './generator/module-schema';
import { versionNumber } from '../../bin/cmds/version-check';
import { ReportParams, ReportRequest } from '../checkpoint'
const VERSION = versionNumber();
export enum Language {
TYPESCRIPT = 'typescript',
PYTHON = 'python',
CSHARP = 'csharp',
JAVA = 'java',
GO = 'go',
}
export const LANGUAGES = [ Language.TYPESCRIPT, Language.PYTHON, Language.JAVA, Language.CSHARP, Language.GO ];
export interface GetOptions {
readonly targetLanguage: Language;
readonly codeMakerOutput: string;
/**
* Path to copy the output .jsii file.
* @default - jsii file is not emitted
*/
readonly outputJsii?: string;
}
export abstract class ConstructsMakerTarget {
public readonly fileName: string;
constructor(public readonly constraint: TerraformDependencyConstraint, public readonly targetLanguage: Language) {
if (this.constraint instanceof TerraformModuleConstraint) {
this.fileName = `${this.typesPath(this.constraint.fqn)}.ts`
} else {
this.fileName = `${this.typesPath(this.constraint.name)}.ts`
}
}
public static from(constraint: TerraformDependencyConstraint, targetLanguage: Language) {
if (constraint instanceof TerraformModuleConstraint) {
return new ConstructsMakerModuleTarget(constraint, targetLanguage)
} else {
return new ConstructsMakerProviderTarget(constraint, targetLanguage)
}
}
public get version() {
return this.constraint.version
}
public get source() {
return this.constraint.source
}
public get name() {
return this.constraint.name
}
public get fqn() {
return this.constraint.fqn
}
public get namespace() {
return this.constraint.namespace
}
public get moduleKey() {
return this.fqn.replace(/\//gi, '_')
}
public abstract get srcMakName(): string;
public abstract get isModule(): boolean;
public abstract get isProvider(): boolean;
public abstract get trackingPayload(): Record<string, any>;
protected abstract get simplifiedName(): string;
protected abstract typesPath(name: string): string;
}
export class ConstructsMakerModuleTarget extends ConstructsMakerTarget {
public spec?: ModuleSchema
public get isModule() {
return true
}
public get isProvider() {
return false
}
public get srcMakName(): string {
switch (this.targetLanguage) {
case Language.PYTHON:
case Language.GO:
return this.simplifiedName;
case Language.JAVA:
case Language.CSHARP:
default:
return this.constraint.fqn;
}
}
public get trackingPayload() {
return {
name: this.name,
fullName: this.source,
version: this.version,
type: 'module'
}
}
protected typesPath(name: string): string {
return `modules/${name}`;
}
protected get simplifiedName(): string {
return this.fqn.replace(/\//gi, '.').replace(/-/gi, '_');
}
}
export class ConstructsMakerProviderTarget extends ConstructsMakerTarget {
public spec?: ProviderSchema
public get isModule() {
return false
}
public get isProvider() {
return true
}
public get srcMakName(): string {
switch (this.targetLanguage) {
case Language.JAVA:
// "null" is a reserved keyword and can't be used as a package name
return this.isNullProvider ? "nullprovider" : this.simplifiedName;
case Language.CSHARP:
// "null" is a reserved keyword and can't be used as a namespace
return this.isNullProvider ? "Providers.Null" : this.simplifiedName;
case Language.PYTHON:
return this.simplifiedName;
case Language.GO:
return this.name;
default:
return this.constraint.fqn;
}
}
public get trackingPayload() {
return {
name: this.name,
fullName: this.source,
version: this.version,
type: 'provider'
}
}
protected typesPath(name: string): string {
return `providers/${name}/index`;
}
private get isNullProvider() {
return this.constraint.name === "null"
}
protected get simplifiedName(): string {
return this.name.replace(/\//gi, '.').replace(/-/gi, '_');
}
}
export class ConstructsMaker {
private readonly codeMakerOutdir: string;
private readonly code: CodeMaker;
private readonly targets: ConstructsMakerTarget[];
constructor(private readonly options: GetOptions, private readonly constraints: TerraformDependencyConstraint[]) {
this.codeMakerOutdir = path.resolve(this.options.codeMakerOutput);
fs.mkdirpSync(this.codeMakerOutdir);
this.code = new CodeMaker();
this.targets = this.constraints.map(constraint => ConstructsMakerTarget.from(constraint, this.options.targetLanguage))
}
private async generateTypeScript() {
const schema = await readSchema(this.targets);
const moduleTargets: ConstructsMakerModuleTarget[] = this.targets.filter(target => target instanceof ConstructsMakerModuleTarget) as ConstructsMakerModuleTarget[]
for (const target of moduleTargets) {
target.spec = schema.moduleSchema[target.moduleKey]
}
const providerTargets: ConstructsMakerProviderTarget[] = this.targets.filter(target => target instanceof ConstructsMakerProviderTarget) as ConstructsMakerProviderTarget[];
if (providerTargets.length > 0) {
new TerraformProviderGenerator(this.code, schema.providerSchema, providerTargets);
}
if (moduleTargets.length > 0) {
new ModuleGenerator(this.code, moduleTargets);
}
}
public async generate() {
await this.generateTypeScript();
if (this.isJavascriptTarget) {
await this.save()
}
if (!this.isJavascriptTarget || this.options.outputJsii) {
for (const target of this.targets) {
await mkdtemp(async staging => {
// this is not typescript, so we generate in a staging directory and
// use jsii-srcmak to compile and extract the language-specific source
// into our project.
await this.save(staging);
// these are the module dependencies we compile against
const deps = ['@types/node', 'constructs', 'cdktf'];
const opts: srcmak.Options = {
entrypoint: target.fileName,
deps: deps.map(dep => path.dirname(require.resolve(`${dep}/package.json`))),
moduleKey: target.moduleKey
};
// used for testing.
if (this.options.outputJsii) {
opts.jsii = { path: this.options.outputJsii };
}
if (this.isPythonTarget) {
opts.python = {
outdir: this.codeMakerOutdir,
moduleName: target.srcMakName
};
}
if (this.isJavaTarget) {
opts.java = {
outdir: '.', // generated java files aren't packaged, so just include directly in app
package: `imports.${target.srcMakName}`
}
}
if (this.isCsharpTarget) {
opts.csharp = {
outdir: this.codeMakerOutdir,
namespace: target.srcMakName
}
}
if (this.isGoTarget) {
// TODO: check if needed for modules somehow
// const targetType = target.isProvider ? 'provider' : 'module';
// jsii-srcmac will produce a folder inside this dir named after "packageName"
// so this results in e.g. .gen/hashicorp/random
const outdir = path.join(this.codeMakerOutdir, target.namespace ?? '');
opts.golang = {
outdir,
moduleName: (await determineGoModuleName(outdir)), // e.g. `github.com/org/userproject/.gen/hashicorp`
packageName: target.srcMakName // package will be named e.g. random for hashicorp/random
}
}
// increase memory to allow generating large providers (i.e. aws for Go)
// srcmak is going to spawn a childprocess (for jsii-pacmak) which is going to be affected by this env var
process.env.NODE_OPTIONS = "--max-old-space-size=8192";
await srcmak.srcmak(staging, opts);
});
}
}
for (const target of this.targets) {
await report(target)
}
}
private async save(outdir = this.codeMakerOutdir) {
await this.code.save(outdir);
}
private get isJavascriptTarget() {
return this.options.targetLanguage === Language.TYPESCRIPT
}
private get isPythonTarget() {
return this.options.targetLanguage === Language.PYTHON
}
private get isJavaTarget() {
return this.options.targetLanguage === Language.JAVA
}
private get isCsharpTarget() {
return this.options.targetLanguage === Language.CSHARP
}
private get isGoTarget() {
return this.options.targetLanguage === Language.GO
}
}
const report = async (target: ConstructsMakerTarget): Promise<void> => {
const reportParams: ReportParams = {
command: 'get',
product: 'cdktf',
version: VERSION,
dateTime: new Date(),
payload: target.trackingPayload,
language: target.targetLanguage
};
await ReportRequest(reportParams);
}
/**
* searches for the closest `go.mod` file and returns the nested go module name for `dir`
* e.g. (/dir/.gen/) => cdk.tf/stack/.gen if the parent dir of .gen has a go.mod for "module cdk.tf/stack"
*
* @param dir the directory to start the search from (searches upwards)
* @returns the package name for `dir`
* @throws an Error if no go.mod was found
*/
export const determineGoModuleName = async (dir: string): Promise<string> => {
let previousDir;
let currentDir = path.resolve(dir);
do {
let files: string[] = [];
try {
files = await fs.readdir(currentDir);
} catch (e) {
// directory might not exist yet, but we still walk upwards from there, so ignore 'ENOENT'
if (e.code !== 'ENOENT') {
throw e;
}
}
if (files.includes('go.mod')) {
const file = path.resolve(currentDir, 'go.mod');
const gomod = await fs.readFile(file);
const match = /^module\s*(\S*)\s*$/m.exec(gomod.toString())
if (match && match[1]) {
const childdir = path.relative(currentDir, dir).replace(/\\/g, '/'); // replace '\' with '/' for windows paths
return childdir.length > 0 ? `${match[1]}/${childdir}` : match[1];
}
throw new Error(`Could not determine the root Go module name. Found ${file} but failed to regex match the module name directive`);
}
// go up one directory. As dirname('/') will return '/' we cancel the loop
// as soon as the dir does not change anymore.
previousDir = currentDir;
currentDir = path.dirname(currentDir);
} while (currentDir !== previousDir);
throw new Error(`Could not determine the root Go module name. No go.mod found in ${dir} and any parent directories`);
}