5
5
import * as program from "commander" ;
6
6
import * as fs from "fs-extra" ;
7
7
import * as _ from "lodash" ;
8
- import * as path from "path" ;
9
- import { BlankLine , Braces , Class , FnCall , Return , Symbol } from "./JavaCode" ;
8
+ import * as pathUtils from "path" ;
9
+ import { BlankLine , Block , Class , FnCall , Method , Null , Return , Statement } from "./JavaCode" ;
10
10
import * as model from "./model" ;
11
11
import parseSpringXmlConfigFile from "./parse-spring-xml" ;
12
12
import { createPath } from "./utils" ;
13
13
14
- function collectOption ( value : string , collection : string [ ] ) {
15
- if ( ! collection )
14
+ function collectOption ( value : string , collection ? : string [ ] ) {
15
+ if ( collection === undefined )
16
16
collection = [ ] ;
17
17
collection . push ( value ) ;
18
18
return collection ;
@@ -23,18 +23,22 @@ program
23
23
. arguments ( "<input-file>" )
24
24
. option ( "-i --input-file <path>" , "Additional DI configuration file to read" , collectOption )
25
25
. option ( "-o --output-path <output-path>" , "Path to write output under" )
26
- . option ( "--verbose" )
27
26
. description ( "Create code from DI configuration" )
28
- . action ( function ( inputFile : string , options ) {
29
- transformConfigFile ( inputFile , options ) ;
30
- } ) ;
27
+ . action ( transformConfigFile ) ;
31
28
program . parse ( process . argv ) ;
32
29
33
30
// If program was called with no arguments then show help.
34
31
if ( program . args . length === 0 ) {
35
32
program . help ( ) ;
36
33
}
37
34
35
+ enum ReturnValue {
36
+ Success,
37
+ ParseError,
38
+ CreateJavaError,
39
+ OutputFolderNotFound,
40
+ OutputError,
41
+ }
38
42
39
43
async function transformConfigFile ( fileName : string , options : any ) {
40
44
let fileNames = [ fileName ] ;
@@ -43,71 +47,93 @@ async function transformConfigFile(fileName: string, options: any) {
43
47
try {
44
48
await parseSpringXmlConfigFile ( fileNames ) ;
45
49
} catch ( err ) {
46
- console . error ( err . message ) ;
47
- process . exit ( 2 ) ;
50
+ console . error ( `Error parsing config file: ${ err . message } ` ) ;
51
+ process . exit ( ReturnValue . ParseError ) ;
48
52
return ;
49
53
}
50
54
const packageName = "org.springframework.context.support" ;
51
55
let output = `package ${ packageName } ;\n\n` ;
52
- const applicationContext =
53
- new Class (
54
- "public" , "ClassPathXmlApplicationContext" ,
55
- [
56
- new Braces (
57
- "public ClassPathXmlApplicationContext(String[] args)" ,
58
- [ ]
59
- ) ,
60
- new Braces (
61
- "public Object getBean(String name)" ,
62
- [
63
- new Braces (
64
- "try" ,
65
- model . Bean . getNamed ( ) . map ( ( bean ) =>
66
- new Braces (
67
- `if (name.equals("${ bean . name } "))` ,
68
- [
69
- new Return ( new FnCall ( bean . getter , [ ] ) ) ,
70
- ] ) ) ,
71
- ) ,
72
- new Braces (
73
- "catch(Exception ex)" ,
74
- [ ] ) ,
75
- new Return ( new Symbol ( "null" ) ) ,
76
- ]
77
- ) ,
78
- new BlankLine ( ) ,
79
- new BlankLine ( ) ,
80
- ..._ . flatMap ( [ ...model . Bean . getAll ( ) ] , ( bean ) => bean . toJava ( ) [ 0 ] ) ,
81
- new BlankLine ( ) ,
82
- ...model . PropertiesValue . utilityMethods ( ) ,
83
- ...model . BeanValue . utilityMethods ( ) ,
84
- ] ) ;
85
- output += applicationContext . toString ( ) + "\n" ;
56
+ 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 Block (
68
+ "try" ,
69
+ ( < Statement [ ] > [ ] ) . concat (
70
+ model . Bean . getNamed ( ) . map ( ( bean ) =>
71
+ new Block (
72
+ `if (name.equals("${ bean . name } "))` ,
73
+ [
74
+ new Return ( new FnCall ( bean . getter , [ ] ) ) ,
75
+ ] ) ) ,
76
+ [ ...model . Bean . getAliases ( ) ] . map (
77
+ function ( [ alias , beanId ] ) {
78
+ const bean = model . Bean . tryGet ( beanId ) ;
79
+ if ( bean === undefined )
80
+ throw new Error ( "Found alias to bean that doesn't exist" ) ;
81
+ return new Block (
82
+ `if (name.equals("${ alias } "))` ,
83
+ [
84
+ new Return ( new FnCall ( bean . getter , [ ] ) ) ,
85
+ ] ) ;
86
+ } ) ,
87
+ ) ,
88
+ ) ,
89
+ new Block (
90
+ "catch(Exception ex)" ,
91
+ [ ] ) ,
92
+ new Return ( new Null ( ) ) ,
93
+ ] ,
94
+ [ ] ,
95
+ [ "OverlayMethodImplementation" ]
96
+ ) ,
97
+ new BlankLine ( ) ,
98
+ new BlankLine ( ) ,
99
+ ..._ . flatMap ( [ ...model . Bean . getAll ( ) ] , ( bean ) => bean . javaMembers ) ,
100
+ new BlankLine ( ) ,
101
+ ...model . PropertiesValue . utilityMethods ( ) ,
102
+ ...model . MapValue . utilityMethods ( ) ,
103
+ ...model . BeanValue . utilityMethods ( ) ,
104
+ ] ,
105
+ [ "OverlayClassImplementation" ] ) ;
106
+ output += applicationContext . toString ( ) + "\n" ;
107
+ } catch ( err ) {
108
+ console . error ( `Error converting to Java: ${ err . message } ` ) ;
109
+ process . exit ( ReturnValue . CreateJavaError ) ;
110
+ return ;
111
+ }
86
112
// Write out the generated Java
87
113
let outputPath = options . outputPath ;
88
114
if ( outputPath ) {
89
115
if ( ! fs . existsSync ( outputPath ) ) {
90
116
console . error ( `Given output folder '${ outputPath } ' does not exist` ) ;
91
- process . exit ( 5 ) ;
117
+ process . exit ( ReturnValue . OutputFolderNotFound ) ;
92
118
return ;
93
119
}
94
120
// Create folder to store it in based on the package name
95
- const appContextFolder = path . join ( ...packageName . split ( "." ) ) ;
121
+ const appContextFolder = pathUtils . join ( ...packageName . split ( "." ) ) ;
96
122
try {
97
123
outputPath = createPath ( appContextFolder , outputPath ) ;
98
124
} catch ( err ) {
99
125
console . error ( `Can't create output folder '${ appContextFolder } ': ${ err . message } ` ) ;
100
- process . exit ( 3 ) ;
126
+ process . exit ( ReturnValue . OutputError ) ;
101
127
return ;
102
128
}
103
129
// Write file
104
- const outputFileName = path . resolve ( outputPath , "ClassPathXmlApplicationContext.java" ) ;
130
+ const outputFileName = pathUtils . resolve ( outputPath , "ClassPathXmlApplicationContext.java" ) ;
105
131
try {
106
132
await fs . writeFile ( outputFileName , output , "utf8" ) ;
107
133
console . log ( "Output written to %s" , outputFileName ) ;
108
134
} catch ( err ) {
109
135
console . error ( `Can't write output file '${ outputFileName } ': ${ err . message } ` ) ;
110
- process . exit ( 4 ) ;
136
+ process . exit ( ReturnValue . OutputError ) ;
111
137
return ;
112
138
}
113
139
} else
0 commit comments