forked from lefou/mill-osgi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OsgiBundleModule.scala
305 lines (252 loc) · 9.69 KB
/
OsgiBundleModule.scala
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package de.tobiasroeser.mill.osgi
import scala.collection.JavaConverters._
import scala.util.Try
import aQute.bnd.osgi.{Builder, Constants}
import de.tobiasroeser.mill.osgi.internal.BuildInfo
import mill._
import mill.define.Task
import mill.eval.PathRef
import mill.scalalib.{JavaModule, PublishModule}
import os.Path
trait OsgiBundleModule extends JavaModule {
import OsgiBundleModule._
/**
* The transitive version of `localClasspath`.
* This overrides [[JavaModule.transitiveLocalClasspath]], but uses the final
* JAR files instead of just the classes directories where possible.
* This is needed, as only the final JARs contain proper OSGi manifest entries.
*/
override def transitiveLocalClasspath: T[Agg[PathRef]] = T {
Task.traverse(recursiveModuleDeps) { m =>
T.task {
Agg(m.jar())
}
}().flatten
}
/**
* Build the final bundle.
* Overrides [[JavaModule.jar]] and links to [[osgiBundle]] instead.
*/
override def jar: T[PathRef] = T {
osgiBundle()
}
/**
* The bundle symbolic name used to initialize [[osgiHeaders]].
* If the module is a [[PublishModule]], it calculated the bundle symbolic name
* from [[PublishModule.artifactMetadata]]
*/
def bundleSymbolicName: T[String] = this match {
case pm: PublishModule => T {
calcBundleSymbolicName(pm.pomSettings().organization, artifactId())
}
case _ =>
artifactId()
}
/**
* The bundle version used to initialize [[osgiHeaders]].
* If the module is a [[PublishModule]], it uses the [[PublishModule.publishVersion]]
*/
def bundleVersion: T[String] = this match {
case pm: PublishModule => T {
pm.publishVersion()
}
case _ => "0.0.0"
}
/**
* Instruct bnd to create a reproducible bundle file.
*/
def reproducibleBundle: T[Boolean] = T {
true
}
/**
* Embed these JAR files and also add them to the bundle classpath.
*/
def embeddedJars: T[Seq[PathRef]] = T {
Seq[PathRef]()
}
/**
* Embed the content of the given JAR files into the bundle.
*/
def explodedJars: T[Seq[PathRef]] = T {
Seq[PathRef]()
}
def osgiHeaders: T[OsgiHeaders] = {
def withDefaults: OsgiHeaders => OsgiHeaders = h => h.copy(
`Import-Package` = Seq("*")
)
this match {
case pm: PublishModule => T {
val pom = pm.pomSettings()
withDefaults(OsgiHeaders(
`Bundle-SymbolicName` = bundleSymbolicName(),
`Bundle-Version` = Option(bundleVersion()),
`Bundle-License` = pom.licenses.map(l => l.url.toString),
`Bundle-Vendor` = Option(pom.organization),
`Bundle-Description` = Option(pom.description)
))
}
case _ => T {
withDefaults(OsgiHeaders(
`Bundle-SymbolicName` = bundleSymbolicName(),
`Bundle-Version` = Option(bundleVersion())
))
}
}
}
/**
* Iff `true` include sources in the final bundle under `OSGI-OPT/src`.
*/
def includeSources: T[Boolean] = T {
false
}
/**
* Resources to include into the final bundle.
* Defaults to include [[JavaModule.resources()]].
*/
def includeResource: T[Seq[String]] = T {
// default: add contents of resources to final bundle
resources()
// only take non-empty directories to avoid bnd warning/error
.filter(p => p.path.toIO.exists()) // && Option(p.path.toIO.list()).map(!_.isEmpty).getOrElse(false))
// add to the root of the JAR
.map(dir => dir.path.toIO.getAbsolutePath())
}
/**
* Exports the given packages but does not try to include them from the class path.
* The packages should be loaded with alternative means.
*/
def exportContents: T[Seq[String]] = T {
Seq[String]()
}
// TODO: do we want support default Mill Jar headers?
/**
* Additional headers to add to the bundle manifest.
* Warning: All headers added here will override their previous value,
* hence, be careful to not add standard OSGi headers here, but via [[osgiHeaders]].
*/
def additionalHeaders: T[Map[String, String]] = T {
Map[String, String]()
}
/**
* Build the OSGi Bundle.
*/
def osgiBundle: T[PathRef] = T {
val log = T.ctx().log
val currentRunningMillVersion = T.ctx().env.get("MILL_VERSION")
if (!checkMillVersion(currentRunningMillVersion)) {
log.error(s"Your used mill version is most probably too old. In case of errors use (at least) mill version ${BuildInfo.millVersion}.")
}
val builder = new Builder()
if (reproducibleBundle()) {
builder.setProperty(Constants.REPRODUCIBLE, "true")
}
// TODO: check if all dependencies have proper Manifests (are bundled as jars instead of class folders)
val bndClasspath = (compileClasspath() ++ localClasspath()).toList.map(p => p.path.toIO).filter(_.exists()).asJava
builder.setClasspath(bndClasspath)
// TODO: scan classes directory and auto-add all dirs as private package
val classesPath = compile().classes.path
val ps: Seq[Path] = if (!os.exists(classesPath)) Seq() else os.walk(classesPath)
val packages = ps.filter(_.toIO.isFile()).flatMap { pFull =>
val p = pFull.relativeTo(classesPath)
if (p.segments.size > 1) {
Seq((p / os.up).segments.mkString("."))
} else {
// Find way to include top-level package
Seq(".")
}
}.distinct
if (!packages.isEmpty) {
builder.setProperty(Constants.PRIVATE_PACKAGE, packages.mkString(","))
}
// // Special case, files in top level package
// // Those can't be exported, but we include them
// val rootPackageFiles: LsSeq = ammonite.ops.ls ! (classesPath)
// if (!rootPackageFiles.filter(_.isFile).isEmpty) {
// println("Found files in top level package")
// // mergeSeqProps(builder, Constants.INCLUDERESOURCE, Seq(classesPath.toIO.getAbsolutePath() + ";recursive:=false"))
// mergeSeqProps(builder, Constants.PRIVATE_PACKAGE, Seq(".;-split-package:=last"))
// }
allSources().foreach { dir =>
builder.setProperty(Constants.SOURCEPATH, dir.path.toIO.getAbsolutePath())
}
if (includeSources()) {
mergeSeqProps(builder, Constants.INCLUDERESOURCE,
allSources().filter(_.path.toIO.exists()).map(s => "OSGI-OPT/src=" + s.path.toIO.getAbsolutePath()).toList)
}
// TODO: Some validation that should at least war
// * Fragment and activator at the same time
// * Activator in exported package
// * Packages not part of export or private
// TODO: handle special props with defaults
// handle included resources
mergeSeqProps(builder, Constants.INCLUDERESOURCE, includeResource())
// handle embedded Jars
embeddedJars().foreach { jar =>
mergeSeqProps(builder, Constants.INCLUDERESOURCE, Seq(jar.path.toIO.getAbsolutePath()))
}
// handle exploded Jars
explodedJars().foreach { jar =>
mergeSeqProps(builder, Constants.INCLUDERESOURCE, Seq("@" + jar.path.toIO.getAbsolutePath()))
}
mergeSeqProps(builder, Constants.EXPORT_CONTENTS, exportContents())
builder.addProperties(osgiHeaders().toProperties)
builder.addProperties(additionalHeaders().asJava)
// println("Props:" + builder.getProperties().asScala.toList.map {
// case (k, v) =>
// if (v.indexOf(",") > 0) {
// s"${k}:\n ${v.split("[,]").mkString(",\n ")}"
// } else {
// s"${k}: ${v}"
// }
// }.mkString("\n "))
val jar = builder.build()
builder.getErrors().asScala.foreach(msg => log.error("bnd error: " + msg))
builder.getWarnings().asScala.foreach(msg => log.error("bnd warning: " + msg))
val outputPath = T.ctx().dest / s"${bundleSymbolicName()}-${bundleVersion()}.jar"
os.makeDir.all(outputPath / os.up)
os.remove.all(outputPath)
jar.write(outputPath.toIO)
PathRef(outputPath)
}
}
object OsgiBundleModule {
def calcBundleSymbolicName(group: String, artifact: String): String = {
val groupParts = group.split("[.]")
val nameParts = artifact.split("[.]").flatMap(_.split("[-]"))
val parts =
if (nameParts.startsWith(groupParts)) nameParts
else (groupParts.lastOption, nameParts.headOption) match {
case (Some(last), Some(head)) if last == head => groupParts ++ nameParts.tail
case (Some(last), Some(head)) if head.startsWith(last) => groupParts.take(groupParts.size - 1) ++ nameParts
case _ => groupParts ++ nameParts
}
parts.mkString(".")
}
def mergeSeqProps(builder: Builder, key: String, value: Seq[String]): Unit = {
val existing = builder.getProperty(key) match {
case null => Seq()
case p => Seq(p)
}
builder.setProperty(key, (existing ++ value).mkString(","))
}
protected[osgi] def checkMillVersion(millVersion: Option[String]): Boolean = checkMillVersion(BuildInfo.millVersion, millVersion)
protected[osgi] def checkMillVersion(buildVersion: String, millVersion: Option[String]): Boolean = millVersion match {
case Some(v) =>
/** Extract the major, minor and micro version parts of the given version string. */
def parseVersion(version: String): Try[Array[Int]] = Try {
version
.split("[-]", 2)(0)
.split("[.]", 4)
.take(3)
.map(_.toInt)
}
val buildMillVersion = parseVersion(buildVersion).getOrElse(Array(0, 0, 0))
val runMillVersion = parseVersion(v).getOrElse(Array(999, 999, 999))
(runMillVersion(0) > buildMillVersion(0)) ||
(runMillVersion(0) == buildMillVersion(0) && runMillVersion(1) > buildMillVersion(1)) ||
(runMillVersion(0) == buildMillVersion(0) && runMillVersion(1) == buildMillVersion(1) && runMillVersion(2) >= buildMillVersion(2))
case _ =>
// ignore
true
}
}