1
+ @groovy.transform.BaseScript com.ibm.dbb.groovy.ScriptLoader baseScript
2
+ import com.ibm.dbb.dependency.*
3
+ import com.ibm.dbb.build.*
4
+ import groovy.transform.*
5
+ import com.ibm.dbb.build.report.*
6
+ import com.ibm.dbb.build.report.records.*
7
+ import java.nio.file.* ;
8
+
9
+
10
+ // define script properties
11
+ @Field BuildProperties props = BuildProperties . getInstance()
12
+ @Field def buildUtils= loadScript(new File (" ${ props.zAppBuildDir} /utilities/BuildUtilities.groovy" ))
13
+
14
+ // verify required build properties
15
+ buildUtils. assertBuildProperties(props. zcee2_requiredBuildProperties)
16
+
17
+ // create updated build map, removing duplicates in case of PROJECT input Type
18
+ HashMap<String , String > updatedBuildMap = new HashMap<String , String > ()
19
+
20
+ println (" ** Streamlining the build list to remove duplicates" )
21
+ argMap. buildList. each { buildFile ->
22
+ PropertyMappings inputTypeMappings = new PropertyMappings (" zcee2_inputType" )
23
+ inputType = inputTypeMappings. getValue(buildFile)
24
+
25
+ if (inputType) {
26
+ if (inputType == " PROJECT" ) {
27
+ File changedBuildFile = new File (buildFile);
28
+ File projectDir = changedBuildFile. getParentFile()
29
+ boolean projectDirFound = false
30
+ while (projectDir != null && ! projectDirFound) {
31
+ File projectFile = new File (projectDir. getPath() + ' /.project' )
32
+ if (projectFile. exists()) {
33
+ projectDirFound = true
34
+ } else {
35
+ projectDir = projectDir. getParentFile()
36
+ }
37
+ }
38
+ if (projectDirFound) {
39
+ updatedBuildMap. put(projectDir. getPath(), " PROJECT" )
40
+ } else {
41
+ if (props. verbose) println (" !* No project directory found for file '${ buildFile} '. Skipping..." )
42
+ }
43
+ } else {
44
+ updatedBuildMap. put(buildFile, inputType);
45
+ }
46
+ } else {
47
+ println (" !* No Input Type mapping for file ${ buildFile} , skipping it..." )
48
+ }
49
+ }
50
+
51
+ println (" ** Building ${ updatedBuildMap.size()} API ${ updatedBuildMap.size() == 1 ? 'definition' : 'definitions'} mapped to ${ this.class.getName()} .groovy script" )
52
+ // sort the build list based on build file rank if provided
53
+ HashMap<String , String > sortedMap = buildUtils. sortBuildMap(updatedBuildMap, ' zcee2_fileBuildRank' )
54
+
55
+ int currentBuildFileNumber = 1
56
+
57
+ // iterate through build list
58
+ sortedMap. each { buildFile , inputType ->
59
+ println " *** (${ currentBuildFileNumber++} /${ sortedMap.size()} ) Building ${ inputType == "PROJECT" ? 'project' : 'properties file'} $buildFile "
60
+
61
+ String parameters = " "
62
+ String outputDir = " "
63
+ String outputFile = " "
64
+ if (inputType == " PROJECT" ) {
65
+ outputDir = " ${ props.buildOutDir} /zCEE2/$buildFile "
66
+ parameters = " -od $outputDir -pd $buildFile "
67
+ } else {
68
+ File changedBuildFile = new File (buildFile);
69
+ String outputFileName = changedBuildFile. getName(). split(" \\ ." )[0 ] + " ." + inputType. toLowerCase()
70
+ File projectDir = changedBuildFile. getParentFile()
71
+ outputFile = " ${ props.buildOutDir} /zCEE2/${ projectDir.getPath()} /${ outputFileName} "
72
+ outputDir = " ${ props.buildOutDir} /zCEE2/${ projectDir.getPath()} "
73
+ parameters = " -f $outputFile -p $buildFile "
74
+ }
75
+ File outputDirectory = new File (outputDir)
76
+ outputDirectory. mkdirs()
77
+
78
+
79
+ Properties ARAproperties = new Properties ()
80
+ File dataStructuresLocation
81
+ File apiInfoFileLocation
82
+ File logFileDirectory
83
+ if (inputType == " ARA" ) {
84
+ File ARApropertiesFile = new File (buildFile)
85
+ ARApropertiesFile . withInputStream {
86
+ ARAproperties . load(it)
87
+ }
88
+ println (" *** dataStructuresLocation: ${ ARAproperties.dataStructuresLocation} " )
89
+ println (" *** apiInfoFileLocation: ${ ARAproperties.apiInfoFileLocation} " )
90
+ println (" *** logFileDirectory: ${ ARAproperties.logFileDirectory} " )
91
+ dataStructuresLocation = new File (ARAproperties . dataStructuresLocation)
92
+ dataStructuresLocation. mkdirs()
93
+ apiInfoFileLocation = new File (ARAproperties . apiInfoFileLocation)
94
+ apiInfoFileLocation. mkdirs()
95
+ logFileDirectory = new File (ARAproperties . logFileDirectory)
96
+ logFileDirectory. mkdirs()
97
+ }
98
+
99
+
100
+ // log file - Changing slashes with dots to avoid conflicts
101
+ String logFilePath = buildFile. replace(" /" , " ." )
102
+ File logFile = new File (" ${ props.buildOutDir} /${ logFilePath} .zCEE2.log" )
103
+ if (logFile. exists())
104
+ logFile. delete()
105
+
106
+ String zconbtPath = props. getFileProperty(' zcee2_zconbtPath' , buildFile)
107
+
108
+ File zconbt = new File (zconbtPath)
109
+ if (! zconbt. exists()) {
110
+ def errorMsg = " *! zconbt wasn't find at location '$zconbtPath '"
111
+ println (errorMsg)
112
+ props. error = " true"
113
+ buildUtils. updateBuildResult(errorMsg :errorMsg)
114
+ } else {
115
+ String [] command;
116
+
117
+ command = [zconbtPath, parameters]
118
+ String commandString = command. join(" " )
119
+ if (props. verbose)
120
+ println (" ** Executing command '${ commandString} '..." )
121
+
122
+ StringBuffer shellOutput = new StringBuffer ()
123
+ StringBuffer shellError = new StringBuffer ()
124
+
125
+ String JAVA_HOME = props. getFileProperty(' zcee2_JAVA_HOME' , buildFile)
126
+
127
+ ProcessBuilder cmd = new ProcessBuilder (zconbtPath, parameters);
128
+ Map<String , String > env = cmd. environment();
129
+ env. put(" JAVA_HOME" , JAVA_HOME );
130
+ env. put(" PATH" , JAVA_HOME + " /bin" + " ;" + env. get(" PATH" ))
131
+ Process process = cmd. start()
132
+ process. consumeProcessOutput(shellOutput, shellError)
133
+ process. waitFor()
134
+ if (props. verbose)
135
+ println (" ** Exit value for the zconbt process: ${ process.exitValue()} " );
136
+
137
+ // write outputs to log file
138
+ String enc = props. logEncoding ?: ' IBM-1047'
139
+ logFile. withWriter(enc) { writer ->
140
+ writer. append(shellOutput)
141
+ writer. append(shellError)
142
+ }
143
+
144
+ if (process. exitValue() != 0 ) {
145
+ def errorMsg = " *! Error during the zconbt process"
146
+ println (errorMsg)
147
+ if (props. verbose)
148
+ println (" *! zconbt error message:\n ${ shellError} " )
149
+ props. error = " true"
150
+ buildUtils. updateBuildResult(errorMsg :errorMsg)
151
+ } else {
152
+ if (props. verbose)
153
+ println (" ** zconbt output:\n ${ shellOutput} " )
154
+
155
+ ArrayList<String > outputProperty = []
156
+ Path outputDirectoryPath = Paths . get(props. buildOutDir)
157
+ if (inputType == " PROJECT" ) {
158
+ String [] outputFiles = outputDirectory. list()
159
+ for (int i= 0 ; i< outputFiles. length; i++ ) {
160
+ Path outputFilePath = Paths . get(outputDir + " /" + outputFiles[i])
161
+ Path relativeOutputFilePath = outputDirectoryPath. relativize(outputFilePath)
162
+ outputProperty. add(" [${ props.buildOutDir} , ${ relativeOutputFilePath.toString()} , zCEE2]" )
163
+ }
164
+ } else {
165
+ Path outputFilePath = Paths . get(outputFile)
166
+ Path relativeOutputFilePath = outputDirectoryPath. relativize(outputFilePath)
167
+ outputProperty. add(" [${ props.buildOutDir} , ${ relativeOutputFilePath.toString()} , zCEE2]" )
168
+
169
+ if (inputType == " ARA" ) {
170
+ def ARA_PackageArtifacts = props. getFileProperty(' zcee2_ARA_PackageArtifacts' , buildFile)
171
+ if (ARA_PackageArtifacts && ARA_PackageArtifacts . toBoolean()) {
172
+
173
+ String [] outputFiles
174
+ Path finalOutputFilePath
175
+
176
+ outputFiles = dataStructuresLocation. list()
177
+ File dataStructuresLocationDir = new File (" ${ props.buildOutDir} /zCEE2/dataStructures" )
178
+ dataStructuresLocationDir. mkdirs()
179
+ for (int i= 0 ; i< outputFiles. length; i++ ) {
180
+ outputFilePath = Paths . get(dataStructuresLocation. getPath() + " /" + outputFiles[i])
181
+ finalOutputFilePath = Paths . get(dataStructuresLocationDir. getPath() + " /" + outputFiles[i])
182
+ Files . copy(outputFilePath, finalOutputFilePath, StandardCopyOption . COPY_ATTRIBUTES );
183
+ relativeOutputFilePath = outputDirectoryPath. relativize(finalOutputFilePath)
184
+ outputProperty. add(" [${ props.buildOutDir} , ${ relativeOutputFilePath.toString()} , zCEE2-Copy]" )
185
+ }
186
+ outputFiles = apiInfoFileLocation. list()
187
+ File apiInfoFileLocationDir = new File (" ${ props.buildOutDir} /zCEE2/apiInfoFiles" )
188
+ apiInfoFileLocationDir. mkdirs()
189
+ for (int i= 0 ; i< outputFiles. length; i++ ) {
190
+ outputFilePath = Paths . get(apiInfoFileLocation. getPath() + " /" + outputFiles[i])
191
+ finalOutputFilePath = Paths . get(apiInfoFileLocationDir. getPath() + " /" + outputFiles[i])
192
+ Files . copy(outputFilePath, finalOutputFilePath, StandardCopyOption . COPY_ATTRIBUTES );
193
+ relativeOutputFilePath = outputDirectoryPath. relativize(finalOutputFilePath)
194
+ outputProperty. add(" [${ props.buildOutDir} , ${ relativeOutputFilePath.toString()} , zCEE2-Info]" )
195
+ }
196
+ outputFiles = logFileDirectory. list()
197
+ File logFileDirectoryDir = new File (" ${ props.buildOutDir} /zCEE2/logs" )
198
+ logFileDirectoryDir. mkdirs()
199
+ for (int i= 0 ; i< outputFiles. length; i++ ) {
200
+ if (outputFiles[i]. endsWith(" .log" )) {
201
+ outputFilePath = Paths . get(logFileDirectory. getPath() + " /" + outputFiles[i])
202
+ finalOutputFilePath = Paths . get(logFileDirectoryDir. getPath() + " /" + outputFiles[i])
203
+ Files . copy(outputFilePath, finalOutputFilePath, StandardCopyOption . COPY_ATTRIBUTES );
204
+ relativeOutputFilePath = outputDirectoryPath. relativize(finalOutputFilePath)
205
+ outputProperty. add(" [${ props.buildOutDir} , ${ relativeOutputFilePath.toString()} , zCEE2-Log]" )
206
+ }
207
+ }
208
+ }
209
+ }
210
+ }
211
+ AnyTypeRecord zCEEWARRecord = new AnyTypeRecord (" USS_RECORD" )
212
+ zCEEWARRecord. setAttribute(" file" , buildFile)
213
+ zCEEWARRecord. setAttribute(" label" , " z/OS Connect EE OpenAPI 2 definition" )
214
+ zCEEWARRecord. setAttribute(" outputs" , outputProperty. join(" ;" ))
215
+ zCEEWARRecord. setAttribute(" command" , commandString);
216
+ BuildReportFactory . getBuildReport(). addRecord(zCEEWARRecord)
217
+ }
218
+ }
219
+ }
0 commit comments