1
1
package io .swagger .codegen .cmd ;
2
2
3
- import ch .lambdaj .function .convert .Converter ;
4
3
import com .google .common .base .CaseFormat ;
5
4
import com .google .common .collect .ImmutableList ;
6
5
import com .google .common .collect .ImmutableMap ;
17
16
import java .util .List ;
18
17
import java .util .Map ;
19
18
20
- import static ch .lambdaj .collection .LambdaCollections .with ;
21
- import static com .google .common .base .Joiner .on ;
22
-
23
19
/**
24
20
* User: lanwen Date: 24.03.15 Time: 20:22
25
21
*/
26
22
public class Meta implements Runnable {
27
23
28
24
private static final Logger LOGGER = LoggerFactory .getLogger (Meta .class );
29
25
30
- private static final String TEMPLATE_DIR_CLASSPATH = "codegen" ;
26
+ private static final String TEMPLATE_DIR_CLASSPATH = "v2/ codegen" ;
31
27
private static final String MUSTACHE_EXTENSION = ".mustache" ;
32
28
33
29
private String outputFolder = "" ;
@@ -56,27 +52,28 @@ public void run() {
56
52
List <SupportingFile > supportingFiles =
57
53
ImmutableList
58
54
.of (new SupportingFile ("pom.mustache" , "" , "pom.xml" ),
59
- new SupportingFile ("generatorClass.mustache" , on (File .separator )
60
- .join ("src/main/java" , asPath (targetPackage )), mainClass
61
- .concat (".java" )), new SupportingFile ("README.mustache" ,
62
- "" , "README.md" ), new SupportingFile ("api.template" ,
63
- "src/main/resources" + File .separator + name ,
64
- "api.mustache" ), new SupportingFile ("model.template" ,
65
- "src/main/resources" + File .separator + name ,
66
- "model.mustache" ), new SupportingFile ("services.mustache" ,
67
- "src/main/resources/META-INF/services" ,
68
- "io.swagger.codegen.CodegenConfig" ));
69
-
70
- String swaggerVersion = Version .readVersionFromResources ();
55
+ new SupportingFile ("generatorClass.mustache" ,
56
+ String .join (File .separator , "src" , "main" , "java" , asPath (targetPackage )),
57
+ mainClass .concat (".java" )),
58
+ new SupportingFile ("README.mustache" , "" , "README.md" ),
59
+ new SupportingFile ("api.template" , String .join (File .separator , "src" , "main" , "resources" , name ), "api.mustache" ),
60
+ new SupportingFile ("model.template" , String .join (File .separator , "src" , "main" , "resources" , name ), "model.mustache" ),
61
+ new SupportingFile ("myFile.template" , String .join (File .separator , "src" , "main" , "resources" , name ), "myFile.mustache" ),
62
+ new SupportingFile ("services.mustache" , String .join (File .separator , "src" , "main" , "resources" , "META-INF" , "services" ), "io.swagger.codegen.CodegenConfig" ));
63
+
64
+ String swaggerCodegenVersion = Version .readVersionFromResources (Version .SWAGGER_CODEGEN_VERSION );
65
+ String swaggerCodegenGeneratorsVersion = Version .readVersionFromResources (Version .SWAGGER_CODEGEN_GENERATORS_VERSION );
71
66
72
67
Map <String , Object > data =
73
68
new ImmutableMap .Builder <String , Object >().put ("generatorPackage" , targetPackage )
74
69
.put ("generatorClass" , mainClass ).put ("name" , name )
75
70
.put ("fullyQualifiedGeneratorClass" , targetPackage + "." + mainClass )
76
- .put ("swaggerCodegenVersion" , swaggerVersion ).build ();
77
-
71
+ .put ("swaggerCodegenVersion" , swaggerCodegenVersion )
72
+ .put ("swaggerCodegenGeneratorsVersion" , swaggerCodegenGeneratorsVersion )
73
+ .build ();
78
74
79
- with (supportingFiles ).convert (processFiles (targetDir , data ));
75
+ DefaultGenerator generator = new DefaultGenerator ();
76
+ supportingFiles .stream ().forEach (f -> processFiles (generator , f , targetDir , data ));
80
77
}
81
78
82
79
/**
@@ -87,40 +84,27 @@ public void run() {
87
84
* @param data - map with additional params needed to process templates
88
85
* @return converter object to pass to lambdaj
89
86
*/
90
- private static Converter < SupportingFile , File > processFiles (final File targetDir ,
91
- final Map < String , Object > data ) {
92
- return new Converter < SupportingFile , File >() {
93
- private DefaultGenerator generator = new DefaultGenerator ( );
87
+ private static void processFiles (DefaultGenerator generator , SupportingFile support , final File targetDir , final Map < String , Object > data ) {
88
+ try {
89
+ File destinationFolder = new File ( new File ( targetDir . getAbsolutePath ()), support . folder );
90
+ File outputFile = new File ( destinationFolder , support . destinationFilename );
94
91
95
- @ Override
96
- public File convert (SupportingFile support ) {
97
- try {
98
- File destinationFolder =
99
- new File (new File (targetDir .getAbsolutePath ()), support .folder );
100
- File outputFile = new File (destinationFolder , support .destinationFilename );
101
-
102
- String template =
103
- generator .readTemplate (new File (TEMPLATE_DIR_CLASSPATH ,
104
- support .templateFile ).getPath ());
105
- String formatted = template ;
106
-
107
- if (support .templateFile .endsWith (MUSTACHE_EXTENSION )) {
108
- LOGGER .info ("writing file to {}" , outputFile .getAbsolutePath ());
109
- formatted =
110
- Mustache .compiler ().withLoader (loader (generator )).defaultValue ("" )
111
- .compile (template ).execute (data );
112
- } else {
113
- LOGGER .info ("copying file to {}" , outputFile .getAbsolutePath ());
114
- }
115
-
116
- FileUtils .writeStringToFile (outputFile , formatted );
117
- return outputFile ;
118
-
119
- } catch (IOException e ) {
120
- throw new RuntimeException ("Can't generate project" , e );
121
- }
92
+ String template = generator .readTemplate (new File (TEMPLATE_DIR_CLASSPATH , support .templateFile ).getPath ());
93
+ String formatted = template ;
94
+
95
+ if (support .templateFile .endsWith (MUSTACHE_EXTENSION )) {
96
+ LOGGER .info ("writing file to {}" , outputFile .getAbsolutePath ());
97
+ formatted = Mustache .compiler ().withLoader (loader (generator )).defaultValue ("" ).compile (template ).execute (data );
122
98
}
123
- };
99
+ else {
100
+ LOGGER .info ("copying file to {}" , outputFile .getAbsolutePath ());
101
+ }
102
+
103
+ FileUtils .writeStringToFile (outputFile , formatted );
104
+ }
105
+ catch (IOException e ) {
106
+ throw new RuntimeException ("Can't generate project" , e );
107
+ }
124
108
}
125
109
126
110
/**
0 commit comments