@@ -6,7 +6,7 @@ import * as program from "commander";
6
6
import * as fs from "fs-extra" ;
7
7
import * as _ from "lodash" ;
8
8
import * as pathUtils from "path" ;
9
- import { BlankLine , Braces , Class , FnCall , Null , Return } from "./JavaCode" ;
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" ;
@@ -32,6 +32,13 @@ if (program.args.length === 0) {
32
32
program . help ( ) ;
33
33
}
34
34
35
+ enum ReturnValue {
36
+ Success,
37
+ ParseError,
38
+ CreateJavaError,
39
+ OutputFolderNotFound,
40
+ OutputError,
41
+ }
35
42
36
43
async function transformConfigFile ( fileName : string , options : any ) {
37
44
let fileNames = [ fileName ] ;
@@ -41,51 +48,73 @@ async function transformConfigFile(fileName: string, options: any) {
41
48
await parseSpringXmlConfigFile ( fileNames ) ;
42
49
} catch ( err ) {
43
50
console . error ( `Error parsing config file: ${ err . message } ` ) ;
44
- process . exit ( 2 ) ;
51
+ process . exit ( ReturnValue . ParseError ) ;
45
52
return ;
46
53
}
47
54
const packageName = "org.springframework.context.support" ;
48
55
let output = `package ${ packageName } ;\n\n` ;
49
- const applicationContext =
50
- new Class (
51
- "public" , "ClassPathXmlApplicationContext" , undefined , [ "org.springframework.context.ApplicationContext" ] ,
52
- [
53
- new Braces (
54
- "public ClassPathXmlApplicationContext(String[] args)" ,
55
- [ ]
56
- ) ,
57
- new Braces (
58
- "public Object getBean(String name)" ,
59
- [
60
- new Braces (
61
- "try" ,
62
- model . Bean . getNamed ( ) . map ( ( bean ) =>
63
- new Braces (
64
- `if (name.equals("${ bean . name } "))` ,
65
- [
66
- new Return ( new FnCall ( bean . getter , [ ] ) ) ,
67
- ] ) ) ,
68
- ) ,
69
- new Braces (
70
- "catch(Exception ex)" ,
71
- [ ] ) ,
72
- new Return ( new Null ( ) ) ,
73
- ]
74
- ) ,
75
- new BlankLine ( ) ,
76
- new BlankLine ( ) ,
77
- ..._ . flatMap ( [ ...model . Bean . getAll ( ) ] , ( bean ) => bean . toJava ( ) [ 0 ] ) ,
78
- new BlankLine ( ) ,
79
- ...model . PropertiesValue . utilityMethods ( ) ,
80
- ...model . BeanValue . utilityMethods ( ) ,
81
- ] ) ;
82
- 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
+ }
83
112
// Write out the generated Java
84
113
let outputPath = options . outputPath ;
85
114
if ( outputPath ) {
86
115
if ( ! fs . existsSync ( outputPath ) ) {
87
116
console . error ( `Given output folder '${ outputPath } ' does not exist` ) ;
88
- process . exit ( 5 ) ;
117
+ process . exit ( ReturnValue . OutputFolderNotFound ) ;
89
118
return ;
90
119
}
91
120
// Create folder to store it in based on the package name
@@ -94,7 +123,7 @@ async function transformConfigFile(fileName: string, options: any) {
94
123
outputPath = createPath ( appContextFolder , outputPath ) ;
95
124
} catch ( err ) {
96
125
console . error ( `Can't create output folder '${ appContextFolder } ': ${ err . message } ` ) ;
97
- process . exit ( 3 ) ;
126
+ process . exit ( ReturnValue . OutputError ) ;
98
127
return ;
99
128
}
100
129
// Write file
@@ -104,7 +133,7 @@ async function transformConfigFile(fileName: string, options: any) {
104
133
console . log ( "Output written to %s" , outputFileName ) ;
105
134
} catch ( err ) {
106
135
console . error ( `Can't write output file '${ outputFileName } ': ${ err . message } ` ) ;
107
- process . exit ( 4 ) ;
136
+ process . exit ( ReturnValue . OutputError ) ;
108
137
return ;
109
138
}
110
139
} else
0 commit comments