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 4 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: 2 additions & 0 deletions docs/api/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -2257,6 +2257,7 @@ new SourceCode(project: Project, filePath: string, options?: SourceCodeOptions)
* **filePath** (<code>string</code>) *No description*
* **options** (<code>[SourceCodeOptions](#projen-sourcecodeoptions)</code>) *No description*
* **indent** (<code>number</code>) Indentation size. __*Default*__: 2
* **readonly** (<code>boolean</code>) Whether the generated file should be readonly. __*Default*__: true



Expand Down Expand Up @@ -11327,6 +11328,7 @@ Options for `SourceCodeFile`.
Name | Type | Description
-----|------|-------------
**indent**?🔹 | <code>number</code> | Indentation size.<br/>__*Default*__: 2
**readonly**?🔹 | <code>boolean</code> | Whether the generated file should be readonly.<br/>__*Default*__: true



Expand Down
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
24 changes: 11 additions & 13 deletions src/awscdk/awscdk-app-py.ts
Original file line number Diff line number Diff line change
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 SourceCode(project, fileName, {
readonly: false,
});

src.line("import os");
if (cdkVersion < 2) {
src.line("from aws_cdk.core import App, Environment");
} else {
src.line("from aws_cdk import App, Environment");
}
src.line(`from ${project.moduleName}.my_stack import MyStack`);
src.line(`from ${project.moduleName}.main 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.line(" account=os.getenv('CDK_DEFAULT_ACCOUNT'),");
src.line(" region=os.getenv('CDK_DEFAULT_REGION'),");
marciocadev marked this conversation as resolved.
Show resolved Hide resolved
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(`MyStack(app, "${this.project.name}-dev", env=dev_env)`);
src.line(`# MyStack(app, "${this.project.name}-prod", env=prod_env)`);
src.line("");
src.line("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
11 changes: 10 additions & 1 deletion src/source-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export interface SourceCodeOptions {
* @default 2
*/
readonly indent?: number;

/**
* Whether the generated file should be readonly.
*
* @default true
*/
readonly readonly?: boolean;
}

/**
Expand All @@ -28,7 +35,9 @@ export class SourceCode extends Component {
) {
super(project);
this.indent = options.indent ?? 2;
this.file = new TextFile(project, filePath);
this.file = new TextFile(project, filePath, {
readonly: options.readonly ?? true,
});
}

public get marker(): string | undefined {
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