4
4
5
5
import * as program from "commander" ;
6
6
import * as fs from "fs-extra" ;
7
- import * as _ from "lodash" ;
8
7
import * as pathUtils from "path" ;
9
- import { BlankLine , CatchBlock , Class , FnCall , IfThenElse , Method , Null , Return , Statement , StringConstant , TryCatch } from "./javaCode " ;
10
- import * as model from "./model " ;
11
- import parseSpringConfigFiles from "./parserDriver" ;
8
+ import { processEntryPointConfigAsync } from "./createEntryPoint " ;
9
+ import { Class } from "./javaCode " ;
10
+ import parseSpringConfigFiles , { createApplicationContextClass } from "./parserDriver" ;
12
11
import { createPath } from "./utils" ;
13
12
14
13
function collectOption ( value : string , collection ?: string [ ] ) {
@@ -21,7 +20,8 @@ function collectOption(value: string, collection?: string[]) {
21
20
program
22
21
. version ( "0.0.1" )
23
22
. arguments ( "<input-file>" )
24
- . option ( "-i --input-file <path>" , "Additional DI configuration file to read" , collectOption )
23
+ . option ( "-i --di-input-file <path>" , "Additional DI configuration file to read" , collectOption )
24
+ . option ( "-e --entry-points-input-file <path>" , "Entry point configuration file to read" )
25
25
. option ( "-o --output-path <output-path>" , "Path to write output under" )
26
26
. description ( "Create code from DI configuration" )
27
27
. action ( transformConfigFiles ) ;
@@ -42,100 +42,63 @@ enum ReturnValue {
42
42
43
43
async function transformConfigFiles ( fileName : string , options : any ) {
44
44
let fileNames = [ fileName ] ;
45
- if ( options . inputFile )
46
- fileNames = fileNames . concat ( options . inputFile ) ;
45
+ if ( options . diInputFile )
46
+ fileNames = fileNames . concat ( options . diInputFile ) ;
47
47
try {
48
48
await parseSpringConfigFiles ( fileNames ) ;
49
49
} catch ( err ) {
50
50
console . error ( `Error parsing config file: ${ err . message } ` ) ;
51
51
process . exit ( ReturnValue . ParseError ) ;
52
52
return ;
53
53
}
54
- const packageName = "org.springframework.context.support" ;
55
- let output = `package ${ packageName } ;\n\n` ;
54
+ let applicationContext ;
56
55
try {
57
- const applicationContext =
58
- new Class (
59
- "public" , "ClassPathXmlApplicationContext" , undefined , [ "org.springframework.context.ApplicationContext" ] ,
60
- [
61
- new Method (
62
- "public ClassPathXmlApplicationContext(String[] args)" ,
63
- [ ] ) ,
64
- new Method (
65
- "public Object getBean(String name)" ,
66
- [
67
- new TryCatch (
68
- ( < Statement [ ] > [ ] ) . concat (
69
- model . Repository . getNamed ( )
70
- . sort ( ( a , b ) => a . name < b . name ? - 1 : a . name === b . name ? 0 : 1 )
71
- . map ( ( bean ) =>
72
- new IfThenElse (
73
- new FnCall ( "name.equals" , [ new StringConstant ( bean . name ) ] ) ,
74
- [
75
- new Return ( new FnCall ( bean . getter , [ ] ) ) ,
76
- ] ) ) ,
77
- [ ...model . Repository . getAliases ( ) ] . map (
78
- function ( [ alias , beanId ] ) {
79
- const bean = model . Repository . tryGet ( beanId ) ;
80
- if ( bean === undefined )
81
- throw new Error ( "Found alias to bean that doesn't exist" ) ;
82
- return new IfThenElse (
83
- new FnCall ( "name.equals" , [ new StringConstant ( alias ) ] ) ,
84
- [
85
- new Return ( new FnCall ( bean . getter , [ ] ) ) ,
86
- ] ) ;
87
- } ) ,
88
- ) ,
89
- [ new CatchBlock ( "Exception" ) ] ) ,
90
- new Return ( new Null ( ) ) ,
91
- ] ,
92
- [ ] ,
93
- [ "OverlayMethodImplementation" ]
94
- ) ,
95
- new BlankLine ( ) ,
96
- new BlankLine ( ) ,
97
- ..._ . flatMap ( [ ...model . Repository . getAll ( ) ] , ( bean ) => bean . javaMembers ) ,
98
- new BlankLine ( ) ,
99
- ...model . PropertiesValue . utilityMethods ( ) ,
100
- ...model . MapValue . utilityMethods ( ) ,
101
- ...model . BeanValue . utilityMethods ( ) ,
102
- ] ,
103
- [ "OverlayClassImplementation" ] ) ;
104
- output += applicationContext . toString ( ) + "\n" ;
56
+ applicationContext = createApplicationContextClass ( ) ;
105
57
} catch ( err ) {
106
58
console . error ( `Error converting to Java: ${ err . message } ` ) ;
107
59
process . exit ( ReturnValue . CreateJavaError ) ;
108
60
return ;
109
61
}
62
+
63
+ if ( options . outputPath && ! fs . existsSync ( options . outputPath ) ) {
64
+ console . error ( `Given output folder '${ options . outputPath } ' does not exist` ) ;
65
+ process . exit ( ReturnValue . OutputFolderNotFound ) ;
66
+ return ;
67
+ }
68
+
110
69
// Write out the generated Java
111
- let outputPath = options . outputPath ;
112
- if ( outputPath ) {
113
- if ( ! fs . existsSync ( outputPath ) ) {
114
- console . error ( `Given output folder '${ outputPath } ' does not exist` ) ;
115
- process . exit ( ReturnValue . OutputFolderNotFound ) ;
116
- return ;
117
- }
118
- // Create folder to store it in based on the package name
119
- const appContextFolder = pathUtils . join ( ...packageName . split ( "." ) ) ;
70
+ await saveGeneratedFile ( applicationContext , options ) ;
71
+
72
+ if ( options . entryPointsInputFile ) {
73
+ await saveGeneratedFile ( await processEntryPointConfigAsync ( options . entryPointsInputFile ) , options ) ;
74
+ }
75
+
76
+ // Success
77
+ process . exit ( ) ;
78
+ }
79
+
80
+ async function saveGeneratedFile ( saveableType : Class , options : any ) {
81
+ if ( options . outputPath ) {
82
+ let outputPath ;
120
83
try {
121
- outputPath = createPath ( appContextFolder , outputPath ) ;
84
+ outputPath = createPath ( saveableType . folderPath , options . outputPath ) ;
122
85
} catch ( err ) {
123
- console . error ( `Can't create output folder '${ appContextFolder } ': ${ err . message } ` ) ;
86
+ console . error ( `Can't create output folder '${ saveableType . folderPath } ': ${ err . message } ` ) ;
124
87
process . exit ( ReturnValue . OutputError ) ;
125
88
return ;
126
89
}
90
+
127
91
// Write file
128
- const outputFileName = pathUtils . resolve ( outputPath , "ClassPathXmlApplicationContext.java" ) ;
92
+ const outputFileName = pathUtils . resolve ( outputPath , saveableType . fileName ) ;
129
93
try {
130
- await fs . writeFile ( outputFileName , output , "utf8" ) ;
131
- console . log ( "Output written to %s" , outputFileName ) ;
94
+ await fs . writeFile ( outputFileName , saveableType . toString ( ) , "utf8" ) ;
95
+ console . log ( `Wrote ${ outputFileName } ` ) ;
132
96
} catch ( err ) {
133
97
console . error ( `Can't write output file '${ outputFileName } ': ${ err . message } ` ) ;
134
98
process . exit ( ReturnValue . OutputError ) ;
135
99
return ;
136
100
}
137
- } else
138
- console . log ( output ) ;
139
- // Success
140
- process . exit ( ) ;
101
+ } else {
102
+ console . log ( saveableType . toString ( ) ) ;
103
+ }
141
104
}
0 commit comments