forked from ballerina-platform/openapi-connectors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
287 lines (255 loc) · 11.4 KB
/
build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import org.ballerinax.openapi.validator.Utils
buildscript {
dependencies {
classpath files("libs/ballerinax-openapi-validator-1.0-SNAPSHOT-all.jar")
}
}
plugins {
id 'java-library'
id "com.github.spotbugs" version "${githubSpotbugsVersion}"
id "com.github.johnrengelman.shadow" version "${githubJohnrengelmanShadowVersion}"
id "de.undercouch.download" version "${underCouchDownloadVersion}"
id "net.researchgate.release" version "${researchgateReleaseVersion}"
}
apply plugin: 'jacoco'
apply plugin: 'maven-publish'
def ballerinaDistributionPath = System.getenv("BALLERINA_HOME")
List<String> ballerinaPackages = new ArrayList<>();
List<String> updatedBallerinaPackages = new ArrayList<>();
boolean release = false;
boolean remote = false;
boolean buildAll = false;
boolean testTool = false;
boolean testConnectorGeneration = false;
String clientMethod = "remote";
String customDistributionPath = null;
String connectorList = null;
if (project.hasProperty("remote")){
remote = new Boolean(project.property("remote").toString())
}
if (project.hasProperty("release")){
release = new Boolean(project.property("release").toString())
}
if (project.hasProperty("buildAll")){
buildAll = new Boolean(project.property("buildAll").toString())
}
if (project.hasProperty("testTool")){
testTool = new Boolean(project.property("testTool").toString())
}
if (project.hasProperty("testConnectorGeneration")){
testConnectorGeneration = new Boolean(project.property("testConnectorGeneration").toString())
}
if (project.hasProperty("clientMethod")){
clientMethod = new String(project.property("clientMethod").toString())
}
if (project.hasProperty("customDistributionPath")){
customDistributionPath = new String(project.property("customDistributionPath").toString())
}
if (project.hasProperty("connectorList")){
connectorList = new String(project.property("connectorList").toString())
}
String toolTestsDirPath = project.projectDir.absolutePath + "/tool-tests";
List<String> toolTestPackages = new ArrayList<>();
toolTestPackages = Utils.findBallerinaPackages(toolTestsDirPath);
Utils.loadOpenAPIProperties(project.projectDir.absolutePath)
String openApiPackageDirPath = project.projectDir.absolutePath + "/openapi";
updatedBallerinaPackages = Utils.findUpdatedBallerinaPackages(openApiPackageDirPath);
ballerinaPackages = Utils.findBallerinaPackages(openApiPackageDirPath);
if (buildAll){
updatedBallerinaPackages = ballerinaPackages;
}
task codeBuild {
if (!testTool && !testConnectorGeneration) {
println "Task: Building connectors..."
for (String path : updatedBallerinaPackages) {
Utils.executePrechecks(path);
exec {
commandLine 'sh', '-c', "${ballerinaDistributionPath}/bin/bal pack ${path}"
}
}
if (!release) {
println "Task: Pushing connectors to local..."
for (String path : updatedBallerinaPackages) {
try {
exec {
workingDir "${path}"
commandLine 'sh', '-c', "${ballerinaDistributionPath}/bin/bal push --repository=local"
}
}
catch (Exception ex) {
println "Failed to push connector [" + path + "] to local repository. Error : " + ex.toString();
}
}
}
}
}
task releaseConnector {
if(release){
println "Task: Pushing connectors to Ballerina Central..."
for (String path : updatedBallerinaPackages) {
try {
exec {
println "Pushing connector [" + path + "] to Ballerina Central"
workingDir "${path}"
commandLine 'sh', '-c', "${ballerinaDistributionPath}/bin/bal push"
}
} catch (Exception ex) {
println "Failed to push connector [" + path + "] to Ballerina Central. Error : " + ex.toString();
}
try {
Utils.bumpBallerinaTomlVersion(path);
} catch (Exception ex) {
println "Failed to bump version of connector [" + path + "] to next. Error : " + ex.toString();
}
println "---------------------------------------"
}
println "Task: Pushed all connectors to Ballerina Central successfully..."
print "Task: Updating package hashes: "
Utils.writeUpdatedFileHashes(project.projectDir.absolutePath, updatedBallerinaPackages)
println "Success"
}
}
task testOpenAPITool {
if (testTool) {
println "Task: Testing OpenAPI tool ..."
for (String path : toolTestPackages) {
println "----- Case - " + path
exec {
commandLine 'sh', '-c', "${ballerinaDistributionPath}/bin/bal openapi -i openapi.yaml --mode client"
workingDir(path)
}
println "Connector generation done for " + path
exec {
commandLine 'sh', '-c', "${ballerinaDistributionPath}/bin/bal pack ${path}"
}
println "bal pack done for " + path
}
// Test variations of commands in the openapi tool
exec {
commandLine 'sh', '-c', "${ballerinaDistributionPath}/bin/bal openapi -i openapi.yaml --mode client --nullable"
workingDir(toolTestsDirPath + "/15-nullable")
}
exec {
commandLine 'sh', '-c', "${ballerinaDistributionPath}/bin/bal pack"
workingDir(toolTestsDirPath + "/15-nullable")
}
exec {
commandLine 'sh', '-c', "${ballerinaDistributionPath}/bin/bal openapi -i openapi.yaml --mode client " +
"--license "+ toolTestsDirPath + "/resources/licence.txt"
workingDir(toolTestsDirPath + "/16-license")
}
exec {
commandLine 'sh', '-c', "${ballerinaDistributionPath}/bin/bal pack"
workingDir(toolTestsDirPath + "/16-license")
}
exec {
commandLine 'sh', '-c', "${ballerinaDistributionPath}/bin/bal openapi -i openapi.yaml --mode client --with-tests"
workingDir(toolTestsDirPath + "/17-with-tests")
}
exec {
commandLine 'sh', '-c', "${ballerinaDistributionPath}/bin/bal pack"
workingDir(toolTestsDirPath + "/17-with-tests")
}
}
}
task generateNbuildLatestOpenAPIConnectors {
if (testConnectorGeneration) {
println "Task: Re-generate and build connectors..."
def generationFailedConnectors = [] // List to store generation failed connectors
def compilationFailedConnectors = [] // List to store compilation failed connectors
def execConnectors = 0;
if (customDistributionPath == null || customDistributionPath == "") {
println "Custom distribution path is not provided. Using the default distribution path."
} else {
println "Using the custom distribution path : " + customDistributionPath
ballerinaDistributionPath = customDistributionPath;
}
if (connectorList != null && connectorList != "") {
println "Connector list is provided. Using the provided connector list."
def selectedConnectorPath = []
for (String connector : connectorList.split(",")) {
def path = openApiPackageDirPath + "/" + connector.trim();
if (ballerinaPackages.contains(path)) {
selectedConnectorPath.add(path)
} else {
println "Connector [" + connector + "] is not found in the repository."
}
}
ballerinaPackages = selectedConnectorPath;
}
for (String path : ballerinaPackages) {
def connector = path;
// Extract the connector name from the path
def regex = /\/openapi\/(.*?)$/
def matcher = (path =~ regex)
if (matcher.find()) {
connector = matcher.group(1)
}
// Re-generate the connector
println "--- Generating the connector : " + connector
try {
exec {
execConnectors++;
workingDir(path)
commandLine 'sh', '-c', "${ballerinaDistributionPath}/bin/bal openapi -i openapi.* --mode client --client-methods ${clientMethod}"
}
println "Connector generation is successful for " + connector
} catch (Exception genEx) {
println "Failed to re-generate the connector [" + connector + "]. Error : " + genEx.toString()
generationFailedConnectors << connector // Add the failed connector to the list
continue
}
// Build the connector
try {
exec {
commandLine 'sh', '-c', "${ballerinaDistributionPath}/bin/bal pack ${path}"
}
println "bal pack success for " + connector
} catch (Exception buildEx) {
println "Failed to build the re-generate the connector [" + connector + "]. Error : " + buildEx.toString()
compilationFailedConnectors << connector // Add the failed connector to the list
}
}
// Generate the summary report
def summary = constructSummaryReport(ballerinaDistributionPath, ballerinaPackages.size(), generationFailedConnectors, compilationFailedConnectors, execConnectors, clientMethod)
def reportFile = file("${project.projectDir}/.github/summary.log")
reportFile.text = summary
if (generationFailedConnectors.size() == 0 && compilationFailedConnectors.size() == 0) {
println "All connectors executed successfully."
}
}
}
def constructSummaryReport(ballerinaDistributionPath, totalConnectors, generationFailedConnectors, compilationFailedConnectors, execConnectors, clientMethod) {
def summary = "# Summary \n\n"
summary += "Date and Time: ${new Date()}\n"
summary += "Ballerina Version: ${getBallerinaVersion(ballerinaDistributionPath).toString()}\n"
summary += "Client Methid Type: ${clientMethod}\n"
summary += "Total Connectors in the Repository: ${totalConnectors}\n"
summary += "Number of Connectors Executed: ${execConnectors}\n\n"
if (generationFailedConnectors.size() > 0 || compilationFailedConnectors.size() > 0) {
summary += "Failed Connectors:\n\n"
if (generationFailedConnectors.size() > 0) {
summary += "Generation Failed Connectors (count: ${generationFailedConnectors.size()}) :\n"
generationFailedConnectors.each { connector ->
summary += "- ${connector}\n"
}
}
if (compilationFailedConnectors.size() > 0) {
summary += "Compilation Failed Connectors (count: ${compilationFailedConnectors.size()}) :\n"
compilationFailedConnectors.each { connector ->
summary += "- ${connector}\n"
}
}
} else {
summary += "All connectors executed successfully.\n"
}
return summary
}
def getBallerinaVersion(ballerinaDistributionPath) {
def command = ballerinaDistributionPath + "/bin/bal -v"
def process = command.execute()
process.waitFor()
def output = process.text.trim()
def ballerinaVersion = output.split('\n').find { it.startsWith("Ballerina") }
return ballerinaVersion
}