-
Notifications
You must be signed in to change notification settings - Fork 1
/
generateFhir.main.kts
executable file
·234 lines (194 loc) · 7.31 KB
/
generateFhir.main.kts
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
#!/usr/bin/env kotlin
import kotlin.system.exitProcess
import java.io.File
val fhirSpecPath = "fhir-spec"
enum class FhirVersion(val value: String) {
FHIR3("stu3"), FHIR4("r4")
}
val parserResourcesPath = "fhir/parser"
fun sourceParserConfig(fhirVersion: FhirVersion) =
"$parserResourcesPath/${fhirVersion.value}/config"
fun sourceParserTemplates(fhirVersion: FhirVersion) =
"$parserResourcesPath/${fhirVersion.value}/templates"
val parserPath = "fhir-spec-parser"
val sourceCodesystems = "$parserPath/codesystems"
val sourceModels = "$parserPath/models"
val sourceTests = "$parserPath/tests"
val sourceTestJsons = "$parserPath/downloads"
fun targetCodesystems(fhirVersion: FhirVersion) =
"fhir/src-gen/commonMain/kotlin/care/data4life/hl7/fhir/${fhirVersion.value}/codesystem"
fun targetModels(fhirVersion: FhirVersion) =
"fhir/src-gen/commonMain/kotlin/care/data4life/hl7/fhir/${fhirVersion.value}/model"
fun targetTests(fhirVersion: FhirVersion) =
"fhir/src-gen/jvmTest/kotlin/care/data4life/hl7/fhir/${fhirVersion.value}/model"
fun targetTestJsons(fhirVersion: FhirVersion) =
"fhir/src-gen/jvmTest/resources/${fhirVersion.value}"
fun modelExclusionList(fhirVersion: FhirVersion) = when (fhirVersion) {
// this exclusions need to be in sync with 'fhir/parser/r4/templates/template-elementfactory.kt' -> 'exclude_resources'
FhirVersion.FHIR4 -> listOf(
"ExampleScenario.kt",
)
// this exclusions need to be in sync with 'fhir/parser/stu3/templates/template-elementfactory.kt' -> 'exclude_resources'
FhirVersion.FHIR3 -> listOf(
"ExplanationOfBenefit.kt",
"Claim.kt",
)
}
fun staticReplacementMap(fhirVersion: FhirVersion) = when (fhirVersion) {
FhirVersion.FHIR4 -> mapOf(
"MedicationStatementStatusCodes.kt" to targetCodesystems(fhirVersion),
"MedicationStatementFhirTest.kt" to targetTests(fhirVersion),
"MedicationStatement.kt" to targetModels(fhirVersion),
)
FhirVersion.FHIR3 -> mapOf()
}
var fhirVersions = listOf(
FhirVersion.FHIR4,
FhirVersion.FHIR3,
)
if (args.isNotEmpty()) {
when (args[0]) {
"fhir4" -> fhirVersions = listOf(FhirVersion.FHIR4)
"fhir3" -> fhirVersions = listOf(FhirVersion.FHIR3)
"all" -> {
}
"-h", "--help" -> printUsage()
else -> {
println("Sorry I didn't understand ${args[0]}")
printUsage()
exitProcess(1)
}
}
}
fun printUsage() {
println(
"""
Usage: ./generateFhir.main.kts [<option>]
all Generates all FHIR versions. Default is <all>.
fhir3 Generate FHIR3 sources and tests
fhir4 Generate FHIR4 sources and tests
""".trimIndent()
)
}
if (!File(parserPath).exists()) {
println("""
This script depends on the fhir parser: https://github.com/gesundheitscloud/fhir-parser/
Please call git submodule update --init --recursive to ensure it's available.
""".trimIndent())
exitProcess(1)
}
for (fhirVersion in fhirVersions) {
println("Start to generate ${fhirVersion.name}")
cleanup()
generateFhirModels(fhirVersion)
integrateFhirModels(fhirVersion)
integrateStatics(fhirVersion)
cleanup()
formatCode()
}
fun generateFhirModels(fhirVersion: FhirVersion) {
println("Copy FHIR parser configuration")
File(sourceParserConfig(fhirVersion)).walk()
.filter { !it.isDirectory }
.forEach { it.copyTo(File("$parserPath/${it.name}")) }
File(sourceParserTemplates(fhirVersion)).walk()
.filter { !it.isDirectory }
.forEach { it.copyTo(File("$parserPath/templates/${it.name}")) }
println("Copy FHIR specification")
cmd("mkdir $parserPath/downloads")
cmd("cp -a $fhirSpecPath/hl7.org/fhir/${fhirVersion.value}/version.info $parserPath/downloads/")
cmd("unzip -o $fhirSpecPath/hl7.org/fhir/${fhirVersion.value}/examples-json.zip -d $parserPath/downloads/")
println("Generate FHIR files")
if (!cmd("test -d venv", parserPath)) {
if (!cmd("python3 -m venv venv", parserPath)) {
println("Python virtual environment not present and couldn't be created")
exitProcess(1)
}
}
cmd("venv/bin/pip install --upgrade pip", parserPath)
cmd("venv/bin/pip install -Ur requirements.txt", parserPath)
cmd("venv/bin/python generate.py --cache-only", parserPath)
println(" ✅ Done generating ${fhirVersion.name} model")
}
fun integrateFhirModels(fhirVersion: FhirVersion) {
val targetCodesystems = targetCodesystems(fhirVersion)
val targetModels = targetModels(fhirVersion)
val targetTests = targetTests(fhirVersion)
val targetTestJsons = targetTestJsons(fhirVersion)
// Remove old models
cmd("rm -rf $targetCodesystems")
cmd("rm -rf $targetModels")
cmd("rm -rf $targetTests")
cmd("rm -rf $targetTestJsons")
// Move codesystems
cmd("mkdir $targetCodesystems")
cmd("cp -a $sourceCodesystems/. $targetCodesystems")
// Move models
cmd("mkdir $targetModels")
File(sourceModels)
.walk()
.forEach { file ->
if (!modelExclusionList(fhirVersion).contains(file.name)) {
file.copyTo(File("$targetModels/${file.name}"))
}
}
// Move tests
cmd("mkdir $targetTests")
File(sourceTests)
.walk()
.forEach { file ->
if (!modelExclusionList(fhirVersion).contains(file.name.replace("FhirTest", ""))) {
file.copyTo(File("$targetTests/${file.name}"))
}
}
// Move JSON
cmd("mkdir $targetTestJsons")
File(sourceTestJsons)
.walk()
.forEach { file ->
if (file.name.contains("example")) {
file.copyTo(File("$targetTestJsons/${file.name}"))
}
}
println(" ✅ Done integrating $fhirVersion generated codesystems, models and tests")
}
/**
* Hotfix for https://gesundheitscloud.atlassian.net/browse/SDK-576
* TODO Remove once the Spec is fixed
*/
fun integrateStatics(fhirVersion: FhirVersion) {
println("Include static FHIR replacements")
val staticSourcePath = "fhir/parser/${fhirVersion.value}/statics"
for ((file, target) in staticReplacementMap(fhirVersion)) {
cmd("cp $staticSourcePath/$file $target")
}
println(" ✅ Done including static FHIR replacements")
}
fun formatCode() {
cmd("./gradlew spotlessApply")
println(" ✅ Done formatting generated FHIR code")
}
fun cleanup() {
println("Start cleanup")
cmd("rm -rf ${parserPath}/codesystems")
cmd("rm -rf ${parserPath}/dependencies.json")
cmd("rm -rf ${parserPath}/downloads")
cmd("rm -rf ${parserPath}/mappings.py")
cmd("rm -rf ${parserPath}/models")
cmd("rm -rf ${parserPath}/settings.py")
cmd("rm -rf ${parserPath}/templates")
cmd("rm -rf ${parserPath}/tests")
println(" ✅ Done cleanup")
}
fun cmd(command: String, path: String = "./"): Boolean {
val commands = command.split(" ")
val arguments = commands.subList(1, commands.size)
val result = ProcessBuilder()
.directory(File(path))
.command(commands[0], *arguments.toTypedArray())
.redirectError(ProcessBuilder.Redirect.INHERIT)
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
.start()
.waitFor()
return result == 0
}