Skip to content

Commit 135d088

Browse files
authored
chore: add clean models to preGen (#271)
1 parent 6a1b653 commit 135d088

File tree

4 files changed

+65
-10
lines changed

4 files changed

+65
-10
lines changed

config/clients.config.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"java": {
33
"folder": "clients/algoliasearch-client-java-2",
4+
"modelFolder": "algoliasearch-core/com/algolia/model",
45
"customGenerator": "algolia-java",
56
"tests": {
67
"extension": ".test.java",
@@ -9,12 +10,14 @@
910
},
1011
"javascript": {
1112
"folder": "clients/algoliasearch-client-javascript",
13+
"modelFolder": "model",
1214
"tests": {
1315
"extension": ".test.ts",
1416
"outputFolder": "src"
1517
}
1618
},
1719
"php": {
18-
"folder": "clients/algoliasearch-client-php"
20+
"folder": "clients/algoliasearch-client-php",
21+
"modelFolder": "lib/Model"
1922
}
2023
}

scripts/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ export function getLanguageFolder(language: string): string {
44
return clientsConfig[language].folder;
55
}
66

7+
export function getLanguageModelFolder(language: string): string {
8+
return clientsConfig[language].modelFolder;
9+
}
10+
711
export function getTestExtension(language: string): string | undefined {
812
return clientsConfig[language]?.tests?.extension;
913
}

scripts/generate.ts

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,66 @@
1+
import path from 'path';
2+
13
import { buildJSClientUtils } from './buildClients';
24
import { buildSpecs } from './buildSpecs';
3-
import { buildCustomGenerators, CI, run, runIfExists } from './common';
4-
import { getCustomGenerator, getLanguageFolder } from './config';
5+
import {
6+
buildCustomGenerators,
7+
CI,
8+
run,
9+
runIfExists,
10+
toAbsolutePath,
11+
} from './common';
12+
import {
13+
getCustomGenerator,
14+
getLanguageFolder,
15+
getLanguageModelFolder,
16+
} from './config';
517
import { formatter } from './formatter';
618
import { createSpinner } from './oraLog';
719
import { setHostsOptions } from './pre-gen/setHostsOptions';
820
import type { Generator } from './types';
921

10-
async function preGen(
11-
{ language, client, key, output }: Generator,
22+
/**
23+
* Remove `model` folder for the current language and client.
24+
*/
25+
async function removeExistingModel(
26+
{ language, client, output }: Generator,
1227
verbose?: boolean
1328
): Promise<void> {
14-
await runIfExists(`./scripts/pre-gen/${language}.sh`, `${output} ${key}`, {
15-
verbose,
16-
});
29+
const baseModelFolder = getLanguageModelFolder(language);
30+
31+
let clientModel = '';
32+
switch (language) {
33+
case 'java':
34+
clientModel = client;
35+
break;
36+
default:
37+
break;
38+
}
39+
40+
await run(
41+
`rm -rf ${toAbsolutePath(
42+
path.resolve('..', output, baseModelFolder, clientModel)
43+
)}`,
44+
{
45+
verbose,
46+
}
47+
);
48+
}
49+
50+
async function preGen(gen: Generator, verbose?: boolean): Promise<void> {
51+
// Run bash pre-gen script
52+
await runIfExists(
53+
`./scripts/pre-gen/${gen.language}.sh`,
54+
`${gen.output} ${gen.key}`,
55+
{
56+
verbose,
57+
}
58+
);
59+
60+
await removeExistingModel(gen);
1761

18-
await setHostsOptions({ client, key });
62+
// Updates `openapitools.json` file based on the spec `servers`
63+
await setHostsOptions({ client: gen.client, key: gen.key });
1964
}
2065

2166
async function generateClient(

scripts/pre-gen/setHostsOptions.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ export async function setHostsOptions({
122122
...additionalProperties,
123123
};
124124

125-
await writeFile(openapitoolsPath, JSON.stringify(openapitools, null, 2));
125+
await writeFile(
126+
openapitoolsPath,
127+
JSON.stringify(openapitools, null, 2).concat('\n')
128+
);
126129
} catch (e) {
127130
throw new Error(`Error reading yaml file ${generator}: ${e}`);
128131
}

0 commit comments

Comments
 (0)