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`.
- 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.

Fixes: #813
  • Loading branch information
jstrunk committed Aug 1, 2024
1 parent 883e782 commit 5a93d6a
Show file tree
Hide file tree
Showing 15 changed files with 308 additions and 47 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,10 @@ 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");
} else {
this.gitignore.addPatterns(".openapi-generator");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ 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"
);
} else {
this.gitignore.addPatterns(".openapi-generator");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ 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");
} else {
this.gitignore.addPatterns(".openapi-generator");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,16 @@ 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"
);
} else {
this.gitignore.addPatterns(".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 @@ -159,12 +159,18 @@ 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");
} else {
this.gitignore.addPatterns(".openapi-generator");
}

// 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,18 @@ 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"
);
} else {
this.gitignore.addPatterns(".openapi-generator");
}
}

public buildGenerateCommandArgs = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,17 @@ 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"
);
} else {
this.gitignore.addPatterns(".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,17 @@ 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"
);
} else {
this.gitignore.addPatterns(".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
46 changes: 46 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.
* @default false
*/
readonly commitGeneratedCode?: boolean;
}

/**
Expand Down Expand Up @@ -265,16 +270,28 @@ 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: "apj-cope@amazon.com",
version: "0.0.0",
commitGeneratedCode:
options.runtime?.options?.python?.commitGeneratedCode ??
options.commitGeneratedCode ??
false,
...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 +344,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 +418,28 @@ 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: "apj-cope@amazon.com",
version: "0.0.0",
commitGeneratedCode:
options.handlers?.options?.python?.commitGeneratedCode ??
options.commitGeneratedCode ??
false,
...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 +504,28 @@ 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: "apj-cope@amazon.com",
version: "0.0.0",
commitGeneratedCode:
options.infrastructure.options?.python?.commitGeneratedCode ??
options.commitGeneratedCode ??
false,
...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.
* @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 5a93d6a

Please sign in to comment.