Skip to content

Commit

Permalink
chore(cli): remove unreachable v1 compat in stack selection (#32620)
Browse files Browse the repository at this point in the history
### Reason for this change

Removing dead code. The version number of the `CLI is never < 2` in release and thus this code cannot be reached.
This allowed to remove the env variable hack that enabled test scenarios to use v2 behavior.

### Description of changes

Remove the unreachable if statement and then optimize code.
Also includes some minor reformatting of method parameters.

### Describe any new or updated permissions being added

n/a

### Description of how you validated changes

Test pipeline.

### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
mrgrain authored Dec 21, 2024
1 parent c4136bd commit 0546ec2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 29 deletions.
41 changes: 17 additions & 24 deletions packages/aws-cdk/lib/api/cxapp/cloud-assembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { minimatch } from 'minimatch';
import * as semver from 'semver';
import { error, print, warning } from '../../logging';
import { flatten } from '../../util';
import { versionNumber } from '../../version';

export enum DefaultSelection {
/**
Expand Down Expand Up @@ -122,43 +121,35 @@ export class CloudAssembly {
}
}

private selectTopLevelStacks(stacks: cxapi.CloudFormationStackArtifact[],
private selectTopLevelStacks(
stacks: cxapi.CloudFormationStackArtifact[],
topLevelStacks: cxapi.CloudFormationStackArtifact[],
extend: ExtendedStackSelection = ExtendedStackSelection.None): StackCollection {
extend: ExtendedStackSelection = ExtendedStackSelection.None,
): StackCollection {
if (topLevelStacks.length > 0) {
return this.extendStacks(topLevelStacks, stacks, extend);
} else {
throw new Error('No stack found in the main cloud assembly. Use "list" to print manifest');
}
}

private selectMatchingStacks(stacks: cxapi.CloudFormationStackArtifact[],
private selectMatchingStacks(
stacks: cxapi.CloudFormationStackArtifact[],
patterns: string[],
extend: ExtendedStackSelection = ExtendedStackSelection.None): StackCollection {

// cli tests use this to ensure tests do not depend on legacy behavior
// (otherwise they will fail in v2)
const disableLegacy = process.env.CXAPI_DISABLE_SELECT_BY_ID === '1';

const matchingPattern = (pattern: string) => (stack: cxapi.CloudFormationStackArtifact) => {
if (minimatch(stack.hierarchicalId, pattern)) {
return true;
} else if (!disableLegacy && stack.id === pattern && semver.major(versionNumber()) < 2) {
warning('Selecting stack by identifier "%s". This identifier is deprecated and will be removed in v2. Please use "%s" instead.', chalk.bold(stack.id), chalk.bold(stack.hierarchicalId));
warning('Run "cdk ls" to see a list of all stack identifiers');
return true;
}
return false;
};
extend: ExtendedStackSelection = ExtendedStackSelection.None,
): StackCollection {

const matchingPattern = (pattern: string) => (stack: cxapi.CloudFormationStackArtifact) => minimatch(stack.hierarchicalId, pattern);
const matchedStacks = flatten(patterns.map(pattern => stacks.filter(matchingPattern(pattern))));

return this.extendStacks(matchedStacks, stacks, extend);
}

private selectDefaultStacks(stacks: cxapi.CloudFormationStackArtifact[],
private selectDefaultStacks(
stacks: cxapi.CloudFormationStackArtifact[],
topLevelStacks: cxapi.CloudFormationStackArtifact[],
defaultSelection: DefaultSelection) {
defaultSelection: DefaultSelection,
) {
switch (defaultSelection) {
case DefaultSelection.MainAssembly:
return new StackCollection(this, topLevelStacks);
Expand All @@ -178,9 +169,11 @@ export class CloudAssembly {
}
}

private extendStacks(matched: cxapi.CloudFormationStackArtifact[],
private extendStacks(
matched: cxapi.CloudFormationStackArtifact[],
all: cxapi.CloudFormationStackArtifact[],
extend: ExtendedStackSelection = ExtendedStackSelection.None) {
extend: ExtendedStackSelection = ExtendedStackSelection.None,
) {
const allStacks = new Map<string, cxapi.CloudFormationStackArtifact>();
for (const stack of all) {
allStacks.set(stack.hierarchicalId, stack);
Expand Down
3 changes: 0 additions & 3 deletions packages/aws-cdk/test/api/cloud-assembly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import { DefaultSelection } from '../../lib/api/cxapp/cloud-assembly';
import { MockCloudExecutable } from '../util';
import { cliAssemblyWithForcedVersion } from './assembly-versions';

// behave like v2
process.env.CXAPI_DISABLE_SELECT_BY_ID = '1';

test('do not throw when selecting stack without errors', async () => {
// GIVEN
const cxasm = await testCloudAssembly();
Expand Down
2 changes: 0 additions & 2 deletions packages/aws-cdk/test/cdk-toolkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ import { flatten } from '../lib/util';

markTesting();

process.env.CXAPI_DISABLE_SELECT_BY_ID = '1';

const defaultBootstrapSource: BootstrapSource = { source: 'default' };
const bootstrapEnvironmentMock = jest.spyOn(Bootstrapper.prototype, 'bootstrapEnvironment');
let cloudExecutable: MockCloudExecutable;
Expand Down

0 comments on commit 0546ec2

Please sign in to comment.