Skip to content

Commit 92e5fab

Browse files
Merge staging into feature/c9toolkit
2 parents bce3f3e + 6121ce6 commit 92e5fab

File tree

7 files changed

+41
-16
lines changed

7 files changed

+41
-16
lines changed

src/shared/sam/cli/samCliBuild.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { getLogger, Logger } from '../../logger'
88
import { logAndThrowIfUnexpectedExitCode, SamCliProcessInvoker } from './samCliInvokerUtils'
99
import { DefaultSamCliProcessInvoker } from './samCliInvoker'
1010
import { pushIf } from '../../utilities/collectionUtils'
11+
import { ext } from '../../extensionGlobals'
12+
import { getChannelLogger } from '../../utilities/vsCodeUtils'
1113

1214
export interface SamCliBuildInvocationArguments {
1315
/**
@@ -107,6 +109,7 @@ export class SamCliBuildInvocation {
107109
const childProcessResult = await this.args.invoker.invoke({
108110
spawnOptions: { env },
109111
arguments: invokeArgs,
112+
channelLogger: getChannelLogger(ext.outputChannel),
110113
})
111114

112115
logAndThrowIfUnexpectedExitCode(childProcessResult, 0)

src/shared/sam/cli/samCliInvoker.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ export class DefaultSamCliProcessInvoker implements SamCliProcessInvoker {
4141

4242
public async invoke(options?: SamCliProcessInvokeOptions): Promise<ChildProcessResult> {
4343
const invokeOptions = makeRequiredSamCliProcessInvokeOptions(options)
44+
const logger = getLogger()
4445

4546
const sam = await this.context.cliConfig.getOrDetectSamCli()
4647
if (!sam.path) {
47-
getLogger().warn('SAM CLI not found and not configured')
48+
logger.warn('SAM CLI not found and not configured')
4849
} else if (sam.autoDetected) {
49-
getLogger().info('SAM CLI not configured, using SAM found at: %O', sam.path)
50+
logger.info('SAM CLI not configured, using SAM found at: %O', sam.path)
5051
}
5152

5253
const samCommand = sam.path ? sam.path : 'sam'
@@ -56,6 +57,17 @@ export class DefaultSamCliProcessInvoker implements SamCliProcessInvoker {
5657
...invokeOptions.arguments
5758
)
5859

59-
return await childProcess.run()
60+
options?.channelLogger?.info('AWS.running.command', 'Running command: {0}', `${childProcess}`)
61+
logger.verbose(`running: ${childProcess}`)
62+
return await childProcess.run(
63+
(text: string) => {
64+
options?.channelLogger?.emitMessage(text)
65+
logger.verbose(`stdout: ${text}`)
66+
},
67+
(text: string) => {
68+
options?.channelLogger?.emitMessage(text)
69+
logger.verbose(`stderr: ${text}`)
70+
}
71+
)
6072
}
6173
}

src/shared/sam/cli/samCliInvokerUtils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
import { SpawnOptions } from 'child_process'
77
import { getLogger } from '../../logger'
88
import { ChildProcessResult } from '../../utilities/childProcess'
9+
import { ChannelLogger } from '../../utilities/vsCodeUtils'
910

1011
export interface SamCliProcessInvokeOptions {
1112
spawnOptions?: SpawnOptions
1213
arguments?: string[]
14+
/** Optionally log stdout and stderr to the specified logger */
15+
channelLogger?: ChannelLogger
1316
}
1417

1518
export function makeRequiredSamCliProcessInvokeOptions(
1619
options?: SamCliProcessInvokeOptions
17-
): Required<SamCliProcessInvokeOptions> {
20+
): Required<Omit<SamCliProcessInvokeOptions, 'channelLogger'>> {
1821
options = options || {}
1922

2023
return {

src/shared/sam/cli/samCliLocalInvoke.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55

66
import * as child_process from 'child_process'
77
import { pushIf } from '../../utilities/collectionUtils'
8-
import * as vscode from 'vscode'
98
import * as nls from 'vscode-nls'
109
import { fileExists } from '../../filesystemUtilities'
1110
import { getLogger, Logger } from '../../logger'
1211
import { ChildProcess } from '../../utilities/childProcess'
13-
import { removeAnsi } from '../../utilities/textUtilities'
1412
import { Timeout } from '../../utilities/timeoutUtils'
1513
import { ChannelLogger } from '../../utilities/vsCodeUtils'
1614
import { DefaultSamCliProcessInvokerContext, SamCliProcessInvokerContext } from './samCliInvoker'
@@ -59,13 +57,13 @@ export class DefaultSamLocalInvokeCommand implements SamLocalInvokeCommand {
5957

6058
await childProcess.start({
6159
onStdout: (text: string): void => {
62-
this.emitMessage(text)
60+
this.channelLogger.emitMessage(text)
6361
// If we have a timeout (as we do on debug) refresh the timeout as we receive text
6462
params.timeout?.refresh()
6563
this.logger.verbose(`stdout: ${text}`)
6664
},
6765
onStderr: (text: string): void => {
68-
this.emitMessage(text)
66+
this.channelLogger.emitMessage(text)
6967
// If we have a timeout (as we do on debug) refresh the timeout as we receive text
7068
params.timeout?.refresh()
7169
this.logger.verbose(`stderr: ${text}`)
@@ -135,13 +133,6 @@ export class DefaultSamLocalInvokeCommand implements SamLocalInvokeCommand {
135133
}
136134
})
137135
}
138-
139-
private emitMessage(text: string): void {
140-
// From VS Code API: If no debug session is active, output sent to the debug console is not shown.
141-
// We send text to output channel and debug console to ensure no text is lost.
142-
this.channelLogger.channel.append(removeAnsi(text))
143-
vscode.debug.activeDebugConsole.append(text)
144-
}
145136
}
146137

147138
export interface SamCliLocalInvokeInvocationArguments {

src/shared/utilities/childProcess.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ export class ChildProcess {
6464
/**
6565
* Calls `start()` with default listeners that resolve()/reject() on process end.
6666
*/
67-
public async run(): Promise<ChildProcessResult> {
67+
public async run(
68+
onStdout?: (text: string) => void,
69+
onStderr?: (text: string) => void
70+
): Promise<ChildProcessResult> {
6871
return await new Promise<ChildProcessResult>(async (resolve, reject) => {
6972
await this.start({
7073
collect: true,
@@ -79,6 +82,8 @@ export class ChildProcess {
7982
resolve(this.processResult)
8083
}
8184
},
85+
onStderr: onStderr,
86+
onStdout: onStdout,
8287
}).catch(reject)
8388
if (!this.childProcess) {
8489
reject('child process not started')

src/shared/utilities/vsCodeUtils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as nls from 'vscode-nls'
88
import { getLogger, LogLevel } from '../logger'
99
import { Loggable } from '../logger/loggableType'
1010
import { ext } from '../../shared/extensionGlobals'
11+
import { removeAnsi } from './textUtilities'
1112

1213
// TODO: Consider NLS initialization/configuration here & have packages to import localize from here
1314
export const localize = nls.loadMessageBundle()
@@ -58,6 +59,8 @@ export interface ChannelLogger {
5859
info: TemplateHandler
5960
warn: TemplateHandler
6061
error: TemplateHandler
62+
/** For when it's necessary to emit non-templated text (e.g. output from a CLI) */
63+
emitMessage: (message: string) => void
6164
}
6265

6366
/**
@@ -115,6 +118,12 @@ export function getChannelLogger(channel: vscode.OutputChannel): ChannelLogger {
115118
nlsTemplate,
116119
templateTokens,
117120
}),
121+
emitMessage: (text: string) => {
122+
// From VS Code API: If no debug session is active, output sent to the debug console is not shown.
123+
// We send text to output channel and debug console to ensure no text is lost.
124+
channel.append(removeAnsi(text))
125+
vscode.debug.activeDebugConsole.append(text)
126+
},
118127
})
119128
}
120129

src/test/shared/fakeChannelLogger.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ export class FakeChannelLogger implements ChannelLogger {
3636
public verbose(nlsKey: string, nlsTemplate: string, ...templateTokens: Loggable[]): void {
3737
this.loggedVerboseKeys.add(nlsKey)
3838
}
39+
40+
public emitMessage(message: string): void {}
3941
}

0 commit comments

Comments
 (0)