Skip to content

Commit 4dbf9ff

Browse files
committed
feat: custom model class
1 parent 337766a commit 4dbf9ff

File tree

5 files changed

+54
-12
lines changed

5 files changed

+54
-12
lines changed

utils/crd-generate/src/cli.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,25 @@ export async function run(): Promise<void> {
3232
describe: "YAML version.",
3333
choices: ["1.0", "1.1", "1.2"]
3434
})
35+
.option("customBaseClassName", {
36+
type: "string",
37+
describe: "Base class import name",
38+
default: "Model"
39+
})
40+
.option("customBaseClassImportPath", {
41+
type: "string",
42+
describe: "Base class import path",
43+
default: "@kubernetes-models/base"
44+
})
3545
.parse();
3646

3747
try {
3848
await generate({
3949
input: await readFiles(args.input),
4050
outputPath: args.output,
41-
yamlVersion: args.yamlVersion as GenerateOptions["yamlVersion"]
51+
yamlVersion: args.yamlVersion as GenerateOptions["yamlVersion"],
52+
customBaseClassName: args.customBaseClassName,
53+
customBaseClassImportPath: args.customBaseClassImportPath
4254
});
4355
} catch (err) {
4456
console.error(err);

utils/crd-generate/src/generate.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ export interface GenerateOptions {
144144
input: string;
145145
outputPath: string;
146146
yamlVersion?: DocumentOptions["version"];
147+
customBaseClassName?: string;
148+
customBaseClassImportPath?: string;
147149
}
148150

149151
export async function generate(options: GenerateOptions): Promise<void> {
@@ -191,7 +193,10 @@ export async function generate(options: GenerateOptions): Promise<void> {
191193
}
192194
}
193195

194-
const files = await generator(dedupeDefinitions(definitions));
196+
const files = await generator(dedupeDefinitions(definitions), {
197+
baseClassName: options.customBaseClassName,
198+
baseClassImportPath: options.customBaseClassImportPath
199+
});
195200

196201
await writeOutputFiles(options.outputPath, files);
197202
}

utils/crd-generate/src/generators/definition.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import {
66
getAPIVersion,
77
GroupVersionKind,
88
Import,
9-
OutputFile
9+
OutputFile,
10+
GenerateOptions as BaseGenerateOptions
1011
} from "@kubernetes-models/generate";
12+
import { GenerateOptions } from "../generate";
1113
import { formatComment, trimSuffix } from "@kubernetes-models/string-util";
1214
import { getRelativePath, getSchemaPath } from "../utils";
1315

@@ -19,7 +21,8 @@ function getFieldType(key: string[]): string | undefined {
1921

2022
function generateDefinition(
2123
gvk: GroupVersionKind,
22-
def: Definition
24+
def: Definition,
25+
options: GenerateOptions
2326
): OutputFile {
2427
const apiVersion = getAPIVersion(gvk);
2528
const className = gvk.kind;
@@ -66,8 +69,9 @@ constructor(data?: ModelData<${interfaceName}>) {
6669
});
6770

6871
imports.push({
69-
name: "Model",
70-
path: "@kubernetes-models/base"
72+
alias: "Model",
73+
name: options.customBaseClassName as string,
74+
path: options.customBaseClassImportPath as string
7175
});
7276

7377
imports.push({
@@ -114,14 +118,22 @@ setValidateFunc(${className}, validate as ValidateFunc<${interfaceName}>);
114118
};
115119
}
116120

117-
const generateDefinitions: Generator = async (definitions) => {
121+
const generateDefinitions: Generator = async (definitions, options = {}) => {
118122
const output: OutputFile[] = [];
119123

124+
// Convert base options to local options format
125+
const localOptions: GenerateOptions = {
126+
input: "",
127+
outputPath: "",
128+
customBaseClassName: options.baseClassName,
129+
customBaseClassImportPath: options.baseClassImportPath
130+
};
131+
120132
for (const def of definitions) {
121133
const gvks = def.gvk;
122134

123135
if (gvks && gvks.length) {
124-
output.push(generateDefinition(gvks[0], def));
136+
output.push(generateDefinition(gvks[0], def, localOptions));
125137
}
126138
}
127139

utils/generate/src/types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,16 @@ export interface OutputFile {
3535
content: string;
3636
}
3737

38+
export interface GenerateOptions {
39+
baseClassName?: string;
40+
baseClassImportPath?: string;
41+
}
42+
3843
export interface Generator {
39-
(definitions: readonly Definition[]): Promise<OutputFile[]>;
44+
(
45+
definitions: readonly Definition[],
46+
options?: GenerateOptions
47+
): Promise<OutputFile[]>;
4048
}
4149

4250
export interface SchemaTransformer {

utils/generate/src/utils.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { Generator, GroupVersionKind, OutputFile } from "./types";
1+
import {
2+
Generator,
3+
GenerateOptions,
4+
GroupVersionKind,
5+
OutputFile
6+
} from "./types";
27
import { outputFile } from "fs-extra";
38
import { join } from "path";
49

@@ -11,11 +16,11 @@ export class PathConflictError extends Error {
1116
PathConflictError.prototype.name = "PathConflictError";
1217

1318
export function composeGenerators(generators: readonly Generator[]): Generator {
14-
return async (definitions) => {
19+
return async (definitions, options: GenerateOptions = {}) => {
1520
const fileMap = new Map<string, OutputFile>();
1621

1722
for (const g of generators) {
18-
const files = await g(definitions);
23+
const files = await g(definitions, options);
1924

2025
for (const f of files) {
2126
if (fileMap.has(f.path)) {

0 commit comments

Comments
 (0)