Skip to content

Commit b5cb910

Browse files
authored
refactor(toolkit): move simple apis to shared helper package (#331)
Move the easier apis from cli package to the shared helper. Easier here means little or no dependencies on other apis. Tests will be moved later to keep the diff size managable. Code is still tested as the tests are still running from `aws-cdk` package. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent e3c5f55 commit b5cb910

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1083
-1128
lines changed

.projenrc.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,11 @@ const toolkitLib = configureProject(
11621162
name: '@aws-cdk/toolkit-lib',
11631163
description: 'AWS CDK Programmatic Toolkit Library',
11641164
srcdir: 'lib',
1165+
tsconfigDev: {
1166+
compilerOptions: {
1167+
rootDir: '.', // shouldn't be required but something broke... check again once we have gotten rid of the tmpToolkitHelpers package
1168+
},
1169+
},
11651170
deps: [
11661171
cloudAssemblySchema,
11671172
// Purposely a ^ dependency so that clients selecting old toolkit library
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { Settings } from './settings';
2+
import { ToolkitError } from './toolkit-error';
3+
4+
export { TRANSIENT_CONTEXT_KEY } from './settings';
5+
export const PROJECT_CONTEXT = 'cdk.context.json';
6+
7+
interface ContextBag {
8+
/**
9+
* The file name of the context. Will be used to potentially
10+
* save new context back to the original file.
11+
*/
12+
fileName?: string;
13+
14+
/**
15+
* The context values.
16+
*/
17+
bag: Settings;
18+
}
19+
20+
/**
21+
* Class that supports overlaying property bags
22+
*
23+
* Reads come from the first property bag that can has the given key,
24+
* writes go to the first property bag that is not readonly. A write
25+
* will remove the value from all property bags after the first
26+
* writable one.
27+
*/
28+
export class Context {
29+
private readonly bags: Settings[];
30+
private readonly fileNames: (string | undefined)[];
31+
32+
constructor(...bags: ContextBag[]) {
33+
this.bags = bags.length > 0 ? bags.map((b) => b.bag) : [new Settings()];
34+
this.fileNames =
35+
bags.length > 0 ? bags.map((b) => b.fileName) : ['default'];
36+
}
37+
38+
public get keys(): string[] {
39+
return Object.keys(this.all);
40+
}
41+
42+
public has(key: string) {
43+
return this.keys.indexOf(key) > -1;
44+
}
45+
46+
public get all(): { [key: string]: any } {
47+
let ret = new Settings();
48+
49+
// In reverse order so keys to the left overwrite keys to the right of them
50+
for (const bag of [...this.bags].reverse()) {
51+
ret = ret.merge(bag);
52+
}
53+
54+
return ret.all;
55+
}
56+
57+
public get(key: string): any {
58+
for (const bag of this.bags) {
59+
const v = bag.get([key]);
60+
if (v !== undefined) {
61+
return v;
62+
}
63+
}
64+
return undefined;
65+
}
66+
67+
public set(key: string, value: any) {
68+
for (const bag of this.bags) {
69+
if (bag.readOnly) {
70+
continue;
71+
}
72+
73+
// All bags past the first one have the value erased
74+
bag.set([key], value);
75+
value = undefined;
76+
}
77+
}
78+
79+
public unset(key: string) {
80+
this.set(key, undefined);
81+
}
82+
83+
public clear() {
84+
for (const key of this.keys) {
85+
this.unset(key);
86+
}
87+
}
88+
89+
/**
90+
* Save a specific context file
91+
*/
92+
public async save(fileName: string): Promise<this> {
93+
const index = this.fileNames.indexOf(fileName);
94+
95+
// File not found, don't do anything in this scenario
96+
if (index === -1) {
97+
return this;
98+
}
99+
100+
const bag = this.bags[index];
101+
if (bag.readOnly) {
102+
throw new ToolkitError(`Context file ${fileName} is read only!`);
103+
}
104+
105+
await bag.save(fileName);
106+
return this;
107+
}
108+
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
export * from './aws-auth';
22
export * from './cloud-assembly';
33
export * from './cloudformation';
4+
export * from './context';
45
export * from './diff';
56
export * from './io';
7+
export * from './notices';
68
export * from './plugin';
79
export * from './require-approval';
810
export * from './resource-import';
9-
export * from './toolkit-error';
11+
export * from './rwlock';
1012
export * from './settings';
13+
export * from './stack-events';
14+
export * from './toolkit-error';
15+
export * from './work-graph';
16+
export * from './tree';
17+
export * from './tags';

0 commit comments

Comments
 (0)