From 9b07997ea623e9dcf809a817d2e2b1dbfd44b6ad Mon Sep 17 00:00:00 2001 From: xyny Date: Sun, 30 Jun 2024 20:28:05 +0300 Subject: [PATCH] feat: support module default config options and custom modules --- fetchModuleSchemas.js | 21 +++++++++++++++++++-- src-tsp/main.tsp | 44 ++++++++++++++++++++++++++++++++----------- 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/fetchModuleSchemas.js b/fetchModuleSchemas.js index 7094504..c3372e6 100644 --- a/fetchModuleSchemas.js +++ b/fetchModuleSchemas.js @@ -18,7 +18,24 @@ for (const module of modules) { const moduleYml = parse(await res.text()) if (moduleYml.typespec) { const res = await fetch(moduleYml.typespec) - fs.writeFileSync(`${modulesDir}/${moduleYml.name}.tsp`, await res.text()) + let text = await res.text() + + text = text // add `...ModuleDefaults;` after the model type + .split("\n") + .flatMap(line => { + if (line.trimStart().startsWith(`type: "${moduleYml.name}"`)) { + return [ + line, + '', + ' ...ModuleDefaults; // added by fetchModuleSchemas.js', + ] + } else { + return line; + } + }) + .join('\n') + + fs.writeFileSync(`${modulesDir}/${moduleYml.name}.tsp`, text) moduleImports.push(`${moduleYml.name}.tsp`) moduleModels.push(`${pascalCase(moduleYml.name)}`) @@ -27,5 +44,5 @@ for (const module of modules) { fs.writeFileSync(`${modulesDir}/index.tsp`, moduleImports.map(m => `import "./${m}";`).join("\n") + ` -alias Module = ${moduleModels.map(m => `${m}Module`).join(" | ")};` +alias RepoModule = ${moduleModels.map(m => `${m}Module`).join(" | ")};` ) \ No newline at end of file diff --git a/src-tsp/main.tsp b/src-tsp/main.tsp index ab62874..b0a90e0 100644 --- a/src-tsp/main.tsp +++ b/src-tsp/main.tsp @@ -8,18 +8,18 @@ model Recipe { /** * The image name. Used when publishing to GHCR as `ghcr.io/user/name`. */ - name: string, + name: string; /** * The image description. Published to GHCR in the image metadata. */ - description: string, + description: string; /** * Allows setting custom tags on the recipe’s final image. * Adding tags to this property will override the `latest` and timestamp tags. */ - "alt-tags"?: Array, + "alt-tags"?: Array; /** * The [OCI](https://opencontainers.org/) image to base your custom image on. @@ -27,14 +27,14 @@ model Recipe { * Universal Blue is recommended. [A list of Universal Blue's images](https://universal-blue.org/images/) can be found on their website * BlueBuild-built images can be used as well. */ - "base-image": string, + "base-image": string; /** * The tag of the base image to build on. * Used to select a version explicitly (`40`) or to always use the latest stable version (`latest`). * A list of all available tags can be viewed by pasting your `base-image` url into your browser. */ - "image-version": string, + "image-version": string; /** * A list of [stages](https://blue-build.org/reference/stages/) that are executed before the build of the final image. @@ -45,24 +45,46 @@ model Recipe { * The name of the stage. This is used when referencing * the stage when using the from: property in the [`copy` module](https://blue-build.org/reference/modules/copy/). */ - name: string, + name: string; /** The full image ref (image name + tag). This will be set in the FROM statement of the stage. */ - from: string, + from: string; /** Allows a user to pass in an array of strings that are passed directly into the [`SHELL` instruction](https://docs.docker.com/reference/dockerfile/#shell). */ - shell?: string, + shell?: string; /** * The list of modules to execute. The exact same syntax used by the main recipe `modules:` property. */ - modules: Array, - }>, + modules: Array; + }>; /** * A list of [modules](https://blue-build.org/reference/module/) that is executed in order. Multiple of the same module can be included. * * Each item in this list should have at least a `type:` or be specified to be included from an external file in the `recipes/` directory with `from-file:`. */ - modules: Array + modules: Array; +} + +alias Module = RepoModule | CustomModule; + +model ModuleDefaults { + /** Whether to disabling caching for this layer. + * https://blue-build.org/reference/module/#no-cache-optional + */ + "no-cache"?: boolean = false; +} + +model CustomModule { + /** This is not a built-in module. */ + type: string; + + /** The URL of the module repository (an OCI image) to pull the module from. + * https://blue-build.org/reference/module/#source-optional + */ + source?: string; + + ...ModuleDefaults; + ...Record; } \ No newline at end of file