Skip to content

Commit

Permalink
feat(type-safe-api): add commitGeneratedCode option to control genera…
Browse files Browse the repository at this point in the history
…ted code

This commit introduces a new `commitGeneratedCode` option to the
TypeSafeApiProject, which allows controlling whether generated code should be
committed or ignored for all generated projects.

The main changes include:

- Add a `commitGeneratedCode` option to the TypeSafeApiProject and related
  options interfaces.
- Set the default value of `commitGeneratedCode` to `false`, except for Python
  projects where it defaults to `true` due to the Poetry package manager
  requirements.
- Conditionally add patterns to .gitignore based on the `commitGeneratedCode`
  option for all generated projects.
- Update tests to cover the new `commitGeneratedCode` option.

By default, the generated code will be ignored in the repository, except for
Python projects where it will be included to allow for easier distribution and
deployment of the generated artifacts using Poetry.

Resolves: #813
  • Loading branch information
jstrunk committed Jul 19, 2024
1 parent 7f24295 commit 6107da9
Show file tree
Hide file tree
Showing 18 changed files with 298 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export class GeneratedAsyncApiHtmlDocumentationProject extends Project {
);
this.compileTask.spawn(this.generateTask);

this.gitignore.addPatterns("index.html");
if (!options.commitGeneratedCode) {
this.gitignore.addPatterns("index.html");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export class GeneratedAsyncApiMarkdownDocumentationProject extends Project {
);
this.compileTask.spawn(this.generateTask);

this.gitignore.addPatterns("index.md");
if (!options.commitGeneratedCode) {
this.gitignore.addPatterns("index.md");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export class GeneratedHtmlRedocDocumentationProject extends Project {
);
this.compileTask.spawn(this.generateTask);

this.gitignore.addPatterns("index.html");
if (!options.commitGeneratedCode) {
this.gitignore.addPatterns("index.html");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export class GeneratedHtml2DocumentationProject extends Project {

this.compileTask.spawn(this.generateTask);

this.gitignore.addPatterns(".openapi-generator", "index.html");
if (!options.commitGeneratedCode) {
this.gitignore.addPatterns(".openapi-generator", "index.html");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ export class GeneratedMarkdownDocumentationProject extends Project {

this.compileTask.spawn(this.generateTask);

this.gitignore.addPatterns(
".openapi-generator",
"Apis",
"Models",
"README.md"
);
if (!options.commitGeneratedCode) {
this.gitignore.addPatterns(
".openapi-generator",
"Apis",
"Models",
"README.md"
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export class GeneratedPlantumlDocumentationProject extends Project {

this.compileTask.spawn(this.generateTask);

this.gitignore.addPatterns(".openapi-generator", "schemas.plantuml");
if (!options.commitGeneratedCode) {
this.gitignore.addPatterns(".openapi-generator", "schemas.plantuml");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,10 @@ export abstract class GeneratedJavaHandlersBaseProject extends JavaProject {

this.preCompileTask.spawn(generateTask);

// Ignore the openapi generator metadata files
this.gitignore.addPatterns(".openapi-generator");
if (!options.commitGeneratedCode) {
// Ignore the openapi generator metadata files
this.gitignore.addPatterns(".openapi-generator");
}

// Use the maven shade plugin to build a "super jar" which we can deploy to AWS Lambda
this.pom.addPlugin("org.apache.maven.plugins/[email protected]", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ export abstract class GeneratedPythonHandlersBaseProject extends PythonProject {

this.preCompileTask.spawn(generateTask);

// Ignore the generated code
this.gitignore.addPatterns(".openapi-generator");
if (!options.commitGeneratedCode) {
// Ignore the generated code
this.gitignore.addPatterns(".openapi-generator");
}

// Write __init__.py as sample code
new SampleFile(this, path.join(this.moduleName, "__init__.py"), {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ export abstract class GeneratedTypescriptHandlersBaseProject extends TypeScriptP

this.preCompileTask.spawn(generateTask);

// Ignore the openapi generator metadata
this.gitignore.addPatterns(".openapi-generator");
if (!options.commitGeneratedCode) {
// Ignore the openapi generator metadata
this.gitignore.addPatterns(".openapi-generator");
}

// Create a separate lambda bundle for each handler as part of the package task.
// Note that every typescript file directly in src is bundled by default, but users may specify their own
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,14 @@ export abstract class GeneratedPythonCdkInfrastructureBaseProject extends Python

this.preCompileTask.spawn(generateTask);

// Ignore the generated code
this.gitignore.addPatterns(this.moduleName, ".openapi-generator", "mocks");
if (!options.commitGeneratedCode) {
// Ignore the generated code
this.gitignore.addPatterns(
this.moduleName,
".openapi-generator",
"mocks"
);
}

// The poetry install that runs as part of post synthesis expects there to be some code present, but code isn't
// generated until build time. This means that the first install will fail when either generating the project for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,16 @@ export abstract class GeneratedTypescriptCdkInfrastructureBaseProject extends Ty
generateTask.exec(
`cp -f ${this.options.specPath} ${this.packagedSpecPath}`
);
this.gitignore.addPatterns(`/${this.packagedSpecPath}`);
if (!options.commitGeneratedCode) {
this.gitignore.addPatterns(`/${this.packagedSpecPath}`);
}

this.preCompileTask.spawn(generateTask);

// Ignore the generated code
this.gitignore.addPatterns(this.srcdir, ".openapi-generator", "mocks");
if (!options.commitGeneratedCode) {
// Ignore the generated code
this.gitignore.addPatterns(this.srcdir, ".openapi-generator", "mocks");
}

// If we're not in a monorepo, we need to link the generated types such that the local dependency can be resolved
if (!options.isWithinMonorepo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ export abstract class GeneratedTypescriptLibraryProject extends TypeScriptProjec

this.preCompileTask.spawn(generateTask);

// Ignore all the generated code
this.gitignore.addPatterns(
"src",
".npmignore",
"README.md",
".openapi-generator"
);
if (!options.commitGeneratedCode) {
// Ignore all the generated code
this.gitignore.addPatterns(
"src",
".npmignore",
"README.md",
".openapi-generator"
);
}

// If we're not in a monorepo, we need to link the generated client such that any local dependency on it can be
// resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,16 @@ export abstract class GeneratedJavaRuntimeBaseProject extends JavaProject {

this.preCompileTask.spawn(generateTask);

// Ignore all the generated code
this.gitignore.addPatterns(
"src",
"docs",
"api",
"README.md",
".openapi-generator"
);
if (!options.commitGeneratedCode) {
// Ignore all the generated code
this.gitignore.addPatterns(
"src",
"docs",
"api",
"README.md",
".openapi-generator"
);
}
}

public buildGenerateCommandArgs = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,15 @@ export abstract class GeneratedPythonRuntimeBaseProject extends PythonProject {

this.preCompileTask.spawn(generateTask);

// Ignore all the generated code
this.gitignore.addPatterns(
this.moduleName,
"docs",
"README.md",
".openapi-generator"
);
if (!this.options.commitGeneratedCode) {
// Ignore all the generated code
this.gitignore.addPatterns(
this.moduleName,
"docs",
"README.md",
".openapi-generator"
);
}

// The poetry install that runs as part of post synthesis expects there to be some code present, but code isn't
// generated until build time. This means that the first install will fail when either generating the project for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,15 @@ export abstract class GeneratedTypescriptRuntimeBaseProject extends TypeScriptPr

this.preCompileTask.spawn(generateTask);

// Ignore all the generated code
this.gitignore.addPatterns(
this.srcdir,
".npmignore",
"README.md",
".openapi-generator"
);
if (!options.commitGeneratedCode) {
// Ignore all the generated code
this.gitignore.addPatterns(
this.srcdir,
".npmignore",
"README.md",
".openapi-generator"
);
}

// If we're not in a monorepo, we need to link the generated client such that any local dependency on it can be
// resolved
Expand Down
40 changes: 40 additions & 0 deletions packages/type-safe-api/src/project/type-safe-api-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ export interface TypeSafeApiProjectOptions extends ProjectOptions {
* fully-fledged runtimes, for example react hooks or clients in languages that aren't supported as runtimes.
*/
readonly library?: LibraryConfiguration;
/**
* Whether to commit the code generated by the OpenAPI Generator. Defaults to false unless it is a Python project.
* @default false
*/
readonly commitGeneratedCode?: boolean;
}

/**
Expand Down Expand Up @@ -265,16 +270,26 @@ export class TypeSafeApiProject extends Project {
ProjectUtils.isNamedInstanceOf(this.parent, NodeProject)
? this.parent.package.packageManager
: NodePackageManager.PNPM,
commitGeneratedCode:
options.runtime?.options?.typescript?.commitGeneratedCode ??
options.commitGeneratedCode ??
false,
...options.runtime?.options?.typescript,
},
pythonOptions: {
authorName: "APJ Cope",
authorEmail: "[email protected]",
version: "0.0.0",
commitGeneratedCode:
options.runtime?.options?.python?.commitGeneratedCode ?? true,
...options.runtime?.options?.python,
},
javaOptions: {
version: "0.0.0",
commitGeneratedCode:
options.runtime?.options?.java?.commitGeneratedCode ??
options.commitGeneratedCode ??
false,
...options.runtime?.options?.java,
},
});
Expand Down Expand Up @@ -327,6 +342,11 @@ export class TypeSafeApiProject extends Project {
ProjectUtils.isNamedInstanceOf(this.parent, NodeProject)
? this.parent.package.packageManager
: NodePackageManager.PNPM,
commitGeneratedCode:
options.library?.options?.typescriptReactQueryHooks
?.commitGeneratedCode ??
options.commitGeneratedCode ??
false,
...options.library?.options?.typescriptReactQueryHooks,
},
});
Expand Down Expand Up @@ -396,16 +416,26 @@ export class TypeSafeApiProject extends Project {
ProjectUtils.isNamedInstanceOf(this.parent, NodeProject)
? this.parent.package.packageManager
: NodePackageManager.PNPM,
commitGeneratedCode:
options.handlers?.options?.typescript?.commitGeneratedCode ??
options.commitGeneratedCode ??
false,
...options.handlers?.options?.typescript,
},
pythonOptions: {
authorName: "APJ Cope",
authorEmail: "[email protected]",
version: "0.0.0",
commitGeneratedCode:
options.handlers?.options?.python?.commitGeneratedCode ?? true,
...options.handlers?.options?.python,
},
javaOptions: {
version: "0.0.0",
commitGeneratedCode:
options.handlers?.options?.java?.commitGeneratedCode ??
options.commitGeneratedCode ??
false,
...options.handlers?.options?.java,
},
generatedRuntimes: {
Expand Down Expand Up @@ -470,16 +500,26 @@ export class TypeSafeApiProject extends Project {
ProjectUtils.isNamedInstanceOf(this.parent, NodeProject)
? this.parent.package.packageManager
: NodePackageManager.PNPM,
commitGeneratedCode:
options.infrastructure.options?.typescript?.commitGeneratedCode ??
options.commitGeneratedCode ??
false,
...options.infrastructure.options?.typescript,
},
pythonOptions: {
authorName: "APJ Cope",
authorEmail: "[email protected]",
version: "0.0.0",
commitGeneratedCode:
options.infrastructure.options?.python?.commitGeneratedCode ?? true,
...options.infrastructure.options?.python,
},
javaOptions: {
version: "0.0.0",
commitGeneratedCode:
options.infrastructure.options?.java?.commitGeneratedCode ??
options.commitGeneratedCode ??
false,
...options.infrastructure.options?.java,
},
generatedRuntimes: {
Expand Down
11 changes: 9 additions & 2 deletions packages/type-safe-api/src/project/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ export interface OpenApiGeneratorCliConfig {
* Options for a code project generated with OpenAPI Generator
*/
export interface GeneratedWithOpenApiGeneratorOptions {
/**
* Whether to commit the code generated by the OpenAPI Generator. Defaults to false unless it is a Python project.
* @default false
*/
readonly commitGeneratedCode?: boolean;
/**
* Configuration for the OpenAPI Generator CLI. Overrides default values if specified.
* @see https://github.com/OpenAPITools/openapi-generator-cli#configuration
Expand Down Expand Up @@ -481,12 +486,14 @@ export interface GeneratedPlantumlDocumentationOptions
/**
* Options for the async api html documentation project
*/
export interface GeneratedAsyncApiHtmlDocumentationOptions {}
export interface GeneratedAsyncApiHtmlDocumentationOptions
extends GeneratedWithOpenApiGeneratorOptions {}

/**
* Options for the async api markdown documentation project
*/
export interface GeneratedAsyncApiMarkdownDocumentationOptions {}
export interface GeneratedAsyncApiMarkdownDocumentationOptions
extends GeneratedWithOpenApiGeneratorOptions {}

/**
* Options for generated documentation projects
Expand Down
Loading

0 comments on commit 6107da9

Please sign in to comment.