Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add clean models to preGen #271

Merged
merged 6 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`;
shortcuts marked this conversation as resolved.
Show resolved Hide resolved
break;
default:
break;
}

await run(
`rm -rf ${toAbsolutePath(
path.resolve(output, `/${baseModelFolder}`, clientModel)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
path.resolve(output, `/${baseModelFolder}`, clientModel)
path.resolve(output, `${baseModelFolder}`, clientModel)

You have to remove the leading \ otherwise it's interpreted as root path, and ignores the param before it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL!!! Good to know (and weird behavior), but at least it's much cleaner

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolve is the glue of pieces for path, like / for mac and linux, and \ for windows. So you don't need any :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks both!!

)}`,
{
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')
shortcuts marked this conversation as resolved.
Show resolved Hide resolved
);
} catch (e) {
throw new Error(`Error reading yaml file ${generator}: ${e}`);
}
Expand Down