-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathPomGenerator.kt
244 lines (203 loc) · 9.33 KB
/
PomGenerator.kt
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
/*
* Copyright (C) 2021 Vaticle
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.vaticle.bazel.distribution.maven
import com.eclipsesource.json.Json
import com.eclipsesource.json.JsonObject
import org.w3c.dom.Document
import org.w3c.dom.Element
import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import java.io.File
import java.util.concurrent.Callable
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.transform.OutputKeys
import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult
import kotlin.system.exitProcess
@Command(name = "pom-generator", mixinStandardHelpOptions = true)
class PomGenerator : Callable<Unit> {
@Option(names = ["--version_file"])
lateinit var versionFile: File
@Option(names = ["--output_file"])
lateinit var outputFile: File
@Option(names = ["--workspace_refs_file"])
lateinit var workspaceRefsFile: File
@Option(names = ["--project_name"])
var projectName = ""
@Option(names = ["--project_description"])
var projectDescription = ""
@Option(names = ["--project_url"])
var projectUrl = ""
@Option(names = ["--license"])
var license = ""
@Option(names = ["--scm_url"])
var scmUrl = ""
@Option(names = ["--target_group_id"])
var targetGroupId = ""
@Option(names = ["--target_artifact_id"])
var targetArtifactId = ""
@Option(names = ["--target_deps_coordinates"])
lateinit var dependencyCoordinates: String
@Option(names = ["--packaging"])
var packaging = ""
fun getLicenseInfo(license_id: String): Pair<String, String> {
return when {
license_id.equals("apache") -> {
Pair("Apache License, Version 2.0", "https://www.apache.org/licenses/LICENSE-2.0.txt")
}
license_id.equals("mit") -> {
Pair("MIT License", "https://opensource.org/licenses/MIT")
}
else -> {
throw RuntimeException("unknown license identifier: $license_id")
}
}
}
fun parseMavenCoordinate(coordinate: String): Triple<String, String, String> {
val (groupId, artifactId, version) = coordinate.split(":")
return Triple(groupId, artifactId, version)
}
fun version(originalVersion: String, version: String, workspace_refs: JsonObject): String {
if (originalVersion.equals("{pom_version}")) {
return version
}
val versionCommit = workspace_refs.get("commits").asObject().get(originalVersion)
if (versionCommit != null) {
return versionCommit.asString()
}
val tagCommit = workspace_refs.get("tags").asObject().get(originalVersion)
if (tagCommit != null) {
return tagCommit.asString()
}
return originalVersion
}
fun licenses(pom: Document): Element {
val licenses = pom.createElement("licenses")
val licenseElem = pom.createElement("license")
val licenseNameElem = pom.createElement("name")
// obtain full license name and URL from an identifier
val (licenseName, licenseUrl) = getLicenseInfo(license)
licenseNameElem.appendChild(pom.createTextNode(licenseName))
val licenseUrlElem = pom.createElement("url")
licenseUrlElem.appendChild(pom.createTextNode(licenseUrl))
licenseElem.appendChild(licenseNameElem)
licenseElem.appendChild(licenseUrlElem)
licenses.appendChild(licenseElem)
return licenses
}
fun scm(pom: Document, version: String): Element {
val scm = pom.createElement("scm")
val connection = pom.createElement("connection")
connection.appendChild(pom.createTextNode(scmUrl))
val developerConnection = pom.createElement("developerConnection")
developerConnection.appendChild(pom.createTextNode(scmUrl))
val tag = pom.createElement("tag")
tag.appendChild(pom.createTextNode(version))
val scmUrlElem = pom.createElement("url")
scmUrlElem.appendChild(pom.createTextNode(this.scmUrl))
scm.appendChild(connection)
scm.appendChild(developerConnection)
scm.appendChild(tag)
scm.appendChild(scmUrlElem)
return scm
}
fun dependencies(pom: Document, version: String, workspace_refs: JsonObject): Element {
val dependenciesElem = pom.createElement("dependencies")
val coordinates = if (dependencyCoordinates.length == 0) emptyArray<String>() else dependencyCoordinates.split(";").toTypedArray()
for (dep in coordinates) {
val depCoordinates = parseMavenCoordinate(dep)
val dependencyElem = pom.createElement("dependency")
val dependencyGroupId = pom.createElement("groupId")
dependencyGroupId.appendChild(pom.createTextNode(depCoordinates.first))
val dependencyArtifactId = pom.createElement("artifactId")
dependencyArtifactId.appendChild(pom.createTextNode(depCoordinates.second))
val dependencyVersion = pom.createElement("version")
dependencyVersion.appendChild(pom.createTextNode(version(depCoordinates.third, version, workspace_refs)))
dependencyElem.appendChild(dependencyGroupId)
dependencyElem.appendChild(dependencyArtifactId)
dependencyElem.appendChild(dependencyVersion)
dependenciesElem.appendChild(dependencyElem)
}
return dependenciesElem
}
private fun outputDocumentToFile(pom: Document) {
val transformer = TransformerFactory.newInstance().newTransformer()
transformer.setOutputProperty(OutputKeys.INDENT, "yes")
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2")
transformer.transform(DOMSource(pom), StreamResult(outputFile))
}
override fun call() {
val version = versionFile.readText()
val workspace_refs = Json.parse(workspaceRefsFile.readText()).asObject()
// Create an XML document for constructing the POM
val pom = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()
// root element describing the project
val rootElement = pom.createElement("project")
rootElement.setAttribute("xmlns", "http://maven.apache.org/POM/4.0.0")
rootElement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
rootElement.setAttribute("xsi:schemaLocation", "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd")
pom.appendChild(rootElement)
// version of the Maven POM format
val modelVersion = pom.createElement("modelVersion")
modelVersion.appendChild(pom.createTextNode("4.0.0"))
rootElement.appendChild(modelVersion)
// project name
val name = pom.createElement("name")
name.appendChild(pom.createTextNode(projectName))
rootElement.appendChild(name)
// project description
val description = pom.createElement("description")
description.appendChild(pom.createTextNode(projectDescription))
rootElement.appendChild(description)
// project URL
val url = pom.createElement("url")
url.appendChild(pom.createTextNode(projectUrl))
rootElement.appendChild(url)
// licenses
rootElement.appendChild(licenses(pom))
// source control management information
rootElement.appendChild(scm(pom, version))
// group id of the library
val groupIdElem = pom.createElement("groupId")
groupIdElem.appendChild(pom.createTextNode(targetGroupId))
rootElement.appendChild(groupIdElem)
// artifact id of the library
val artifactIdElem = pom.createElement("artifactId")
artifactIdElem.appendChild(pom.createTextNode(targetArtifactId))
rootElement.appendChild(artifactIdElem)
// version of the library
val versionElem = pom.createElement("version")
versionElem.appendChild(pom.createTextNode(version))
rootElement.appendChild(versionElem)
if (packaging.isNotEmpty() && packaging != "jar") {
val packagingElem = pom.createElement("packaging")
packagingElem.appendChild(pom.createTextNode(packaging))
rootElement.appendChild(packagingElem)
}
// add dependency information
rootElement.appendChild(dependencies(pom, version, workspace_refs))
// write the final result
outputDocumentToFile(pom)
}
}
fun main(args: Array<String>): Unit = exitProcess(CommandLine(PomGenerator()).execute(*args))