Skip to content

Commit c71caf0

Browse files
authored
refactor(cli): use IoHelper in internal APIs (#209)
This PR is a lot. There is no good way of reviewing it, so thanks for taking the time. The main change here is the usage of the `IoHelper` in all internal APIs. Previously we would pass an `IoHost` and an `action` around as pair, but separate value. Instead we now use the `IoHelper` which knows how to send messages with an action. I also decided to rename all properties and params respectively. This is partly what makes the PR so large. Note that all changes are to private APIs. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent da515f4 commit c71caf0

Some content is hidden

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

46 files changed

+527
-593
lines changed

packages/@aws-cdk/tmp-toolkit-helpers/src/api/io/private/io-helper.ts

-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export type ActionLessRequest<T, U> = Omit<IoRequest<T, U>, 'action'>;
1313
* Wraps a client provided IoHost and provides additional features & services to toolkit internal classes.
1414
*/
1515
export interface IoHelper extends IIoHost {
16-
action: ToolkitAction;
1716
notify<T>(msg: ActionLessMessage<T>): Promise<void>;
1817
requestResponse<T, U>(msg: ActionLessRequest<T, U>): Promise<U>;
1918
}
@@ -23,7 +22,6 @@ export interface IoHelper extends IIoHost {
2322
*/
2423
export function asIoHelper(ioHost: IIoHost, action: ToolkitAction): IoHelper {
2524
return {
26-
action,
2725
notify: async <T>(msg: Omit<IoMessage<T>, 'action'>) => {
2826
await ioHost.notify({
2927
...msg,

packages/@aws-cdk/toolkit-lib/lib/api/cloud-assembly/private/context-aware-source.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ export class ContextAwareCloudAssembly implements ICloudAssemblySource {
4343
private canLookup: boolean;
4444
private context: Context;
4545
private contextFile: string;
46-
private ioHost: IoHelper;
46+
private ioHelper: IoHelper;
4747

4848
constructor(private readonly source: ICloudAssemblySource, private readonly props: ContextAwareCloudAssemblyProps) {
4949
this.canLookup = props.lookups ?? true;
5050
this.context = props.context;
5151
this.contextFile = props.contextFile ?? PROJECT_CONTEXT; // @todo new feature not needed right now
52-
this.ioHost = props.services.ioHost;
52+
this.ioHelper = props.services.ioHelper;
5353
}
5454

5555
/**
@@ -77,22 +77,22 @@ export class ContextAwareCloudAssembly implements ICloudAssemblySource {
7777

7878
let tryLookup = true;
7979
if (previouslyMissingKeys && equalSets(missingKeysSet, previouslyMissingKeys)) {
80-
await this.ioHost.notify(IO.CDK_ASSEMBLY_I0240.msg('Not making progress trying to resolve environmental context. Giving up.', { missingKeys }));
80+
await this.ioHelper.notify(IO.CDK_ASSEMBLY_I0240.msg('Not making progress trying to resolve environmental context. Giving up.', { missingKeys }));
8181
tryLookup = false;
8282
}
8383

8484
previouslyMissingKeys = missingKeysSet;
8585

8686
if (tryLookup) {
87-
await this.ioHost.notify(IO.CDK_ASSEMBLY_I0241.msg('Some context information is missing. Fetching...', { missingKeys }));
87+
await this.ioHelper.notify(IO.CDK_ASSEMBLY_I0241.msg('Some context information is missing. Fetching...', { missingKeys }));
8888
await contextproviders.provideContextValues(
8989
assembly.manifest.missing,
9090
this.context,
9191
this.props.services.sdkProvider,
9292
);
9393

9494
// Cache the new context to disk
95-
await this.ioHost.notify(IO.CDK_ASSEMBLY_I0042.msg(`Writing updated context to ${this.contextFile}...`, {
95+
await this.ioHelper.notify(IO.CDK_ASSEMBLY_I0042.msg(`Writing updated context to ${this.contextFile}...`, {
9696
contextFile: this.contextFile,
9797
context: this.context.all,
9898
}));

packages/@aws-cdk/toolkit-lib/lib/api/cloud-assembly/private/prepare-source.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function determineOutputDirectory(outdir?: string) {
3838
* @param context The context key/value bash.
3939
*/
4040
export async function prepareDefaultEnvironment(services: ToolkitServices, props: { outdir?: string } = {}): Promise<Env> {
41-
const logFn = (msg: string, ...args: any) => services.ioHost.notify(IO.CDK_ASSEMBLY_I0010.msg(format(msg, ...args)));
41+
const logFn = (msg: string, ...args: any) => services.ioHelper.notify(IO.CDK_ASSEMBLY_I0010.msg(format(msg, ...args)));
4242
const env = await oldPrepare(services.sdkProvider, logFn);
4343

4444
if (props.outdir) {

packages/@aws-cdk/toolkit-lib/lib/api/cloud-assembly/private/source-builder.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export abstract class CloudAssemblySourceBuilder {
6565
return assembly;
6666
}
6767

68-
return assemblyFromDirectory(assembly.directory, services.ioHost, props.loadAssemblyOptions);
68+
return assemblyFromDirectory(assembly.directory, services.ioHelper, props.loadAssemblyOptions);
6969
},
7070
},
7171
contextAssemblyProps,
@@ -89,8 +89,8 @@ export abstract class CloudAssemblySourceBuilder {
8989
{
9090
produce: async () => {
9191
// @todo build
92-
await services.ioHost.notify(IO.CDK_ASSEMBLY_I0150.msg('--app points to a cloud assembly, so we bypass synth'));
93-
return assemblyFromDirectory(directory, services.ioHost, props.loadAssemblyOptions);
92+
await services.ioHelper.notify(IO.CDK_ASSEMBLY_I0150.msg('--app points to a cloud assembly, so we bypass synth'));
93+
return assemblyFromDirectory(directory, services.ioHelper, props.loadAssemblyOptions);
9494
},
9595
},
9696
contextAssemblyProps,
@@ -139,17 +139,17 @@ export abstract class CloudAssemblySourceBuilder {
139139
eventPublisher: async (type, line) => {
140140
switch (type) {
141141
case 'data_stdout':
142-
await services.ioHost.notify(IO.CDK_ASSEMBLY_I1001.msg(line));
142+
await services.ioHelper.notify(IO.CDK_ASSEMBLY_I1001.msg(line));
143143
break;
144144
case 'data_stderr':
145-
await services.ioHost.notify(IO.CDK_ASSEMBLY_E1002.msg(line));
145+
await services.ioHelper.notify(IO.CDK_ASSEMBLY_E1002.msg(line));
146146
break;
147147
}
148148
},
149149
extraEnv: envWithContext,
150150
cwd: props.workingDirectory,
151151
});
152-
return assemblyFromDirectory(outdir, services.ioHost, props.loadAssemblyOptions);
152+
return assemblyFromDirectory(outdir, services.ioHelper, props.loadAssemblyOptions);
153153
});
154154
} finally {
155155
await lock?.release();

packages/@aws-cdk/toolkit-lib/lib/toolkit/private/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { IoHelper } from '../../api/shared-private';
99
*/
1010
export interface ToolkitServices {
1111
sdkProvider: SdkProvider;
12-
ioHost: IoHelper;
12+
ioHelper: IoHelper;
1313
}
1414

1515
/**

0 commit comments

Comments
 (0)