Skip to content

feat(generators): factor utilsPackageVersion for JavaScript #511

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

Merged
merged 3 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions config/clients.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"javascript": {
"folder": "clients/algoliasearch-client-javascript",
"gitRepoId": "algoliasearch-client-javascript",
"utilsPackageVersion": "0.2.0",
"modelFolder": "model",
"apiFolder": "src",
"customGenerator": "algolia-javascript",
Expand Down
16 changes: 3 additions & 13 deletions config/openapitools.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
"npmName": "@experimental-api-clients-automation/client-search",
"buildFile": "client-search",
"packageVersion": "0.2.0",
"packageName": "@experimental-api-clients-automation/client-search",
"utilsPackageVersion": "0.2.0"
"packageName": "@experimental-api-clients-automation/client-search"
}
},
"javascript-algoliasearch-lite": {
Expand All @@ -20,8 +19,7 @@
"npmName": "@experimental-api-clients-automation/algoliasearch-lite",
"buildFile": "algoliasearch-lite",
"packageVersion": "0.2.0",
"packageName": "@experimental-api-clients-automation/algoliasearch-lite",
"utilsPackageVersion": "0.2.0"
"packageName": "@experimental-api-clients-automation/algoliasearch-lite"
}
},
"javascript-recommend": {
Expand All @@ -31,8 +29,7 @@
"npmName": "@experimental-api-clients-automation/recommend",
"buildFile": "recommend",
"packageVersion": "0.2.0",
"packageName": "@experimental-api-clients-automation/recommend",
"utilsPackageVersion": "0.2.0"
"packageName": "@experimental-api-clients-automation/recommend"
}
},
"javascript-personalization": {
Expand All @@ -42,7 +39,6 @@
"buildFile": "client-personalization",
"packageVersion": "0.2.0",
"packageName": "@experimental-api-clients-automation/client-personalization",
"utilsPackageVersion": "0.2.0",
"hasRegionalHost": true
}
},
Expand All @@ -53,7 +49,6 @@
"buildFile": "client-analytics",
"packageVersion": "0.2.0",
"packageName": "@experimental-api-clients-automation/client-analytics",
"utilsPackageVersion": "0.2.0",
"hasRegionalHost": true
}
},
Expand All @@ -64,7 +59,6 @@
"buildFile": "client-insights",
"packageVersion": "0.2.0",
"packageName": "@experimental-api-clients-automation/client-insights",
"utilsPackageVersion": "0.2.0",
"hasRegionalHost": true
}
},
Expand All @@ -75,7 +69,6 @@
"buildFile": "client-abtesting",
"packageVersion": "0.2.0",
"packageName": "@experimental-api-clients-automation/client-abtesting",
"utilsPackageVersion": "0.2.0",
"hasRegionalHost": true
}
},
Expand All @@ -86,7 +79,6 @@
"buildFile": "client-query-suggestions",
"packageVersion": "0.2.0",
"packageName": "@experimental-api-clients-automation/client-query-suggestions",
"utilsPackageVersion": "0.2.0",
"hasRegionalHost": true
}
},
Expand All @@ -97,7 +89,6 @@
"buildFile": "client-sources",
"packageVersion": "0.2.0",
"packageName": "@experimental-api-clients-automation/client-sources",
"utilsPackageVersion": "0.2.0",
"hasRegionalHost": true
}
},
Expand All @@ -109,7 +100,6 @@
"buildFile": "client-predict",
"packageVersion": "0.2.0",
"packageName": "@experimental-api-clients-automation/client-predict",
"utilsPackageVersion": "0.2.0",
"hasRegionalHost": true
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Map<String, Object> postProcessOperationsWithModels(
);
additionalProperties.put(
"packageVersion",
Utils.getPackageVersion("java")
Utils.getClientConfigField("java", "packageVersion")
);
} catch (GenerationException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.languages.TypeScriptNodeClientCodegen;

public class AlgoliaJavascriptGenerator extends TypeScriptNodeClientCodegen {
public class AlgoliaJavaScriptGenerator extends TypeScriptNodeClientCodegen {

private String CLIENT;

Expand Down Expand Up @@ -82,6 +82,10 @@ public Map<String, Object> postProcessOperationsWithModels(
Utils.getClientNameKebabCase(results),
additionalProperties
);
additionalProperties.put(
"utilsPackageVersion",
Utils.getClientConfigField("javascript", "utilsPackageVersion")
);
} catch (GenerationException e) {
e.printStackTrace();
System.exit(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Map<String, Object> postProcessOperationsWithModels(
Utils.generateServer(client, additionalProperties);
additionalProperties.put(
"packageVersion",
Utils.getPackageVersion("php")
Utils.getClientConfigField("php", "packageVersion")
);
} catch (GenerationException e) {
e.printStackTrace();
Expand Down
13 changes: 7 additions & 6 deletions generators/src/main/java/com/algolia/codegen/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,21 @@ public static void generateServer(
}
}

// Get the package version from clients.config.json (doesn't work for JavaScript)
public static String getPackageVersion(String language)
/** Get the `field` value in the `config/clients.config.json` file for the given language */
public static String getClientConfigField(String language, String field)
throws GenerationException {
if (language.equals("javascript")) {
if (language.equals("javascript") && field.equals("packageVersion")) {
throw new GenerationException(
"Cannot use getPackageVersion with language=\"javascript\", " +
"read openapitools.json instead"
"Cannot read 'packageVersion' with language=\"javascript\", " +
"read configs/openapitools.json instead"
);
}

try {
JsonNode config = Json
.mapper()
.readTree(new File("config/clients.config.json"));
return config.get(language).get("packageVersion").asText();
return config.get(language).get(field).asText();
} catch (IOException e) {
throw new GenerationException(
"Couldn't read packageVersion from clients.config.json",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.algolia.codegen.cts.manager;

import com.algolia.codegen.GenerationException;
import com.algolia.codegen.Utils;
import java.util.*;
import org.openapitools.codegen.SupportingFile;
Expand All @@ -13,13 +14,14 @@ public void addSupportingFiles(List<SupportingFile> supportingFiles) {
}

protected void addExtraToBundle(Map<String, Object> bundle) {
bundle.put(
"packageVersion",
Utils
.readJsonFile("config/clients.config.json")
.get("java")
.get("packageVersion")
.asText()
);
try {
bundle.put(
"packageVersion",
Utils.getClientConfigField("java", "packageVersion")
);
} catch (GenerationException e) {
e.printStackTrace();
System.exit(1);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.algolia.codegen.cts.manager;

import com.algolia.codegen.GenerationException;
import com.algolia.codegen.Utils;
import com.fasterxml.jackson.databind.JsonNode;
import java.util.*;
Expand Down Expand Up @@ -46,22 +47,15 @@ private List<Object> getPackageDependencies() {

protected void addExtraToBundle(Map<String, Object> bundle) {
bundle.put("packageDependencies", this.getPackageDependencies());
bundle.put("utilsPackageVersion", this.getUtilsPackageVersion());
}

private String getUtilsPackageVersion() {
JsonNode openApiToolsConfig = Utils.readJsonFile(
"config/openapitools.json"
);

String utilsPackageVersion = openApiToolsConfig
.get("generator-cli")
.get("generators")
.get("javascript-search")
.get("additionalProperties")
.get("utilsPackageVersion")
.asText();

return utilsPackageVersion;
try {
bundle.put(
"utilsPackageVersion",
Utils.getClientConfigField("javascript", "utilsPackageVersion")
);
} catch (GenerationException e) {
e.printStackTrace();
System.exit(1);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
com.algolia.codegen.AlgoliaJavaGenerator
com.algolia.codegen.AlgoliaJavascriptGenerator
com.algolia.codegen.AlgoliaJavaScriptGenerator
com.algolia.codegen.AlgoliaPhpGenerator
com.algolia.codegen.cts.AlgoliaCtsGenerator
2 changes: 1 addition & 1 deletion scripts/ci/githubActions/setRunVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const DEPENDENCIES = {
...CLIENTS_COMMON_FILES,
JS_CLIENT_FOLDER,
'templates/javascript',
'generators/src/main/java/com/algolia/codegen/AlgoliaJavascriptGenerator.java',
'generators/src/main/java/com/algolia/codegen/AlgoliaJavaScriptGenerator.java',
`:!${JS_CLIENT_FOLDER}/.github`,
`:!${JS_CLIENT_FOLDER}/README.md`,
'tests/CTS/methods/requests/templates/javascript',
Expand Down
17 changes: 16 additions & 1 deletion scripts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export function getCustomGenerator(language: Language): string {
return clientsConfig[language].customGenerator;
}

// Returns the version of the package from clients.config.json, except for JavaScript where it returns the version of javascript-search
/**
* Returns the version of the package from clients.config.json, except for JavaScript where it returns the version of javascript-search.
*/
export function getPackageVersionDefault(language: Language): string {
if (language === 'javascript') {
return openapiConfig['generator-cli'].generators['javascript-search']
Expand All @@ -36,6 +38,19 @@ export function getPackageVersionDefault(language: Language): string {
return clientsConfig[language].packageVersion;
}

/**
* Returns the value of the `utilsPackageVersion`.
*
* Currently supported languages other than `javascript` does not have a utils package.
*/
export function getUtilsPackageVersionDefault(language: Language): string {
if (language !== 'javascript') {
return '';
}

return clientsConfig[language].utilsPackageVersion;
}

export function getGitHubUrl(
language: Language,
options?: { token: string }
Expand Down
24 changes: 17 additions & 7 deletions scripts/release/process-release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
getGitHubUrl,
getLanguageFolder,
getPackageVersionDefault,
getUtilsPackageVersionDefault,
} from '../config';
import type { Language } from '../types';

Expand Down Expand Up @@ -113,13 +114,9 @@ async function updateVersionForJavascript(
if (!versionsToRelease.javascript) {
return;
}

// Sets the new version of the JavaScript client
const jsVersion = versionsToRelease.javascript;
const nextUtilsPackageVersion =
semver.inc(
openapiConfig['generator-cli'].generators['javascript-search']
.additionalProperties.utilsPackageVersion,
jsVersion.releaseType
) || '';
Object.values(GENERATORS)
.filter((gen) => gen.language === 'javascript')
.forEach((gen) => {
Expand All @@ -136,12 +133,25 @@ async function updateVersionForJavascript(
);
}
additionalProperties.packageVersion = newVersion;
additionalProperties.utilsPackageVersion = nextUtilsPackageVersion;
});

await fsp.writeFile(
toAbsolutePath('config/openapitools.json'),
JSON.stringify(openapiConfig, null, 2)
);

// Sets the new version of the utils package
const nextUtilsPackageVersion =
semver.inc(
getUtilsPackageVersionDefault('javascript'),
jsVersion.releaseType
) || '';

clientsConfig.javascript.utilsPackageVersion = nextUtilsPackageVersion;
await fsp.writeFile(
toAbsolutePath('config/clients.config.json'),
JSON.stringify(clientsConfig, null, 2)
);
}

async function updateConfigFiles(
Expand Down
13 changes: 6 additions & 7 deletions website/docs/automation/add-new-api-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ Generators are referenced by key with the following pattern `<languageName>-<cli

Below are the options you need to **make sure to define for your client**, other options are automatically added in the [`setDefaultOptions` script](https://github.com/algolia/api-clients-automation/blob/main/scripts/pre-gen/setDefaultGeneratorOptions.ts) and the generators.

| Option | Type | Language | Example | Definition |
| ------------------- | :----: | :--------: | :-----------------------------: | :------------------------------------------------------------------------------------------------------------------- |
| output | string | Common | `path/to/client/client-sources` | The output path of the client. |
| gitRepoId | string | Common | `algoliasearch-client-java-2` | The name of the repository under the Algolia org. |
| packageName | string | common | `AlgoliaSearch` | Name of the API package, used in [CTS](/docs/automation/testing/common-test-suite). |
| packageVersion | string | JavaScript | `1.2.3` | The version you'd like to publish the first iteration of the generated client. It will be automatically incremented. |
| utilsPackageVersion | string | JavaScript | `0.1.2` | The version you'd like to publish the first iteration of the utils package. It will be automatically incremented. |
| Option | Type | Language | Example | Definition |
| -------------- | :----: | :--------: | :-----------------------------: | :------------------------------------------------------------------------------------------------------------------- |
| output | string | Common | `path/to/client/client-sources` | The output path of the client. |
| gitRepoId | string | Common | `algoliasearch-client-java-2` | The name of the repository under the Algolia org. |
| packageName | string | common | `AlgoliaSearch` | Name of the API package, used in [CTS](/docs/automation/testing/common-test-suite). |
| packageVersion | string | JavaScript | `1.2.3` | The version you'd like to publish the first iteration of the generated client. It will be automatically incremented. |

### GitHub actions

Expand Down