Skip to content

Commit

Permalink
fix: use internal cli templates (#3590)
Browse files Browse the repository at this point in the history
* bug fix for sunning samcli when cloning fails

* fixed failing test in Appveyor

* change os.path to pathlib.Path
  • Loading branch information
jonife authored Jan 19, 2022
1 parent 2aa7bf0 commit cc8de72
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 2 deletions.
10 changes: 8 additions & 2 deletions samcli/commands/init/init_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
from samcli.commands.exceptions import UserException, AppTemplateUpdateException
from samcli.lib.utils.git_repo import GitRepo, CloneRepoException, CloneRepoUnstableStateException
from samcli.lib.utils.packagetype import IMAGE
from samcli.local.common.runtime_template import RUNTIME_DEP_TEMPLATE_MAPPING, get_local_lambda_images_location
from samcli.local.common.runtime_template import (
RUNTIME_DEP_TEMPLATE_MAPPING,
get_local_lambda_images_location,
get_local_manifest_path,
)

LOG = logging.getLogger(__name__)
APP_TEMPLATES_REPO_URL = "https://github.com/aws/aws-sam-cli-app-templates"
Expand Down Expand Up @@ -124,7 +128,9 @@ def get_app_template_location(self, template_directory):
return os.path.normpath(os.path.join(self._git_repo.local_path, template_directory))

def get_manifest_path(self):
return Path(self._git_repo.local_path, self.manifest_file_name)
if self._git_repo.local_path and Path(self._git_repo.local_path, self.manifest_file_name).exists():
return Path(self._git_repo.local_path, self.manifest_file_name)
return get_local_manifest_path()

def get_preprocessed_manifest(
self,
Expand Down
146 changes: 146 additions & 0 deletions samcli/lib/init/local_manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
{
"dotnetcore3.1": [
{
"directory": "template/cookiecutter-aws-sam-hello-dotnet",
"displayName": "Hello World Example",
"dependencyManager": "cli-package",
"appTemplate": "hello-world",
"packageType": "Zip",
"useCaseName": "Hello World Example"
}
],
"go1.x": [
{
"directory": "template/cookiecutter-aws-sam-hello-golang",
"displayName": "Hello World Example",
"dependencyManager": "mod",
"appTemplate": "hello-world",
"packageType": "Zip",
"useCaseName": "Hello World Example"
}
],
"java11": [
{
"directory": "template/cookiecutter-aws-sam-hello-java-gradle",
"displayName": "Hello World Example: Gradle",
"dependencyManager": "gradle",
"appTemplate": "hello-world",
"packageType": "Zip",
"useCaseName": "Hello World Example"
},
{
"directory": "template/cookiecutter-aws-sam-hello-java-gradle",
"displayName": "Hello World Example: Maven",
"dependencyManager": "maven",
"appTemplate": "hello-world",
"packageType": "Zip",
"useCaseName": "Hello World Example"
}
],
"java8": [
{
"directory": "template/cookiecutter-aws-sam-hello-java-gradle",
"displayName": "Hello World Example: Gradle",
"dependencyManager": "gradle",
"appTemplate": "hello-world",
"packageType": "Zip",
"useCaseName": "Hello World Example"
},
{
"directory": "template/cookiecutter-aws-sam-hello-java-gradle",
"displayName": "Hello World Example: Maven",
"dependencyManager": "maven",
"appTemplate": "hello-world",
"packageType": "Zip",
"useCaseName": "Hello World Example"
}
],
"java8.al2": [
{
"directory": "template/cookiecutter-aws-sam-hello-java-gradle",
"displayName": "Hello World Example: Gradle",
"dependencyManager": "gradle",
"appTemplate": "hello-world",
"packageType": "Zip",
"useCaseName": "Hello World Example"
},
{
"directory": "template/cookiecutter-aws-sam-hello-java-maven",
"displayName": "Hello World Example: Maven",
"dependencyManager": "maven",
"appTemplate": "hello-world",
"packageType": "Zip",
"useCaseName": "Hello World Example"
}
],
"nodejs14.x": [
{
"directory": "template/cookiecutter-aws-sam-hello-nodejs",
"displayName": "Hello World Example",
"dependencyManager": "npm",
"appTemplate": "hello-world",
"packageType": "Zip",
"useCaseName": "Hello World Example"
}
],
"nodejs12.x": [
{
"directory": "template/cookiecutter-aws-sam-hello-nodejs",
"displayName": "Hello World Example",
"dependencyManager": "npm",
"appTemplate": "hello-world",
"packageType": "Zip",
"useCaseName": "Hello World Example"
}
],
"python3.9": [
{
"directory": "template/cookiecutter-aws-sam-hello-python",
"displayName": "Hello World Example",
"dependencyManager": "pip",
"appTemplate": "hello-world",
"packageType": "Zip",
"useCaseName": "Hello World Example"
}
],
"python3.8": [
{
"directory": "template/cookiecutter-aws-sam-hello-python",
"displayName": "Hello World Example",
"dependencyManager": "pip",
"appTemplate": "hello-world",
"packageType": "Zip",
"useCaseName": "Hello World Example"
}
],
"python3.7": [
{
"directory": "template/cookiecutter-aws-sam-hello-python",
"displayName": "Hello World Example",
"dependencyManager": "pip",
"appTemplate": "hello-world",
"packageType": "Zip",
"useCaseName": "Hello World Example"
}
],
"python3.6": [
{
"directory": "template/cookiecutter-aws-sam-hello-python",
"displayName": "Hello World Example",
"dependencyManager": "pip",
"appTemplate": "hello-world",
"packageType": "Zip",
"useCaseName": "Hello World Example"
}
],
"ruby2.7": [
{
"directory": "template/cookiecutter-aws-sam-hello-ruby",
"displayName": "Hello World Example",
"dependencyManager": "bundler",
"appTemplate": "hello-world",
"packageType": "Zip",
"useCaseName": "Hello World Example"
}
]
}
4 changes: 4 additions & 0 deletions samcli/local/common/runtime_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
}


def get_local_manifest_path():
return pathlib.Path(_init_path, "lib", "init", "local_manifest.json")


def get_local_lambda_images_location(mapping, runtime):
dir_name = os.path.basename(mapping["init_location"])
if dir_name.endswith("-lambda-image"):
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/commands/init/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2467,3 +2467,44 @@ def does_template_meet_filter_criteria(self):
}
dependency_manager = "Gradle"
self.assertTrue(template_does_not_meet_filter_criteria(app_template, None, dependency_manager, template3))

@patch("samcli.lib.utils.git_repo.GitRepo")
@patch.object(InitTemplates, "__init__", MockInitTemplates.__init__)
def test_must_get_local_manifest_path(self, git_repo):
template = InitTemplates()
template._git_repo.local_path = None
manifest_path = str(template.get_manifest_path())
file_name_path = "local_manifest.json"
self.assertIn(file_name_path, manifest_path)

@patch.object(Path, "exists")
@patch("samcli.commands.init.init_generator.generate_project")
@patch.object(InitTemplates, "__init__", MockInitTemplates.__init__)
def test_init_cli_generate_app_template_from_local_cli_templates(self, generate_project_patch, path_exist_mock):
path_exist_mock.return_value = False

# WHEN the user follows interactive init prompts
# 1: AWS Quick Start Templates
# 2: Java 11
# test-project: response to name
user_input = """
1
N
3
2
test-project
"""

runner = CliRunner()
result = runner.invoke(init_cmd, input=user_input)
self.assertFalse(result.exception)
generate_project_patch.assert_called_once_with(
ANY,
ZIP,
"java11",
"maven",
".",
"test-project",
True,
{"project_name": "test-project", "runtime": "java11", "architectures": {"value": ["x86_64"]}},
)

0 comments on commit cc8de72

Please sign in to comment.