Skip to content

Commit baa4ce3

Browse files
committed
feat(generators): factor utilsPackageVersion for JavaScript
1 parent d0b9625 commit baa4ce3

File tree

13 files changed

+79
-63
lines changed

13 files changed

+79
-63
lines changed

config/clients.config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"javascript": {
1515
"folder": "clients/algoliasearch-client-javascript",
1616
"gitRepoId": "algoliasearch-client-javascript",
17+
"utilsPackageVersion": "0.2.0",
1718
"modelFolder": "model",
1819
"apiFolder": "src",
1920
"customGenerator": "algolia-javascript",

config/openapitools.json

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
"npmName": "@experimental-api-clients-automation/client-search",
1010
"buildFile": "client-search",
1111
"packageVersion": "0.2.0",
12-
"packageName": "@experimental-api-clients-automation/client-search",
13-
"utilsPackageVersion": "0.2.0"
12+
"packageName": "@experimental-api-clients-automation/client-search"
1413
}
1514
},
1615
"javascript-algoliasearch-lite": {
@@ -20,8 +19,7 @@
2019
"npmName": "@experimental-api-clients-automation/algoliasearch-lite",
2120
"buildFile": "algoliasearch-lite",
2221
"packageVersion": "0.2.0",
23-
"packageName": "@experimental-api-clients-automation/algoliasearch-lite",
24-
"utilsPackageVersion": "0.2.0"
22+
"packageName": "@experimental-api-clients-automation/algoliasearch-lite"
2523
}
2624
},
2725
"javascript-recommend": {
@@ -31,8 +29,7 @@
3129
"npmName": "@experimental-api-clients-automation/recommend",
3230
"buildFile": "recommend",
3331
"packageVersion": "0.2.0",
34-
"packageName": "@experimental-api-clients-automation/recommend",
35-
"utilsPackageVersion": "0.2.0"
32+
"packageName": "@experimental-api-clients-automation/recommend"
3633
}
3734
},
3835
"javascript-personalization": {
@@ -42,7 +39,6 @@
4239
"buildFile": "client-personalization",
4340
"packageVersion": "0.2.0",
4441
"packageName": "@experimental-api-clients-automation/client-personalization",
45-
"utilsPackageVersion": "0.2.0",
4642
"hasRegionalHost": true
4743
}
4844
},
@@ -53,7 +49,6 @@
5349
"buildFile": "client-analytics",
5450
"packageVersion": "0.2.0",
5551
"packageName": "@experimental-api-clients-automation/client-analytics",
56-
"utilsPackageVersion": "0.2.0",
5752
"hasRegionalHost": true
5853
}
5954
},
@@ -64,7 +59,6 @@
6459
"buildFile": "client-insights",
6560
"packageVersion": "0.2.0",
6661
"packageName": "@experimental-api-clients-automation/client-insights",
67-
"utilsPackageVersion": "0.2.0",
6862
"hasRegionalHost": true
6963
}
7064
},
@@ -75,7 +69,6 @@
7569
"buildFile": "client-abtesting",
7670
"packageVersion": "0.2.0",
7771
"packageName": "@experimental-api-clients-automation/client-abtesting",
78-
"utilsPackageVersion": "0.2.0",
7972
"hasRegionalHost": true
8073
}
8174
},
@@ -86,7 +79,6 @@
8679
"buildFile": "client-query-suggestions",
8780
"packageVersion": "0.2.0",
8881
"packageName": "@experimental-api-clients-automation/client-query-suggestions",
89-
"utilsPackageVersion": "0.2.0",
9082
"hasRegionalHost": true
9183
}
9284
},
@@ -97,7 +89,6 @@
9789
"buildFile": "client-sources",
9890
"packageVersion": "0.2.0",
9991
"packageName": "@experimental-api-clients-automation/client-sources",
100-
"utilsPackageVersion": "0.2.0",
10192
"hasRegionalHost": true
10293
}
10394
},
@@ -109,7 +100,6 @@
109100
"buildFile": "client-predict",
110101
"packageVersion": "0.2.0",
111102
"packageName": "@experimental-api-clients-automation/client-predict",
112-
"utilsPackageVersion": "0.2.0",
113103
"hasRegionalHost": true
114104
}
115105
},

generators/src/main/java/com/algolia/codegen/AlgoliaJavaGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public Map<String, Object> postProcessOperationsWithModels(
5252
);
5353
additionalProperties.put(
5454
"packageVersion",
55-
Utils.getPackageVersion("java")
55+
Utils.getClientConfigField("java", "packageVersion")
5656
);
5757
} catch (GenerationException e) {
5858
e.printStackTrace();

generators/src/main/java/com/algolia/codegen/AlgoliaJavascriptGenerator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import org.openapitools.codegen.SupportingFile;
1111
import org.openapitools.codegen.languages.TypeScriptNodeClientCodegen;
1212

13-
public class AlgoliaJavascriptGenerator extends TypeScriptNodeClientCodegen {
13+
public class AlgoliaJavaScriptGenerator extends TypeScriptNodeClientCodegen {
1414

1515
private String CLIENT;
1616

@@ -82,6 +82,10 @@ public Map<String, Object> postProcessOperationsWithModels(
8282
Utils.getClientNameKebabCase(results),
8383
additionalProperties
8484
);
85+
additionalProperties.put(
86+
"utilsPackageVersion",
87+
Utils.getClientConfigField("javascript", "utilsPackageVersion")
88+
);
8589
} catch (GenerationException e) {
8690
e.printStackTrace();
8791
System.exit(1);

generators/src/main/java/com/algolia/codegen/AlgoliaPhpGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public Map<String, Object> postProcessOperationsWithModels(
5757
Utils.generateServer(client, additionalProperties);
5858
additionalProperties.put(
5959
"packageVersion",
60-
Utils.getPackageVersion("php")
60+
Utils.getClientConfigField("php", "packageVersion")
6161
);
6262
} catch (GenerationException e) {
6363
e.printStackTrace();

generators/src/main/java/com/algolia/codegen/Utils.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,21 @@ public static void generateServer(
153153
}
154154
}
155155

156-
// Get the package version from clients.config.json (doesn't work for JavaScript)
157-
public static String getPackageVersion(String language)
156+
/** Get the `field` value in the `config/clients.config.json` file for the given language */
157+
public static String getClientConfigField(String language, String field)
158158
throws GenerationException {
159-
if (language.equals("javascript")) {
159+
if (language.equals("javascript") && field.equals("packageVersion")) {
160160
throw new GenerationException(
161-
"Cannot use getPackageVersion with language=\"javascript\", " +
162-
"read openapitools.json instead"
161+
"Cannot read 'packageVersion' with language=\"javascript\", " +
162+
"read configs/openapitools.json instead"
163163
);
164164
}
165+
165166
try {
166167
JsonNode config = Json
167168
.mapper()
168169
.readTree(new File("config/clients.config.json"));
169-
return config.get(language).get("packageVersion").asText();
170+
return config.get(language).get(field).asText();
170171
} catch (IOException e) {
171172
throw new GenerationException(
172173
"Couldn't read packageVersion from clients.config.json",
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.algolia.codegen.cts.manager;
22

3+
import com.algolia.codegen.GenerationException;
34
import com.algolia.codegen.Utils;
45
import java.util.*;
56
import org.openapitools.codegen.SupportingFile;
@@ -13,13 +14,14 @@ public void addSupportingFiles(List<SupportingFile> supportingFiles) {
1314
}
1415

1516
protected void addExtraToBundle(Map<String, Object> bundle) {
16-
bundle.put(
17-
"packageVersion",
18-
Utils
19-
.readJsonFile("config/clients.config.json")
20-
.get("java")
21-
.get("packageVersion")
22-
.asText()
23-
);
17+
try {
18+
bundle.put(
19+
"packageVersion",
20+
Utils.getClientConfigField("java", "packageVersion")
21+
);
22+
} catch (GenerationException e) {
23+
e.printStackTrace();
24+
System.exit(1);
25+
}
2426
}
2527
}

generators/src/main/java/com/algolia/codegen/cts/manager/JavaScriptCtsManager.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.algolia.codegen.cts.manager;
22

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

4748
protected void addExtraToBundle(Map<String, Object> bundle) {
4849
bundle.put("packageDependencies", this.getPackageDependencies());
49-
bundle.put("utilsPackageVersion", this.getUtilsPackageVersion());
50-
}
51-
52-
private String getUtilsPackageVersion() {
53-
JsonNode openApiToolsConfig = Utils.readJsonFile(
54-
"config/openapitools.json"
55-
);
5650

57-
String utilsPackageVersion = openApiToolsConfig
58-
.get("generator-cli")
59-
.get("generators")
60-
.get("javascript-search")
61-
.get("additionalProperties")
62-
.get("utilsPackageVersion")
63-
.asText();
64-
65-
return utilsPackageVersion;
51+
try {
52+
bundle.put(
53+
"utilsPackageVersion",
54+
Utils.getClientConfigField("javascript", "utilsPackageVersion")
55+
);
56+
} catch (GenerationException e) {
57+
e.printStackTrace();
58+
System.exit(1);
59+
}
6660
}
6761
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
com.algolia.codegen.AlgoliaJavaGenerator
2-
com.algolia.codegen.AlgoliaJavascriptGenerator
2+
com.algolia.codegen.AlgoliaJavaScriptGenerator
33
com.algolia.codegen.AlgoliaPhpGenerator
44
com.algolia.codegen.cts.AlgoliaCtsGenerator

scripts/ci/githubActions/setRunVariables.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export const DEPENDENCIES = {
6060
...CLIENTS_COMMON_FILES,
6161
JS_CLIENT_FOLDER,
6262
'templates/javascript',
63-
'generators/src/main/java/com/algolia/codegen/AlgoliaJavascriptGenerator.java',
63+
'generators/src/main/java/com/algolia/codegen/AlgoliaJavaScriptGenerator.java',
6464
`:!${JS_CLIENT_FOLDER}/.github`,
6565
`:!${JS_CLIENT_FOLDER}/README.md`,
6666
'tests/CTS/methods/requests/templates/javascript',

scripts/config.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ export function getCustomGenerator(language: Language): string {
2727
return clientsConfig[language].customGenerator;
2828
}
2929

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

41+
/**
42+
* Returns the value of the `utilsPackageVersion`.
43+
*
44+
* Currently supported languages other than `javascript` does not have a utils package.
45+
*/
46+
export function getUtilsPackageVersionDefault(language: Language): string {
47+
if (language !== 'javascript') {
48+
return '';
49+
}
50+
51+
return clientsConfig[language].utilsPackageVersion;
52+
}
53+
3954
export function getGitHubUrl(
4055
language: Language,
4156
options?: { token: string }

scripts/release/process-release.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
getGitHubUrl,
2525
getLanguageFolder,
2626
getPackageVersionDefault,
27+
getUtilsPackageVersionDefault,
2728
} from '../config';
2829
import type { Language } from '../types';
2930

@@ -113,13 +114,9 @@ async function updateVersionForJavascript(
113114
if (!versionsToRelease.javascript) {
114115
return;
115116
}
117+
118+
// Sets the new version of the JavaScript client
116119
const jsVersion = versionsToRelease.javascript;
117-
const nextUtilsPackageVersion =
118-
semver.inc(
119-
openapiConfig['generator-cli'].generators['javascript-search']
120-
.additionalProperties.utilsPackageVersion,
121-
jsVersion.releaseType
122-
) || '';
123120
Object.values(GENERATORS)
124121
.filter((gen) => gen.language === 'javascript')
125122
.forEach((gen) => {
@@ -136,12 +133,25 @@ async function updateVersionForJavascript(
136133
);
137134
}
138135
additionalProperties.packageVersion = newVersion;
139-
additionalProperties.utilsPackageVersion = nextUtilsPackageVersion;
140136
});
137+
141138
await fsp.writeFile(
142139
toAbsolutePath('config/openapitools.json'),
143140
JSON.stringify(openapiConfig, null, 2)
144141
);
142+
143+
// Sets the new version of the utils package
144+
const nextUtilsPackageVersion =
145+
semver.inc(
146+
getUtilsPackageVersionDefault('javascript'),
147+
jsVersion.releaseType
148+
) || '';
149+
150+
clientsConfig.javascript.utilsPackageVersion = nextUtilsPackageVersion;
151+
await fsp.writeFile(
152+
toAbsolutePath('config/clients.config.json'),
153+
JSON.stringify(clientsConfig, null, 2)
154+
);
145155
}
146156

147157
async function updateConfigFiles(

website/docs/automation/add-new-api-client.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,12 @@ Generators are referenced by key with the following pattern `<languageName>-<cli
5757

5858
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.
5959

60-
| Option | Type | Language | Example | Definition |
61-
| ------------------- | :----: | :--------: | :-----------------------------: | :------------------------------------------------------------------------------------------------------------------- |
62-
| output | string | Common | `path/to/client/client-sources` | The output path of the client. |
63-
| gitRepoId | string | Common | `algoliasearch-client-java-2` | The name of the repository under the Algolia org. |
64-
| packageName | string | common | `AlgoliaSearch` | Name of the API package, used in [CTS](/docs/automation/testing/common-test-suite). |
65-
| 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. |
66-
| 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. |
60+
| Option | Type | Language | Example | Definition |
61+
| -------------- | :----: | :--------: | :-----------------------------: | :------------------------------------------------------------------------------------------------------------------- |
62+
| output | string | Common | `path/to/client/client-sources` | The output path of the client. |
63+
| gitRepoId | string | Common | `algoliasearch-client-java-2` | The name of the repository under the Algolia org. |
64+
| packageName | string | common | `AlgoliaSearch` | Name of the API package, used in [CTS](/docs/automation/testing/common-test-suite). |
65+
| 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. |
6766

6867
### GitHub actions
6968

0 commit comments

Comments
 (0)