Skip to content

chore(cts): update dependencies on cts generation for javascript #490

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 16 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
15 changes: 15 additions & 0 deletions generators/src/main/java/com/algolia/codegen/Utils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.algolia.codegen;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Sets;
import io.swagger.v3.core.util.Json;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.*;
import org.openapitools.codegen.CodegenOperation;
Expand Down Expand Up @@ -149,4 +153,15 @@ public static void generateServer(
System.exit(1);
}
}

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.CtsManager;
import com.algolia.codegen.cts.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 All @@ -114,6 +121,28 @@ protected Builder<String, Lambda> addMustacheLambdas() {
return lambdas;
}

private Map<String, String> getPackageVersionMap() {
HashMap<String, String> packageVersionMap = new HashMap<>();
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");
packageVersionMap.put(
additionalProperties.get("packageName").asText(),
additionalProperties.get("packageVersion").asText()
);
}

return packageVersionMap;
}

@Override
public Map<String, Object> postProcessSupportingFileData(
Map<String, Object> objs
Expand Down Expand Up @@ -147,6 +176,8 @@ public Map<String, Object> postProcessSupportingFileData(
bundle.put("hasRegionalHost", hasRegionalHost);
bundle.put("defaultRegion", client.equals("predict") ? "ew" : "us");
bundle.put("lambda", lambda);
bundle.put("packageDependencies", ctsManager.getPackageDependencies());
ctsManager.addExtraToBundle(bundle);
Copy link
Collaborator

Choose a reason for hiding this comment

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

it would be clearer to combine those into one like addSupportingData(bundle), and let the manager add what it wants

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it makes sense 0303223


List<Object> blocks = new ArrayList<>();
ParametersWithDataType paramsType = new ParametersWithDataType(
Expand Down
42 changes: 42 additions & 0 deletions generators/src/main/java/com/algolia/codegen/cts/CtsManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.algolia.codegen.cts;

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

abstract class CtsManager {

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

public abstract String getPackageDependencies();

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

protected Map<String, String> getFilteredPackageVersionMap(
List<String> packages
) {
HashMap<String, String> result = new HashMap<>();
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");
String packageName = additionalProperties.get("packageName").asText();
String packageVersion = additionalProperties
.get("packageVersion")
.asText();
if (packages.contains(packageName)) {
result.put(packageName, packageVersion);
}
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.algolia.codegen.cts;

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

public class CtsManagerFactory {

public static CtsManager getManager(String language) {
if (language == null) {
return null;
}
switch (language) {
case "javascript":
return new JavaScriptCtsManager();
case "java":
return new JavaCtsManager();
case "php":
return new PhpCtsManager();
}
return null;
}

public static class JavaScriptCtsManager extends CtsManager {

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

public String getPackageDependencies() {
return String.join(
",\n",
this.getFilteredPackageVersionMap(
List.of(
"@experimental-api-clients-automation/algoliasearch-lite",
"@experimental-api-clients-automation/client-abtesting",
"@experimental-api-clients-automation/client-analytics",
"@experimental-api-clients-automation/client-common",
"@experimental-api-clients-automation/client-insights",
"@experimental-api-clients-automation/client-personalization",
"@experimental-api-clients-automation/client-predict",
"@experimental-api-clients-automation/client-query-suggestions",
"@experimental-api-clients-automation/client-search",
"@experimental-api-clients-automation/client-sources",
"@experimental-api-clients-automation/recommend",
"@experimental-api-clients-automation/requester-node-http"
)
)
.entrySet()
.stream()
.map(entry -> {
return (
" \"" + entry.getKey() + "\": \"" + entry.getValue() + "\""
);
})
.toArray(String[]::new)
);
}

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

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

String mainPackage = clientsConfig
.get("javascript")
.get("mainPackage")
.asText();

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

return utilsPackageVersion;
}
}

public static class JavaCtsManager extends CtsManager {

public void addSupportingFiles(List<SupportingFile> supportingFiles) {}

public String getPackageDependencies() {
return null;
}
}

public static class PhpCtsManager extends CtsManager {

public void addSupportingFiles(List<SupportingFile> supportingFiles) {}

public String getPackageDependencies() {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "javascript-tests",
"version": "1.0.0",
"scripts": {
"test": "jest"
},
"dependencies": {
{{{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"
}
}
12 changes: 6 additions & 6 deletions tests/output/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
"test": "jest"
Copy link
Member

Choose a reason for hiding this comment

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

We should maybe sort those deps otherwise it will be sorted locally and then unsorted when bumped etc

Copy link
Member

Choose a reason for hiding this comment

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

ah or maybe just the yarn install that will come after will sort them actually

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually I was going to ask this question, but why aren't this deps sorted automatically? I thought formatting after the generation would do it, but didn't.

Copy link
Member

Choose a reason for hiding this comment

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

I believe it's yarn that sorts the dependencies in the package.json

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just found it:

After the cts generation, it runs this formatter: https://github.com/algolia/api-clients-automation/blob/7c434506d7d6f23f044841024b0dbc8e9af26514/scripts/formatter.ts#L16:L16

and it runs:

yarn eslint --ext=ts tests/output/javascript/src --fix --no-error-on-unmatched-pattern

that's why tests/output/javascript/package.json is being excluded from the eslint.

Copy link
Member

Choose a reason for hiding this comment

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

ahhh yes we can maybe add an extra line for the package.json if it works then

Copy link
Contributor Author

Choose a reason for hiding this comment

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

d3c14dc

What do you think?

},
"dependencies": {
"@experimental-api-clients-automation/client-insights": "0.2.0",
"@experimental-api-clients-automation/recommend": "0.2.0",
"@experimental-api-clients-automation/client-search": "0.2.0",
"@experimental-api-clients-automation/algoliasearch-lite": "0.2.0",
"@experimental-api-clients-automation/client-sources": "0.2.0",
"@experimental-api-clients-automation/client-personalization": "0.2.0",
"@experimental-api-clients-automation/client-abtesting": "0.2.0",
"@experimental-api-clients-automation/client-analytics": "0.2.0",
"@experimental-api-clients-automation/client-common": "0.2.0",
"@experimental-api-clients-automation/client-insights": "0.2.0",
"@experimental-api-clients-automation/client-personalization": "0.2.0",
"@experimental-api-clients-automation/client-predict": "0.2.0",
"@experimental-api-clients-automation/client-query-suggestions": "0.2.0",
"@experimental-api-clients-automation/client-search": "0.2.0",
"@experimental-api-clients-automation/client-sources": "0.2.0",
"@experimental-api-clients-automation/recommend": "0.2.0",
"@experimental-api-clients-automation/client-common": "0.2.0",
"@experimental-api-clients-automation/requester-node-http": "0.2.0"
},
"devDependencies": {
Expand Down