forked from finos/morphir
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix hidden issue with morphir-generate-test-data
- Loading branch information
1 parent
1d283a4
commit cf110d0
Showing
6 changed files
with
212 additions
and
131 deletions.
There are no files selected for viewing
97 changes: 46 additions & 51 deletions
97
packages/morphir-elm-compiler/src/elm/Morphir/Elm/CLI.d.elm.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,64 @@ | ||
export default CLI = { | ||
init: (flags?: CLI.Args) => CLI.ElmApp | ||
} | ||
init: (flags?: CLI.Args) => CLI.ElmApp, | ||
}; | ||
|
||
export namespace CLI { | ||
|
||
interface ElmApp extends Object { | ||
ports: Ports; | ||
} | ||
|
||
interface Args { | ||
node?: HTMLElement; | ||
flags?: any; | ||
} | ||
|
||
interface Subscribe<T> { | ||
subscribe(callback: (value: T) => any): void; | ||
unsubscribe(callback: (value: T) => any): void; | ||
} | ||
|
||
interface Send<T> { | ||
send(value: T): void; | ||
} | ||
|
||
function init(args: Args): ElmApp; | ||
|
||
interface Ports { | ||
buildFromScratch: Send<any>; | ||
reportProgress: Subscribe<string>; | ||
} | ||
interface ElmApp extends Object { | ||
ports: Ports; | ||
} | ||
|
||
interface Args { | ||
node?: HTMLElement; | ||
flags?: any; | ||
} | ||
|
||
interface Subscribe<T> { | ||
subscribe(callback: (value: T) => any): void; | ||
unsubscribe(callback: (value: T) => any): void; | ||
} | ||
|
||
interface Send<T> { | ||
send(value: T): void; | ||
} | ||
|
||
function init(args: Args): ElmApp; | ||
|
||
interface Ports { | ||
buildFromScratch: Send<any>; | ||
reportProgress: Subscribe<string>; | ||
} | ||
} | ||
|
||
function init(args: CLI.Args): CLI.ElmApp; | ||
|
||
|
||
// export declare class Main { | ||
// init(flags?: any): ElmApp; | ||
|
||
// } | ||
|
||
|
||
|
||
/* | ||
* Prelude | ||
*/ | ||
*/ | ||
|
||
export type Unit = | ||
{ | ||
variant: "()" | ||
} | ||
export type Unit = { | ||
variant: "()"; | ||
}; | ||
|
||
export type Maybe<T> | ||
= { | ||
variant: "Just" | ||
, _0: T | ||
export type Maybe<T> = | ||
| { | ||
variant: "Just"; | ||
_0: T; | ||
} | ||
| { | ||
variant: "Nothing" | ||
} | ||
|
||
export type Result<E, T> | ||
= { | ||
variant: "Ok" | ||
, _0: T | ||
| { | ||
variant: "Nothing"; | ||
}; | ||
|
||
export type Result<E, T> = | ||
| { | ||
variant: "Ok"; | ||
_0: T; | ||
} | ||
| { | ||
variant: "Err" | ||
, _0: E | ||
} | ||
| { | ||
variant: "Err"; | ||
_0: E; | ||
}; |
66 changes: 66 additions & 0 deletions
66
packages/morphir-elm-compiler/src/elm/Morphir/Elm/Generator.d.elm.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
export default Generator = { | ||
init: (flags?: Generator.Args) => Generator.ElmApp, | ||
}; | ||
|
||
export namespace Generator { | ||
interface ElmApp extends Object { | ||
ports: Ports; | ||
} | ||
|
||
interface Args { | ||
node?: HTMLElement; | ||
flags?: any; | ||
} | ||
|
||
interface Subscribe<T> { | ||
subscribe(callback: (value: T) => any): void; | ||
unsubscribe(callback: (value: T) => any): void; | ||
} | ||
|
||
interface Send<T> { | ||
send(value: T): void; | ||
} | ||
|
||
function init(args: Args): ElmApp; | ||
|
||
interface Ports { | ||
generate: Send<any>; | ||
generated: Subscribe<string>; | ||
generationFailed: Subscribe<string>; | ||
decodeFailed: Subscribe<string>; | ||
} | ||
} | ||
|
||
function init(args: Generator.Args): Generator.ElmApp; | ||
|
||
// export declare class Main { | ||
// init(flags?: any): ElmApp; | ||
|
||
// } | ||
|
||
/* | ||
* Prelude | ||
*/ | ||
|
||
export type Unit = { | ||
variant: "()"; | ||
}; | ||
|
||
export type Maybe<T> = | ||
| { | ||
variant: "Just"; | ||
_0: T; | ||
} | ||
| { | ||
variant: "Nothing"; | ||
}; | ||
|
||
export type Result<E, T> = | ||
| { | ||
variant: "Ok"; | ||
_0: T; | ||
} | ||
| { | ||
variant: "Err"; | ||
_0: E; | ||
}; |
170 changes: 96 additions & 74 deletions
170
packages/morphir-elm-compiler/src/morphir-generate-test-data.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,114 @@ | ||
#!/usr/bin/env node | ||
|
||
// NPM imports | ||
import { Command } from 'commander' | ||
import { Command } from "commander"; | ||
|
||
import * as fs from 'fs' | ||
import * as util from 'util' | ||
import * as path from 'path' | ||
import * as fs from "fs"; | ||
import * as util from "util"; | ||
import * as path from "path"; | ||
import Generator from "./elm/Morphir/Elm/Generator.elm"; | ||
|
||
const fsExists = util.promisify(fs.exists) | ||
const fsWriteFile = util.promisify(fs.writeFile) | ||
const fsExists = util.promisify(fs.exists); | ||
const fsWriteFile = util.promisify(fs.writeFile); | ||
//const fsMakeDir = util.promisify(fs.mkdir) | ||
const fsReadFile = util.promisify(fs.readFile) | ||
const fsReadFile = util.promisify(fs.readFile); | ||
//const readdir = util.promisify(fs.readdir) | ||
|
||
const worker = require('./../Morphir.Elm.Generator').Elm.Morphir.Elm.Generator.init() | ||
const worker = Generator.init(); | ||
|
||
// logging | ||
require('log-timestamp') | ||
require("log-timestamp"); | ||
|
||
// Set up Commander | ||
const program = new Command() | ||
program | ||
.name('morphir generate-test-data') | ||
.description('Generate test data for Models (types) in a Morphir IR') | ||
.option('-p, --project-dir <path>', 'Root directory of the project where morphir.json is located.', '.') | ||
.option('-o, --output <path>', 'Target file location where the test data will be saved.', 'test-data.json') | ||
.option('--seed <seed>', 'seed to use for randomness', Date.now().toString()) | ||
.option('--size <size>', 'size of the data to be generated for each target', '1') | ||
.option('--targets <typefqns...>', 'Fully qualified names of types you want to generate test data for.') | ||
.option( | ||
'--config <path-to-config>', | ||
'specify a json file where configuration can be read from. Overrides other command options.' | ||
) | ||
.parse(process.argv) | ||
export const command = new Command(); | ||
command | ||
.name("generate-test-data") | ||
.description("Generate test data for Models (types) in a Morphir IR") | ||
.option( | ||
"-p, --project-dir <path>", | ||
"Root directory of the project where morphir.json is located.", | ||
"." | ||
) | ||
.option( | ||
"-o, --output <path>", | ||
"Target file location where the test data will be saved.", | ||
"test-data.json" | ||
) | ||
.option("--seed <seed>", "seed to use for randomness", Date.now().toString()) | ||
.option( | ||
"--size <size>", | ||
"size of the data to be generated for each target", | ||
"1" | ||
) | ||
.option( | ||
"--targets <typefqns...>", | ||
"Fully qualified names of types you want to generate test data for." | ||
) | ||
.option( | ||
"--config <path-to-config>", | ||
"specify a json file where configuration can be read from. Overrides other command options." | ||
) | ||
.parse(process.argv); | ||
|
||
interface GenerationOptions { | ||
morphirIrJson: any | ||
targets: [string] | ||
seed: number | ||
size: number | ||
morphirIrJson: any; | ||
targets: [string]; | ||
seed: number; | ||
size: number; | ||
} | ||
|
||
// run data generation | ||
async function generateData() { | ||
const programOptions = program.opts() | ||
|
||
// CREATE CONFIG OPTIONS | ||
const morphirJsonPath: string = path.join(programOptions['projectDir'], 'morphir-ir.json') | ||
if (!(await fsExists(morphirJsonPath))) throw Error('Not a morphir directory') | ||
const distroData = (await fsReadFile(morphirJsonPath)).toString() | ||
const distroJson = JSON.parse(distroData) | ||
|
||
if (!programOptions['targets'] || programOptions['targets'].length <= 0) throw 'targets not provided' | ||
|
||
const opts: GenerationOptions = { | ||
morphirIrJson: distroJson, | ||
targets: programOptions['targets'], | ||
seed: parseInt(programOptions['seed']), | ||
size: parseInt(programOptions['size']) | ||
} | ||
|
||
// SEND OFF TO ELM | ||
|
||
worker.ports.decodeFailed.subscribe((err: any) => { | ||
console.log('Decode Failed') | ||
console.log(err) | ||
}) | ||
|
||
worker.ports.generationFailed.subscribe((err: any) => { | ||
console.log('Generation Failed', err) | ||
console.log(err) | ||
}) | ||
|
||
worker.ports.generated.subscribe((data: any) => { | ||
const dataString: string = JSON.stringify(data, null, 4) | ||
const outputPath: string = path.join(programOptions['projectDir'], programOptions['output']) | ||
|
||
console.log('Writing test data to ' + outputPath) | ||
fsWriteFile(outputPath, dataString) | ||
.then(() => console.log('Done.')) | ||
.catch(err => { | ||
console.log(err) | ||
}) | ||
}) | ||
|
||
console.log('starting test data generation with options', opts) | ||
|
||
worker.ports.generate.send(opts) | ||
async function generateData(options) { | ||
const programOptions = options; | ||
|
||
// CREATE CONFIG OPTIONS | ||
const morphirJsonPath: string = path.join( | ||
programOptions["projectDir"], | ||
"morphir-ir.json" | ||
); | ||
if (!(await fsExists(morphirJsonPath))) | ||
throw Error("Not a morphir directory"); | ||
const distroData = (await fsReadFile(morphirJsonPath)).toString(); | ||
const distroJson = JSON.parse(distroData); | ||
|
||
if (!programOptions["targets"] || programOptions["targets"].length <= 0) | ||
throw "targets not provided"; | ||
|
||
const opts: GenerationOptions = { | ||
morphirIrJson: distroJson, | ||
targets: programOptions["targets"], | ||
seed: parseInt(programOptions["seed"]), | ||
size: parseInt(programOptions["size"]), | ||
}; | ||
|
||
// SEND OFF TO ELM | ||
|
||
worker.ports.decodeFailed.subscribe((err: any) => { | ||
console.log("Decode Failed"); | ||
console.log(err); | ||
}); | ||
|
||
worker.ports.generationFailed.subscribe((err: any) => { | ||
console.log("Generation Failed", err); | ||
console.log(err); | ||
}); | ||
|
||
worker.ports.generated.subscribe((data: any) => { | ||
const dataString: string = JSON.stringify(data, null, 4); | ||
const outputPath: string = path.join( | ||
programOptions["projectDir"], | ||
programOptions["output"] | ||
); | ||
|
||
console.log("Writing test data to " + outputPath); | ||
fsWriteFile(outputPath, dataString) | ||
.then(() => console.log("Done.")) | ||
.catch((err) => { | ||
console.log(err); | ||
}); | ||
}); | ||
|
||
console.log("starting test data generation with options", opts); | ||
|
||
worker.ports.generate.send(opts); | ||
} | ||
|
||
generateData() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.