Skip to content

Commit

Permalink
feat(pipelines): add static PipelineBase.isPipeline method (#21075)
Browse files Browse the repository at this point in the history
This change adds a new `isPipeline` method to `PipelineBase`. This method works the same way as `Stack.isStack` (checking if a specific `Symbol` property is defined on the provided object), and serves the same purpose: to check if a provided object extends `PipelineBase` (e.g., `CodePipeline` would return true, while `Stack` or `s3.Bucket` would return false).

----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### ~Adding new Unconventional Dependencies:~

~* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)~

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Rabadash8820 authored Aug 15, 2022
1 parent f44eb98 commit ea11f33
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
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);
});

});

0 comments on commit ea11f33

Please sign in to comment.