forked from finos/morphir-elm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
456 lines (391 loc) · 13.1 KB
/
gulpfile.js
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
const { series, parallel, src, dest } = require('gulp');
const os = require('os')
const path = require('path')
const util = require('util')
const fs = require('fs')
const tmp = require('tmp')
const git = require('isomorphic-git')
const http = require('isomorphic-git/http/node')
const del = require('del')
const elmMake = require('node-elm-compiler').compile
const execa = require('execa');
const shell = require('shelljs')
const mocha = require('gulp-mocha');
const ts = require('gulp-typescript');
const { isExpressionWithTypeArguments } = require('typescript');
const mainTsProject = ts.createProject('./tsconfig.json')
const cliTsProject = ts.createProject('./cli2/tsconfig.json')
const readFile = util.promisify(fs.readFile)
const config = {
morphirJvmVersion: '0.18.2',
morphirJvmCloneDir: tmp.dirSync()
}
const stdio = 'inherit';
async function clean() {
del(['tests-integration/reference-model/Dockerfile'])
return del(['dist'])
}
async function cloneMorphirJVM() {
return await git.clone({
fs,
http,
dir: config.morphirJvmCloneDir.name,
url: 'https://github.com/finos/morphir-jvm',
ref: `tags/v${config.morphirJvmVersion}`,
singleBranch: true
})
}
function copyMorphirJVMAssets() {
const sdkFiles = path.join(config.morphirJvmCloneDir.name, 'morphir/sdk/**')
return src(sdkFiles).pipe(dest('redistributable/Scala/sdk'))
}
async function cleanupMorphirJVM() {
return del(config.morphirJvmCloneDir.name + '/**', { force: true });
}
function checkElmDocs() {
return elmMake([], { docs: "docs.json" })
}
function make(rootDir, source, target) {
return elmMake([source], { cwd: path.join(process.cwd(), rootDir), output: target }) // // nosemgrep : path-join-resolve-traversal
}
function makeCLI() {
return make('cli', 'src/Morphir/Elm/CLI.elm', 'Morphir.Elm.CLI.js')
}
function makeCLI2() {
return make('cli2', 'src/Morphir/Elm/CLI.elm', 'Morphir.Elm.CLI.js')
}
function makeDevCLI() {
return make('cli', 'src/Morphir/Elm/DevCLI.elm', 'Morphir.Elm.DevCLI.js')
}
function makeDevServer() {
return make('cli', 'src/Morphir/Web/DevelopApp.elm', 'web/index.js')
}
function makeDevServerAPI() {
return make('cli', 'src/Morphir/Web/DevelopApp.elm', 'web/insightapp.js')
}
function makeInsightAPI() {
return make('cli', 'src/Morphir/Web/Insight.elm', 'web/insight.js')
}
function makeTryMorphir() {
return make('cli', 'src/Morphir/Web/TryMorphir.elm', 'web/try-morphir.html')
}
const buildCLI2 =
parallel(
compileCli2Ts,
makeCLI2
)
const buildMorphirAPI2 = async ()=>{
try {
await morphirElmMakeRunOldCli('.', './morphir-ir.json', {typesOnly: true})
await morphirElmGen('./morphir-ir.json', './lib/generated', 'TypeScript')
src('./lib/sdk/**/*')
.pipe(dest('./lib/generated/morphir/sdk'))
return await execa('npx tsc', ['--project',path.join('.','lib','tsconfig.json')])
} catch (error) {
console.error("Error building morphir API 2", error);
return error
}
}
const build =
series(
checkElmDocs,
makeCLI,
makeDevCLI,
buildCLI2,
buildMorphirAPI2,
makeDevServer,
makeDevServerAPI,
makeInsightAPI,
makeTryMorphir
)
function morphirElmMake(projectDir, outputPath, options = {}) {
args = ['./cli/morphir-elm.js', 'make', '-p', projectDir, '-o', outputPath]
if (options.typesOnly) {
args.push('--types-only')
}
console.log("Running: " + args.join(' '));
return execa('node', args, { stdio })
}
function morphirElmMakeRunOldCli(projectDir, outputPath, options = {}) {
args = ['./cli/morphir-elm.js', 'make', '-f', '-p', projectDir, '-o', outputPath]
if (options.typesOnly) {
args.push('--types-only')
}
console.log("Running: " + args.join(' '));
return execa('node', args, { stdio })
}
function morphirElmMake2(projectDir, outputPath, options = {}) {
args = ['./cli2/lib/morphir.js', 'make', '-p', projectDir, '-o', outputPath]
if (options.typesOnly) {
args.push('--types-only')
}
console.log("Running: " + args.join(' '));
return execa('node', args, { stdio })
}
// Generate the IR for the Json Schema mdel
function morphirElmMakeJsonSchema(projectDir, outputPath, options = {}) {
args = ['./cli2/lib/morphir.js', 'make', '-p', projectDir, '-o', outputPath]
if (options.typesOnly) {
args.push('--types-only')
}
console.log("Running: " + args.join(' '));
return execa('node', args, { stdio })
}
function morphirElmGen(inputPath, outputDir, target) {
args = ['./cli/morphir-elm.js', 'gen', '-i', inputPath, '-o', outputDir, '-t', target]
console.log("Running: " + args.join(' '));
return execa('node', args, { stdio })
}
// Test the json-schema-gen command.
async function morphirJsonSchemaGen(inputPath, outputDir, target) {
args = ['./cli2/lib/morphir-json-schema-gen.js', 'json-schema-gen', '-i', inputPath, '-o', outputDir, '-t', target]
console.log("Running: " + args.join(' '));
try {
await execa('node', args, {stdio})
} catch (err) {
console.log("Error running json-schema-gen command", err);
throw(err)
}
}
function morphirDockerize(projectDir, options = {}) {
let command = 'dockerize'
let funcLocation = './cli2/lib/morphir-dockerize.js'
let projectDirFlag = '-p'
let overwriteDockerfileFlag = '-f'
let projectDirArgs = [projectDirFlag, projectDir]
args = [
funcLocation,
command,
projectDirArgs.join(' '),
overwriteDockerfileFlag
]
console.log("Running: " + args.join);
return execa('node', args, { stdio })
}
async function testUnit(cb) {
await execa('elm-test');
}
async function compileCli2Ts() {
src('./cli2/*.ts').pipe(cliTsProject()).pipe(dest('./cli2/lib/'))
}
async function compileMain2Ts() {
src('./lib/main.ts').pipe(cliTsProject()).pipe(dest('./cli2/lib/main.js'))
}
function testIntegrationClean() {
return del([
'tests-integration/generated',
'tests-integration/reference-model/morphir-ir.json',
'tests-integration/json-schema/model/dist',
'tests-integration/json-schema/model/morphir-ir.json'
])
}
async function testIntegrationMake(cb) {
await morphirElmMake(
'./tests-integration/reference-model',
'./tests-integration/generated/refModel/morphir-ir.json')
await morphirElmMakeRunOldCli(
'./tests-integration/reference-model',
'./tests-integration/generated/refModel/morphir-ir.json')
await morphirElmMakeJsonSchema(
'./tests-integration/json-schema/model',
'./tests-integration/json-schema/model/morphir-ir.json')
}
async function testIntegrationDockerize() {
await morphirDockerize(
'./tests-integration/reference-model',
)
}
async function testIntegrationJsonSchemaGen() {
await morphirJsonSchemaGen(
'./tests-integration/json-schema/model/morphir-ir.json',
'./tests-integration/json-schema/model/dist',
'JsonSchema'
)
}
async function testIntegrationMorphirTest(cb) {
src('./tests-integration/generated/refModel/morphir-ir.json')
.pipe(dest('./tests-integration/reference-model/'))
await execa(
'node',
['./cli/morphir-elm.js', 'test', '-p', './tests-integration/reference-model'],
{ stdio },
)
}
async function testIntegrationGenScala(cb) {
await morphirElmGen(
'./tests-integration/generated/refModel/morphir-ir.json',
'./tests-integration/generated/refModel/src/scala/',
'Scala')
}
async function testIntegrationBuildScala(cb) {
// try {
// await execa(
// 'mill', ['__.compile'],
// { stdio, cwd: 'tests-integration' },
// )
// } catch (err) {
// if (err.code == 'ENOENT') {
console.log("Skipping testIntegrationBuildScala as `mill` build tool isn't available.");
// } else {
// throw err;
// }
// }
}
async function testIntegrationMakeSpark(cb) {
await morphirElmMake(
'./tests-integration/spark/model',
'./tests-integration/generated/sparkModel/morphir-ir.json')
}
async function testIntegrationGenSpark(cb) {
await morphirElmGen(
'./tests-integration/generated/sparkModel/morphir-ir.json',
'./tests-integration/generated/sparkModel/src/spark/',
'Spark')
}
async function testIntegrationBuildSpark(cb) {
try {
await execa(
'mill', ['__.compile'],
{ stdio, cwd: 'tests-integration' },
)
} catch (err) {
if (err.code == 'ENOENT') {
console.error("Skipping testIntegrationBuildSpark as `mill` build tool isn't available.");
} else {
throw err;
}
}
}
async function testIntegrationTestSpark(cb) {
try {
await execa(
'mill', ['spark.test'],
{ stdio, cwd: 'tests-integration' },
)
} catch (err) {
if (err.code == 'ENOENT') {
console.error("Skipping testIntegrationTestSpark as `mill` build tool isn't available.");
} else {
throw err;
}
}
}
// Generate TypeScript API for reference model.
async function testIntegrationGenTypeScript(cb) {
await morphirElmGen(
'./tests-integration/generated/refModel/morphir-ir.json',
'./tests-integration/generated/refModel/src/typescript/',
'TypeScript')
}
// Compile generated Typescript API and run integration tests.
function testIntegrationTestTypeScript(cb) {
return src('tests-integration/typescript/TypesTest-refModel.ts')
.pipe(mocha({ require: 'ts-node/register' }));
}
async function testCreateCSV(cb) {
if (!shell.which('bash')) {
console.log("Automatically creating CSV files is not available on this platform");
} else {
code_no = shell.exec('bash ./create_csv_files.sh', { cwd: './tests-integration/spark/elm-tests/tests' }).code
if (code_no != 0) {
console.error('ERROR: CSV files cannot be created')
return false;
}
}
}
testIntegrationSpark = series(
testIntegrationMakeSpark,
testIntegrationGenSpark,
testIntegrationBuildSpark,
testIntegrationTestSpark,
)
testIntegration = series(
testIntegrationClean,
testIntegrationMake,
testCreateCSV,
parallel(
testIntegrationMorphirTest,
//testIntegrationSpark,
series(
testIntegrationGenScala,
testIntegrationBuildScala,
),
series(
testIntegrationGenTypeScript,
testIntegrationTestTypeScript,
),
),testIntegrationDockerize,
testIntegrationJsonSchemaGen
)
async function testMorphirIRMake(cb) {
await morphirElmMake('.', 'tests-integration/generated/morphirIR/morphir-ir.json',
{ typesOnly: true })
}
// Generate TypeScript API for Morphir.IR itself.
async function testMorphirIRGenTypeScript(cb) {
await morphirElmGen(
'./tests-integration/generated/morphirIR/morphir-ir.json',
'./tests-integration/generated/morphirIR/src/typescript/',
'TypeScript')
}
// Compile generated Typescript API and run integration tests.
function testMorphirIRTestTypeScript(cb) {
return src('tests-integration/typescript/CodecsTest-Morphir-IR.ts')
.pipe(mocha({ require: 'ts-node/register' }));
}
// Make sure all dependencies are permitted in highly-restricted environments as well
async function checkPackageLockJson() {
const packageLockJson = JSON.parse((await readFile('package-lock.json')).toString())
const hasRuntimeDependencyOnPackage = (packageName) => {
const runtimeDependencyInPackages =
packageLockJson.packages
&& packageLockJson.packages[`node_modules/${packageName}`]
&& !packageLockJson.packages[`node_modules/${packageName}`].dev
const runtimeDependencyInDependencies =
packageLockJson.dependencies
&& packageLockJson.dependencies[packageName]
&& !packageLockJson.dependencies[packageName].dev
return runtimeDependencyInPackages || runtimeDependencyInDependencies
}
if (hasRuntimeDependencyOnPackage('binwrap')) {
throw Error('Runtime dependency on binwrap was detected!')
}
}
testMorphirIR = series(
testMorphirIRMake,
testMorphirIRGenTypeScript,
testMorphirIRTestTypeScript,
)
const test =
parallel(
testUnit,
testIntegration,
// testMorphirIR,
)
const csvfiles = series(
testCreateCSV,
)
exports.clean = clean;
exports.makeCLI = makeCLI;
exports.makeDevCLI = makeDevCLI;
exports.buildCLI2 = buildCLI2;
exports.compileMain2Ts = compileMain2Ts;
exports.build = build;
exports.test = test;
exports.csvfiles = csvfiles;
exports.testIntegration = testIntegration;
exports.testIntegrationSpark = testIntegrationSpark;
exports.testMorphirIR = testMorphirIR;
exports.testMorphirIRTypeScript = testMorphirIR;
exports.checkPackageLockJson = checkPackageLockJson;
exports.default =
series(
clean,
checkPackageLockJson,
series(
cloneMorphirJVM,
copyMorphirJVMAssets,
cleanupMorphirJVM
),
build
);