diff --git a/samcli/commands/init/__init__.py b/samcli/commands/init/__init__.py index 9e77e1fa8a..e015a08df5 100644 --- a/samcli/commands/init/__init__.py +++ b/samcli/commands/init/__init__.py @@ -3,29 +3,31 @@ Init command to scaffold a project app from a template """ import logging + import click from samcli.cli.main import pass_context, common_options +from samcli.commands.exceptions import UserException +from samcli.local.common.runtime_template import INIT_RUNTIMES, SUPPORTED_DEP_MANAGERS from samcli.local.init import generate_project -from samcli.local.init import RUNTIME_TEMPLATE_MAPPING from samcli.local.init.exceptions import GenerateProjectFailedError -from samcli.commands.exceptions import UserException LOG = logging.getLogger(__name__) -SUPPORTED_RUNTIME = [r for r in RUNTIME_TEMPLATE_MAPPING] @click.command(context_settings=dict(help_option_names=[u'-h', u'--help'])) @click.option('-l', '--location', help="Template location (git, mercurial, http(s), zip, path)") -@click.option('-r', '--runtime', type=click.Choice(SUPPORTED_RUNTIME), default="nodejs8.10", +@click.option('-r', '--runtime', type=click.Choice(INIT_RUNTIMES), default="nodejs8.10", help="Lambda Runtime of your app") +@click.option('-d', '--dependency-manager', type=click.Choice(SUPPORTED_DEP_MANAGERS), default=None, + help="Dependency manager of your Lambda runtime", required=False) @click.option('-o', '--output-dir', default='.', type=click.Path(), help="Where to output the initialized app into") @click.option('-n', '--name', default="sam-app", help="Name of your project to be generated as a folder") @click.option('--no-input', is_flag=True, default=False, help="Disable prompting and accept default values defined template config") @common_options @pass_context -def cli(ctx, location, runtime, output_dir, name, no_input): +def cli(ctx, location, runtime, dependency_manager, output_dir, name, no_input): """ \b Initialize a serverless application with a SAM template, folder structure for your Lambda functions, connected to an event source such as APIs, @@ -43,6 +45,10 @@ def cli(ctx, location, runtime, output_dir, name, no_input): \b $ sam init --runtime python3.6 \b + Initializes a new SAM project using Java 8 and Gradle dependency manager + \b + $ sam init --runtime java8 --dependency-manager gradle + \b Initializes a new SAM project using custom template in a Git/Mercurial repository \b # gh being expanded to github url @@ -66,11 +72,11 @@ def cli(ctx, location, runtime, output_dir, name, no_input): """ # All logic must be implemented in the `do_cli` method. This helps ease unit tests - do_cli(ctx, location, runtime, output_dir, + do_cli(ctx, location, runtime, dependency_manager, output_dir, name, no_input) # pragma: no cover -def do_cli(ctx, location, runtime, output_dir, name, no_input): +def do_cli(ctx, location, runtime, dependency_manager, output_dir, name, no_input): """ Implementation of the ``cli`` method, just separated out for unit testing purposes """ @@ -101,7 +107,7 @@ def do_cli(ctx, location, runtime, output_dir, name, no_input): next_step_msg = no_build_msg if runtime in no_build_step_required else build_msg try: - generate_project(location, runtime, output_dir, name, no_input) + generate_project(location, runtime, dependency_manager, output_dir, name, no_input) if not location: click.secho(next_step_msg, bold=True) click.secho("Read {name}/README.md for further instructions\n".format(name=name), bold=True) diff --git a/samcli/local/common/__init__.py b/samcli/local/common/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/samcli/local/common/runtime_template.py b/samcli/local/common/runtime_template.py new file mode 100644 index 0000000000..04672af324 --- /dev/null +++ b/samcli/local/common/runtime_template.py @@ -0,0 +1,83 @@ +""" +All-in-one metadata about runtimes +""" + +import itertools +import os + +try: + import pathlib +except ImportError: + import pathlib2 as pathlib + +_init_path = str(pathlib.Path(os.path.dirname(__file__)).parent) +_templates = os.path.join(_init_path, 'init', 'templates') + +RUNTIME_DEP_TEMPLATE_MAPPING = { + "python": [ + { + "runtimes": ["python2.7", "python3.6", "python3.7"], + "dependency_manager": "pip", + "init_location": os.path.join(_templates, "cookiecutter-aws-sam-hello-python"), + "build": True + } + ], + "ruby": [ + { + "runtimes": ["ruby2.5"], + "dependency_manager": "bundler", + "init_location": os.path.join(_templates, "cookiecutter-aws-sam-hello-ruby"), + "build": True + } + ], + "nodejs": [ + { + "runtimes": ["nodejs8.10"], + "dependency_manager": "npm", + "init_location": os.path.join(_templates, "cookiecutter-aws-sam-hello-nodejs"), + "build": True + }, + { + "runtimes": ["nodejs6.10"], + "dependency_manager": "npm", + "init_location": os.path.join(_templates, "cookiecutter-aws-sam-hello-nodejs6"), + "build": True + }, + ], + "dotnet": [ + { + "runtimes": ["dotnetcore", "dotnetcore1.0", "dotnetcore2.0", "dotnetcore2.1"], + "dependency_manager": "cli-package", + "init_location": os.path.join(_templates, "cookiecutter-aws-sam-hello-dotnet"), + "build": False + }, + ], + "go": [ + { + "runtimes": ["go1.x"], + "dependency_manager": None, + "init_location": os.path.join(_templates, "cookiecutter-aws-sam-hello-go"), + "build": False + } + ], + "java": [ + { + "runtimes": ["java8"], + "dependency_manager": "maven", + "init_location": os.path.join(_templates, "cookiecutter-aws-sam-hello-java-maven"), + "build": False + }, + { + "runtimes": ["java8"], + "dependency_manager": "gradle", + "init_location": os.path.join(_templates, "cookiecutter-aws-sam-hello-java-gradle"), + "build": True + } + ] +} + +SUPPORTED_DEP_MANAGERS = set([c['dependency_manager'] for c in list( + itertools.chain(*(RUNTIME_DEP_TEMPLATE_MAPPING.values()))) if c['dependency_manager']]) +RUNTIMES = set(itertools.chain(*[c['runtimes'] for c in list( + itertools.chain(*(RUNTIME_DEP_TEMPLATE_MAPPING.values())))])) +INIT_RUNTIMES = RUNTIMES.union(RUNTIME_DEP_TEMPLATE_MAPPING.keys()) diff --git a/samcli/local/init/__init__.py b/samcli/local/init/__init__.py index 02c8a10358..89e4bc52c9 100644 --- a/samcli/local/init/__init__.py +++ b/samcli/local/init/__init__.py @@ -1,41 +1,20 @@ """ Init module to scaffold a project app from a template """ +import itertools import logging -import os -from cookiecutter.main import cookiecutter from cookiecutter.exceptions import CookiecutterException +from cookiecutter.main import cookiecutter + +from samcli.local.common.runtime_template import RUNTIME_DEP_TEMPLATE_MAPPING from samcli.local.init.exceptions import GenerateProjectFailedError LOG = logging.getLogger(__name__) -_init_path = os.path.dirname(__file__) -_templates = os.path.join(_init_path, 'templates') - -RUNTIME_TEMPLATE_MAPPING = { - "python3.7": os.path.join(_templates, "cookiecutter-aws-sam-hello-python"), - "python3.6": os.path.join(_templates, "cookiecutter-aws-sam-hello-python"), - "python2.7": os.path.join(_templates, "cookiecutter-aws-sam-hello-python"), - "python": os.path.join(_templates, "cookiecutter-aws-sam-hello-python"), - "ruby2.5": os.path.join(_templates, "cookiecutter-aws-sam-hello-ruby"), - "nodejs6.10": os.path.join(_templates, "cookiecutter-aws-sam-hello-nodejs6"), - "nodejs8.10": os.path.join(_templates, "cookiecutter-aws-sam-hello-nodejs"), - "nodejs": os.path.join(_templates, "cookiecutter-aws-sam-hello-nodejs"), - "dotnetcore2.0": os.path.join(_templates, "cookiecutter-aws-sam-hello-dotnet"), - "dotnetcore2.1": os.path.join(_templates, "cookiecutter-aws-sam-hello-dotnet"), - "dotnetcore1.0": os.path.join(_templates, "cookiecutter-aws-sam-hello-dotnet"), - "dotnetcore": os.path.join(_templates, "cookiecutter-aws-sam-hello-dotnet"), - "dotnet": os.path.join(_templates, "cookiecutter-aws-sam-hello-dotnet"), - "go1.x": os.path.join(_templates, "cookiecutter-aws-sam-hello-golang"), - "go": os.path.join(_templates, "cookiecutter-aws-sam-hello-golang"), - "java8": os.path.join(_templates, "cookiecutter-aws-sam-hello-java"), - "java": os.path.join(_templates, "cookiecutter-aws-sam-hello-java") -} - def generate_project( - location=None, runtime="nodejs", + location=None, runtime="nodejs", dependency_manager=None, output_dir=".", name='sam-sample-app', no_input=False): """Generates project using cookiecutter and options given @@ -50,6 +29,8 @@ def generate_project( (the default is None, which means no custom template) runtime: str, optional Lambda Runtime (the default is "nodejs", which creates a nodejs project) + dependency_manager: str, optional + Dependency Manager for the Lambda Runtime Project(the default is "npm" for a "nodejs" Lambda runtime) output_dir: str, optional Output directory where project should be generated (the default is ".", which implies current folder) @@ -66,8 +47,22 @@ def generate_project( If the process of baking a project fails """ + template = None + + for mapping in list(itertools.chain(*(RUNTIME_DEP_TEMPLATE_MAPPING.values()))): + if runtime in mapping['runtimes'] or any([r.startswith(runtime) for r in mapping['runtimes']]): + if not dependency_manager: + template = mapping['init_location'] + break + elif dependency_manager == mapping['dependency_manager']: + template = mapping['init_location'] + + if not template: + msg = "Lambda Runtime {} does not support dependency manager: {}".format(runtime, dependency_manager) + raise GenerateProjectFailedError(project=name, provider_error=msg) + params = { - "template": location if location else RUNTIME_TEMPLATE_MAPPING[runtime], + "template": location if location else template, "output_dir": output_dir, "no_input": no_input } diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/LICENSE b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/LICENSE similarity index 100% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-java/LICENSE rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/LICENSE diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/README.md b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/README.md similarity index 100% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-java/README.md rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/README.md diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/cookiecutter.json b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/cookiecutter.json similarity index 100% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-java/cookiecutter.json rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/cookiecutter.json diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/tests/test_cookiecutter.py b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/tests/test_cookiecutter.py similarity index 100% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-java/tests/test_cookiecutter.py rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/tests/test_cookiecutter.py diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/build.gradle b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/build.gradle new file mode 100644 index 0000000000..5df0f0ab7a --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/build.gradle @@ -0,0 +1,23 @@ +plugins { + id 'java' +} + +repositories { + mavenCentral() +} + +dependencies { + implementation 'software.amazon.awssdk:annotations:2.1.0' + compile ( + 'com.amazonaws:aws-lambda-java-core:1.1.0' + ) + testCompile 'junit:junit:4.12' +} + +sourceSets { + main { + java { + srcDir 'src/main' + } + } +} \ No newline at end of file diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/gradle/wrapper/gradle-wrapper.properties b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000..558870dad5 --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/gradlew b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/gradlew new file mode 100755 index 0000000000..af6708ff22 --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/gradlew.bat b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/gradlew.bat new file mode 100644 index 0000000000..0f8d5937c4 --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/src/main/java/helloworld/App.java b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/App.java similarity index 100% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/src/main/java/helloworld/App.java rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/App.java diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/src/main/java/helloworld/GatewayResponse.java b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/GatewayResponse.java similarity index 100% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/src/main/java/helloworld/GatewayResponse.java rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/GatewayResponse.java diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/src/test/java/helloworld/AppTest.java b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/src/test/java/helloworld/AppTest.java similarity index 100% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/src/test/java/helloworld/AppTest.java rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/HelloWorldFunction/src/test/java/helloworld/AppTest.java diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/README.md b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/README.md new file mode 100644 index 0000000000..daa130ff80 --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/README.md @@ -0,0 +1,146 @@ +# {{ cookiecutter.project_name }} + +This is a sample template for {{ cookiecutter.project_name }} - Below is a brief explanation of what we have generated for you: + +```bash +. +├── HelloWorldFunction +│   ├── build.gradle <-- Java Dependencies +│   ├── gradle <-- Gradle related Boilerplate +│   │   └── wrapper +│   │   ├── gradle-wrapper.jar +│   │   └── gradle-wrapper.properties +│   ├── gradlew <-- Linux/Mac Gradle Wrapper +│   ├── gradlew.bat <-- Windows Gradle Wrapper +│   └── src +│   ├── main +│   │   └── java +│   │   └── helloworld <-- Source code for a lambda function +│   │   ├── App.java <-- Lambda function code +│   │   └── GatewayResponse.java <-- POJO for API Gateway Responses object +│   └── test <-- Unit tests +│   └── java +│   └── helloworld +│   └── AppTest.java +├── README.md <-- This instructions file +└── template.yaml +``` + +## Requirements + +* AWS CLI already configured with Administrator permission +* [Java SE Development Kit 8 installed](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) +* [Docker installed](https://www.docker.com/community-edition) + +## Setup process + +### Installing dependencies + +```bash +sam build +``` + +You can also build on a Lambda like environment by using: + +```bash +sam build --use-container +``` + +### Local development + +**Invoking function locally through local API Gateway** + +```bash +sam local start-api +``` + +If the previous command ran successfully you should now be able to hit the following local endpoint to invoke your function `http://localhost:3000/hello` + +**SAM CLI** is used to emulate both Lambda and API Gateway locally and uses our `template.yaml` to understand how to bootstrap this environment (runtime, where the source code is, etc.) - The following excerpt is what the CLI will read in order to initialize an API and its routes: + +```yaml +... +Events: + HelloWorld: + Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api + Properties: + Path: /hello + Method: get +``` + +## Packaging and deployment + +Firstly, we need a `S3 bucket` where we can upload our Lambda functions packaged as ZIP before we deploy anything - If you don't have a S3 bucket to store code artifacts then this is a good time to create one: + +```bash +aws s3 mb s3://BUCKET_NAME +``` + +Next, run the following command to package our Lambda function to S3: + +```bash +sam package \ + --output-template-file packaged.yaml \ + --s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME +``` + +Next, the following command will create a Cloudformation Stack and deploy your SAM resources. + +```bash +sam deploy \ + --template-file packaged.yaml \ + --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ + --capabilities CAPABILITY_IAM +``` + +> **See [Serverless Application Model (SAM) HOWTO Guide](https://github.com/awslabs/serverless-application-model/blob/master/HOWTO.md) for more details in how to get started.** + +After deployment is complete you can run the following command to retrieve the API Gateway Endpoint URL: + +```bash +aws cloudformation describe-stacks \ + --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ + --query 'Stacks[].Outputs' +``` + +## Testing + +We use `JUnit` for testing our code and you can simply run the following command to run our tests: + +```bash +gradle test +``` + +# Appendix + +## AWS CLI commands + +AWS CLI commands to package, deploy and describe outputs defined within the cloudformation stack: + +```bash +sam package \ + --template-file template.yaml \ + --output-template-file packaged.yaml \ + --s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME + +sam deploy \ + --template-file packaged.yaml \ + --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} \ + --capabilities CAPABILITY_IAM \ + --parameter-overrides MyParameterSample=MySampleValue + +aws cloudformation describe-stacks \ + --stack-name {{ cookiecutter.project_name.lower().replace(' ', '-') }} --query 'Stacks[].Outputs' +``` + +## Bringing to the next level + +Here are a few ideas that you can use to get more acquainted as to how this overall process works: + +* Create an additional API resource (e.g. /hello/{proxy+}) and return the name requested through this new path +* Update unit test to capture that +* Package & Deploy + +Next, you can use the following resources to know more about beyond hello world samples and how others structure their Serverless applications: + +* [AWS Serverless Application Repository](https://aws.amazon.com/serverless/serverlessrepo/) diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/template.yaml b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/template.yaml new file mode 100644 index 0000000000..cbd079179a --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-gradle/{{cookiecutter.project_name}}/template.yaml @@ -0,0 +1,42 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: > + {{ cookiecutter.project_name }} + + Sample SAM Template for {{ cookiecutter.project_name }} + +# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst +Globals: + Function: + Timeout: 20 + +Resources: + HelloWorldFunction: + Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction + Properties: + CodeUri: HelloWorldFunction + Handler: helloworld.App::handleRequest + Runtime: java8 + Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object + Variables: + PARAM1: VALUE + Events: + HelloWorld: + Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api + Properties: + Path: /hello + Method: get + +Outputs: + # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function + # Find out more about other implicit resources you can reference within SAM + # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api + HelloWorldApi: + Description: "API Gateway endpoint URL for Prod stage for Hello World function" + Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/" + HelloWorldFunction: + Description: "Hello World Lambda Function ARN" + Value: !GetAtt HelloWorldFunction.Arn + HelloWorldFunctionIamRole: + Description: "Implicit IAM Role created for Hello World function" + Value: !GetAtt HelloWorldFunctionRole.Arn diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/LICENSE b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/LICENSE new file mode 100644 index 0000000000..f19aaa6d09 --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/LICENSE @@ -0,0 +1,14 @@ +MIT No Attribution + +Permission is hereby granted, free of charge, to any person obtaining a copy of this +software and associated documentation files (the "Software"), to deal in the Software +without restriction, including without limitation the rights to use, copy, modify, +merge, publish, distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/README.md b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/README.md new file mode 100644 index 0000000000..ec9954e48b --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/README.md @@ -0,0 +1,42 @@ +# Cookiecutter SAM for Java Lambda functions + +This is a [Cookiecutter](https://github.com/audreyr/cookiecutter) template to create a Serverless Hello World App based on Serverless Application Model (SAM) and Java. + +It is important to note that you should not try to `git clone` this project but use `cookiecutter` CLI instead as ``{{cookiecutter.project_name}}`` will be rendered based on your input and therefore all variables and files will be rendered properly. + +## Requirements + +Install `cookiecutter` command line: + +**Pip users**: + +* `pip install cookiecutter` + +**Homebrew users**: + +* `brew install cookiecutter` + +**Windows or Pipenv users**: + +* `pipenv install cookiecutter` + +**NOTE**: [`Pipenv`](https://github.com/pypa/pipenv) is the new and recommended Python packaging tool that works across multiple platforms and makes Windows a first-class citizen. + +## Usage + +Generate a new SAM based Serverless App: `cookiecutter gh:aws-samples/cookiecutter-aws-sam-hello-java`. + +You'll be prompted a few questions to help this cookiecutter template to scaffold this project and after its completed you should see a new folder at your current path with the name of the project you gave as input. + +**NOTE**: After you understand how cookiecutter works (cookiecutter.json, mainly), you can fork this repo and apply your own mechanisms to accelerate your development process and this can be followed for any programming language and OS. + + +# Credits + +* This project has been generated with [Cookiecutter](https://github.com/audreyr/cookiecutter) + + +License +------- + +This project is licensed under the terms of the [MIT License with no attribution](/LICENSE) diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/cookiecutter.json b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/cookiecutter.json new file mode 100644 index 0000000000..9d928e8536 --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/cookiecutter.json @@ -0,0 +1,4 @@ +{ + "project_name": "Name of the project", + "runtime": "java8" +} diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/tests/test_cookiecutter.py b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/tests/test_cookiecutter.py new file mode 100644 index 0000000000..8f2a6cf519 --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/tests/test_cookiecutter.py @@ -0,0 +1,41 @@ +""" + Tests cookiecutter baking process and rendered content +""" + +def test_project_tree(cookies): + result = cookies.bake(extra_context={ + 'project_name': 'hello sam' + }) + assert result.exit_code == 0 + assert result.exception is None + assert result.project.basename == 'hello sam' + assert result.project.isdir() + assert result.project.join('template.yaml').isfile() + assert result.project.join('README.md').isfile() + assert result.project.join('src').isdir() + assert result.project.join('src', 'main').isdir() + assert result.project.join('src', 'main', 'java').isdir() + assert result.project.join('src', 'main', 'java', 'helloworld').isdir() + assert result.project.join('src', 'main', 'java', 'helloworld', 'App.java').isfile() + assert result.project.join('src', 'main', 'java', 'helloworld', 'GatewayResponse.java').isfile() + assert result.project.join('src', 'test', 'java').isdir() + assert result.project.join('src', 'test', 'java', 'helloworld').isdir() + assert result.project.join('src', 'test', 'java', 'helloworld', 'AppTest.java').isfile() + + +def test_app_content(cookies): + result = cookies.bake(extra_context={'project_name': 'my_lambda'}) + app_file = result.project.join('src', 'main', 'java', 'helloworld', 'App.java') + app_content = app_file.readlines() + app_content = ''.join(app_content) + + contents = ( + "package helloword", + "class App implements RequestHandler", + "https://checkip.amazonaws.com", + "return new GatewayResponse", + "getPageContents", + ) + + for content in contents: + assert content in app_content diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/pom.xml b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/pom.xml similarity index 100% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/pom.xml rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/pom.xml diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/App.java b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/App.java new file mode 100644 index 0000000000..18890033e1 --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/App.java @@ -0,0 +1,38 @@ +package helloworld; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.IOException; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; + +/** + * Handler for requests to Lambda function. + */ +public class App implements RequestHandler { + + public Object handleRequest(final Object input, final Context context) { + Map headers = new HashMap<>(); + headers.put("Content-Type", "application/json"); + headers.put("X-Custom-Header", "application/json"); + try { + final String pageContents = this.getPageContents("https://checkip.amazonaws.com"); + String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\" }", pageContents); + return new GatewayResponse(output, headers, 200); + } catch (IOException e) { + return new GatewayResponse("{}", headers, 500); + } + } + + private String getPageContents(String address) throws IOException{ + URL url = new URL(address); + try(BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) { + return br.lines().collect(Collectors.joining(System.lineSeparator())); + } + } +} diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/GatewayResponse.java b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/GatewayResponse.java new file mode 100644 index 0000000000..ea58a79c99 --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/src/main/java/helloworld/GatewayResponse.java @@ -0,0 +1,33 @@ +package helloworld; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +/** + * POJO containing response object for API Gateway. + */ +public class GatewayResponse { + + private final String body; + private final Map headers; + private final int statusCode; + + public GatewayResponse(final String body, final Map headers, final int statusCode) { + this.statusCode = statusCode; + this.body = body; + this.headers = Collections.unmodifiableMap(new HashMap<>(headers)); + } + + public String getBody() { + return body; + } + + public Map getHeaders() { + return headers; + } + + public int getStatusCode() { + return statusCode; + } +} diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/src/test/java/helloworld/AppTest.java b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/src/test/java/helloworld/AppTest.java new file mode 100644 index 0000000000..e64bdfc55a --- /dev/null +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/HelloWorldFunction/src/test/java/helloworld/AppTest.java @@ -0,0 +1,21 @@ +package helloworld; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import org.junit.Test; + +public class AppTest { + @Test + public void successfulResponse() { + App app = new App(); + GatewayResponse result = (GatewayResponse) app.handleRequest(null, null); + assertEquals(result.getStatusCode(), 200); + assertEquals(result.getHeaders().get("Content-Type"), "application/json"); + String content = result.getBody(); + assertNotNull(content); + assertTrue(content.contains("\"message\"")); + assertTrue(content.contains("\"hello world\"")); + assertTrue(content.contains("\"location\"")); + } +} diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/README.md b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/README.md similarity index 85% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/README.md rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/README.md index 980f230f17..91899e4c3d 100644 --- a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/README.md +++ b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/README.md @@ -4,18 +4,19 @@ This is a sample template for {{ cookiecutter.project_name }} - Below is a brief ```bash . -├── README.md <-- This instructions file -├── pom.xml <-- Java dependencies -├── src -│ ├── main -│ │ └── java -│ │ └── helloworld <-- Source code for a lambda function -│ │ ├── App.java <-- Lambda function code -│ │ └── GatewayResponse.java <-- POJO for API Gateway Responses object -│ └── test <-- Unit tests -│ └── java -│ └── helloworld -│ └── AppTest.java +├── HelloWorldFunction +│   ├── pom.xml <-- Java dependencies +│   └── src +│   ├── main +│   │   └── java +│   │   └── helloworld <-- Source code for a lambda function +│   │   ├── App.java <-- Lambda function code +│   │   └── GatewayResponse.java <-- POJO for API Gateway Responses object +│   └── test <-- Unit tests +│   └── java +│   └── helloworld +│   └── AppTest.java +├── README.md <-- This instructions file └── template.yaml ``` diff --git a/samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/template.yaml b/samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/template.yaml similarity index 100% rename from samcli/local/init/templates/cookiecutter-aws-sam-hello-java/{{cookiecutter.project_name}}/template.yaml rename to samcli/local/init/templates/cookiecutter-aws-sam-hello-java-maven/{{cookiecutter.project_name}}/template.yaml diff --git a/tests/unit/commands/init/test_cli.py b/tests/unit/commands/init/test_cli.py index 2fe29d6d40..25fe7142c3 100644 --- a/tests/unit/commands/init/test_cli.py +++ b/tests/unit/commands/init/test_cli.py @@ -12,6 +12,7 @@ def setUp(self): self.ctx = None self.location = None self.runtime = "python3.6" + self.dependency_manager = "pip" self.output_dir = "." self.name = "testing project" self.no_input = False @@ -21,12 +22,13 @@ def test_init_cli(self, generate_project_patch): # GIVEN generate_project successfully created a project # WHEN a project name has been passed init_cli( - ctx=self.ctx, location=self.location, runtime=self.runtime, output_dir=self.output_dir, + ctx=self.ctx, location=self.location, runtime=self.runtime, + dependency_manager=self.dependency_manager, output_dir=self.output_dir, name=self.name, no_input=self.no_input) # THEN we should receive no errors generate_project_patch.assert_called_once_with( - self.location, self.runtime, + self.location, self.runtime, self.dependency_manager, self.output_dir, self.name, self.no_input) @patch("samcli.commands.init.generate_project") @@ -42,8 +44,9 @@ def test_init_cli_generate_project_fails(self, generate_project_patch): with self.assertRaises(UserException): init_cli( self.ctx, location="self.location", runtime=self.runtime, + dependency_manager=self.dependency_manager, output_dir=self.output_dir, name=self.name, no_input=self.no_input) generate_project_patch.assert_called_with( - self.location, self.runtime, + self.location, self.runtime, self.dependency_manager, self.output_dir, self.name, self.no_input) diff --git a/tests/unit/local/init/test_init.py b/tests/unit/local/init/test_init.py index 4e79db081e..8ecb941a85 100644 --- a/tests/unit/local/init/test_init.py +++ b/tests/unit/local/init/test_init.py @@ -4,7 +4,7 @@ from cookiecutter.exceptions import CookiecutterException from samcli.local.init import generate_project from samcli.local.init import GenerateProjectFailedError -from samcli.local.init import RUNTIME_TEMPLATE_MAPPING +from samcli.local.init import RUNTIME_DEP_TEMPLATE_MAPPING class TestInit(TestCase): @@ -12,18 +12,20 @@ class TestInit(TestCase): def setUp(self): self.location = None self.runtime = "python3.6" + self.dependency_manager = "pip" self.output_dir = "." self.name = "testing project" self.no_input = True self.extra_context = {'project_name': 'testing project', "runtime": self.runtime} - self.template = RUNTIME_TEMPLATE_MAPPING[self.runtime] + self.template = RUNTIME_DEP_TEMPLATE_MAPPING["python"][0]["init_location"] @patch("samcli.local.init.cookiecutter") def test_init_successful(self, cookiecutter_patch): # GIVEN generate_project successfully created a project # WHEN a project name has been passed generate_project( - location=self.location, runtime=self.runtime, output_dir=self.output_dir, + location=self.location, runtime=self.runtime, dependency_manager=self.dependency_manager, + output_dir=self.output_dir, name=self.name, no_input=self.no_input) # THEN we should receive no errors @@ -31,6 +33,27 @@ def test_init_successful(self, cookiecutter_patch): extra_context=self.extra_context, no_input=self.no_input, output_dir=self.output_dir, template=self.template) + @patch("samcli.local.init.cookiecutter") + def test_init_successful_with_no_dep_manager(self, cookiecutter_patch): + generate_project( + location=self.location, runtime=self.runtime, dependency_manager=None, + output_dir=self.output_dir, + name=self.name, no_input=self.no_input) + + # THEN we should receive no errors + cookiecutter_patch.assert_called_once_with( + extra_context=self.extra_context, no_input=self.no_input, + output_dir=self.output_dir, template=self.template) + + def test_init_error_with_non_compatible_dependency_manager(self): + with self.assertRaises(GenerateProjectFailedError) as ctx: + generate_project( + location=self.location, runtime=self.runtime, dependency_manager="gradle", + output_dir=self.output_dir, name=self.name, no_input=self.no_input) + self.assertEquals("An error occurred while generating this " + "testing project: Lambda Runtime python3.6 " + "does not support dependency manager: gradle", str(ctx.exception)) + @patch("samcli.local.init.cookiecutter") def test_when_generate_project_returns_error(self, cookiecutter_patch): @@ -44,7 +67,7 @@ def test_when_generate_project_returns_error(self, cookiecutter_patch): # THEN we should receive a GenerateProjectFailedError Exception with self.assertRaises(GenerateProjectFailedError) as ctx: generate_project( - location=self.location, runtime=self.runtime, + location=self.location, runtime=self.runtime, dependency_manager=self.dependency_manager, output_dir=self.output_dir, name=self.name, no_input=self.no_input) self.assertEquals(expected_msg, str(ctx.exception))