Skip to content

Commit 292b476

Browse files
authored
Adding fields to sam init for SAM CLI 0.30.0 (#786)
* Adding --app-template and --dependency-manager args for SAM CLI 0.30.0+ * Corrected spelling * Changelog entry * Wrong switch/case syntax
1 parent 88abd1d commit 292b476

File tree

7 files changed

+44
-7
lines changed

7 files changed

+44
-7
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Feature",
3+
"description": "Adding support for SAM CLI 0.30.0 features in `sam init`: --app-template and --dependency-manager"
4+
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/integrationTest/sam.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as assert from 'assert'
77
import { mkdirpSync, readFileSync, removeSync } from 'fs-extra'
88
import * as path from 'path'
99
import * as vscode from 'vscode'
10-
import { SamLambdaRuntime } from '../../src/lambda/models/samLambdaRuntime'
10+
import { getDependencyManager, SamLambdaRuntime } from '../../src/lambda/models/samLambdaRuntime'
1111
import { getSamCliContext } from '../../src/shared/sam/cli/samCliContext'
1212
import { runSamCliInit, SamCliInitArgs } from '../../src/shared/sam/cli/samCliInit'
1313
import { assertThrowsError } from '../../src/test/shared/utilities/assertUtils'
@@ -130,10 +130,12 @@ for (const runtime of runtimes) {
130130
tryRemoveProjectFolder()
131131
mkdirpSync(projectFolder)
132132
// this is really test 1, but since it has to run before everything it's in the before section
133+
const runtimeArg = projectSDK as SamLambdaRuntime
133134
const initArguments: SamCliInitArgs = {
134135
name: 'testProject',
135136
location: projectFolder,
136-
runtime: projectSDK as SamLambdaRuntime
137+
runtime: runtimeArg,
138+
dependencyManager: getDependencyManager(runtimeArg)
137139
}
138140
const samCliContext = getSamCliContext()
139141
await runSamCliInit(initArguments, samCliContext)
@@ -158,10 +160,12 @@ for (const runtime of runtimes) {
158160
})
159161

160162
it('Fails to create template when it already exists', async () => {
163+
const runtimeArg = projectSDK as SamLambdaRuntime
161164
const initArguments: SamCliInitArgs = {
162165
name: 'testProject',
163166
location: projectFolder,
164-
runtime: projectSDK as SamLambdaRuntime
167+
runtime: runtimeArg,
168+
dependencyManager: getDependencyManager(runtimeArg)
165169
}
166170
console.log(initArguments.location)
167171
const samCliContext = getSamCliContext()

src/lambda/commands/createNewSamApp.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { METADATA_FIELD_NAME, MetadataResult } from '../../shared/telemetry/tele
1818
import { makeCheckLogsMessage } from '../../shared/utilities/messages'
1919
import { ChannelLogger } from '../../shared/utilities/vsCodeUtils'
2020
import { addFolderToWorkspace } from '../../shared/utilities/workspaceUtils'
21+
import { getDependencyManager } from '../models/samLambdaRuntime'
2122
import {
2223
CreateNewSamAppWizard,
2324
CreateNewSamAppWizardResponse,
@@ -86,11 +87,16 @@ export async function createNewSamApplication(
8687

8788
results.runtime = config.runtime
8889

90+
// TODO: Make this selectable in the wizard to account for runtimes with multiple dependency managers
91+
const dependencyManager = getDependencyManager(config.runtime)
92+
8993
const initArguments: SamCliInitArgs = {
9094
name: config.name,
9195
location: config.location.fsPath,
92-
runtime: config.runtime
96+
runtime: config.runtime,
97+
dependencyManager
9398
}
99+
94100
await runSamCliInit(initArguments, samCliContext)
95101

96102
results.result = 'pass'

src/lambda/models/samLambdaRuntime.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@ export const samLambdaRuntimes: immutable.Set<SamLambdaRuntime> = immutable.Set(
1818
'dotnetcore2.1'
1919
] as SamLambdaRuntime[])
2020

21+
export type DependencyManager = 'cli-package' | 'mod' | 'gradle' | 'pip' | 'npm' | 'maven' | 'bundler'
22+
23+
// TODO: Make this return an array of DependencyManagers when we add runtimes with multiple dependency managers
24+
export function getDependencyManager (runtime: SamLambdaRuntime): DependencyManager {
25+
switch (runtime) {
26+
case 'nodejs10.x':
27+
case 'nodejs8.10':
28+
return 'npm'
29+
case 'python2.7':
30+
case 'python3.6':
31+
case 'python3.7':
32+
return 'pip'
33+
case 'dotnetcore2.1':
34+
return 'cli-package'
35+
default:
36+
throw new Error(`Runtime ${runtime} does not have an associated DependencyManager`)
37+
}
38+
}
39+
2140
export enum SamLambdaRuntimeFamily {
2241
Python,
2342
NodeJS,

src/shared/sam/cli/samCliInit.ts

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

66
import * as semver from 'semver'
7-
import { SamLambdaRuntime } from '../../../lambda/models/samLambdaRuntime'
7+
import { DependencyManager, SamLambdaRuntime } from '../../../lambda/models/samLambdaRuntime'
88
import { getSamCliVersion, SamCliContext } from './samCliContext'
99
import { logAndThrowIfUnexpectedExitCode } from './samCliInvokerUtils'
1010
import { SAM_CLI_VERSION_0_30 } from './samCliValidator'
@@ -13,6 +13,7 @@ export interface SamCliInitArgs {
1313
runtime: SamLambdaRuntime
1414
location: string
1515
name: string
16+
dependencyManager: DependencyManager
1617
}
1718

1819
export async function runSamCliInit(initArguments: SamCliInitArgs, context: SamCliContext): Promise<void> {
@@ -21,6 +22,8 @@ export async function runSamCliInit(initArguments: SamCliInitArgs, context: SamC
2122

2223
if (semver.gte(samCliVersion, SAM_CLI_VERSION_0_30)) {
2324
args.push('--no-interactive')
25+
args.push('--app-template', 'hello-world')
26+
args.push('--dependency-manager', initArguments.dependencyManager)
2427
}
2528

2629
const childProcessResult = await context.invoker.invoke({

src/test/shared/sam/cli/samCliInit.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ describe('runSamCliInit', async () => {
6666
const sampleSamInitArgs: SamCliInitArgs = {
6767
name: 'qwerty',
6868
location: '/some/path/to/code.js',
69-
runtime: 'nodejs8.10'
69+
runtime: 'nodejs8.10',
70+
dependencyManager: 'npm'
7071
}
7172

7273
it('Passes init command to sam cli', async () => {

0 commit comments

Comments
 (0)