Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(awscdk-app): initialize sample code stack names based on project name #1711

Merged
merged 6 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/awscdk/awscdk-app-java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class AwsCdkJavaApp extends JavaProject {
`public class ${this.mainClassName} {`,
" public static void main(final String[] args) {",
" App app = new App();",
' new Stack(app, "MyStack");',
` new Stack(app, "${this.name}");`,
" app.synth();",
" }",
"}",
Expand Down
48 changes: 23 additions & 25 deletions src/awscdk/awscdk-app-py.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
CdkConfigCommonOptions,
CdkTasks,
} from ".";
import { Component, DependencyType, SampleDir, SourceCode } from "..";
import { Component, DependencyType, SampleDir, TextFile } from "..";
import { Pytest } from "../python/pytest";
import { PythonProject, PythonProjectOptions } from "../python/python-project";
import { AwsCdkDepsPy } from "./awscdk-deps-py";
Expand Down Expand Up @@ -113,36 +113,34 @@ class AppCode extends Component {
constructor(project: AwsCdkPythonApp, fileName: string, cdkVersion: number) {
super(project);

const src = new SourceCode(project, fileName);
const src = new TextFile(project, fileName, {
readonly: false,
marciocadev marked this conversation as resolved.
Show resolved Hide resolved
});

src.line("import os");
src.addLine("import os");
if (cdkVersion < 2) {
src.line("from aws_cdk.core import App, Environment");
src.addLine("from aws_cdk.core import App, Environment");
} else {
src.line("from aws_cdk import App, Environment");
src.addLine("from aws_cdk import App, Environment");
}
src.line(`from ${project.moduleName}.my_stack import MyStack`);
src.line("");
src.line("# for development, use account/region from cdk cli");
src.open("dev_env = Environment(");
src.line("account=os.getenv('CDK_DEFAULT_ACCOUNT'),");
src.line("region=os.getenv('CDK_DEFAULT_REGION'),");
src.close(")");
src.line("");
src.line("app = App()");
src.line('MyStack(app, "my-stack-dev", env=dev_env)');
src.line('# MyStack(app, "my-stack-prod", env=prod_env)');
src.line("");
src.line("app.synth()");
src.addLine(`from ${project.moduleName}.main import MyStack`);
src.addLine("");
src.addLine("# for development, use account/region from cdk cli");
src.addLine("dev_env = Environment(");
src.addLine(" account=os.getenv('CDK_DEFAULT_ACCOUNT'),");
src.addLine(" region=os.getenv('CDK_DEFAULT_REGION'),");
src.addLine(")");
src.addLine("");
src.addLine("app = App()");
src.addLine(`MyStack(app, "${this.project.name}-dev", env=dev_env)`);
src.addLine(`# MyStack(app, "${this.project.name}-prod", env=prod_env)`);
src.addLine("");
src.addLine("app.synth()");
}
}

class MyStackCode extends Component {
constructor(
project: AwsCdkPythonApp,
sampleFile: string,
cdkMajorVersion: number
) {
constructor(project: AwsCdkPythonApp, dir: string, cdkMajorVersion: number) {
super(project);

let appFile: string[] = [];
Expand All @@ -165,10 +163,10 @@ class MyStackCode extends Component {
appFile.push(" # The code that defines your stack goes here");
appFile.push("");

new SampleDir(project, sampleFile, {
new SampleDir(project, dir, {
files: {
"__init__.py": "",
"my_stack.py": appFile.join("\n"),
"main.py": appFile.join("\n"),
},
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/awscdk/awscdk-app-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ const devEnv = {

const app = new App();

new MyStack(app, 'my-stack-dev', { env: devEnv });
// new MyStack(app, 'my-stack-prod', { env: prodEnv });
new MyStack(app, '${this.project.name}-dev', { env: devEnv });
// new MyStack(app, '${this.project.name}-prod', { env: prodEnv });

app.synth();`;

Expand Down
2 changes: 1 addition & 1 deletion test/awscdk/python-app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test("create cdk python project", () => {
"requirements-dev.txt",
"requirements.txt",
"test_cdk_python_app_project/__init__.py",
"test_cdk_python_app_project/my_stack.py",
"test_cdk_python_app_project/main.py",
"tests/__init__.py",
"tests/test_example.py",
]);
Expand Down