-
Notifications
You must be signed in to change notification settings - Fork 6k
Support for file based config. Implementation for #616. #805
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 all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f8cafaa
new CliOption class for wrapping org.apache.commons.cli.Option
9f15040
add List<CliOption> cliOptions() method to CodegenConfig interface wi…
33436d6
Get all cliOptions for available codegen configs and add to command l…
d7dec5a
Use new functionality to add 't' as a command line option to set 'tem…
e08a5a9
Add 'modelPackage' and 'apiPackage' as command line options
1e09f51
Add 'invokerPackage', 'groupId', 'arifactId', 'artifactVersion', 'sou…
74f5c76
removing extra file, was committed by mistake
a9e767c
Rolling back changes to deprecated class
2cca1a8
Changing templateDir back to explicit option for cli
c3055c7
Adding Config and ConfigParser classes
c9c58cb
new -c/--config option for swagger-cli to specify json config file path
26a2290
adding classPrefix, sourceFolder, projectName cli options to objc
c5a7800
fixing typo
08647b3
fixing property name
1f36096
Simplifying CliOption class
ba7025d
Adding config-help command
0996901
add documentation -c option and config-help
hyeghiazaryan 3e0913a
removing generic header comments
d258a6c
Merge branch 'develop_2.0' of https://github.com/hyeghiazaryan/swagge…
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
49 changes: 49 additions & 0 deletions
49
modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/cmd/ConfigHelp.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,49 @@ | ||
package com.wordnik.swagger.codegen.cmd; | ||
|
||
import com.wordnik.swagger.codegen.CliOption; | ||
import com.wordnik.swagger.codegen.CodegenConfig; | ||
import io.airlift.airline.Command; | ||
import io.airlift.airline.Option; | ||
import java.util.ServiceLoader; | ||
import static java.util.ServiceLoader.load; | ||
|
||
@Command(name = "config-help", description = "Config help for chosen lang") | ||
public class ConfigHelp implements Runnable { | ||
|
||
@Option(name = {"-l", "--lang"}, title = "language", required = true, | ||
description = "language to get config help for") | ||
private String lang; | ||
|
||
@Override | ||
public void run() { | ||
System.out.println(); | ||
CodegenConfig config = forName(lang); | ||
System.out.println("CONFIG OPTIONS"); | ||
for (CliOption langCliOption : config.cliOptions()) { | ||
System.out.println("\t" + langCliOption.getOpt()); | ||
System.out.println("\t " + langCliOption.getDescription()); | ||
System.out.println(); | ||
} | ||
} | ||
|
||
/** | ||
* Tries to load config class with SPI first, then with class name directly from classpath | ||
* @param name name of config, or full qualified class name in classpath | ||
* @return config class | ||
*/ | ||
private static CodegenConfig forName(String name) { | ||
ServiceLoader<CodegenConfig> loader = load(CodegenConfig.class); | ||
for (CodegenConfig config : loader) { | ||
if (config.getName().equals(name)) { | ||
return config; | ||
} | ||
} | ||
|
||
// else try to load directly | ||
try { | ||
return (CodegenConfig) Class.forName(name).newInstance(); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Can't load config class with name ".concat(name), e); | ||
} | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/CliOption.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,23 @@ | ||
package com.wordnik.swagger.codegen; | ||
|
||
public class CliOption { | ||
private final String opt; | ||
private String description; | ||
|
||
public CliOption(String opt, String description) { | ||
this.opt = opt; | ||
this.description = description; | ||
} | ||
|
||
public String getOpt() { | ||
return opt; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -140,4 +140,4 @@ public static CodegenConfig getConfig(String name) { | |
} | ||
} | ||
} | ||
} | ||
} |
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.
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.
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.
Is it possible to introduce a general method of reading and storing the options from the provided JSON file (maybe place that method in
DefaultCodegen
)? So that it doesn't need to do the same thing for every language separately.A rough thought in my mind is using a
Map
to store any keys and values read from the JSON config file.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.
It seems no validations have been made to these options, e.g. I could use
petstore model
as the modelPackage (containing a space), which will generate source files that could not compile.It's not a big deal, and the PR is really great, allowing users customizing various options for the generated code. Just pointed it out FYI (maybe the validations would be added in the future).
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.
@xhh I did remove the template generated comments, thanks.
RE: reading and storing config options in
DefaultCodegen
Right now there are two config options supported by all of the languages,
modelPackage
andapiPackage
, those are being read and set in theprocessOpts
method ofDefaultCodegen
, all the other config options that are language specific should be managed by that lang specific config itself. If in the the future there is another config option that is supported by all the languages then it should be managed it DefaultCodegen.RE: validation
No validation is necessary on the config options. Even if model package does not contain spaces it still can be out of sync with apiPackage and it will not compile. Using these custom config options assumes some level of competency.