-
Notifications
You must be signed in to change notification settings - Fork 27
guides: add new generator #3837
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
Changes from 16 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
2f86564
rename to guide
millotp 941a6b7
compile works
millotp ff8900e
feat: snippets live on the CI now
shortcuts 8a59c83
feat: ignore snippets totally
shortcuts 96c6a0c
chore: move snippets/guides to snippets
shortcuts 8ddbb4e
almost there
millotp ff4a629
chore: pr title
shortcuts 4fc0618
fix snippets
millotp 52a93a9
delete guides
millotp 8c85aee
fix c#
millotp b2b78fb
fix c# fr
millotp 0257acf
fix: guides on ci
shortcuts 08d504c
feat: buildGuides
shortcuts 1f69a01
fix: run only on existing guides
shortcuts 9aa1672
output
millotp 0a7e137
fix: build only existing guides
shortcuts 6e614ae
build js
millotp 8cfc133
build js ci
millotp 7f5644b
build playground
millotp 707df25
fix: js default region
shortcuts 8b3ca13
fix: js default region comment
shortcuts 5896759
partial snippets
millotp 8e25df9
conflict
millotp 9005027
undo prout
millotp 2796014
good ?
millotp 361291b
chore: review
shortcuts 151f7f9
Merge branch 'main' into feat/guides-gen
millotp 8639c26
chore: bump timeout :(
shortcuts 5929d04
Merge branch 'main' into feat/guides-gen
shortcuts 78d0dd6
fix: cts guides file is enough
shortcuts fbb05dc
Merge remote-tracking branch 'origin/main' into feat/guides-gen
millotp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -16,4 +16,4 @@ jobs: | |
| - name: Pull Request title rules | ||
| uses: Slashgear/[email protected] | ||
| with: | ||
| regexp: '^(docs|chore|snippets)|((?:feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)\((?:clients|generators|playground|csharp|dart|go|java|javascript|kotlin|php|python|ruby|scala|swift|cts|specs|scripts|ci|templates|deps)\)): .+' | ||
| regexp: '^(docs|chore|snippets|guides)|((?:feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)\((?:clients|generators|playground|csharp|dart|go|java|javascript|kotlin|php|python|ruby|scala|swift|cts|specs|scripts|ci|templates|deps)\)): .+' | ||
This file contains hidden or 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 hidden or 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 hidden or 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
55 changes: 55 additions & 0 deletions
55
generators/src/main/java/com/algolia/codegen/cts/guides/GuidesGenerator.java
This file contains hidden or 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,55 @@ | ||
| package com.algolia.codegen.cts.guides; | ||
|
|
||
| import com.algolia.codegen.cts.manager.CTSManager; | ||
| import com.algolia.codegen.cts.tests.TestsGenerator; | ||
| import com.algolia.codegen.utils.*; | ||
| import java.io.File; | ||
| import java.util.*; | ||
| import org.openapitools.codegen.CodegenModel; | ||
| import org.openapitools.codegen.CodegenOperation; | ||
| import org.openapitools.codegen.SupportingFile; | ||
|
|
||
| public class GuidesGenerator extends TestsGenerator { | ||
|
|
||
| public GuidesGenerator(CTSManager ctsManager) { | ||
| super(ctsManager); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean available() { | ||
| File templates = new File("templates/" + language + "/guides/" + client); | ||
| return templates.exists(); | ||
| } | ||
|
|
||
| @Override | ||
| public void addSupportingFiles(List<SupportingFile> supportingFiles, String outputFolder, String extension) { | ||
| if (!available()) { | ||
| return; | ||
| } | ||
|
|
||
| // same extension as the snippets | ||
| extension = Helpers.getClientConfigField(language, "snippets", "extension"); | ||
| outputFolder = Helpers.getClientConfigField(language, "snippets", "outputFolder"); | ||
|
|
||
| if (!outputFolder.equals("")) { | ||
| outputFolder = "/" + outputFolder + "/"; | ||
| } else { | ||
| outputFolder = "/"; | ||
| } | ||
|
|
||
| File templates = new File("templates/" + language + "/guides/" + client); | ||
| for (File f : templates.listFiles()) { | ||
| supportingFiles.add( | ||
| new SupportingFile( | ||
| "guides/" + client + "/" + f.getName(), | ||
| "guides/" + language + outputFolder + f.getName().replace(".mustache", "") + extension | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation> operations, Map<String, Object> bundle) throws Exception { | ||
| // nothing to do here, the mustache uses dynamicSnippets lambda | ||
| } | ||
| } |
81 changes: 81 additions & 0 deletions
81
generators/src/main/java/com/algolia/codegen/cts/lambda/DynamicSnippetLambda.java
This file contains hidden or 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,81 @@ | ||
| package com.algolia.codegen.cts.lambda; | ||
|
|
||
| import com.algolia.codegen.cts.tests.ParametersWithDataType; | ||
| import com.algolia.codegen.cts.tests.Snippet; | ||
| import com.algolia.codegen.exceptions.CTSException; | ||
| import com.algolia.codegen.utils.Helpers; | ||
| import com.fasterxml.jackson.core.type.TypeReference; | ||
| import com.fasterxml.jackson.databind.*; | ||
| import com.samskivert.mustache.Mustache; | ||
| import com.samskivert.mustache.Template; | ||
| import io.swagger.v3.core.util.Json; | ||
| import java.io.IOException; | ||
| import java.io.Writer; | ||
| import java.util.Map; | ||
| import org.openapitools.codegen.CodegenModel; | ||
| import org.openapitools.codegen.CodegenOperation; | ||
| import org.openapitools.codegen.DefaultCodegen; | ||
| import org.openapitools.codegen.TemplateManager; | ||
| import org.openapitools.codegen.api.TemplatePathLocator; | ||
| import org.openapitools.codegen.api.TemplatingEngineAdapter; | ||
| import org.openapitools.codegen.api.TemplatingExecutor; | ||
| import org.openapitools.codegen.templating.CommonTemplateContentLocator; | ||
| import org.openapitools.codegen.templating.GeneratorTemplateContentLocator; | ||
| import org.openapitools.codegen.templating.TemplateManagerOptions; | ||
|
|
||
| public class DynamicSnippetLambda implements Mustache.Lambda { | ||
|
|
||
| private final TemplatingExecutor executor; | ||
| private final TemplatingEngineAdapter adaptor; | ||
|
|
||
| private final ParametersWithDataType paramsType; | ||
| private final Map<String, CodegenOperation> operations; | ||
|
|
||
| private final Map<String, Snippet> snippets; | ||
|
|
||
| public DynamicSnippetLambda( | ||
| DefaultCodegen generator, | ||
| Map<String, CodegenModel> models, | ||
| Map<String, CodegenOperation> operations, | ||
| String language, | ||
| String client | ||
| ) { | ||
| this.operations = operations; | ||
| this.paramsType = new ParametersWithDataType(models, language, client, true); | ||
|
|
||
| JsonNode snippetsFile = Helpers.readJsonFile("tests/CTS/guides/search/snippets.json"); | ||
| this.snippets = Json.mapper().convertValue(snippetsFile, new TypeReference<Map<String, Snippet>>() {}); | ||
|
|
||
| // we can't access the default template manager, so we have to create our own | ||
| TemplateManager templateManager = new TemplateManager( | ||
| new TemplateManagerOptions(generator.isEnableMinimalUpdate(), generator.isSkipOverwrite()), | ||
| generator.getTemplatingEngine(), | ||
| new TemplatePathLocator[] { new GeneratorTemplateContentLocator(generator), new CommonTemplateContentLocator() } | ||
| ); | ||
|
|
||
| this.executor = templateManager; | ||
| this.adaptor = generator.getTemplatingEngine(); | ||
| } | ||
|
|
||
| @Override | ||
| public void execute(Template.Fragment fragment, Writer writer) throws IOException, CTSException { | ||
| String snippetName = fragment.execute(); | ||
| Snippet snippet = snippets.get(snippetName); | ||
| if (snippet == null) { | ||
| throw new CTSException("Cannot find snippet: " + snippetName); | ||
| } | ||
|
|
||
| String operationId = snippet.method; | ||
|
|
||
| CodegenOperation operation = operations.get(operationId); | ||
| if (operation == null) { | ||
| throw new CTSException("Cannot find operation for method: " + operationId); | ||
| } | ||
|
|
||
| // set the method attributes | ||
| Map<String, Object> context = (Map<String, Object>) fragment.context(); | ||
| snippet.addMethodCall(context, paramsType, operation); | ||
|
|
||
| writer.write(adaptor.compileTemplate(executor, context, "tests/client/method.mustache")); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.