Skip to content

Commit

Permalink
chore(cts): update dependencies on cts generation for javascript (#490)
Browse files Browse the repository at this point in the history
* chore(cts): accept an option to update versions

* chore: WIP

* chore: extract to Utils

* chore: if condition for js

* chore: clean up duplicated code

* chore: introduce CtsManager

* chore: run formatter on java generators

* chore: split classes into multiple files

* chore: remove unused code and run formatter

* chore: update formatter to include json

* chore: remove redundant check

* chore: adjust to the latest main branch

* chore: wrap with addDataToBundle

* move the implementation of getPackageDependencies to the js level

* chore: update generation list
  • Loading branch information
eunjae-lee authored May 16, 2022
1 parent 5804840 commit 27f5e4a
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 26 deletions.
1 change: 1 addition & 0 deletions config/generation.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = {
'!clients/algoliasearch-client-javascript/packages/requester-*/**',
'!clients/algoliasearch-client-javascript/packages/client-common/**',

'tests/output/javascript/package.json',
'tests/output/javascript/src/methods/**',
'tests/output/javascript/src/client/**',

Expand Down
11 changes: 11 additions & 0 deletions generators/src/main/java/com/algolia/codegen/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,15 @@ public static String getPackageVersion(String language)
);
}
}

public static JsonNode readJsonFile(String filePath) {
JsonNode json = null;
try {
json = Json.mapper().readTree(new File(filePath));
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
return json;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.algolia.codegen.cts;

import com.algolia.codegen.Utils;
import com.algolia.codegen.cts.manager.CtsManager;
import com.algolia.codegen.cts.manager.CtsManagerFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.ImmutableMap.Builder;
Expand All @@ -22,6 +25,7 @@ public class AlgoliaCtsGenerator extends DefaultCodegen {
private String language;
private String client;
private String packageName;
private CtsManager ctsManager;

/**
* Configures the type of generator.
Expand Down Expand Up @@ -63,31 +67,34 @@ public void processOpts() {
language = (String) additionalProperties.get("language");
client = (String) additionalProperties.get("client");
packageName = (String) additionalProperties.get("packageName");
ctsManager = CtsManagerFactory.getManager(language);

JsonNode config = Utils.readJsonFile("config/clients.config.json");
TestConfig testConfig = null;
try {
JsonNode config = Json
.mapper()
.readTree(new File("config/clients.config.json"));
TestConfig testConfig = Json
.mapper()
.treeToValue(config.get(language).get("tests"), TestConfig.class);

setTemplateDir("tests/CTS/methods/requests/templates/" + language);
setOutputDir("tests/output/" + language);
String clientName = language.equals("php")
? Utils.createClientName(client, language)
: client;
supportingFiles.add(
new SupportingFile(
"requests.mustache",
testConfig.outputFolder + "/methods/requests",
clientName + testConfig.extension
)
);
} catch (IOException e) {
testConfig =
Json
.mapper()
.treeToValue(config.get(language).get("tests"), TestConfig.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
System.exit(1);
}

setTemplateDir("tests/CTS/methods/requests/templates/" + language);
setOutputDir("tests/output/" + language);
String clientName = language.equals("php")
? Utils.createClientName(client, language)
: client;
supportingFiles.add(
new SupportingFile(
"requests.mustache",
testConfig.outputFolder + "/methods/requests",
clientName + testConfig.extension
)
);

ctsManager.addSupportingFiles(supportingFiles);
}

@Override
Expand Down Expand Up @@ -147,6 +154,7 @@ public Map<String, Object> postProcessSupportingFileData(
bundle.put("hasRegionalHost", hasRegionalHost);
bundle.put("defaultRegion", client.equals("predict") ? "ew" : "us");
bundle.put("lambda", lambda);
ctsManager.addDataToBundle(bundle);

List<Object> blocks = new ArrayList<>();
ParametersWithDataType paramsType = new ParametersWithDataType(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.algolia.codegen.cts.manager;

import com.algolia.codegen.Utils;
import com.fasterxml.jackson.databind.JsonNode;
import java.util.*;
import org.openapitools.codegen.SupportingFile;

public abstract class CtsManager {

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

public List<Object> getPackageDependencies() {
return null;
}

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

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

protected Object[] getFilteredPackageVersions(List<String> packages) {
HashMap<String, String> result = new HashMap<>();

// Read config/openapitools.js for JavaScript
JsonNode openApiToolsConfig = Utils.readJsonFile(
"config/openapitools.json"
);
Iterator<JsonNode> generatorIterator = openApiToolsConfig
.get("generator-cli")
.get("generators")
.elements();
while (generatorIterator.hasNext()) {
JsonNode generator = generatorIterator.next();
JsonNode additionalProperties = generator.get("additionalProperties");
if (!additionalProperties.has("packageVersion")) {
continue;
}
String packageName = additionalProperties.get("packageName").asText();
String packageVersion = additionalProperties
.get("packageVersion")
.asText();
if (packages.contains(packageName)) {
result.put(packageName, packageVersion);
}
}

JsonNode clientsConfig = Utils.readJsonFile("config/clients.config.json");
Iterator<JsonNode> clientsIterator = clientsConfig.elements();
while (clientsIterator.hasNext()) {
JsonNode client = clientsIterator.next();

if (!client.has("packageVersion")) {
continue;
}
String packageName = client.get("packageName").asText();
String packageVersion = client.get("packageVersion").asText();
if (packages.contains(packageName)) {
result.put(packageName, packageVersion);
}
}

return result
.entrySet()
.stream()
.map(entry -> {
Map<String, String> newEntry = new HashMap<>();
newEntry.put("packageName", entry.getKey());
newEntry.put("packageVersion", entry.getValue());
return newEntry;
})
.toArray(Object[]::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.algolia.codegen.cts.manager;

import java.util.*;

public class CtsManagerFactory {

public static CtsManager getManager(String language) {
switch (language) {
case "javascript":
return new JavaScriptCtsManager();
case "java":
return new JavaCtsManager();
case "php":
return new PhpCtsManager();
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.algolia.codegen.cts.manager;

import java.util.*;
import org.openapitools.codegen.SupportingFile;

public class JavaCtsManager extends CtsManager {

public void addSupportingFiles(List<SupportingFile> supportingFiles) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.algolia.codegen.cts.manager;

import com.algolia.codegen.Utils;
import com.fasterxml.jackson.databind.JsonNode;
import java.util.*;
import org.openapitools.codegen.SupportingFile;

public class JavaScriptCtsManager extends CtsManager {

public void addSupportingFiles(List<SupportingFile> supportingFiles) {
supportingFiles.add(
new SupportingFile("package.mustache", ".", "package.json")
);
}

public List<Object> getPackageDependencies() {
List<Object> result = new ArrayList<Object>();

JsonNode openApiToolsConfig = Utils.readJsonFile(
"config/openapitools.json"
);
Iterator<Map.Entry<String, JsonNode>> fieldIterator = openApiToolsConfig
.get("generator-cli")
.get("generators")
.fields();

while (fieldIterator.hasNext()) {
Map.Entry<String, JsonNode> field = fieldIterator.next();
if (!field.getKey().startsWith("javascript-")) {
continue;
}
JsonNode generator = field.getValue();
JsonNode additionalProperties = generator.get("additionalProperties");
String packageName = additionalProperties.get("packageName").asText();
String packageVersion = additionalProperties
.get("packageVersion")
.asText();

Map<String, String> newEntry = new HashMap<>();
newEntry.put("packageName", packageName);
newEntry.put("packageVersion", packageVersion);
result.add(newEntry);
}
return result;
}

protected void addExtraToBundle(Map<String, Object> bundle) {
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.algolia.codegen.cts.manager;

import java.util.*;
import org.openapitools.codegen.SupportingFile;

public class PhpCtsManager extends CtsManager {

public void addSupportingFiles(List<SupportingFile> supportingFiles) {}
}
6 changes: 1 addition & 5 deletions scripts/cts/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ export async function ctsGenerateMany(
if (!getTestOutputFolder(lang)) {
continue;
}
await formatter(
lang,
toAbsolutePath(`tests/output/${lang}/${getTestOutputFolder(lang)}`),
verbose
);
await formatter(lang, toAbsolutePath(`tests/output/${lang}`), verbose);
}
}
2 changes: 1 addition & 1 deletion scripts/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function formatter(
let cmd = '';
switch (language) {
case 'javascript':
cmd = `yarn eslint --ext=ts ${folder} --fix --no-error-on-unmatched-pattern`;
cmd = `yarn eslint --ext=ts,json ${folder} --fix --no-error-on-unmatched-pattern`;
break;
case 'java':
cmd = `find ${folder} -type f -name "*.java" | xargs java --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
Expand Down
22 changes: 22 additions & 0 deletions tests/CTS/methods/requests/templates/javascript/package.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "javascript-tests",
"version": "1.0.0",
"scripts": {
"test": "jest"
},
"dependencies": {
{{#packageDependencies}}
"{{{packageName}}}": "{{packageVersion}}",
{{/packageDependencies}}
"@experimental-api-clients-automation/client-common": "{{utilsPackageVersion}}",
"@experimental-api-clients-automation/requester-node-http": "{{utilsPackageVersion}}"
},
"devDependencies": {
"@types/jest": "27.4.1",
"@types/node": "16.11.26",
"jest": "27.5.1",
"ts-jest": "27.1.4",
"ts-node": "10.7.0",
"typescript": "4.6.3"
}
}

0 comments on commit 27f5e4a

Please sign in to comment.