11// vim: set ts=2 sw=2 sts=2 expandtab :
22const fs = require ( 'fs' ) ;
33const path = require ( 'path' ) ;
4- const ScsLib = require ( '../lib/scsLib .js' ) ;
5- const scsLib = new ScsLib ( ) ;
4+ const ApplicationModel = require ( '../lib/applicationModel .js' ) ;
5+ const applicationModel = new ApplicationModel ( 'post' ) ;
66// To enable debug logging, set the env var DEBUG="postProcess" with whatever things you want to see.
77const debugPostProcess = require ( 'debug' ) ( 'postProcess' ) ;
8-
98const sourceHead = '/src/main/java/' ;
109
1110module . exports = {
1211 'generate:after' : generator => {
1312 const asyncapi = generator . asyncapi ;
14- let sourcePath = generator . targetDir + sourceHead ;
15- const info = asyncapi . info ( ) ;
16- let javaPackage = generator . templateParams [ 'javaPackage' ] ;
17- const extensions = info . extensions ( ) ;
18- let overridePath ;
19-
20- if ( ! javaPackage && info && extensions ) {
21- javaPackage = extensions [ 'x-java-package' ] ;
22- }
13+ const sourcePath = generator . targetDir + sourceHead ;
2314
24- if ( javaPackage ) {
25- debugPostProcess ( `package: ${ javaPackage } ` ) ;
26- overridePath = `${ generator . targetDir + sourceHead + javaPackage . replace ( / \. / g, '/' ) } /` ;
27- }
28-
29- asyncapi . allSchemas ( ) . forEach ( ( value , key , map ) => {
30- processSchema ( key , value ) ;
31- } ) ;
15+ // NEW
3216
33- if ( javaPackage ) {
34- debugPostProcess ( `Moving files from ${ sourcePath } to ${ overridePath } ` ) ;
35- let first = true ;
36- fs . readdirSync ( sourcePath ) . forEach ( file => {
37- if ( ! fs . lstatSync ( path . resolve ( sourcePath , file ) ) . isDirectory ( ) ) {
38- if ( first ) {
39- first = false ;
40- debugPostProcess ( `Making ${ overridePath } ` ) ;
41- fs . mkdirSync ( overridePath , { recursive : true } ) ;
42- }
43-
44- debugPostProcess ( `Copying ${ file } ` ) ;
45- moveFile ( sourcePath , overridePath , file ) ;
46- }
47- } ) ;
48- sourcePath = overridePath ;
49- }
17+ const defaultJavaPackage = getDefaultJavaPackage ( generator ) ;
18+ const defaultJavaPackageDir = getDefaultJavaPackageDir ( generator , defaultJavaPackage ) ;
19+
20+ asyncapi . allSchemas ( ) . forEach ( ( schema , schemaName ) => {
21+ processSchema ( generator , schemaName , schema , sourcePath , defaultJavaPackageDir ) ;
22+ } ) ;
5023
5124 // Rename the pom file if necessary, and only include Application.java when an app is requested.
5225 const artifactType = generator . templateParams [ 'artifactType' ] ;
@@ -58,67 +31,104 @@ module.exports = {
5831 } else {
5932 fs . renameSync ( path . resolve ( generator . targetDir , 'pom.app' ) , path . resolve ( generator . targetDir , 'pom.xml' ) ) ;
6033 fs . unlinkSync ( path . resolve ( generator . targetDir , 'pom.lib' ) ) ;
34+ if ( defaultJavaPackageDir ) {
35+ moveFile ( sourcePath , defaultJavaPackageDir , 'Application.java' ) ;
36+ }
6137 }
38+ applicationModel . reset ( ) ; // Must clear its cache for when we run the jest tests.
39+ }
40+ } ;
6241
63- // This renames schema objects ensuring they're proper Java class names. It also removes files that are schemas of simple types.
42+ function getDefaultJavaPackage ( generator ) {
43+ const asyncapi = generator . asyncapi ;
44+ const info = asyncapi . info ( ) ;
45+ let javaPackage = generator . templateParams [ 'javaPackage' ] ;
46+ const extensions = info . extensions ( ) ;
6447
65- function processSchema ( schemaName , schema ) {
66- if ( schemaName . startsWith ( '<' ) ) {
67- debugPostProcess ( `found an anonymous schema ${ schemaName } ` ) ;
68- schemaName = schemaName . replace ( '<' , '' ) ;
69- schemaName = schemaName . replace ( '>' , '' ) ;
70- }
48+ if ( ! javaPackage && info && extensions ) {
49+ javaPackage = extensions [ 'x-java-package' ] ;
50+ }
7151
72- // First see if we need to move it to a different package based on its namespace.
73- // This mainly applies to Avro files which have the fully qualified name.
74- let newSourceDir = sourcePath ;
75- const generatedFileName = `${ schemaName } .java` ;
76- let desiredClassName = scsLib . getClassName ( schemaName ) ;
77-
78- const indexOfDot = schemaName . lastIndexOf ( '.' ) ;
79- if ( indexOfDot > 0 ) {
80- const newPackage = schemaName . substring ( 0 , indexOfDot ) ;
81- const className = schemaName . substring ( indexOfDot + 1 ) ;
82- debugPostProcess ( `package: ${ newPackage } className: ${ className } ` ) ;
83- newSourceDir = `${ generator . targetDir + sourceHead + newPackage . replace ( / \. / g, '/' ) } /` ;
84- moveFile ( sourcePath , newSourceDir , generatedFileName ) ;
85- desiredClassName = scsLib . getClassName ( className ) ;
86- }
52+ debugPostProcess ( `getDefaultJavaPackage: ${ javaPackage } ` ) ;
53+ return javaPackage ;
54+ }
8755
88- const oldPath = path . resolve ( newSourceDir , generatedFileName ) ;
89- debugPostProcess ( `old path: ${ oldPath } ` ) ;
90-
91- if ( fs . existsSync ( oldPath ) ) {
92- const schemaType = schema . type ( ) ;
93- debugPostProcess ( `Old path exists. schemaType: ${ schemaType } ` ) ;
94- if ( schemaType === 'object' || schemaType === 'enum' ) {
95- const javaName = scsLib . getClassName ( schemaName ) ;
96- debugPostProcess ( `desiredClassName: ${ desiredClassName } schemaName: ${ schemaName } ` ) ;
97-
98- if ( javaName !== schemaName ) {
99- const newPath = path . resolve ( newSourceDir , `${ desiredClassName } .java` ) ;
100- fs . renameSync ( oldPath , newPath ) ;
101- debugPostProcess ( `Renamed class file ${ schemaName } to ${ desiredClassName } ` ) ;
102- }
103- } else {
104- // In this case it's an anonymous schema for a primitive type or something.
105- debugPostProcess ( `deleting ${ oldPath } ` ) ;
106- fs . unlinkSync ( oldPath ) ;
107- }
108- }
56+ function getDefaultJavaPackageDir ( generator , defaultJavaPackage ) {
57+ let defaultPackageDir ;
58+
59+ if ( defaultJavaPackage ) {
60+ const packageDir = packageToPath ( defaultJavaPackage ) ;
61+ defaultPackageDir = `${ generator . targetDir } ${ sourceHead } ${ packageDir } ` ;
62+ }
63+
64+ debugPostProcess ( `getDefaultJavaPackageDir: ${ defaultPackageDir } ` ) ;
65+ return defaultPackageDir ;
66+ }
67+
68+ function packageToPath ( javaPackage ) {
69+ return javaPackage . replace ( / \. / g, '/' ) ;
70+ }
71+
72+ function processSchema ( generator , schemaName , schema , sourcePath , defaultJavaPackageDir ) {
73+ const fileName = getFileName ( schemaName ) ;
74+ const filePath = path . resolve ( sourcePath , fileName ) ;
75+ debugPostProcess ( `processSchema ${ schemaName } ` ) ;
76+ debugPostProcess ( schema ) ;
77+ if ( schema . type ( ) !== 'object' ) {
78+ debugPostProcess ( `deleting ${ filePath } ` ) ;
79+ fs . unlinkSync ( filePath ) ;
80+ } else {
81+ const modelClass = applicationModel . getModelClass ( schemaName ) ;
82+ const javaName = modelClass . getClassName ( ) ;
83+ const packageDir = getPackageDir ( generator , defaultJavaPackageDir , modelClass ) ;
84+ debugPostProcess ( `packageDir: ${ packageDir } ` ) ;
85+
86+ if ( packageDir ) {
87+ moveFile ( sourcePath , packageDir , fileName ) ;
10988 }
11089
111- function moveFile ( oldDirectory , newDirectory , fileName ) {
112- if ( ! fs . existsSync ( newDirectory ) ) {
113- fs . mkdirSync ( newDirectory , { recursive : true } ) ;
114- debugPostProcess ( `Made directory ${ newDirectory } ` ) ;
115- }
116- const oldPath = path . resolve ( oldDirectory , fileName ) ;
117- const newPath = path . resolve ( newDirectory , fileName ) ;
118- fs . copyFileSync ( oldPath , newPath ) ;
119- fs . unlinkSync ( oldPath ) ;
120- debugPostProcess ( `Moved ${ fileName } from ${ oldPath } to ${ newPath } ` ) ;
90+ debugPostProcess ( `javaName: ${ javaName } schemaName: ${ schemaName } ` ) ;
91+ if ( javaName !== schemaName ) {
92+ const currentPath = packageDir || sourcePath ;
93+ const newPath = path . resolve ( currentPath , `${ javaName } .java` ) ;
94+ const oldPath = path . resolve ( currentPath , fileName ) ;
95+ fs . renameSync ( oldPath , newPath ) ;
96+ debugPostProcess ( `Renamed class file ${ schemaName } to ${ javaName } ` ) ;
12197 }
12298 }
123- } ;
99+ }
100+
101+ function getFileName ( schemaName ) {
102+ const trimmedSchemaName = trimSchemaName ( schemaName ) ;
103+ return `${ trimmedSchemaName } .java` ;
104+ }
105+
106+ function trimSchemaName ( schemaName ) {
107+ let trimmedSchemaName = schemaName ;
108+ if ( schemaName . startsWith ( '<' ) ) {
109+ trimmedSchemaName = schemaName . replace ( '<' , '' ) ;
110+ trimmedSchemaName = trimmedSchemaName . replace ( / > $ / , '' ) ;
111+ }
112+ return trimmedSchemaName ;
113+ }
114+
115+ function getPackageDir ( generator , defaultJavaPackageDir , modelClass ) {
116+ const fileSpecificPackage = modelClass . getJavaPackage ( ) ;
117+ if ( fileSpecificPackage ) {
118+ const packagePath = packageToPath ( fileSpecificPackage ) ;
119+ return `${ generator . targetDir } ${ sourceHead } ${ packagePath } ` ;
120+ }
121+ return defaultJavaPackageDir ;
122+ }
124123
124+ function moveFile ( oldDirectory , newDirectory , fileName ) {
125+ if ( ! fs . existsSync ( newDirectory ) ) {
126+ fs . mkdirSync ( newDirectory , { recursive : true } ) ;
127+ debugPostProcess ( `Made directory ${ newDirectory } ` ) ;
128+ }
129+ const oldPath = path . resolve ( oldDirectory , fileName ) ;
130+ const newPath = path . resolve ( newDirectory , fileName ) ;
131+ fs . copyFileSync ( oldPath , newPath ) ;
132+ fs . unlinkSync ( oldPath ) ;
133+ debugPostProcess ( `Moved ${ fileName } from ${ oldPath } to ${ newPath } ` ) ;
134+ }
0 commit comments