-
Notifications
You must be signed in to change notification settings - Fork 21
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
Changes from 6 commits
c7a8575
ea23543
8e15f43
1d88899
de0c770
e532455
b136ec6
e271b22
7c43450
d3c14dc
7a8512b
5511ea3
62fc1bb
0303223
2d04ade
70fdd86
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
@@ -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. | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it would be clearer to combine those into one like There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
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 { | ||
eunjae-lee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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) { | ||
eunjae-lee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 { | ||
eunjae-lee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public void addSupportingFiles(List<SupportingFile> supportingFiles) { | ||
supportingFiles.add( | ||
new SupportingFile("package.mustache", ".", "package.json") | ||
); | ||
} | ||
|
||
public String getPackageDependencies() { | ||
eunjae-lee marked this conversation as resolved.
Show resolved
Hide resolved
eunjae-lee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,17 +5,17 @@ | |
"test": "jest" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
that's why There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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": { | ||
|
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
de0c770