Skip to content

Commit 8ba4441

Browse files
author
MerkushevKirill
committed
add - new command line interface in distribution module
can replace Codegen and MetaGenerator classes. Provides a bit different cli interface based on refactored pom from #541, affects #349 ``` usage: swagger <command> [<args>] The most commonly used swagger commands are: generate Generate code with chosen lang help Display help information langs Shows available langs meta MetaGenerator. Generator for creating a new template set and configuration for Codegen. The output will be based on the language you specify, and includes default templates to include. ``` **generate** and **meta** commands have same as current cli options
1 parent 823ac04 commit 8ba4441

File tree

5 files changed

+411
-79
lines changed

5 files changed

+411
-79
lines changed
Lines changed: 94 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,95 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2-
<parent>
3-
<groupId>com.wordnik</groupId>
4-
<artifactId>swagger-codegen-project</artifactId>
5-
<version>2.1.3-M1-SNAPSHOT</version>
6-
<relativePath>../..</relativePath>
7-
</parent>
8-
<modelVersion>4.0.0</modelVersion>
9-
<groupId>com.wordnik</groupId>
10-
<artifactId>swagger-codegen-distribution</artifactId>
11-
<packaging>jar</packaging>
12-
<name>swagger-codegen (executable)</name>
13-
<version>2.1.3-M1-SNAPSHOT</version>
14-
<build>
15-
<testSourceDirectory>src/test/scala</testSourceDirectory>
16-
<outputDirectory>target/classes</outputDirectory>
17-
<testOutputDirectory>target/test-classes</testOutputDirectory>
18-
<defaultGoal>install</defaultGoal>
19-
<directory>target</directory>
20-
<finalName>${project.artifactId}-${project.version}</finalName>
21-
<plugins>
22-
<plugin>
23-
<groupId>org.apache.maven.plugins</groupId>
24-
<artifactId>maven-jar-plugin</artifactId>
25-
<configuration>
26-
<archive>
27-
<manifest>
28-
<mainClass>com.wordnik.swagger.codegen.Codegen</mainClass>
29-
</manifest>
30-
</archive>
31-
</configuration>
32-
</plugin>
33-
<plugin>
34-
<groupId>org.apache.maven.plugins</groupId>
35-
<artifactId>maven-shade-plugin</artifactId>
36-
<version>2.3</version>
37-
<executions>
38-
<execution>
39-
<phase>package</phase>
40-
<goals>
41-
<goal>shade</goal>
42-
</goals>
43-
<configuration>
44-
<minimizeJar>false</minimizeJar>
45-
<createDependencyReducedPom>true</createDependencyReducedPom>
46-
<dependencyReducedPomLocation>
47-
${java.io.tmpdir}/dependency-reduced-pom.xml
48-
</dependencyReducedPomLocation>
49-
</configuration>
50-
</execution>
51-
</executions>
52-
</plugin>
53-
<plugin>
54-
<groupId>org.apache.maven.plugins</groupId>
55-
<artifactId>maven-shade-plugin</artifactId>
56-
<version>2.3</version>
57-
<executions>
58-
<execution>
59-
<phase>package</phase>
60-
<goals>
61-
<goal>shade</goal>
62-
</goals>
63-
<configuration>
64-
<transformers>
65-
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
66-
</transformers>
67-
</configuration>
68-
</execution>
69-
</executions>
70-
</plugin>
71-
</plugins>
72-
</build>
73-
<dependencies>
74-
<dependency>
75-
<groupId>com.wordnik</groupId>
76-
<artifactId>swagger-codegen</artifactId>
77-
<version>${project.parent.version}</version>
78-
</dependency>
79-
</dependencies>
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<parent>
4+
<groupId>com.wordnik</groupId>
5+
<artifactId>swagger-codegen-project</artifactId>
6+
<version>2.1.3-M1-SNAPSHOT</version>
7+
<relativePath>../..</relativePath>
8+
</parent>
9+
<modelVersion>4.0.0</modelVersion>
10+
11+
<artifactId>swagger-codegen-distribution</artifactId>
12+
<packaging>jar</packaging>
13+
14+
<name>swagger-codegen (executable)</name>
15+
16+
<build>
17+
<finalName>swagger-cli</finalName>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.apache.maven.plugins</groupId>
21+
<artifactId>maven-jar-plugin</artifactId>
22+
<configuration>
23+
<archive>
24+
<manifest>
25+
<mainClass>com.wordnik.swagger.codegen.SwaggerCodegen</mainClass>
26+
</manifest>
27+
</archive>
28+
</configuration>
29+
</plugin>
30+
<plugin>
31+
<groupId>org.apache.maven.plugins</groupId>
32+
<artifactId>maven-shade-plugin</artifactId>
33+
<version>2.3</version>
34+
<executions>
35+
<execution>
36+
<id>reduced-pom</id>
37+
<phase>package</phase>
38+
<goals>
39+
<goal>shade</goal>
40+
</goals>
41+
<configuration>
42+
<minimizeJar>false</minimizeJar>
43+
<createDependencyReducedPom>true</createDependencyReducedPom>
44+
<dependencyReducedPomLocation>
45+
${java.io.tmpdir}/dependency-reduced-pom.xml
46+
</dependencyReducedPomLocation>
47+
</configuration>
48+
</execution>
49+
<execution>
50+
<id>process-resources</id>
51+
<phase>package</phase>
52+
<goals>
53+
<goal>shade</goal>
54+
</goals>
55+
<configuration>
56+
<transformers>
57+
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
58+
</transformers>
59+
</configuration>
60+
</execution>
61+
</executions>
62+
</plugin>
63+
</plugins>
64+
</build>
65+
66+
<dependencies>
67+
68+
<dependency>
69+
<groupId>com.wordnik</groupId>
70+
<artifactId>swagger-codegen</artifactId>
71+
<version>${project.version}</version>
72+
</dependency>
73+
74+
<!--https://github.com/airlift/airline-->
75+
<dependency>
76+
<groupId>io.airlift</groupId>
77+
<artifactId>airline</artifactId>
78+
<version>0.7</version>
79+
</dependency>
80+
81+
<dependency>
82+
<groupId>com.googlecode.lambdaj</groupId>
83+
<artifactId>lambdaj</artifactId>
84+
<version>2.3.3</version>
85+
</dependency>
86+
87+
<dependency>
88+
<groupId>org.slf4j</groupId>
89+
<artifactId>slf4j-simple</artifactId>
90+
<version>${slf4j-version}</version>
91+
</dependency>
92+
93+
</dependencies>
94+
8095
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.wordnik.swagger.codegen;
2+
3+
import com.wordnik.swagger.codegen.cmd.Generate;
4+
import com.wordnik.swagger.codegen.cmd.Langs;
5+
import com.wordnik.swagger.codegen.cmd.Meta;
6+
import io.airlift.airline.Cli;
7+
import io.airlift.airline.Help;
8+
9+
/**
10+
* User: lanwen
11+
* Date: 24.03.15
12+
* Time: 17:56
13+
*/
14+
public class SwaggerCodegen {
15+
16+
17+
public static void main(String[] args) {
18+
Cli.CliBuilder<Runnable> builder = Cli.<Runnable>builder("swagger")
19+
.withDescription("Swagger code generator CLI. More info on swagger.io")
20+
.withDefaultCommand(Langs.class)
21+
.withCommands(
22+
Generate.class,
23+
Meta.class,
24+
Langs.class,
25+
Help.class
26+
);
27+
28+
builder.build().parse(args).run();
29+
}
30+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.wordnik.swagger.codegen.cmd;
2+
3+
import com.wordnik.swagger.codegen.ClientOptInput;
4+
import com.wordnik.swagger.codegen.ClientOpts;
5+
import com.wordnik.swagger.codegen.CodegenConfig;
6+
import com.wordnik.swagger.codegen.DefaultGenerator;
7+
import com.wordnik.swagger.models.Swagger;
8+
import io.airlift.airline.Command;
9+
import io.airlift.airline.Option;
10+
import io.swagger.parser.SwaggerParser;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
14+
import java.io.File;
15+
import java.util.ServiceLoader;
16+
17+
import static java.util.ServiceLoader.load;
18+
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
19+
20+
/**
21+
* User: lanwen
22+
* Date: 24.03.15
23+
* Time: 20:22
24+
*/
25+
26+
@Command(name = "generate", description = "Generate code with chosen lang")
27+
public class Generate implements Runnable {
28+
29+
public static final Logger LOG = LoggerFactory.getLogger(Generate.class);
30+
31+
public static final String TEMPLATE_DIR_PARAM = "templateDir";
32+
33+
@Option(name = {"-v", "--verbose"}, description = "verbose mode")
34+
public boolean verbose;
35+
36+
@Option(name = {"-l", "--lang"}, title = "language", required = true,
37+
description = "client language to generate (maybe class name in classpath, required)")
38+
public String lang;
39+
40+
@Option(name = {"-o", "--output"}, title = "output directory",
41+
description = "where to write the generated files (current dir by default)")
42+
public String output = "";
43+
44+
@Option(name = {"-i", "--input-spec"}, title = "spec file", required = true,
45+
description = "location of the swagger spec, as URL or file (required)")
46+
public String spec;
47+
48+
@Option(name = {"-t", "--template-dir"}, title = "template directory",
49+
description = "folder containing the template files")
50+
public String templateDir;
51+
52+
@Option(name = {"-a", "--auth"}, title = "authorization",
53+
description = "adds authorization headers when fetching the swagger definitions remotely. " +
54+
"Pass in a URL-encoded string of name:header with a comma separating multiple values")
55+
public String auth;
56+
57+
@Override
58+
public void run() {
59+
verbosed(verbose);
60+
61+
ClientOptInput input = new ClientOptInput();
62+
63+
if (isNotEmpty(auth)) {
64+
input.setAuth(auth);
65+
}
66+
67+
CodegenConfig config = forName(lang);
68+
config.setOutputDir(new File(output).getAbsolutePath());
69+
70+
if (null != templateDir) {
71+
config.additionalProperties().put(TEMPLATE_DIR_PARAM, new File(templateDir).getAbsolutePath());
72+
}
73+
74+
input.setConfig(config);
75+
76+
Swagger swagger = new SwaggerParser().read(spec, input.getAuthorizationValues(), true);
77+
new DefaultGenerator().opts(input.opts(new ClientOpts()).swagger(swagger)).generate();
78+
}
79+
80+
/**
81+
* If true parameter, adds system properties which enables debug mode in generator
82+
* @param verbose - if true, enables debug mode
83+
*/
84+
private void verbosed(boolean verbose) {
85+
if (!verbose) {
86+
return;
87+
}
88+
LOG.info("\nVERBOSE MODE: ON. Additional debug options are injected" +
89+
"\n - [debugSwagger] prints the swagger specification as interpreted by the codegen" +
90+
"\n - [debugModels] prints models passed to the template engine" +
91+
"\n - [debugOperations] prints operations passed to the template engine" +
92+
"\n - [debugSupportingFiles] prints additional data passed to the template engine");
93+
94+
System.setProperty("debugSwagger", "");
95+
System.setProperty("debugModels", "");
96+
System.setProperty("debugOperations", "");
97+
System.setProperty("debugSupportingFiles", "");
98+
}
99+
100+
/**
101+
* Tries to load config class with SPI first, then with class name directly from classpath
102+
* @param name name of config, or full qualified class name in classpath
103+
* @return config class
104+
*/
105+
private static CodegenConfig forName(String name) {
106+
ServiceLoader<CodegenConfig> loader = load(CodegenConfig.class);
107+
for (CodegenConfig config : loader) {
108+
if (config.getName().equals(name)) {
109+
return config;
110+
}
111+
}
112+
113+
// else try to load directly
114+
try {
115+
return (CodegenConfig) Class.forName(name).newInstance();
116+
} catch (Exception e) {
117+
throw new RuntimeException("Can't load config class with name ".concat(name), e);
118+
}
119+
}
120+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.wordnik.swagger.codegen.cmd;
2+
3+
import ch.lambdaj.collection.LambdaIterable;
4+
import com.wordnik.swagger.codegen.CodegenConfig;
5+
import io.airlift.airline.Command;
6+
7+
import static ch.lambdaj.Lambda.on;
8+
import static ch.lambdaj.collection.LambdaCollections.with;
9+
import static java.util.ServiceLoader.load;
10+
11+
/**
12+
* User: lanwen
13+
* Date: 24.03.15
14+
* Time: 20:25
15+
*/
16+
@Command(name = "langs", description = "Shows available langs")
17+
public class Langs implements Runnable {
18+
@Override
19+
public void run() {
20+
LambdaIterable<String> langs = with(load(CodegenConfig.class)).extract(on(CodegenConfig.class).getName());
21+
System.out.printf("Available languages: %s%n", langs);
22+
}
23+
}

0 commit comments

Comments
 (0)