-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.gradle
112 lines (94 loc) · 3.17 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
plugins {
id 'org.hidetake.swagger.generator' version '2.19.2'
id 'groovy-base'
}
apply plugin: 'org.hidetake.swagger.generator'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/release' }
}
dependencies {
swaggerCodegen 'org.slf4j:slf4j-simple:1.7.36'
swaggerCodegen 'io.swagger.codegen.v3:swagger-codegen-cli:3.0.46'
}
defaultTasks 'clean', 'generateSwaggerCode', 'postGeneration'
def generatedApiDirectory = "generated"
def apiDirectory = "swagger"
def outputDirectory = "swagger_client"
def testDirectory = "test"
def docsDirectory = "docs"
def readmeDest = "${projectDir}"
task swaggerClean(type: Delete) {
doFirst {
delete generatedApiDirectory
}
}
swaggerClean.mustRunAfter(clean)
swaggerSources {
server {
inputFile = file(apiDirectory+'/swagger.yaml')
code {
language = 'python'
outputDir = file (generatedApiDirectory)
configFile = file (apiDirectory+'/config.json')
wipeOutputDir = false
}
}
}
generateSwaggerCode.dependsOn(swaggerClean)
generateSwaggerCode.mustRunAfter(swaggerClean)
ext.generateInitFile = { directory, component ->
// Generate init file
def initFilePath = "${directory}/${component}/__init__.py"
File initFile = new File(initFilePath)
initFile.withWriter { it.println "from __future__ import absolute_import \n# import ${component} into ${component} package" }
fileTree("${directory}/${component}/").matching{ include "**/*.py" exclude "__init__.py" }.sort().forEach { fileName ->
def file = new File("${fileName}")
def basename = file.name.take(file.name.lastIndexOf('.'))
def classLine = file.text.tokenize('\n').find {
it.contains 'class'
}
def matcher = classLine =~ /^class (.*)\(.*/
matcher.find()
initFile.withWriterAppend { it.println "from ${directory}.${component}.${basename} import ${matcher.group(1)}" }
}
println "generated __init__.py for ${component}"
}
task postGeneration() {
doLast() {
// Copy generated files into models folder
copy {
from "${generatedApiDirectory}/${outputDirectory}/"
into outputDirectory
}
copy {
from "${generatedApiDirectory}/${testDirectory}/"
into testDirectory
}
copy {
from "${generatedApiDirectory}/${docsDirectory}/"
into docsDirectory
}
copy {
from "${generatedApiDirectory}/README.md"
into readmeDest
rename { String filename ->
"README-API.md"
}
}
// delete generated api directory
delete generatedApiDirectory
println "removed ${generatedApiDirectory}"
// Generate init files
project.generateInitFile(outputDirectory, 'models')
project.generateInitFile(outputDirectory, 'api')
}
}
postGeneration.mustRunAfter('generateSwaggerCode')
// Ensures tasks are re-run always, needed for swaggerCodeGen
gradle.taskGraph.whenReady { taskGraph ->
taskGraph.getAllTasks().each {
it.setOnlyIf { true }
it.outputs.upToDateWhen { false }
}
}