Skip to content

Commit

Permalink
add constants file
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfreder committed Dec 10, 2024
1 parent 00e99fe commit 3471429
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 32 deletions.
6 changes: 3 additions & 3 deletions packages/core/src/shared/sam/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import {
} from './utils'
import { getConfigFileUri, validateSamBuildConfig } from './config'
import { runInTerminal } from './processTerminal'
import { BUILD_MEMENTO_ROOT_KEY } from './constants'

const buildMementoRootKey = 'samcli.build.params'
export interface BuildParams {
readonly template: TemplateItem
readonly projectRoot: vscode.Uri
Expand Down Expand Up @@ -140,7 +140,7 @@ export class BuildWizard extends Wizard<BuildParams> {
if (this.arg === undefined) {
// "Build" command was invoked on the command palette.
this.form.template.bindPrompter(() =>
createTemplatePrompter(this.registry, buildMementoRootKey, samBuildUrl)
createTemplatePrompter(this.registry, BUILD_MEMENTO_ROOT_KEY, samBuildUrl)
)
this.form.projectRoot.setDefault(({ template }) => getProjectRoot(template))
this.form.paramsSource.bindPrompter(async ({ projectRoot }) => {
Expand Down Expand Up @@ -232,7 +232,7 @@ export async function runBuild(arg?: TreeNode): Promise<SamBuildResult> {
const templatePath = params.template.uri.fsPath
buildFlags.push('--template', `${templatePath}`)

await updateRecentResponse(buildMementoRootKey, 'global', 'templatePath', templatePath)
await updateRecentResponse(BUILD_MEMENTO_ROOT_KEY, 'global', 'templatePath', templatePath)

try {
await vscode.window.withProgress(
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/shared/sam/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* eslint-disable header/header */
/* eslint-disable @typescript-eslint/naming-convention */
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

export const BUILD_PROCESS_MEMENTO_ROOT_KEY = 'samcli.build.processes'
export const GLOBAL_IDENTIFIER = 'global'
export const BUILD_MEMENTO_ROOT_KEY = 'samcli.build.params'
export const DEPLOY_MEMENTO_ROOT_KEY = 'samcli.deploy.params'
export const SYNC_MEMENTO_ROOT_KEY = 'samcli.sync.params'
45 changes: 30 additions & 15 deletions packages/core/src/shared/sam/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
updateRecentResponse,
} from './utils'
import { runInTerminal } from './processTerminal'
import { DEPLOY_MEMENTO_ROOT_KEY } from './constants'

export interface DeployParams {
readonly paramsSource: ParamsSource
Expand All @@ -48,10 +49,8 @@ export interface DeployParams {
[key: string]: any
}

const deployMementoRootKey = 'samcli.deploy.params'

function getRecentDeployParams(identifier: string, key: string): string | undefined {
return getRecentResponse(deployMementoRootKey, identifier, key)
return getRecentResponse(DEPLOY_MEMENTO_ROOT_KEY, identifier, key)
}

function createParamPromptProvider(name: string, defaultValue: string | undefined, templateFsPath: string = 'default') {
Expand Down Expand Up @@ -106,7 +105,11 @@ export class DeployWizard extends Wizard<DeployParams> {
})
this.form.stackName.bindPrompter(
({ region }) =>
createStackPrompter(new DefaultCloudFormationClient(region!), deployMementoRootKey, samDeployUrl),
createStackPrompter(
new DefaultCloudFormationClient(region!),
DEPLOY_MEMENTO_ROOT_KEY,
samDeployUrl
),
{
showWhen: ({ paramsSource }) =>
paramsSource === ParamsSource.Specify || paramsSource === ParamsSource.SpecifyAndSave,
Expand All @@ -118,15 +121,15 @@ export class DeployWizard extends Wizard<DeployParams> {
})
this.form.bucketName.bindPrompter(
({ region }) =>
createBucketNamePrompter(new DefaultS3Client(region!), deployMementoRootKey, samDeployUrl),
createBucketNamePrompter(new DefaultS3Client(region!), DEPLOY_MEMENTO_ROOT_KEY, samDeployUrl),
{
showWhen: ({ bucketSource }) => bucketSource === BucketSource.UserProvided,
}
)
} else if (this.arg && this.arg.regionCode) {
// "Deploy" command was invoked on a regionNode.
this.form.template.bindPrompter(() =>
createTemplatePrompter(this.registry, deployMementoRootKey, samDeployUrl)
createTemplatePrompter(this.registry, DEPLOY_MEMENTO_ROOT_KEY, samDeployUrl)
)
this.form.projectRoot.setDefault(({ template }) => getProjectRoot(template))
this.form.paramsSource.bindPrompter(async ({ projectRoot }) => {
Expand All @@ -136,7 +139,11 @@ export class DeployWizard extends Wizard<DeployParams> {
this.form.region.setDefault(() => this.arg.regionCode)
this.form.stackName.bindPrompter(
({ region }) =>
createStackPrompter(new DefaultCloudFormationClient(region!), deployMementoRootKey, samDeployUrl),
createStackPrompter(
new DefaultCloudFormationClient(region!),
DEPLOY_MEMENTO_ROOT_KEY,
samDeployUrl
),
{
showWhen: ({ paramsSource }) =>
paramsSource === ParamsSource.Specify || paramsSource === ParamsSource.SpecifyAndSave,
Expand All @@ -148,7 +155,7 @@ export class DeployWizard extends Wizard<DeployParams> {
})
this.form.bucketName.bindPrompter(
({ region }) =>
createBucketNamePrompter(new DefaultS3Client(region!), deployMementoRootKey, samDeployUrl),
createBucketNamePrompter(new DefaultS3Client(region!), DEPLOY_MEMENTO_ROOT_KEY, samDeployUrl),
{
showWhen: ({ bucketSource }) => bucketSource === BucketSource.UserProvided,
}
Expand All @@ -171,7 +178,11 @@ export class DeployWizard extends Wizard<DeployParams> {
})
this.form.stackName.bindPrompter(
({ region }) =>
createStackPrompter(new DefaultCloudFormationClient(region!), deployMementoRootKey, samDeployUrl),
createStackPrompter(
new DefaultCloudFormationClient(region!),
DEPLOY_MEMENTO_ROOT_KEY,
samDeployUrl
),
{
showWhen: ({ paramsSource }) =>
paramsSource === ParamsSource.Specify || paramsSource === ParamsSource.SpecifyAndSave,
Expand All @@ -183,7 +194,7 @@ export class DeployWizard extends Wizard<DeployParams> {
})
this.form.bucketName.bindPrompter(
({ region }) =>
createBucketNamePrompter(new DefaultS3Client(region!), deployMementoRootKey, samDeployUrl),
createBucketNamePrompter(new DefaultS3Client(region!), DEPLOY_MEMENTO_ROOT_KEY, samDeployUrl),
{
showWhen: ({ bucketSource }) => bucketSource === BucketSource.UserProvided,
}
Expand All @@ -192,7 +203,7 @@ export class DeployWizard extends Wizard<DeployParams> {
} else {
// "Deploy" command was invoked on the command palette.
this.form.template.bindPrompter(() =>
createTemplatePrompter(this.registry, deployMementoRootKey, samDeployUrl)
createTemplatePrompter(this.registry, DEPLOY_MEMENTO_ROOT_KEY, samDeployUrl)
)
this.form.projectRoot.setDefault(({ template }) => getProjectRoot(template))
this.form.paramsSource.bindPrompter(async ({ projectRoot }) => {
Expand All @@ -205,7 +216,11 @@ export class DeployWizard extends Wizard<DeployParams> {
})
this.form.stackName.bindPrompter(
({ region }) =>
createStackPrompter(new DefaultCloudFormationClient(region!), deployMementoRootKey, samDeployUrl),
createStackPrompter(
new DefaultCloudFormationClient(region!),
DEPLOY_MEMENTO_ROOT_KEY,
samDeployUrl
),
{
showWhen: ({ paramsSource }) =>
paramsSource === ParamsSource.Specify || paramsSource === ParamsSource.SpecifyAndSave,
Expand All @@ -217,7 +232,7 @@ export class DeployWizard extends Wizard<DeployParams> {
})
this.form.bucketName.bindPrompter(
({ region }) =>
createBucketNamePrompter(new DefaultS3Client(region!), deployMementoRootKey, samDeployUrl),
createBucketNamePrompter(new DefaultS3Client(region!), DEPLOY_MEMENTO_ROOT_KEY, samDeployUrl),
{
showWhen: ({ bucketSource }) => bucketSource === BucketSource.UserProvided,
}
Expand Down Expand Up @@ -314,14 +329,14 @@ export async function runDeploy(arg: any, wizardParams?: DeployParams): Promise<
const paramsToSet: string[] = []
for (const name of parameterNames) {
if (params[name]) {
await updateRecentResponse(deployMementoRootKey, params.template.uri.fsPath, name, params[name])
await updateRecentResponse(DEPLOY_MEMENTO_ROOT_KEY, params.template.uri.fsPath, name, params[name])
paramsToSet.push(`ParameterKey=${name},ParameterValue=${params[name]}`)
}
}
paramsToSet.length > 0 && deployFlags.push('--parameter-overrides', paramsToSet.join(' '))
}

await updateRecentResponse(deployMementoRootKey, 'global', 'templatePath', params.template.uri.fsPath)
await updateRecentResponse(DEPLOY_MEMENTO_ROOT_KEY, 'global', 'templatePath', params.template.uri.fsPath)

try {
const { path: samCliPath } = await getSamCliPathAndVersion()
Expand Down
15 changes: 7 additions & 8 deletions packages/core/src/shared/sam/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { ParamsSource, createSyncParamsSourcePrompter } from '../ui/sam/paramsSo
import { createEcrPrompter } from '../ui/sam/ecrPrompter'
import { BucketSource, createBucketNamePrompter, createBucketSourcePrompter } from '../ui/sam/bucketPrompter'
import { runInTerminal } from './processTerminal'
import { SYNC_MEMENTO_ROOT_KEY } from './constants'

export interface SyncParams {
readonly paramsSource: ParamsSource
Expand All @@ -68,11 +69,9 @@ export interface SyncParams {
readonly syncFlags?: string
}

export const syncMementoRootKey = 'samcli.sync.params'

// TODO: hook this up so it prompts the user when more than 1 environment is present in `samconfig.toml`
export function createEnvironmentPrompter(config: SamConfig, environments = config.listEnvironments()) {
const recentEnvironmentName = getRecentResponse(syncMementoRootKey, config.location.fsPath, 'environmentName')
const recentEnvironmentName = getRecentResponse(SYNC_MEMENTO_ROOT_KEY, config.location.fsPath, 'environmentName')
const items = environments.map((env) => ({
label: env.name,
data: env,
Expand Down Expand Up @@ -156,7 +155,7 @@ export class SyncWizard extends Wizard<SyncParams> {
) {
super({ initState: state, exitPrompterProvider: shouldPromptExit ? createExitPrompter : undefined })
this.registry = registry
this.form.template.bindPrompter(() => createTemplatePrompter(this.registry, syncMementoRootKey, samSyncUrl))
this.form.template.bindPrompter(() => createTemplatePrompter(this.registry, SYNC_MEMENTO_ROOT_KEY, samSyncUrl))
this.form.projectRoot.setDefault(({ template }) => getProjectRoot(template))

this.form.paramsSource.bindPrompter(async ({ projectRoot }) => {
Expand All @@ -169,7 +168,7 @@ export class SyncWizard extends Wizard<SyncParams> {
})
this.form.stackName.bindPrompter(
({ region }) =>
createStackPrompter(new DefaultCloudFormationClient(region!), syncMementoRootKey, samSyncUrl),
createStackPrompter(new DefaultCloudFormationClient(region!), SYNC_MEMENTO_ROOT_KEY, samSyncUrl),
{
showWhen: ({ paramsSource }) =>
paramsSource === ParamsSource.Specify || paramsSource === ParamsSource.SpecifyAndSave,
Expand All @@ -181,14 +180,14 @@ export class SyncWizard extends Wizard<SyncParams> {
})

this.form.bucketName.bindPrompter(
({ region }) => createBucketNamePrompter(new DefaultS3Client(region!), syncMementoRootKey, samSyncUrl),
({ region }) => createBucketNamePrompter(new DefaultS3Client(region!), SYNC_MEMENTO_ROOT_KEY, samSyncUrl),
{
showWhen: ({ bucketSource }) => bucketSource === BucketSource.UserProvided,
}
)

this.form.ecrRepoUri.bindPrompter(
({ region }) => createEcrPrompter(new DefaultEcrClient(region!), syncMementoRootKey),
({ region }) => createEcrPrompter(new DefaultEcrClient(region!), SYNC_MEMENTO_ROOT_KEY),
{
showWhen: ({ template, paramsSource }) =>
!!template &&
Expand Down Expand Up @@ -516,7 +515,7 @@ export async function runSync(
}

async function updateSyncRecentResponse(region: string, key: string, value: string | undefined) {
return await updateRecentResponse(syncMementoRootKey, region, key, value)
return await updateRecentResponse(SYNC_MEMENTO_ROOT_KEY, region, key, value)
}

export async function confirmDevStack() {
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/shared/sam/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable header/header */
/* eslint-disable @typescript-eslint/naming-convention */
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
Expand All @@ -18,6 +20,7 @@ import { telemetry } from '../telemetry/telemetry'
import globals from '../extensionGlobals'
import { getLogger } from '../logger/logger'
import { ChildProcessResult } from '../utilities/processUtils'
import { BUILD_PROCESS_MEMENTO_ROOT_KEY, GLOBAL_IDENTIFIER } from './constants'

/**
* @description determines the root directory of the project given Template Item
Expand Down Expand Up @@ -109,15 +112,12 @@ export async function updateRecentResponse(
}
}

const buildProcessMementoRootKey = 'samcli.build.processes'
const globalIdentifier = 'global'

/**
* Returns true if there's an ongoing build process for the provided template, false otherwise
* @Param templatePath The path to the template.yaml file
*/
function isBuildInProgress(templatePath: string): boolean {
return getRecentResponse(buildProcessMementoRootKey, globalIdentifier, templatePath) !== undefined
return getRecentResponse(BUILD_PROCESS_MEMENTO_ROOT_KEY, GLOBAL_IDENTIFIER, templatePath) !== undefined
}

/**
Expand All @@ -131,11 +131,11 @@ export function throwIfTemplateIsBeingBuilt(templatePath: string) {
}

export async function registerTemplateBuild(templatePath: string) {
await updateRecentResponse(buildProcessMementoRootKey, 'global', templatePath, 'true')
await updateRecentResponse(BUILD_PROCESS_MEMENTO_ROOT_KEY, GLOBAL_IDENTIFIER, templatePath, 'true')
}

export async function unregisterTemplateBuild(templatePath: string) {
await updateRecentResponse(buildProcessMementoRootKey, 'global', templatePath, undefined)
await updateRecentResponse(BUILD_PROCESS_MEMENTO_ROOT_KEY, GLOBAL_IDENTIFIER, templatePath, undefined)
}

export function getSamCliErrorMessage(stderr: string): string {
Expand Down

0 comments on commit 3471429

Please sign in to comment.