Skip to content

Commit

Permalink
feat(awscdk-app): initialize sample code stack names based on project…
Browse files Browse the repository at this point in the history
… name (projen#1711)

closes projen#1708 

---
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
  • Loading branch information
marciocadev authored Mar 24, 2022
1 parent 0c4e2ed commit 3605687
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 16 deletions.
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
20 changes: 9 additions & 11 deletions src/awscdk/awscdk-app-py.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,17 @@ 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(");
Expand All @@ -130,19 +132,15 @@ class AppCode extends Component {
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

0 comments on commit 3605687

Please sign in to comment.