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(cts): update dependencies on cts generation for javascript #490

Merged
merged 16 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions config/generation.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Very cool ! can you replace other instance of the code where we load a json by this method ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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);
Copy link
Collaborator

Choose a reason for hiding this comment

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

fancy !


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();
Copy link
Collaborator

Choose a reason for hiding this comment

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

packageName is a lie, it's not used for anything other than js because it doesn't change, unless we know other languages that will be split in clients, I think we can remove this notion from the CTS, and remove them from clients.config.json (but keep them for js)

Copy link
Collaborator

Choose a reason for hiding this comment

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

And have this code that his specific to JS in the JavascriptCtsManager

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Bonne idée 2d04ade

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
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"
}
}