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 1 commit
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
Prev Previous commit
Next Next commit
fix: add readonly option to SourceCodeOptions
  • Loading branch information
marciocadev committed Mar 24, 2022
commit f02a52006c14dd4916452cfeac0491bb8b3751f6
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
36 changes: 18 additions & 18 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, TextFile } from "..";
import { Component, DependencyType, SampleDir, SourceCode } from "..";
import { Pytest } from "../python/pytest";
import { PythonProject, PythonProjectOptions } from "../python/python-project";
import { AwsCdkDepsPy } from "./awscdk-deps-py";
Expand Down Expand Up @@ -113,29 +113,29 @@ class AppCode extends Component {
constructor(project: AwsCdkPythonApp, fileName: string, cdkVersion: number) {
super(project);

const src = new TextFile(project, fileName, {
const src = new SourceCode(project, fileName, {
readonly: false,
});

src.addLine("import os");
src.line("import os");
if (cdkVersion < 2) {
src.addLine("from aws_cdk.core import App, Environment");
src.line("from aws_cdk.core import App, Environment");
} else {
src.addLine("from aws_cdk import App, Environment");
src.line("from aws_cdk import App, Environment");
}
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()");
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.close(")");
src.line("");
src.line("app = App()");
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()");
}
}

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