Skip to content

Commit

Permalink
Fix hidden issue with morphir-generate-test-data
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianReeves committed May 26, 2024
1 parent 1d283a4 commit cf110d0
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 131 deletions.
97 changes: 46 additions & 51 deletions packages/morphir-elm-compiler/src/elm/Morphir/Elm/CLI.d.elm.ts
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;
};
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 packages/morphir-elm-compiler/src/morphir-generate-test-data.ts
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()
6 changes: 2 additions & 4 deletions packages/morphir-elm-compiler/src/morphir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Command } from "commander";
import { readPackageUpSync } from "read-package-up";
import * as morphirDockerize from "./morphir-dockerize";
import * as morphirGenerateTestData from "./morphir-generate-test-data";
import * as morphirInit from "./morphir-init";
import * as morphirJsonSchemaGen from "./morphir-json-schema-gen";
import * as morphirMake from "./morphir-make";
Expand All @@ -26,9 +27,6 @@ program
.addCommand(morphirStats.command)
.addCommand(morphirDockerize.command)
.addCommand(testCoverage.command)
.command(
"generate-test-data",
"Creates a docker image of a Morphir IR and Morphir Develop"
)
.addCommand(morphirGenerateTestData.command)
.addCommand(morphirInit.command)
.parse(process.argv);
File renamed without changes.
Loading

0 comments on commit cf110d0

Please sign in to comment.