Skip to content

Commit

Permalink
feat: generate openapitools.json via task (#732)
Browse files Browse the repository at this point in the history
Create a task that produces the openapitools.json file. This allows
consumers to add their own steps to edit the file with compile-time
values prior to being consumed. This is important for cases such as
inserting a signed repository download URL.
  • Loading branch information
zsstiers authored Mar 22, 2024
1 parent ab8e648 commit 1f530c1
Show file tree
Hide file tree
Showing 32 changed files with 4,977 additions and 2,111 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/*! Copyright [Amazon.com](http://amazon.com/), Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0 */
import { ok as assert } from "node:assert";
import { posix as path } from "path";
import { ProjectUtils } from "@aws/monorepo";
import { JsonFile, Project } from "projen";
import { JsonFile, Project, Task } from "projen";
import { OpenApiGeneratorCliConfig } from "../../types";

/**
Expand Down Expand Up @@ -41,14 +43,18 @@ export class OpenApiToolsJsonFile extends JsonFile {
storageDir: "~/.open-api-generator-cli",
};

public readonly createTask: Task;

constructor(project: Project) {
if (OpenApiToolsJsonFile.of(project)) {
throw new Error(
`Project ${project.name} already has associated OpenApiToolsJsonFile component.`
);
}

super(project, "openapitools.json", {
const localFilePath = "openapitools.json";
const dynamicFilePath = path.join(".pdk/dynamic-files", localFilePath);
super(project, dynamicFilePath, {
obj: {
// Schema is located in node_modules when generator cli is installed in tmp dir
$schema:
Expand All @@ -57,6 +63,21 @@ export class OpenApiToolsJsonFile extends JsonFile {
"generator-cli": () => this.config,
},
});

// Ignore the location that gets copied to
project.addGitIgnore(`/${localFilePath}`);

// Create the task, but don't attach it anywhere yet. This is because of wanting
// to do the generation as part of the "generate" tasks, which may not yet exist.
this.createTask = project.addTask(`create-openapitools.json`, {
steps: [{ exec: `cp -f ${dynamicFilePath} ${localFilePath}` }],
});
}

preSynthesize(): void {
const generateTask = this.project.tasks.tryFind("generate");
assert(generateTask);
generateTask.prependSpawn(this.createTask);
}

/**
Expand Down
Loading

0 comments on commit 1f530c1

Please sign in to comment.