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

chore(cli): remove unreachable v1 compat in stack selection #32620

Merged
merged 2 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading