Skip to content

Commit 7d68864

Browse files
committed
refactoring sam sync, deploy build
1 parent cee0fa3 commit 7d68864

File tree

21 files changed

+2140
-1937
lines changed

21 files changed

+2140
-1937
lines changed

packages/core/src/awsService/appBuilder/explorer/openTemplate.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
*/
55

66
import { CloudFormationTemplateRegistry } from '../../../shared/fs/templateRegistry'
7-
import { createTemplatePrompter, TemplateItem } from '../../../shared/sam/sync'
7+
import { syncMementoRootKey } from '../../../shared/sam/sync'
8+
89
import { createExitPrompter } from '../../../shared/ui/common/exitPrompter'
10+
import { createTemplatePrompter, TemplateItem } from '../../../shared/ui/common/samTemplate'
911
import { Wizard } from '../../../shared/wizards/wizard'
1012

1113
export interface OpenTemplateParams {
@@ -15,6 +17,6 @@ export interface OpenTemplateParams {
1517
export class OpenTemplateWizard extends Wizard<OpenTemplateParams> {
1618
public constructor(state: Partial<OpenTemplateParams>, registry: CloudFormationTemplateRegistry) {
1719
super({ initState: state, exitPrompterProvider: createExitPrompter })
18-
this.form.template.bindPrompter(() => createTemplatePrompter(registry))
20+
this.form.template.bindPrompter(() => createTemplatePrompter(registry, syncMementoRootKey))
1921
}
2022
}

packages/core/src/shared/sam/build.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
*/
55

66
import * as vscode from 'vscode'
7-
import { TemplateItem, createTemplatePrompter, getSamCliPathAndVersion, runInTerminal } from './sync'
7+
import { runInTerminal } from './utils'
8+
import { TemplateItem, createTemplatePrompter } from '../ui/common/samTemplate'
89
import { ChildProcess } from '../utilities/processUtils'
910
import { addTelemetryEnvVar } from './cli/samCliInvokerUtils'
1011
import { Wizard } from '../wizards/wizard'
@@ -19,8 +20,9 @@ import globals from '../extensionGlobals'
1920
import { TreeNode } from '../treeview/resourceTreeDataProvider'
2021
import { telemetry } from '../telemetry/telemetry'
2122
import { getSpawnEnv } from '../env/resolveEnv'
22-
import { getProjectRoot, isDotnetRuntime } from './utils'
23+
import { getProjectRoot, getSamCliPathAndVersion, isDotnetRuntime } from './utils'
2324
import { getConfigFileUri, validateSamBuildConfig } from './config'
25+
import { syncMementoRootKey } from './sync'
2426

2527
export interface BuildParams {
2628
readonly template: TemplateItem
@@ -127,7 +129,7 @@ export class BuildWizard extends Wizard<BuildParams> {
127129
this.arg = arg
128130
if (this.arg === undefined) {
129131
// "Build" command was invoked on the command palette.
130-
this.form.template.bindPrompter(() => createTemplatePrompter(this.registry))
132+
this.form.template.bindPrompter(() => createTemplatePrompter(this.registry, syncMementoRootKey))
131133
this.form.projectRoot.setDefault(({ template }) => getProjectRoot(template))
132134
this.form.paramsSource.bindPrompter(async ({ projectRoot }) => {
133135
const existValidSamConfig: boolean | undefined = await validateSamBuildConfig(projectRoot)

packages/core/src/shared/sam/deploy.ts

Lines changed: 53 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import * as vscode from 'vscode'
7-
import { ToolkitError, getLogger, globals } from '../../shared'
7+
import { ToolkitError, globals } from '../../shared'
88
import * as CloudFormation from '../../shared/cloudformation/cloudformation'
99
import { getParameters } from '../../lambda/config/parameterUtils'
1010
import { DefaultCloudFormationClient } from '../clients/cloudFormationClient'
@@ -17,21 +17,23 @@ import { createCommonButtons } from '../ui/buttons'
1717
import { createExitPrompter } from '../ui/common/exitPrompter'
1818
import { createRegionPrompter } from '../ui/common/region'
1919
import { createInputBox } from '../ui/inputPrompter'
20-
import { DataQuickPickItem, createQuickPick } from '../ui/pickerPrompter'
2120
import { ChildProcess } from '../utilities/processUtils'
2221
import { CancellationError } from '../utilities/timeoutUtils'
2322
import { Wizard } from '../wizards/wizard'
2423
import { addTelemetryEnvVar } from './cli/samCliInvokerUtils'
2524
import { validateSamDeployConfig, SamConfig, writeSamconfigGlobal } from './config'
25+
import { BucketSource, createBucketSourcePrompter, createBucketPrompter } from '../ui/common/bucket'
26+
import { createStackPrompter } from '../ui/common/stack'
27+
import { TemplateItem, createTemplatePrompter } from '../ui/common/samTemplate'
2628
import {
27-
TemplateItem,
28-
createStackPrompter,
29-
createBucketPrompter,
30-
createTemplatePrompter,
29+
getProjectRoot,
30+
getRecentResponse,
3131
getSamCliPathAndVersion,
32+
getSource,
3233
runInTerminal,
33-
} from './sync'
34-
import { getProjectRoot, getSource } from './utils'
34+
updateRecentResponse,
35+
} from './utils'
36+
import { createDeployParamsSourcePrompter, ParamsSource } from '../ui/common/paramsSource'
3537

3638
export interface DeployParams {
3739
readonly paramsSource: ParamsSource
@@ -45,89 +47,24 @@ export interface DeployParams {
4547
[key: string]: any
4648
}
4749

48-
const mementoRootKey = 'samcli.deploy.params'
49-
export function getRecentParams(identifier: string, key: string): string | undefined {
50-
const root = globals.context.workspaceState.get(mementoRootKey, {} as Record<string, Record<string, string>>)
50+
const deployMementoRootKey = 'samcli.deploy.params'
5151

52-
return root[identifier]?.[key]
53-
}
54-
55-
export async function updateRecentParams(identifier: string, key: string, value: string | undefined) {
56-
try {
57-
const root = globals.context.workspaceState.get(mementoRootKey, {} as Record<string, Record<string, string>>)
58-
await globals.context.workspaceState.update(mementoRootKey, {
59-
...root,
60-
[identifier]: { ...root[identifier], [key]: value },
61-
})
62-
} catch (err) {
63-
getLogger().warn(`sam: unable to save response at key "${key}": %s`, err)
64-
}
52+
function getRecentDeployParams(identifier: string, key: string): string | undefined {
53+
return getRecentResponse(deployMementoRootKey, identifier, key)
6554
}
6655

6756
function createParamPromptProvider(name: string, defaultValue: string | undefined, templateFsPath: string = 'default') {
6857
return createInputBox({
6958
title: `Specify SAM parameter value for ${name}`,
7059
buttons: createCommonButtons(samDeployUrl),
71-
value: getRecentParams(templateFsPath, name) ?? defaultValue,
60+
value: getRecentDeployParams(templateFsPath, name) ?? defaultValue,
7261
})
7362
}
74-
function bucketSourcePrompter() {
75-
const items: DataQuickPickItem<BucketSource>[] = [
76-
{
77-
label: 'Create a SAM CLI managed S3 bucket',
78-
data: BucketSource.SamCliManaged,
79-
},
80-
{
81-
label: 'Specify an S3 bucket',
82-
data: BucketSource.UserProvided,
83-
},
84-
]
8563

86-
return createQuickPick(items, {
87-
title: 'Specify S3 bucket for deployment artifacts',
88-
placeholder: 'Press enter to proceed with highlighted option',
89-
buttons: createCommonButtons(samDeployUrl),
90-
})
91-
}
92-
function paramsSourcePrompter(existValidSamconfig: boolean | undefined) {
93-
const items: DataQuickPickItem<ParamsSource>[] = [
94-
{
95-
label: 'Specify required parameters and save as defaults',
96-
data: ParamsSource.SpecifyAndSave,
97-
},
98-
{
99-
label: 'Specify required parameters',
100-
data: ParamsSource.Specify,
101-
},
102-
]
103-
104-
if (existValidSamconfig) {
105-
items.push({
106-
label: 'Use default values from samconfig',
107-
data: ParamsSource.SamConfig,
108-
})
109-
}
110-
111-
return createQuickPick(items, {
112-
title: 'Specify parameters for deploy',
113-
placeholder: 'Press enter to proceed with highlighted option',
114-
buttons: createCommonButtons(samDeployUrl),
115-
})
116-
}
11764
type DeployResult = {
11865
isSuccess: boolean
11966
}
12067

121-
export enum BucketSource {
122-
SamCliManaged,
123-
UserProvided,
124-
}
125-
export enum ParamsSource {
126-
SpecifyAndSave,
127-
Specify,
128-
SamConfig,
129-
}
130-
13168
export class DeployWizard extends Wizard<DeployParams> {
13269
registry: CloudFormationTemplateRegistry
13370
state: Partial<DeployParams>
@@ -159,50 +96,56 @@ export class DeployWizard extends Wizard<DeployParams> {
15996
this.form.template.setDefault(templateItem)
16097
this.form.projectRoot.setDefault(() => projectRootFolder)
16198
this.form.paramsSource.bindPrompter(async () =>
162-
paramsSourcePrompter(await validateSamDeployConfig(projectRootFolder))
99+
createDeployParamsSourcePrompter(await validateSamDeployConfig(projectRootFolder))
163100
)
164101

165102
this.form.region.bindPrompter(() => createRegionPrompter().transform((r) => r.id), {
166103
showWhen: ({ paramsSource }) =>
167104
paramsSource === ParamsSource.Specify || paramsSource === ParamsSource.SpecifyAndSave,
168105
})
169106
this.form.stackName.bindPrompter(
170-
({ region }) => createStackPrompter(new DefaultCloudFormationClient(region!)),
107+
({ region }) => createStackPrompter(new DefaultCloudFormationClient(region!), deployMementoRootKey),
171108
{
172109
showWhen: ({ paramsSource }) =>
173110
paramsSource === ParamsSource.Specify || paramsSource === ParamsSource.SpecifyAndSave,
174111
}
175112
)
176-
this.form.bucketSource.bindPrompter(() => bucketSourcePrompter(), {
113+
this.form.bucketSource.bindPrompter(() => createBucketSourcePrompter(), {
177114
showWhen: ({ paramsSource }) =>
178115
paramsSource === ParamsSource.Specify || paramsSource === ParamsSource.SpecifyAndSave,
179116
})
180-
this.form.bucketName.bindPrompter(({ region }) => createBucketPrompter(new DefaultS3Client(region!)), {
181-
showWhen: ({ bucketSource }) => bucketSource === BucketSource.UserProvided,
182-
})
117+
this.form.bucketName.bindPrompter(
118+
({ region }) => createBucketPrompter(new DefaultS3Client(region!), deployMementoRootKey),
119+
{
120+
showWhen: ({ bucketSource }) => bucketSource === BucketSource.UserProvided,
121+
}
122+
)
183123
} else if (this.arg && this.arg.regionCode) {
184124
// "Deploy" command was invoked on a regionNode.
185-
this.form.template.bindPrompter(() => createTemplatePrompter(this.registry))
125+
this.form.template.bindPrompter(() => createTemplatePrompter(this.registry, deployMementoRootKey))
186126
this.form.projectRoot.setDefault(({ template }) => getProjectRoot(template))
187127
this.form.paramsSource.bindPrompter(async ({ projectRoot }) => {
188128
const existValidSamConfig: boolean | undefined = await validateSamDeployConfig(projectRoot)
189-
return paramsSourcePrompter(existValidSamConfig)
129+
return createDeployParamsSourcePrompter(existValidSamConfig)
190130
})
191131
this.form.region.setDefault(() => this.arg.regionCode)
192132
this.form.stackName.bindPrompter(
193-
({ region }) => createStackPrompter(new DefaultCloudFormationClient(region!)),
133+
({ region }) => createStackPrompter(new DefaultCloudFormationClient(region!), deployMementoRootKey),
194134
{
195135
showWhen: ({ paramsSource }) =>
196136
paramsSource === ParamsSource.Specify || paramsSource === ParamsSource.SpecifyAndSave,
197137
}
198138
)
199-
this.form.bucketSource.bindPrompter(() => bucketSourcePrompter(), {
139+
this.form.bucketSource.bindPrompter(() => createBucketSourcePrompter(), {
200140
showWhen: ({ paramsSource }) =>
201141
paramsSource === ParamsSource.Specify || paramsSource === ParamsSource.SpecifyAndSave,
202142
})
203-
this.form.bucketName.bindPrompter(({ region }) => createBucketPrompter(new DefaultS3Client(region!)), {
204-
showWhen: ({ bucketSource }) => bucketSource === BucketSource.UserProvided,
205-
})
143+
this.form.bucketName.bindPrompter(
144+
({ region }) => createBucketPrompter(new DefaultS3Client(region!), deployMementoRootKey),
145+
{
146+
showWhen: ({ bucketSource }) => bucketSource === BucketSource.UserProvided,
147+
}
148+
)
206149
} else if (this.arg && this.arg.getTreeItem().resourceUri) {
207150
// "Deploy" command was invoked on a TreeNode on the AppBuilder.
208151
const templateUri = this.arg.getTreeItem().resourceUri as vscode.Uri
@@ -212,54 +155,60 @@ export class DeployWizard extends Wizard<DeployParams> {
212155
this.addParameterPromptersIfApplicable(templateUri)
213156
this.form.template.setDefault(templateItem)
214157
this.form.paramsSource.bindPrompter(async () =>
215-
paramsSourcePrompter(await validateSamDeployConfig(projectRootFolder))
158+
createDeployParamsSourcePrompter(await validateSamDeployConfig(projectRootFolder))
216159
)
217160

218161
this.form.region.bindPrompter(() => createRegionPrompter().transform((r) => r.id), {
219162
showWhen: ({ paramsSource }) =>
220163
paramsSource === ParamsSource.Specify || paramsSource === ParamsSource.SpecifyAndSave,
221164
})
222165
this.form.stackName.bindPrompter(
223-
({ region }) => createStackPrompter(new DefaultCloudFormationClient(region!)),
166+
({ region }) => createStackPrompter(new DefaultCloudFormationClient(region!), deployMementoRootKey),
224167
{
225168
showWhen: ({ paramsSource }) =>
226169
paramsSource === ParamsSource.Specify || paramsSource === ParamsSource.SpecifyAndSave,
227170
}
228171
)
229-
this.form.bucketSource.bindPrompter(() => bucketSourcePrompter(), {
172+
this.form.bucketSource.bindPrompter(() => createBucketSourcePrompter(), {
230173
showWhen: ({ paramsSource }) =>
231174
paramsSource === ParamsSource.Specify || paramsSource === ParamsSource.SpecifyAndSave,
232175
})
233-
this.form.bucketName.bindPrompter(({ region }) => createBucketPrompter(new DefaultS3Client(region!)), {
234-
showWhen: ({ bucketSource }) => bucketSource === BucketSource.UserProvided,
235-
})
176+
this.form.bucketName.bindPrompter(
177+
({ region }) => createBucketPrompter(new DefaultS3Client(region!), deployMementoRootKey),
178+
{
179+
showWhen: ({ bucketSource }) => bucketSource === BucketSource.UserProvided,
180+
}
181+
)
236182
this.form.projectRoot.setDefault(() => getProjectRoot(templateItem))
237183
} else {
238184
// "Deploy" command was invoked on the command palette.
239-
this.form.template.bindPrompter(() => createTemplatePrompter(this.registry))
185+
this.form.template.bindPrompter(() => createTemplatePrompter(this.registry, deployMementoRootKey))
240186
this.form.projectRoot.setDefault(({ template }) => getProjectRoot(template))
241187
this.form.paramsSource.bindPrompter(async ({ projectRoot }) => {
242188
const existValidSamConfig: boolean | undefined = await validateSamDeployConfig(projectRoot)
243-
return paramsSourcePrompter(existValidSamConfig)
189+
return createDeployParamsSourcePrompter(existValidSamConfig)
244190
})
245191
this.form.region.bindPrompter(() => createRegionPrompter().transform((r) => r.id), {
246192
showWhen: ({ paramsSource }) =>
247193
paramsSource === ParamsSource.Specify || paramsSource === ParamsSource.SpecifyAndSave,
248194
})
249195
this.form.stackName.bindPrompter(
250-
({ region }) => createStackPrompter(new DefaultCloudFormationClient(region!)),
196+
({ region }) => createStackPrompter(new DefaultCloudFormationClient(region!), deployMementoRootKey),
251197
{
252198
showWhen: ({ paramsSource }) =>
253199
paramsSource === ParamsSource.Specify || paramsSource === ParamsSource.SpecifyAndSave,
254200
}
255201
)
256-
this.form.bucketSource.bindPrompter(() => bucketSourcePrompter(), {
202+
this.form.bucketSource.bindPrompter(() => createBucketSourcePrompter(), {
257203
showWhen: ({ paramsSource }) =>
258204
paramsSource === ParamsSource.Specify || paramsSource === ParamsSource.SpecifyAndSave,
259205
})
260-
this.form.bucketName.bindPrompter(({ region }) => createBucketPrompter(new DefaultS3Client(region!)), {
261-
showWhen: ({ bucketSource }) => bucketSource === BucketSource.UserProvided,
262-
})
206+
this.form.bucketName.bindPrompter(
207+
({ region }) => createBucketPrompter(new DefaultS3Client(region!), deployMementoRootKey),
208+
{
209+
showWhen: ({ bucketSource }) => bucketSource === BucketSource.UserProvided,
210+
}
211+
)
263212
}
264213

265214
return this
@@ -352,7 +301,7 @@ export async function runDeploy(arg: any, wizardParams?: DeployParams): Promise<
352301
const paramsToSet: string[] = []
353302
for (const name of parameterNames) {
354303
if (params[name]) {
355-
await updateRecentParams(params.template.uri.fsPath, name, params[name])
304+
await updateRecentResponse(deployMementoRootKey, params.template.uri.fsPath, name, params[name])
356305
paramsToSet.push(`ParameterKey=${name},ParameterValue=${params[name]}`)
357306
}
358307
}

0 commit comments

Comments
 (0)