Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pipelines): add static PipelineBase.isPipeline method #21075

Merged
merged 5 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/@aws-cdk/pipelines/lib/main/pipeline-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Aspects, Stage } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { AddStageOpts as StageOptions, WaveOptions, Wave, IFileSetProducer, ShellStep, FileSet } from '../blueprint';

const PIPELINE_SYMBOL = Symbol.for('@aws-cdk/pipelines.PipelineBase');

/**
* Properties for a `Pipeline`
*/
Expand Down Expand Up @@ -32,6 +34,15 @@ export interface PipelineBaseProps {
* happens first).
*/
export abstract class PipelineBase extends Construct {
/**
* Return whether the given object extends {@link PipelineBase}.
*
* We do attribute detection since we can't reliably use 'instanceof'.
*/
public static isPipeline(x: any): x is PipelineBase {
return x !== null && typeof (x) === 'object' && PIPELINE_SYMBOL in x;
}

/**
* The build step that produces the CDK Cloud Assembly
*/
Expand All @@ -54,6 +65,8 @@ export abstract class PipelineBase extends Construct {
constructor(scope: Construct, id: string, props: PipelineBaseProps) {
super(scope, id);

Object.defineProperty(this, PIPELINE_SYMBOL, { value: true });

if (props.synth instanceof ShellStep && !props.synth.primaryOutput) {
props.synth.primaryOutputDirectory('cdk.out');
}
Expand Down
29 changes: 29 additions & 0 deletions packages/@aws-cdk/pipelines/test/main/pipeline-base.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';
import { App } from '../../../core/lib';
import { PipelineBase, IFileSetProducer, FileSet } from '../../lib';
import { PIPELINE_ENV } from '../testhelpers';

describe('pipeline base', () => {

test('PipelineBase.isPipeline indicates that a construct extends PipelineBase', () => {
const app = new App();
const pipelineStack = new cdk.Stack(app, 'PipelineStack', { env: PIPELINE_ENV });
const construct = new Construct(pipelineStack, 'Construct');
const pipeline = new class extends PipelineBase {
constructor() {
super(pipelineStack, 'Pipeline', {
synth: new class implements IFileSetProducer {
public readonly primaryOutput = new FileSet('PrimaryOutput');
}(),
});
}
protected doBuildPipeline(): void { }
}();

expect(PipelineBase.isPipeline(pipelineStack)).toStrictEqual(false);
expect(PipelineBase.isPipeline(construct)).toStrictEqual(false);
expect(PipelineBase.isPipeline(pipeline)).toStrictEqual(true);
});

});