-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(cts): update dependencies on cts generation for javascript (#490)
* 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
1 parent
5804840
commit 27f5e4a
Showing
11 changed files
with
241 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
generators/src/main/java/com/algolia/codegen/cts/manager/CtsManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
generators/src/main/java/com/algolia/codegen/cts/manager/CtsManagerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
generators/src/main/java/com/algolia/codegen/cts/manager/JavaCtsManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
} |
66 changes: 66 additions & 0 deletions
66
generators/src/main/java/com/algolia/codegen/cts/manager/JavaScriptCtsManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
generators/src/main/java/com/algolia/codegen/cts/manager/PhpCtsManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
tests/CTS/methods/requests/templates/javascript/package.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |