-
Notifications
You must be signed in to change notification settings - Fork 17
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
Changes from all 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.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; | ||
|
@@ -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 | ||
|
@@ -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( | ||
|
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(); | ||
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.
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. And have this code that his specific to JS in the JavascriptCtsManager 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. 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) {} | ||
} |
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" | ||
} | ||
} |
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