Skip to content

Commit

Permalink
feat(generators): factor utilsPackageVersion for JavaScript (#511)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored May 18, 2022
1 parent d0b9625 commit c971410
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 72 deletions.
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 com.fasterxml.jackson.databind.JsonNode;
import java.util.*;
Expand All @@ -9,10 +10,16 @@ public abstract class CtsManager {

public abstract void addSupportingFiles(List<SupportingFile> supportingFiles);

protected void addExtraToBundle(Map<String, Object> bundle) {}
protected void addExtraToBundle(Map<String, Object> bundle)
throws GenerationException {}

public void addDataToBundle(Map<String, Object> bundle) {
this.addExtraToBundle(bundle);
try {
this.addExtraToBundle(bundle);
} catch (GenerationException e) {
e.printStackTrace();
System.exit(1);
}
}

protected Object[] getFilteredPackageVersions(List<String> packages) {
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 @@ -12,14 +13,11 @@ public void addSupportingFiles(List<SupportingFile> supportingFiles) {
);
}

protected void addExtraToBundle(Map<String, Object> bundle) {
protected void addExtraToBundle(Map<String, Object> bundle)
throws GenerationException {
bundle.put(
"packageVersion",
Utils
.readJsonFile("config/clients.config.json")
.get("java")
.get("packageVersion")
.asText()
Utils.getClientConfigField("java", "packageVersion")
);
}
}
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 @@ -44,24 +45,12 @@ private List<Object> getPackageDependencies() {
return result;
}

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

private String getUtilsPackageVersion() {
JsonNode openApiToolsConfig = Utils.readJsonFile(
"config/openapitools.json"
bundle.put(
"utilsPackageVersion",
Utils.getClientConfigField("javascript", "utilsPackageVersion")
);

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

return utilsPackageVersion;
}
}
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
25 changes: 25 additions & 0 deletions scripts/__tests__/common.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import execa from 'execa';

import { gitCommit } from '../common';
import { getClientsConfigField } from '../config';

jest.mock('execa');

Expand Down Expand Up @@ -51,3 +52,27 @@ describe('gitCommit', () => {
);
});
});

describe('config', () => {
describe('getClientsConfigField', () => {
it('throws if the field is not found', () => {
expect(() => {
getClientsConfigField('javascript', 'packageVersion');
}).toThrowErrorMatchingInlineSnapshot(
`"Unable to find 'packageVersion' for 'javascript'"`
);

expect(() => {
getClientsConfigField('java', 'utilsPackageVersion');
}).toThrowErrorMatchingInlineSnapshot(
`"Unable to find 'utilsPackageVersion' for 'java'"`
);
});

it('find the field if it exists', () => {
expect(getClientsConfigField('java', ['tests', 'extension'])).toEqual(
'.test.java'
);
});
});
});
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
37 changes: 28 additions & 9 deletions scripts/config.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,58 @@
import clientsConfig from '../config/clients.config.json';
import openapiConfig from '../config/openapitools.json';

import type { Language } from './types';
import type { Language, LanguageConfig } from './types';

export function getClientsConfigField(
language: Language,
pathToField: string[] | string
): any {
const config: LanguageConfig = clientsConfig[language];
const path = Array.isArray(pathToField) ? pathToField : [pathToField];

return path.reduce((current, key) => {
if (!current || !current[key]) {
throw new Error(`Unable to find '${pathToField}' for '${language}'`);
}

return current[key];
}, config);
}

export function getLanguageFolder(language: Language): string {
return clientsConfig[language].folder;
return getClientsConfigField(language, 'folder');
}

export function getLanguageApiFolder(language: Language): string {
return clientsConfig[language].apiFolder;
return getClientsConfigField(language, 'apiFolder');
}

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

export function getTestExtension(language: Language): string {
return clientsConfig[language].tests.extension;
return getClientsConfigField(language, ['tests', 'extension']);
}

export function getTestOutputFolder(language: Language): string {
return clientsConfig[language].tests.outputFolder;
return getClientsConfigField(language, ['tests', 'outputFolder']);
}

export function getCustomGenerator(language: Language): string {
return clientsConfig[language].customGenerator;
return getClientsConfigField(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']
.additionalProperties.packageVersion;
}
return clientsConfig[language].packageVersion;

return getClientsConfigField(language, 'packageVersion');
}

export function getGitHubUrl(
Expand Down
Loading

0 comments on commit c971410

Please sign in to comment.