Skip to content

Commit f39154c

Browse files
author
Vandita Patidar
committed
Building Quick Pick structure
1 parent eccc7bd commit f39154c

File tree

9 files changed

+702
-76
lines changed

9 files changed

+702
-76
lines changed

aws-toolkit-vscode.code-workspace

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,50 @@
11
{
2-
"folders": [
3-
{
4-
"path": "."
5-
},
6-
{
7-
"path": "packages/toolkit"
8-
},
9-
{
10-
"path": "packages/core"
11-
},
12-
{
13-
"path": "packages/amazonq"
14-
}
15-
],
16-
"settings": {
17-
"typescript.tsdk": "node_modules/typescript/lib"
18-
}
19-
}
2+
"folders": [
3+
{
4+
"path": ".",
5+
},
6+
{
7+
"path": "packages/toolkit",
8+
},
9+
{
10+
"path": "packages/core",
11+
},
12+
{
13+
"path": "packages/amazonq",
14+
},
15+
],
16+
"settings": {
17+
"typescript.tsdk": "node_modules/typescript/lib",
18+
},
19+
"launch": {
20+
"configurations": [
21+
{
22+
"type": "aws-sam",
23+
"request": "direct-invoke",
24+
"name": "core:src/awsService/appBuilder/serverlessLand.getProjectUri (nodejs14.x)",
25+
"invokeTarget": {
26+
"target": "code",
27+
"projectRoot": "/Users/vanditap/src/github.com/aws-toolkit-vscode/packages/core",
28+
"lambdaHandler": "src/awsService/appBuilder/serverlessLand.getProjectUri",
29+
},
30+
"lambda": {
31+
"runtime": "nodejs14.x",
32+
"payload": {},
33+
"environmentVariables": {},
34+
},
35+
},
36+
{
37+
"name": "Test Lint",
38+
"type": "node",
39+
"request": "launch",
40+
"program": "${workspaceFolder}/scripts/lint/testLint.ts",
41+
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
42+
"preLaunchTask": "build",
43+
"presentation": {
44+
"group": "7_TestLint",
45+
"order": 1,
46+
},
47+
},
48+
],
49+
},
50+
}

packages/core/scripts/build/copyFiles.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ const tasks: CopyTask[] = [
4444
target: path.join('../../node_modules', 'aws-ssm-document-language-service', 'dist', 'server.js.map'),
4545
destination: path.join('src', 'ssmDocument', 'ssm', 'server.js.map'),
4646
},
47+
{
48+
target: path.join('../core', 'src', 'awsService', 'appBuilder', 'serverlessLand', 'metadata.json'),
49+
destination: path.join('src', 'awsService', 'appBuilder', 'serverlessLand', 'metadata.json'),
50+
},
4751
]
4852

4953
function copy(task: CopyTask): void {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"patterns": {
3+
"s3-lambda-resizing-sam": {
4+
"name": "Resizing image",
5+
"description": "Lambda, S3 • Python, Javascript, Java, .NET • SAM",
6+
"implementation": [
7+
{
8+
"iac": "sam",
9+
"runtime": "python",
10+
"assetName": "s3-lambda-resizing-python"
11+
},
12+
{
13+
"iac": "sam",
14+
"runtime": "javascript",
15+
"assetName": "s3-lambda"
16+
},
17+
{
18+
"iac": "sam",
19+
"runtime": "java",
20+
"assetName": "s3-lambda-resizing-java"
21+
},
22+
{
23+
"iac": "sam",
24+
"runtime": "dotnet",
25+
"assetName": "s3-lambda-dotnet"
26+
}
27+
]
28+
},
29+
"apigw-rest-api-lambda-sam": {
30+
"name": "Rest API",
31+
"description": "Lambda, API Gateway • Python, Javascript, Java, .NET • SAM",
32+
"implementation": [
33+
{
34+
"iac": "sam",
35+
"runtime": "python",
36+
"assetName": "apigw-rest-api-lambda-python"
37+
},
38+
{
39+
"iac": "sam",
40+
"runtime": "javascript",
41+
"assetName": "apigw-rest-api-lambda-node"
42+
},
43+
{
44+
"iac": "sam",
45+
"runtime": "java",
46+
"assetName": "apigw-rest-api-lambda-java"
47+
},
48+
{
49+
"iac": "sam",
50+
"runtime": "dotnet",
51+
"assetName": "apigw-rest-api-lambda-dotnet"
52+
}
53+
]
54+
}
55+
}
56+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import * as nls from 'vscode-nls'
7+
const localize = nls.loadMessageBundle()
8+
import * as path from 'path'
9+
import * as vscode from 'vscode'
10+
import { getTelemetryResult, RegionProvider, ToolkitError } from '../../shared'
11+
import { getLogger } from '../../shared/logger'
12+
import { fileExists } from '../../shared/filesystemUtilities'
13+
import { CreateServerlessLandWizardForm } from '../appBuilder/wizards/serverlessLandWizard'
14+
import { Result, telemetry } from '../../shared/telemetry/telemetry'
15+
import { CreateServerlessLandWizard } from '../appBuilder/wizards/serverlessLandWizard'
16+
import { ExtContext } from '../../shared/extensions'
17+
import { addFolderToWorkspace } from '../../shared/utilities/workspaceUtils'
18+
import { getPattern } from '../../shared/utilities/downloadPatterns'
19+
20+
export const readmeFile: string = 'README.md'
21+
const serverlessLandOwner = 'aws-samples'
22+
const serverlessLandRepo = 'serverless-patterns'
23+
24+
/**
25+
* Creates a new Serverless Land project using the provided extension context
26+
* @param extContext Extension context containing AWS credentials and region information
27+
* @returns Promise that resolves when the project creation is complete
28+
*
29+
* This function:
30+
* 1. Validates AWS credentials and regions
31+
* 2. Launches the Serverless Land project creation wizard
32+
* 3. Creates the project structure
33+
* 4. Adds the project folder to the workspace
34+
* 5. Opens the README.md file if available
35+
* 6. Handles errors and emits telemetry
36+
*/
37+
export async function createNewServerlessLandProject(extContext: ExtContext): Promise<void> {
38+
let createResult: Result = 'Succeeded'
39+
let reason: string | undefined
40+
41+
try {
42+
// Launch the project creation wizard
43+
const config = await launchProjectCreationWizard(extContext)
44+
if (!config) {
45+
createResult = 'Cancelled'
46+
reason = 'userCancelled'
47+
return
48+
}
49+
await downloadPatternCode(config)
50+
await openReadmeFile(config)
51+
await addFolderToWorkspace(
52+
{
53+
uri: vscode.Uri.joinPath(config.location, config.name),
54+
name: path.basename(config.name),
55+
},
56+
true
57+
)
58+
} catch (err) {
59+
createResult = getTelemetryResult(err)
60+
reason = getTelemetryResult(err)
61+
throw new ToolkitError('Error creating new ServerlessLand Application')
62+
} finally {
63+
// add telemetry
64+
telemetry.sam_init.emit({
65+
result: createResult,
66+
reason: reason,
67+
})
68+
}
69+
}
70+
71+
async function launchProjectCreationWizard(
72+
extContext: ExtContext
73+
): Promise<CreateServerlessLandWizardForm | undefined> {
74+
const awsContext = extContext.awsContext
75+
const regionProvider: RegionProvider = extContext.regionProvider
76+
const credentials = await awsContext.getCredentials()
77+
const schemaRegions = regionProvider.getRegions().filter((r) => regionProvider.isServiceInRegion('schemas', r.id))
78+
const defaultRegion = awsContext.getCredentialDefaultRegion()
79+
80+
return new CreateServerlessLandWizard({
81+
credentials,
82+
schemaRegions,
83+
defaultRegion,
84+
}).run()
85+
}
86+
87+
async function downloadPatternCode(config: CreateServerlessLandWizardForm): Promise<void> {
88+
const assetName = config.assetName + '.zip'
89+
const location = vscode.Uri.joinPath(config.location, config.name)
90+
try {
91+
await getPattern(serverlessLandOwner, serverlessLandRepo, assetName, location, true)
92+
} catch (error) {
93+
if (error instanceof ToolkitError) {
94+
throw error
95+
}
96+
throw new ToolkitError(`Failed to download pattern: ${error}`)
97+
}
98+
}
99+
100+
async function openReadmeFile(config: CreateServerlessLandWizardForm): Promise<void> {
101+
try {
102+
const projectUri = await getProjectUri(config, readmeFile)
103+
if (!projectUri) {
104+
getLogger().warn('Project URI not found when trying to open README')
105+
return
106+
}
107+
108+
const readmeUri = vscode.Uri.file(path.join(path.dirname(projectUri.fsPath), readmeFile))
109+
if (!(await fileExists(readmeUri.fsPath))) {
110+
getLogger().warn(
111+
localize('AWS.serverlessLand.readme.notFound', 'README.md file not found in the project directory')
112+
)
113+
return
114+
}
115+
116+
try {
117+
const document = await vscode.workspace.openTextDocument(readmeUri)
118+
await vscode.window.showTextDocument(document, { preview: true })
119+
} catch (err) {
120+
getLogger().error(`Failed to open README file: ${err}`)
121+
throw new ToolkitError('Failed to open README file')
122+
}
123+
} catch (err) {
124+
getLogger().error(`Error in openReadmeFile: ${err}`)
125+
throw new ToolkitError('Error processing README file')
126+
}
127+
}
128+
129+
async function getProjectUri(
130+
config: Pick<CreateServerlessLandWizardForm, 'location' | 'name'>,
131+
file: string
132+
): Promise<vscode.Uri | undefined> {
133+
if (!file) {
134+
throw Error('expected "file" parameter to have at least one item')
135+
}
136+
const cfnTemplatePath = path.resolve(config.location.fsPath, config.name, file)
137+
if (await fileExists(cfnTemplatePath)) {
138+
return vscode.Uri.file(cfnTemplatePath)
139+
}
140+
void vscode.window.showWarningMessage(
141+
localize(
142+
'AWS.serverlessLand.initWizard.source.error.notFound',
143+
'Project created successfully, but {0} file not found: {1}',
144+
file!,
145+
cfnTemplatePath!
146+
)
147+
)
148+
}

0 commit comments

Comments
 (0)