Skip to content

Commit

Permalink
feat: propogate flatten (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaryt authored Jun 29, 2022
1 parent b4d790a commit 0d4a872
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/lib/Components/Commands/exports/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ import {
export interface Command extends Generable {
name: StringParameter;
parameters?: CommandParameters;
generate(): CommandShape | CommandShorthandShape;
generate(flatten?: boolean): CommandShape | CommandShorthandShape;
}
19 changes: 1 addition & 18 deletions src/lib/Components/Executors/exports/ReusableExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,7 @@ export class ReusableExecutor
* Generate Reusable Executor schema.
* @returns The generated JSON for the Reusable Executor.
*/
generate(
ctx?: GenerableType,
): ReusableExecutorsShape | ReusableExecutorJobRefShape {
if (ctx == GenerableType.JOB) {
// TODO: Enable for 'minification'
// if (!this.parameters) {
// return {
// executor: this.name;
// }
// }

return {
executor: {
name: this.name,
},
};
}

generate(): ReusableExecutorsShape | ReusableExecutorJobRefShape {
return {
[this.name]: {
...this.executor.generate(),
Expand Down
8 changes: 4 additions & 4 deletions src/lib/Components/Executors/exports/ReusedExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ export class ReusedExecutor implements Generable {
* Generate Reused Executor schema.
* @returns The generated JSON for the Reused Executor.
*/
generate(): ReusedExecutorShape {
generate(flatten?: boolean): ReusedExecutorShape {
return {
executor: this.generateContents(),
executor: this.generateContents(flatten),
};
}
/**
* Generate Reused Executor schema.
* @returns The generated JSON for the Reused Executor.
*/
generateContents(): ReusedExecutorShapeContents {
if (this._parameters) {
generateContents(flatten?: boolean): ReusedExecutorShapeContents {
if (this._parameters || !flatten) {
return {
name: this._executor.name,
...this._parameters,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Components/Job/exports/ParameterizedJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ class ParameterizedJob
* Generate the internal contents of this job.
* @returns The job contents in it's generated shape.
*/
generateContents(): ParameterizedJobContents {
generateContents(flatten?: boolean): ParameterizedJobContents {
return {
parameters: this.parameters.generate(),
...super.generateContents(),
...super.generateContents(flatten),
};
}

Expand Down
10 changes: 5 additions & 5 deletions src/lib/Components/Job/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ export class Job implements Generable {
* Generates the contents of the Job.
* @returns The generated JSON for the Job's contents.
*/
generateContents(): JobContentsShape {
generateContents(flatten?: boolean): JobContentsShape {
const generatedSteps = this.steps.map((step) => {
return step.generate();
return step.generate(flatten);
});
const generatedExecutor = this.executor.generate();
const generatedExecutor = this.executor.generate(flatten);

return { steps: generatedSteps, ...generatedExecutor };
}
/**
* Generate Job schema
* @returns The generated JSON for the Job.
*/
generate(): JobsShape {
generate(flatten?: boolean): JobsShape {
return {
[this.name]: this.generateContents(),
[this.name]: this.generateContents(flatten),
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/Components/Logic/exports/Condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { Evaluable } from './Evaluable';

export abstract class Condition implements Generable, Evaluable<boolean> {
abstract evaluate(): boolean;
abstract generate(ctx?: GenerableType): AnyConditionShape;
abstract generate(flatten?: boolean): AnyConditionShape;
abstract get generableType(): GenerableType;
}
2 changes: 1 addition & 1 deletion src/lib/Components/Logic/exports/When.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class When implements Generable {
}

generate(): AnyConditionShape {
return this.condition.generate(this.generableType);
return this.condition.generate();
}

get generableType(): GenerableType {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Components/Logic/exports/conditions/And.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class And extends Condition {
}

generate(): AndConditionShape {
return { and: this.conditions.map((c) => c.generate(this.generableType)) };
return { and: this.conditions.map((c) => c.generate()) };
}

get generableType(): GenerableType {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Components/Logic/exports/conditions/Not.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class Not extends Condition {
}

generate(): NotConditionShape {
return { not: this.condition.generate(this.generableType) };
return { not: this.condition.generate() };
}

get generableType(): GenerableType {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Components/Logic/exports/conditions/Or.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Or extends Condition {
}

generate(): OrConditionShape {
return { or: this.conditions.map((c) => c.generate(this.generableType)) };
return { or: this.conditions.map((c) => c.generate()) };
}

get generableType(): GenerableType {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Components/Workflow/exports/WorkflowJobAbstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ export abstract class WorkflowJobAbstract implements Generable {
}

abstract get name(): string;
abstract generate(ctx?: GenerableType): WorkflowJobShape;
abstract generate(): WorkflowJobShape;
}
4 changes: 2 additions & 2 deletions src/lib/Components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ export interface Generable {
* Generate the CircleCI YAML equivalent JSON for config compilation
* Generable's name is the key in the output.
*/
generate(ctx?: GenerableType): unknown;
generate(flatten?: boolean): unknown;

/**
* Generate the CircleCI YAML equivalent JSON contents for config compilation
*/
generateContents?(): unknown;
generateContents?(flatten?: boolean): unknown;

/**
* Type of generable object
Expand Down
18 changes: 13 additions & 5 deletions src/lib/Config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,17 @@ export class Config
/**
* Export the CircleCI configuration as a YAML string.
*/
generate(): string {
generate(flatten?: boolean): string {
const generatedWorkflows = generateList<WorkflowsShape>(this.workflows, {});
const generatedJobs = generateList<JobsShape>(this.jobs, {});
const generatedJobs = generateList<JobsShape>(this.jobs, {}, flatten);
const generatedExecutors = generateList<ReusableExecutorsShape>(
this.executors,
);
const generatedCommands = generateList<CustomCommandShape>(this.commands);
const generatedCommands = generateList<CustomCommandShape>(
this.commands,
undefined,
flatten,
);
const generatedParameters = this.parameters?.generate();
const generatedOrbs = generateList<OrbImportsShape>(this.orbs);

Expand Down Expand Up @@ -220,15 +224,19 @@ export class Config
}
}

function generateList<Shape>(listIn?: Generable[], failSafe?: Shape): Shape {
function generateList<Shape>(
listIn?: Generable[],
failSafe?: Shape,
flatten?: boolean,
): Shape {
if (!listIn) {
return failSafe as Shape;
}

return Object.assign(
{},
...listIn.map((generable) => {
return generable.generate();
return generable.generate(flatten);
}),
);
}
Expand Down
8 changes: 2 additions & 6 deletions tests/Executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,11 @@ describe('Generate a config with a Reusable Executor with parameters', () => {
new CircleCI.parameters.CustomParametersList(),
);
const expectedUsageShape = {
executor: {
name: 'default',
},
executor: 'default',
};

it('Should match the expected output in job context', () => {
expect(reusable.generate(CircleCI.mapping.GenerableType.JOB)).toEqual(
expectedUsageShape,
);
expect(reusable.reuse().generate(true)).toEqual(expectedUsageShape);
});

it('Should throw error during parsing', () => {
Expand Down
18 changes: 8 additions & 10 deletions tests/Workflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,17 @@ describe('Instantiate a new Workflow with a when condition', () => {
const job = new CircleCI.Job('my-job', docker, [helloWorld]);
const workflowJob = new CircleCI.workflow.WorkflowJob(job);
const { and, or, equal, not } = CircleCI.logic;
const myWorkflow = new CircleCI.Workflow(
'my-workflow',
[workflowJob],
new CircleCI.logic.When(
or(
and(
'<< parameters.should_retry >>',
equal('<< parameters.attempt >>', 3),
),
not(equal('<< parameters.user >>', 'bob')),
const when = new CircleCI.logic.When(
or(
and(
'<< parameters.should_retry >>',
equal('<< parameters.attempt >>', 3),
),
not(equal('<< parameters.user >>', 'bob')),
),
);

const myWorkflow = new CircleCI.Workflow('my-workflow', [workflowJob], when);
const generatedWorkflow = myWorkflow.generate();
const expected = {
'my-workflow': {
Expand Down Expand Up @@ -166,6 +163,7 @@ describe('Instantiate a new Workflow with a when condition', () => {
expect(workflowJob.generableType).toBe(
CircleCI.mapping.GenerableType.WORKFLOW_JOB,
);
expect(when.generableType).toBe(CircleCI.mapping.GenerableType.WHEN);
});
});

Expand Down

0 comments on commit 0d4a872

Please sign in to comment.