Skip to content

Commit

Permalink
chore: add clean models to preGen (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored Mar 21, 2022
1 parent 6a1b653 commit 135d088
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 10 deletions.
5 changes: 4 additions & 1 deletion config/clients.config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"java": {
"folder": "clients/algoliasearch-client-java-2",
"modelFolder": "algoliasearch-core/com/algolia/model",
"customGenerator": "algolia-java",
"tests": {
"extension": ".test.java",
Expand All @@ -9,12 +10,14 @@
},
"javascript": {
"folder": "clients/algoliasearch-client-javascript",
"modelFolder": "model",
"tests": {
"extension": ".test.ts",
"outputFolder": "src"
}
},
"php": {
"folder": "clients/algoliasearch-client-php"
"folder": "clients/algoliasearch-client-php",
"modelFolder": "lib/Model"
}
}
4 changes: 4 additions & 0 deletions scripts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export function getLanguageFolder(language: string): string {
return clientsConfig[language].folder;
}

export function getLanguageModelFolder(language: string): string {
return clientsConfig[language].modelFolder;
}

export function getTestExtension(language: string): string | undefined {
return clientsConfig[language]?.tests?.extension;
}
Expand Down
61 changes: 53 additions & 8 deletions scripts/generate.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,66 @@
import path from 'path';

import { buildJSClientUtils } from './buildClients';
import { buildSpecs } from './buildSpecs';
import { buildCustomGenerators, CI, run, runIfExists } from './common';
import { getCustomGenerator, getLanguageFolder } from './config';
import {
buildCustomGenerators,
CI,
run,
runIfExists,
toAbsolutePath,
} from './common';
import {
getCustomGenerator,
getLanguageFolder,
getLanguageModelFolder,
} from './config';
import { formatter } from './formatter';
import { createSpinner } from './oraLog';
import { setHostsOptions } from './pre-gen/setHostsOptions';
import type { Generator } from './types';

async function preGen(
{ language, client, key, output }: Generator,
/**
* Remove `model` folder for the current language and client.
*/
async function removeExistingModel(
{ language, client, output }: Generator,
verbose?: boolean
): Promise<void> {
await runIfExists(`./scripts/pre-gen/${language}.sh`, `${output} ${key}`, {
verbose,
});
const baseModelFolder = getLanguageModelFolder(language);

let clientModel = '';
switch (language) {
case 'java':
clientModel = client;
break;
default:
break;
}

await run(
`rm -rf ${toAbsolutePath(
path.resolve('..', output, baseModelFolder, clientModel)
)}`,
{
verbose,
}
);
}

async function preGen(gen: Generator, verbose?: boolean): Promise<void> {
// Run bash pre-gen script
await runIfExists(
`./scripts/pre-gen/${gen.language}.sh`,
`${gen.output} ${gen.key}`,
{
verbose,
}
);

await removeExistingModel(gen);

await setHostsOptions({ client, key });
// Updates `openapitools.json` file based on the spec `servers`
await setHostsOptions({ client: gen.client, key: gen.key });
}

async function generateClient(
Expand Down
5 changes: 4 additions & 1 deletion scripts/pre-gen/setHostsOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ export async function setHostsOptions({
...additionalProperties,
};

await writeFile(openapitoolsPath, JSON.stringify(openapitools, null, 2));
await writeFile(
openapitoolsPath,
JSON.stringify(openapitools, null, 2).concat('\n')
);
} catch (e) {
throw new Error(`Error reading yaml file ${generator}: ${e}`);
}
Expand Down

0 comments on commit 135d088

Please sign in to comment.