Skip to content

Commit f1fb105

Browse files
authored
Fixing template config creation on sam init (#1650)
1 parent 46bb7fa commit f1fb105

File tree

4 files changed

+101
-40
lines changed

4 files changed

+101
-40
lines changed

src/lambda/commands/createNewSamApp.ts

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ import { getIdeProperties, isCloud9 } from '../../shared/extensionUtilities'
5252

5353
type CreateReason = 'unknown' | 'userCancelled' | 'fileNotFound' | 'complete' | 'error'
5454

55-
/** Target file to open after creating a new SAM application */
56-
export const SAM_INIT_OPEN_TARGET: string = 'README.md'
55+
export const SAM_INIT_TEMPLATE_FILE: string = 'template.yaml'
56+
export const SAM_INIT_README_FILE: string = 'README.md'
5757

5858
export async function resumeCreateNewSamApp(
5959
extContext: ExtContext,
@@ -64,8 +64,9 @@ export async function resumeCreateNewSamApp(
6464
let samVersion: string | undefined
6565
const samInitState: SamInitState | undefined = activationReloadState.getSamInitState()
6666
try {
67-
const uri = vscode.Uri.file(samInitState?.path!)
68-
const folder = vscode.workspace.getWorkspaceFolder(uri)
67+
const templateUri = vscode.Uri.file(samInitState?.template!)
68+
const readmeUri = vscode.Uri.file(samInitState?.readme!)
69+
const folder = vscode.workspace.getWorkspaceFolder(templateUri)
6970
if (!folder) {
7071
createResult = 'Failed'
7172
reason = 'error'
@@ -75,7 +76,7 @@ export async function resumeCreateNewSamApp(
7576
localize(
7677
'AWS.samcli.initWizard.source.error.notInWorkspace',
7778
"Could not open file '{0}'. If this file exists on disk, try adding it to your workspace.",
78-
uri.fsPath
79+
templateUri.fsPath
7980
)
8081
)
8182

@@ -87,10 +88,12 @@ export async function resumeCreateNewSamApp(
8788
await addInitialLaunchConfiguration(
8889
extContext,
8990
folder,
90-
uri,
91+
templateUri,
9192
samInitState?.isImage ? samInitState?.runtime : undefined
9293
)
93-
await vscode.window.showTextDocument(uri)
94+
isCloud9()
95+
? await vscode.workspace.openTextDocument(readmeUri)
96+
: await vscode.commands.executeCommand('markdown.showPreviewToSide', readmeUri)
9497
} catch (err) {
9598
createResult = 'Failed'
9699
reason = 'error'
@@ -201,8 +204,9 @@ export async function createNewSamApplication(
201204

202205
await runSamCliInit(initArguments, samCliContext)
203206

204-
const uri = await getMainUri(config)
205-
if (!uri) {
207+
const templateUri = await getProjectUri(config, SAM_INIT_TEMPLATE_FILE)
208+
const readmeUri = await getProjectUri(config, SAM_INIT_README_FILE)
209+
if (!templateUri || !readmeUri) {
206210
reason = 'fileNotFound'
207211

208212
return
@@ -239,7 +243,8 @@ export async function createNewSamApplication(
239243

240244
// In case adding the workspace folder triggers a VS Code restart, persist relevant state to be used after reload
241245
activationReloadState.setSamInitState({
242-
path: uri.fsPath,
246+
template: templateUri.fsPath,
247+
readme: readmeUri.fsPath,
243248
runtime: createRuntime,
244249
isImage: config.packageType === 'Image',
245250
})
@@ -254,7 +259,7 @@ export async function createNewSamApplication(
254259

255260
// Race condition where SAM app is created but template doesn't register in time.
256261
// Poll for 5 seconds, otherwise direct user to codelens.
257-
const isTemplateRegistered = await waitUntil(async () => ext.templateRegistry.getRegisteredItem(uri), {
262+
const isTemplateRegistered = await waitUntil(async () => ext.templateRegistry.getRegisteredItem(templateUri), {
258263
timeout: 5000,
259264
interval: 500,
260265
truthy: false,
@@ -263,8 +268,8 @@ export async function createNewSamApplication(
263268
if (isTemplateRegistered) {
264269
const newLaunchConfigs = await addInitialLaunchConfiguration(
265270
extContext,
266-
vscode.workspace.getWorkspaceFolder(uri)!,
267-
uri,
271+
vscode.workspace.getWorkspaceFolder(templateUri)!,
272+
templateUri,
268273
createRuntime
269274
)
270275
if (newLaunchConfigs && newLaunchConfigs.length > 0) {
@@ -296,8 +301,8 @@ export async function createNewSamApplication(
296301
activationReloadState.clearSamInitState()
297302
// TODO: Replace when Cloud9 supports `markdown` commands
298303
isCloud9()
299-
? await vscode.workspace.openTextDocument(uri)
300-
: await vscode.commands.executeCommand('markdown.showPreviewToSide', uri)
304+
? await vscode.workspace.openTextDocument(readmeUri)
305+
: await vscode.commands.executeCommand('markdown.showPreviewToSide', readmeUri)
301306
} catch (err) {
302307
createResult = 'Failed'
303308
reason = 'error'
@@ -333,18 +338,19 @@ async function validateSamCli(samCliValidator: SamCliValidator): Promise<void> {
333338
throwAndNotifyIfInvalid(validationResult)
334339
}
335340

336-
export async function getMainUri(
337-
config: Pick<CreateNewSamAppWizardResponse, 'location' | 'name'>
341+
export async function getProjectUri(
342+
config: Pick<CreateNewSamAppWizardResponse, 'location' | 'name'>,
343+
file: string
338344
): Promise<vscode.Uri | undefined> {
339-
const cfnTemplatePath = path.resolve(config.location.fsPath, config.name, SAM_INIT_OPEN_TARGET)
345+
const cfnTemplatePath = path.resolve(config.location.fsPath, config.name, file)
340346
if (await fileExists(cfnTemplatePath)) {
341347
return vscode.Uri.file(cfnTemplatePath)
342348
} else {
343349
vscode.window.showWarningMessage(
344350
localize(
345351
'AWS.samcli.initWizard.source.error.notFound',
346352
'Project created successfully, but {0} file not found: {1}',
347-
SAM_INIT_OPEN_TARGET,
353+
file,
348354
cfnTemplatePath
349355
)
350356
)

src/shared/activationReloadState.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import * as vscode from 'vscode'
77
import { ext } from './extensionGlobals'
88
import { Runtime } from 'aws-sdk/clients/lambda'
99

10+
export const ACTIVATION_TEMPLATE_PATH_KEY = 'ACTIVATION_TEMPLATE_PATH_KEY'
1011
export const ACTIVATION_LAUNCH_PATH_KEY = 'ACTIVATION_LAUNCH_PATH_KEY'
1112
export const SAM_INIT_RUNTIME_KEY = 'SAM_INIT_RUNTIME_KEY'
1213
export const SAM_INIT_IMAGE_BOOLEAN_KEY = 'SAM_INIT_IMAGE_BOOLEAN_KEY'
1314

1415
export interface SamInitState {
15-
path: string | undefined
16+
template: string | undefined
17+
readme: string | undefined
1618
runtime: Runtime | undefined
1719
isImage: boolean | undefined
1820
}
@@ -23,19 +25,22 @@ export interface SamInitState {
2325
export class ActivationReloadState {
2426
public getSamInitState(): SamInitState | undefined {
2527
return {
26-
path: this.extensionContext.globalState.get<string>(ACTIVATION_LAUNCH_PATH_KEY),
28+
template: this.extensionContext.globalState.get<string>(ACTIVATION_TEMPLATE_PATH_KEY),
29+
readme: this.extensionContext.globalState.get<string>(ACTIVATION_LAUNCH_PATH_KEY),
2730
runtime: this.extensionContext.globalState.get<string>(SAM_INIT_RUNTIME_KEY),
2831
isImage: this.extensionContext.globalState.get<boolean>(SAM_INIT_IMAGE_BOOLEAN_KEY),
2932
}
3033
}
3134

3235
public setSamInitState(state: SamInitState): void {
33-
this.extensionContext.globalState.update(ACTIVATION_LAUNCH_PATH_KEY, state.path)
36+
this.extensionContext.globalState.update(ACTIVATION_TEMPLATE_PATH_KEY, state.template)
37+
this.extensionContext.globalState.update(ACTIVATION_LAUNCH_PATH_KEY, state.readme)
3438
this.extensionContext.globalState.update(SAM_INIT_RUNTIME_KEY, state.runtime)
3539
this.extensionContext.globalState.update(SAM_INIT_IMAGE_BOOLEAN_KEY, state.isImage)
3640
}
3741

3842
public clearSamInitState(): void {
43+
this.extensionContext.globalState.update(ACTIVATION_TEMPLATE_PATH_KEY, undefined)
3944
this.extensionContext.globalState.update(ACTIVATION_LAUNCH_PATH_KEY, undefined)
4045
this.extensionContext.globalState.update(SAM_INIT_RUNTIME_KEY, undefined)
4146
this.extensionContext.globalState.update(SAM_INIT_IMAGE_BOOLEAN_KEY, undefined)

src/test/lambda/commands/createNewSamApp.test.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import * as fs from 'fs-extra'
1212
import { FakeExtensionContext } from '../../fakeExtensionContext'
1313
import {
1414
addInitialLaunchConfiguration,
15-
getMainUri,
16-
SAM_INIT_OPEN_TARGET,
15+
getProjectUri,
16+
SAM_INIT_TEMPLATE_FILE,
1717
} from '../../../lambda/commands/createNewSamApp'
1818
import { LaunchConfiguration } from '../../../shared/debug/launchConfiguration'
1919
import { anything, capture, instance, mock, when } from 'ts-mockito'
@@ -41,7 +41,7 @@ describe('createNewSamApp', function () {
4141
fakeContext = await FakeExtensionContext.getFakeExtContext()
4242
tempFolder = await makeTemporaryToolkitFolder()
4343
tempTemplate = vscode.Uri.file(path.join(tempFolder, 'test.yaml'))
44-
fakeTarget = path.join(tempFolder, SAM_INIT_OPEN_TARGET)
44+
fakeTarget = path.join(tempFolder, SAM_INIT_TEMPLATE_FILE)
4545
testutil.toFile('target file', fakeTarget)
4646

4747
fakeWorkspaceFolder = {
@@ -71,15 +71,18 @@ describe('createNewSamApp', function () {
7171
ext.templateRegistry.reset()
7272
})
7373

74-
describe('getMainUri', function () {
74+
describe('getProjectUri', function () {
7575
it('returns the target file when it exists', async function () {
76-
assert.strictEqual(normalize((await getMainUri(fakeResponse))?.fsPath ?? ''), normalize(fakeTarget))
76+
assert.strictEqual(
77+
normalize((await getProjectUri(fakeResponse, SAM_INIT_TEMPLATE_FILE))?.fsPath ?? ''),
78+
normalize(fakeTarget)
79+
)
7780
})
7881
it('returns undefined when the target does not exist', async function () {
7982
const badResponse1 = { location: fakeResponse.location, name: 'notreal' }
8083
const badResponse2 = { location: vscode.Uri.parse('fake://notreal'), name: 'notafile' }
81-
assert.strictEqual((await getMainUri(badResponse1))?.fsPath, undefined)
82-
assert.strictEqual((await getMainUri(badResponse2))?.fsPath, undefined)
84+
assert.strictEqual((await getProjectUri(badResponse1, SAM_INIT_TEMPLATE_FILE))?.fsPath, undefined)
85+
assert.strictEqual((await getProjectUri(badResponse2, SAM_INIT_TEMPLATE_FILE))?.fsPath, undefined)
8386
})
8487
})
8588

@@ -94,7 +97,7 @@ describe('createNewSamApp', function () {
9497
const launchConfigs = await addInitialLaunchConfiguration(
9598
fakeContext,
9699
fakeWorkspaceFolder,
97-
(await getMainUri(fakeResponse))!,
100+
(await getProjectUri(fakeResponse, SAM_INIT_TEMPLATE_FILE))!,
98101
undefined,
99102
instance(mockLaunchConfiguration)
100103
)
@@ -201,7 +204,7 @@ describe('createNewSamApp', function () {
201204
testutil.toFile(makeSampleSamTemplateYaml(true), tempTemplate.fsPath)
202205
testutil.toFile(makeSampleSamTemplateYaml(true), otherTemplate1.fsPath)
203206
testutil.toFile(makeSampleSamTemplateYaml(true), otherTemplate2.fsPath)
204-
testutil.toFile('target file', path.join(otherFolder1, SAM_INIT_OPEN_TARGET))
207+
testutil.toFile('target file', path.join(otherFolder1, SAM_INIT_TEMPLATE_FILE))
205208

206209
await ext.templateRegistry.addItemToRegistry(tempTemplate)
207210
await ext.templateRegistry.addItemToRegistry(otherTemplate1)
@@ -210,15 +213,18 @@ describe('createNewSamApp', function () {
210213
const launchConfigs1 = await addInitialLaunchConfiguration(
211214
fakeContext,
212215
fakeWorkspaceFolder,
213-
(await getMainUri(fakeResponse))!,
216+
(await getProjectUri(fakeResponse, SAM_INIT_TEMPLATE_FILE))!,
214217
undefined,
215218
instance(mockLaunchConfiguration)
216219
)
217220

218221
const launchConfigs2 = await addInitialLaunchConfiguration(
219222
fakeContext,
220223
fakeWorkspaceFolder,
221-
(await getMainUri({ location: fakeWorkspaceFolder.uri, name: 'otherFolder' }))!,
224+
(await getProjectUri(
225+
{ location: fakeWorkspaceFolder.uri, name: 'otherFolder' },
226+
SAM_INIT_TEMPLATE_FILE
227+
))!,
222228
undefined,
223229
instance(mockLaunchConfiguration)
224230
)

0 commit comments

Comments
 (0)