From 9f7e31f4add7d95a2d537f9ce98ff2eeac21e927 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Mon, 21 Mar 2022 13:15:26 +0100 Subject: [PATCH 1/3] chore: add clean models to preGen --- scripts/generate.ts | 27 ++++++++++++++++++++++++++- scripts/pre-gen/setHostsOptions.ts | 5 ++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/scripts/generate.ts b/scripts/generate.ts index 8473c9719a..35abda00b7 100644 --- a/scripts/generate.ts +++ b/scripts/generate.ts @@ -1,6 +1,12 @@ import { buildJSClientUtils } from './buildClients'; import { buildSpecs } from './buildSpecs'; -import { buildCustomGenerators, CI, run, runIfExists } from './common'; +import { + buildCustomGenerators, + CI, + run, + runIfExists, + toAbsolutePath, +} from './common'; import { getCustomGenerator, getLanguageFolder } from './config'; import { formatter } from './formatter'; import { createSpinner } from './oraLog'; @@ -15,6 +21,25 @@ async function preGen( verbose, }); + // We clean models to avoid outdated files. + let modelPath = ''; + switch (language) { + case 'javascript': + modelPath = 'model'; + break; + case 'java': + modelPath = `algoliasearch-core/com/algolia/model/${client}`; + break; + default: + return; + } + + if (modelPath) { + await run(`rm -rf ${toAbsolutePath(`${output}/${modelPath}`)}`, { + verbose, + }); + } + await setHostsOptions({ client, key }); } diff --git a/scripts/pre-gen/setHostsOptions.ts b/scripts/pre-gen/setHostsOptions.ts index eeae43775d..9182722459 100644 --- a/scripts/pre-gen/setHostsOptions.ts +++ b/scripts/pre-gen/setHostsOptions.ts @@ -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}`); } From abd3048ee5545a0cfba73f803cf83d98e1afffe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Mon, 21 Mar 2022 16:12:06 +0100 Subject: [PATCH 2/3] add `modelFolder` to config, extract logic --- config/clients.config.json | 5 +++- scripts/config.ts | 4 +++ scripts/generate.ts | 56 ++++++++++++++++++++++++++------------ 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/config/clients.config.json b/config/clients.config.json index 6fcbd37f77..d1a36c2209 100644 --- a/config/clients.config.json +++ b/config/clients.config.json @@ -1,6 +1,7 @@ { "java": { "folder": "clients/algoliasearch-client-java-2", + "modelFolder": "algoliasearch-core/com/algolia/model", "customGenerator": "algolia-java", "tests": { "extension": ".test.java", @@ -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" } } diff --git a/scripts/config.ts b/scripts/config.ts index 76fcee8c61..30638510d8 100644 --- a/scripts/config.ts +++ b/scripts/config.ts @@ -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; } diff --git a/scripts/generate.ts b/scripts/generate.ts index 35abda00b7..acff8437da 100644 --- a/scripts/generate.ts +++ b/scripts/generate.ts @@ -1,3 +1,5 @@ +import path from 'path'; + import { buildJSClientUtils } from './buildClients'; import { buildSpecs } from './buildSpecs'; import { @@ -7,40 +9,58 @@ import { runIfExists, toAbsolutePath, } from './common'; -import { getCustomGenerator, getLanguageFolder } from './config'; +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 { - await runIfExists(`./scripts/pre-gen/${language}.sh`, `${output} ${key}`, { - verbose, - }); + const baseModelFolder = getLanguageModelFolder(language); - // We clean models to avoid outdated files. - let modelPath = ''; + let clientModel = ''; switch (language) { - case 'javascript': - modelPath = 'model'; - break; case 'java': - modelPath = `algoliasearch-core/com/algolia/model/${client}`; + clientModel = `/${client}`; break; default: - return; + break; } - if (modelPath) { - await run(`rm -rf ${toAbsolutePath(`${output}/${modelPath}`)}`, { + await run( + `rm -rf ${toAbsolutePath( + path.resolve(output, `/${baseModelFolder}`, clientModel) + )}`, + { verbose, - }); - } + } + ); +} + +async function preGen(gen: Generator, verbose?: boolean): Promise { + // 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( From 059915e54af2569412c1fa387fc8fad5e227235a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Mon, 21 Mar 2022 16:28:39 +0100 Subject: [PATCH 3/3] fix path --- scripts/generate.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/generate.ts b/scripts/generate.ts index acff8437da..753f3553a1 100644 --- a/scripts/generate.ts +++ b/scripts/generate.ts @@ -31,7 +31,7 @@ async function removeExistingModel( let clientModel = ''; switch (language) { case 'java': - clientModel = `/${client}`; + clientModel = client; break; default: break; @@ -39,7 +39,7 @@ async function removeExistingModel( await run( `rm -rf ${toAbsolutePath( - path.resolve(output, `/${baseModelFolder}`, clientModel) + path.resolve('..', output, baseModelFolder, clientModel) )}`, { verbose,