Skip to content

Commit

Permalink
Remove generated source files (#34)
Browse files Browse the repository at this point in the history
* Remove exponent utils

* Remove generated files

* Change build path
  • Loading branch information
jscheiny authored Jun 22, 2018
1 parent bd5d1e6 commit d6c32fc
Show file tree
Hide file tree
Showing 24 changed files with 92 additions and 1,523 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ yarn-error.log

### Editors
.vscode/

### Generated code
generated/
31 changes: 21 additions & 10 deletions codegen/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,31 @@ export function genFileHeader(disableTslint: boolean = true): string[] {
return header;
}

export function genImport(symbols: string[], source: string): string {
symbols.sort((first, second) => {
if (first < second) {
return -1;
}
if (first > second) {
return 1;
}
return 0;
});
interface ImportSpec {
symbols: string[];
source: string;
}

export function genImports(...imports: ImportSpec[]): string[] {
imports.sort((first, second) => order(first.source, second.source));
return imports.map(genImport);
}

function genImport({ symbols, source }: ImportSpec): string {
symbols.sort(order);
return `import { ${symbols.join(", ")} } from "${source}";`;
}

function order(first: string, second: string) {
if (first < second) {
return -1;
}
if (first > second) {
return 1;
}
return 0;
}

export function genUncurriedTypeName(spec: OperatorSpec, left?: string | number, right?: string | number) {
const args = left !== undefined && right !== undefined ? `<${left}, ${right}>` : "";
return `${spec.uncurriedTypeNamePrefix}Exponents${args}`;
Expand Down
45 changes: 34 additions & 11 deletions codegen/emit.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { writeFile } from "fs";
import { exists, mkdir, writeFile } from "fs";
import { genCommonTypes } from "./genCommon";
import { genOperatorTests } from "./genTests";
import { genOperatorTypes } from "./genTypes";
import { codeGenSpec } from "./spec";

const PATH_PREFIX = "src/exponent";
const PATH_PREFIX = "src/exponent/generated";
const TEST_PREFIX = `${PATH_PREFIX}/__test__`;

export interface EmitPlan {
path: string;
Expand All @@ -13,14 +14,15 @@ export interface EmitPlan {

export function emit(callback?: () => void) {
const emits: EmitPlan[] = getEmitPlans();

let index = -1;
const nextEmit = () => {
index++;
const isLastEmit = index === emits.length - 1;
emitFile(emits[index], isLastEmit ? callback : nextEmit);
};
nextEmit();
prepForEmit(() => {
let index = -1;
const nextEmit = () => {
index++;
const isLastEmit = index === emits.length - 1;
emitFile(emits[index], isLastEmit ? callback : nextEmit);
};
nextEmit();
});
}

export function getEmitPlans(): EmitPlan[] {
Expand All @@ -31,12 +33,31 @@ export function getEmitPlans(): EmitPlan[] {
const { fileNamePrefix } = operator;
emits.push(
{ path: `${PATH_PREFIX}/${fileNamePrefix}.ts`, source: genOperatorTypes(operatorSpec) },
{ path: `${PATH_PREFIX}/__test__/${fileNamePrefix}Spec.ts`, source: genOperatorTests(operatorSpec) },
{ path: `${TEST_PREFIX}/${fileNamePrefix}Spec.ts`, source: genOperatorTests(operatorSpec) },
);
});
return emits;
}

function prepForEmit(callback: () => void) {
makeDirectory(PATH_PREFIX, () => makeDirectory(TEST_PREFIX, callback));
}

function makeDirectory(path: string, callback: () => void) {
exists(path, doesExist => {
if (doesExist) {
return callback();
}
mkdir(path, err => {
if (err) {
console.error(`There was an error creating directory ${path}`);
} else {
callback();
}
});
});
}

function emitFile({ path, source }: EmitPlan, callback?: () => void) {
writeFile(path, source, error => {
if (error) {
Expand All @@ -49,3 +70,5 @@ function emitFile({ path, source }: EmitPlan, callback?: () => void) {
}
});
}

emit();
1 change: 1 addition & 0 deletions codegen/genCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function genErrorType(): string[] {
return [
`export type ArithmeticError = "UnitError" & "Arithmetic out of bounds";`,
`export const ArithmeticError = "Arithmetic out of bounds";`,
`export type IsArithmeticError<T> = T extends ArithmeticError ? true : false;`,
"",
];
}
12 changes: 7 additions & 5 deletions codegen/genTests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
genFileHeader,
genImport,
genImports,
genUncurriedTypeName,
genValueName,
getExponents,
Expand All @@ -9,7 +9,7 @@ import {
} from "./common";

export function genOperatorTests(spec: OperatorSpec): string {
const lines: string[] = [...genFileHeader(), ...genImports(spec)];
const lines: string[] = [...genFileHeader(false), ...genTestsImports(spec)];
const exponents = getExponents(spec);
for (const left of exponents) {
for (const right of exponents) {
Expand All @@ -20,10 +20,12 @@ export function genOperatorTests(spec: OperatorSpec): string {
return lines.join("\n");
}

function genImports(spec: OperatorSpec): string[] {
function genTestsImports(spec: OperatorSpec): string[] {
return [
genImport([genUncurriedTypeName(spec)], `../${spec.fileNamePrefix}`),
genImport(["IsArithmeticError"], "../utils"),
...genImports(
{ symbols: ["IsArithmeticError"], source: "../common" },
{ symbols: [genUncurriedTypeName(spec)], source: `../${spec.fileNamePrefix}` },
),
"",
];
}
Expand Down
8 changes: 4 additions & 4 deletions codegen/genTypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
genFileHeader,
genImport,
genImports,
genUncurriedTypeName,
genValueName,
getExponents,
Expand All @@ -11,7 +11,7 @@ import {

export function genOperatorTypes(spec: OperatorSpec): string {
const exponents = getExponents(spec);
let lines: string[] = [...genFileHeader(), ...genImports(), ...genUncurriedType(spec, exponents)];
let lines: string[] = [...genFileHeader(), ...genTypesImports(), ...genUncurriedType(spec, exponents)];
for (const left of exponents) {
if (!(left in spec.specialCases)) {
lines.push(...genCurriedType(spec, exponents, left));
Expand All @@ -20,8 +20,8 @@ export function genOperatorTypes(spec: OperatorSpec): string {
return lines.join("\n");
}

function genImports(): string[] {
return [genImport(["ArithmeticError", "Exponent"], "./common"), ""];
function genTypesImports(): string[] {
return [...genImports({ symbols: ["ArithmeticError", "Exponent"], source: "./common" }), ""];
}

function genUncurriedType(spec: OperatorSpec, exponents: number[]): string[] {
Expand Down
3 changes: 0 additions & 3 deletions codegen/produce.ts

This file was deleted.

44 changes: 0 additions & 44 deletions codegen/verify.ts

This file was deleted.

8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"scripts": {
"codegen:produce": "npm-run-all -s compile:codegen node:codegen:produce",
"codegen:verify": "npm-run-all -s compile:codegen node:codegen:verify",
"codegen": "npm-run-all -s compile:codegen node:codegen",
"compile:src": "tsc -p src",
"compile:codegen": "tsc -p codegen",
"lint": "tslint -p src/tsconfig.json -c tslint.json '{src,codegen}/**/*.ts'",
"lint:fix": "yarn lint --fix",
"node:codegen:produce": "node codegen/dist/produce",
"node:codegen:verify": "node codegen/dist/verify",
"node:codegen": "node codegen/dist/emit",
"test": "jest --config jest.config.js",
"verify": "npm-run-all -s codegen:verify compile:src lint test"
"verify": "npm-run-all -s codegen compile:src lint test"
},
"devDependencies": {
"@types/jest": "^22.2.3",
Expand Down
Loading

0 comments on commit d6c32fc

Please sign in to comment.