Skip to content

Commit

Permalink
wrap skip bundling logic into a stack getter
Browse files Browse the repository at this point in the history
  • Loading branch information
jihndai committed Jan 28, 2022
1 parent dcb8f53 commit 6416349
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
5 changes: 1 addition & 4 deletions packages/@aws-cdk/core/lib/asset-staging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as path from 'path';
import * as cxapi from '@aws-cdk/cx-api';
import { Construct } from 'constructs';
import * as fs from 'fs-extra';
import * as minimatch from 'minimatch';
import { AssetHashType, AssetOptions, FileAssetPackaging } from './assets';
import { BundlingOptions, BundlingOutput } from './bundling';
import { FileSystem, FingerprintOptions } from './fs';
Expand Down Expand Up @@ -188,9 +187,7 @@ export class AssetStaging extends CoreConstruct {
let skip = false;
if (props.bundling) {
// Check if we actually have to bundle for this stack
const bundlingStacks: string[] = this.node.tryGetContext(cxapi.BUNDLING_STACKS) ?? ['*'];
// bundlingStacks is of the form `Stage/Stack`, convert it to `Stage-Stack` before comparing to stack name
skip = !bundlingStacks.find(pattern => minimatch(Stack.of(this).stackName, pattern.replace('/', '-')));
skip = !Stack.of(this).bundlingRequired;
const bundling = props.bundling;
stageThisAsset = () => this.stageByBundling(bundling, skip);
} else {
Expand Down
14 changes: 14 additions & 0 deletions packages/@aws-cdk/core/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as path from 'path';
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import * as cxapi from '@aws-cdk/cx-api';
import { IConstruct, Construct, Node } from 'constructs';
import * as minimatch from 'minimatch';
import { Annotations } from './annotations';
import { App } from './app';
import { Arn, ArnComponents, ArnFormat } from './arn';
Expand Down Expand Up @@ -1153,6 +1154,19 @@ export class Stack extends CoreConstruct implements ITaggable {

return makeStackName(ids);
}

/**
* Indicates whether the stack requires bundling or not
*/
public get bundlingRequired() {
const bundlingStacks: string[] = this.node.tryGetContext(cxapi.BUNDLING_STACKS) ?? ['*'];

// bundlingStacks is of the form `Stage/Stack`, convert it to `Stage-Stack` before comparing to stack name
return bundlingStacks.some(pattern => minimatch(
this.stackName,
pattern.replace('/', '-'),
));
}
}

function merge(template: any, fragment: any): void {
Expand Down
32 changes: 32 additions & 0 deletions packages/@aws-cdk/core/test/stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,38 @@ describe('stack', () => {
expect(new Stack(app, 'Stack', { analyticsReporting: true })._versionReportingEnabled).toBeDefined();

});

test('requires bundling when wildcard is specified in BUNDLING_STACKS', () => {
const app = new App();
const stack = new Stack(app, 'Stack');
stack.node.setContext(cxapi.BUNDLING_STACKS, ['*']);
expect(stack.bundlingRequired).toBe(true);

});

test('requires bundling when stackName has an exact match in BUNDLING_STACKS', () => {
const app = new App();
const stack = new Stack(app, 'Stack');
stack.node.setContext(cxapi.BUNDLING_STACKS, ['Stack']);
expect(stack.bundlingRequired).toBe(true);

});

test('does not require bundling when no item from BUILDING_STACKS matches stackName', () => {
const app = new App();
const stack = new Stack(app, 'Stack');
stack.node.setContext(cxapi.BUNDLING_STACKS, ['Stac']);
expect(stack.bundlingRequired).toBe(false);

});

test('does not require bundling when BUNDLING_STACKS is empty', () => {
const app = new App();
const stack = new Stack(app, 'Stack');
stack.node.setContext(cxapi.BUNDLING_STACKS, []);
expect(stack.bundlingRequired).toBe(false);

});
});

describe('regionalFact', () => {
Expand Down

0 comments on commit 6416349

Please sign in to comment.