This repository was archived by the owner on Feb 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.sc
433 lines (338 loc) · 13 KB
/
build.sc
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import $ivy.`com.lihaoyi::mill-contrib-bloop:$MILL_VERSION`
import coursier.core.{Organization, Resolution}
import coursier.graph.DependencyTree
import mill.Agg
import mill.define.Task
import mill.modules.Jvm
import mill.scalalib.publish.{Artifact, Dependency, Scope}
// Define the Scala, Akka and Akka Http versions we want to create the bundles for
val scalaVersions : Seq[String] = Seq("2.12.11", "2.13.2")
val akkaVersions : Seq[String] = Seq("2.6.6")
val akkaHttpVersions : Seq[String] = Seq("10.1.12")
import mill._
import mill.scalalib._
import mill.api.{Loose, Result}
import mill.define.Segment
import mill.define.{Command, Target}
import mill.scalalib.publish.{Developer, License, PomSettings, VersionControl}
import $ivy.`de.tototec::de.tobiasroeser.mill.osgi:0.3.0`
import de.tobiasroeser.mill.osgi._
import $file.GitModule
import GitModule.GitModule
import $file.Publish
import Publish.BlendedPublishModule
val projectDir : os.Path = build.millSourcePath
object GitSupport extends GitModule {
override def millSourcePath = projectDir
}
/**
* List of bundles that are currently handled by the project. These are required
* to properly rewrite the dependencies of the published pom file.
*/
// TODO: Can we somehow autodiscover this list ?
val containedBundles : Seq[String] = Seq(
"akka-actor", "akka-protobuf", "akka-protobuf-v3", "akka-stream", "akka-slf4j",
"akka-http", "akka-http-core", "akka-parsing"
)
val revision = T { GitSupport.publishVersion() }
object wrapped extends mill.Cross[wrapped](scalaVersions:_*)
class wrapped(crossScalaVersion : String) extends Module {
trait WrapperProject extends ScalaModule with OsgiBundleModule with BlendedPublishModule { outer =>
/**
* Settings required to publish SNAPSHOT's in a temporary Maven snapshot repo vis scp.
*/
override val githubRepo : String = "akka-osgi"
override val scpTargetDir : String = "akka-osgi"
override def scpSubRepo: T[String] = T { revision() }
/**
* A generated description of the bundle.
*/
override def description : String = {
val jarRef : String =
s"${bundleDep.dep.module.organization.value}::${bundleDep.dep.module.name.value}:${bundleDep.dep.version}"
s"""OSGi Wrapper Bundle for [$jarRef]. This is the original jar provided by the Akka team
| with reviewed OSGi manifest settings. No code has been changed within the jar nor has content
| been removed or added. For the original work, please refer to the Akka project documentation
| at https://akka.io.""".stripMargin
}
override def scalaVersion : T[String] = crossScalaVersion
def scalaBinVersion : T[String] = T { scalaVersion().split("\\.").take(2).mkString(".") }
def typesafeVersion: String
/**
* The artifact to be included in this wrapper bundle
*/
def artifact: String
def bundleDep : Dep = ivy"com.typesafe.akka::${artifact}:${typesafeVersion}"
override def publishVersion = T {
s"${typesafeVersion}.${revision()}"
}
override def artifactName : T[String] = T { artifact }
override def bundleSymbolicName: T[String] = T {
// we want the scala version as part of the bundle symbolic name
OsgiBundleModule.calcBundleSymbolicName(pomSettings().organization, artifactId())
}
override def moduleDeps: Seq[PublishModule] = super.moduleDeps
/** We will copy the direkt dependencies of the wrapped jar into our own pom,
* so that we can use it as a direct drop-in in place of the original jar.
*/
// TODO: copy more settings and from the original pom, handle copyrights etc.
def pomDependencies : T[Agg[Dep]] = T {
val calculateDep : coursier.Dependency => Dep = { d =>
val modOrg : String = d.module.organization.value
val modName : String = d.module.name.value
val (name : String, cross : CrossVersion) = {
if (modName.endsWith("_" + scalaBinVersion())) {
(modName.substring(0, modName.lastIndexOf("_")), CrossVersion.Binary(false))
} else {
(modName, CrossVersion.empty(false))
}
}
Dep(modOrg, name, d.version, cross, true)
}
val rewriteDep : Dep => Dep = { d =>
if (containedBundles.contains(d.dep.module.name.value)) {
val newVersion : String = d.dep.version + "." + revision()
d.copy(dep = d.dep
.withModule(d.dep.module.withOrganization(Organization(organization)))
.withVersion(newVersion))
} else {
d
}
}
val (flattened, res) = Lib.resolveDependenciesMetadata(
repositories,
resolveCoursierDependency().apply(_),
ivyDeps() ++ transitiveIvyDeps(),
Some(mapDependencies())
)
val tree = DependencyTree(res)
val isInThisBundle : coursier.Dependency => Boolean = d =>
d.module.name.value.startsWith(artifact + "_")
val pomDependencies : Seq[coursier.Dependency] =
(tree.map(_.dependency) ++ tree.flatMap(_.children).map(_.dependency))
.distinct
.filter( d=> !isInThisBundle(d))
val includedDeps : Seq[Dep] = pomDependencies.map(calculateDep).map(rewriteDep)
Agg(includedDeps:_*)
}
override def publishXmlDeps: Task[Agg[Dependency]] = T.task {
val toDependency : Dep => Dependency = { d =>
Artifact.fromDep(d, scalaVersion(), scalaBinVersion(), "")
}
pomDependencies()
.map(toDependency)
}
def originalJar: T[PathRef] = T {
resolveDeps(T.task {
Agg(bundleDep.exclude("*" -> "*"))
})().iterator.next
}
def extraImports : T[Seq[String]] = T { Seq(
"com.sun.*;resolution:=optional",
"sun.*;resolution:=optional",
"net.liftweb.*;resolution:=optional",
"play.*;resolution:=optional",
"twirl.*;resolution:=optional",
"org.json4s.*;resolution:=optional"
)}
override def ivyDeps = T {
Agg(bundleDep) ++ scalaLibraryIvyDeps()
}
override def osgiHeaders: T[OsgiHeaders] = T {
super.osgiHeaders().copy(
`Import-Package` = Seq(
s"""scala.util.parsing.*;version="[1.1.2,2.0.0)"""",
s"""scala.compat.java8.*;version="[0.9,1.0.0)"""",
s"""scala.*;version="[${scalaBinVersion()},${scalaBinVersion()}.50]""""
) ++ extraImports() ++ Seq("*"),
`Export-Package` = exportPackages.map(_ + s""";version="${typesafeVersion}""""),
`Require-Capability` = Some("""osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"""")
)
}
/**
* We put the original Manifest entries into the manifest. Otherwise the ManifestInfo.checkSameVersion
* in akka.util may fail
*/
override def additionalHeaders: T[Map[String, String]] = T {
super.additionalHeaders() ++ Map(
"Implementation-Version" -> typesafeVersion,
"Implementation-Title" -> bundleDep.dep.module.name.value,
"Implementation-Vendor-Id" -> bundleDep.dep.module.organization.value,
"Implementation-URL" -> "https://akka.io",
"Implementation-Vendor" -> "Lightbend"
)
}
def includeFromJar: T[Seq[String]] = T {
Seq.empty[String]
}
def exportPackages : Seq[String] = Seq.empty
override def includeResource: T[Seq[String]] = T {
super.includeResource() ++ includeFromJar().map { f =>
s"@${originalJar().path.toIO.getAbsolutePath()}!/$f"
}
}
trait Tests extends super.Tests {
override def moduleDeps: Seq[JavaModule] = Seq(testsupport(crossScalaVersion))
override def testFrameworks: T[Seq[String]] = T {
Seq("org.scalatest.tools.Framework")
}
override def forkEnv: Target[Map[String, String]] = T {
super.forkEnv() ++ Map(
"origJar" -> outer.originalJar().path.toIO.getAbsolutePath(),
"osgiJar" -> outer.osgiBundle().path.toIO.getAbsolutePath()
)
}
override def generatedSources: Target[Seq[PathRef]] = T {
val src =
"""
|class ContentSpec extends testsupport.ContentSpec
|""".stripMargin
os.write(T.ctx.dest / "specs.scala", src)
super.generatedSources() ++ Seq(PathRef(T.ctx.dest))
}
}
// enforce the test for each wrapper project
object test extends Tests
}
object httpWrapped extends mill.Cross[httpWrapped](akkaHttpVersions:_*)
class httpWrapped(akkaHttpVersion : String) extends Module {
trait HttpWrapper extends WrapperProject {
override val typesafeVersion : String = akkaHttpVersion
}
object http extends HttpWrapper {
override def artifact = "akka-http"
override def exportPackages: Seq[String] = Seq(
"akka.http.javadsl.coding.*",
"akka.http.javadsl.common.*",
"akka.http.javadsl.marshalling.*",
"akka.http.javadsl.unmarshalling.*",
"akka.http.javadsl.server.*",
"akka.http.scaladsl.client.*",
"akka.http.scaladsl.coding.*",
"akka.http.scaladsl.common.*",
"akka.http.scaladsl.marshalling.*",
"akka.http.scaladsl.unmarshalling.*",
"akka.http.scaladsl.server.*",
"akka.http.impl.settings;-split-package:=merge-first",
"akka.http.javadsl.settings;-split-package:=merge-first",
"akka.http.scaladsl.settings;-split-package:=merge-first"
)
override def includeFromJar: T[Seq[String]] = T {
Seq(
"reference.conf"
)
}
}
object core extends HttpWrapper {
override def artifact = "akka-http-core"
override def exportPackages = Seq(
"akka.http.ccompat;-split-package:=merge-first",
"akka.http.ccompat.imm",
"akka.http.javadsl",
"akka.http.javadsl.model.*",
"akka.http.scaladsl",
"akka.http.scaladsl.model.*",
"akka.http.scaladsl.util.*",
"akka.http",
"akka.http.impl.engine.*",
"akka.http.impl.model.*",
"akka.http.impl.util.*",
)
override def includeFromJar: T[Seq[String]] = T {
Seq(
"reference.conf",
"akka-http-version.conf"
)
}
}
object parsing extends HttpWrapper {
override def artifact : String = "akka-parsing"
override def exportPackages = Seq(
"akka.macros.*",
"akka.parboiled2.*",
"akka.shapeless.*",
)
}
}
object akkaWrapped extends mill.Cross[akkaWrapped](akkaVersions:_*)
class akkaWrapped(akkaVersion : String) extends Module {
trait AkkaWrapper extends WrapperProject {
override val typesafeVersion : String = akkaVersion
}
object actor extends AkkaWrapper {
override def artifact : String = "akka-actor"
override def exportPackages = Seq(
"akka.*"
)
override def includeFromJar: T[Seq[String]] = T {
Seq(
"reference.conf",
"version.conf"
)
}
}
object protobuf extends AkkaWrapper {
override def artifact : String = "akka-protobuf"
override def exportPackages: Seq[String] = Seq(
"akka.protobuf",
)
}
object protobufv3 extends AkkaWrapper {
override def artifact : String = "akka-protobuf-v3"
override def exportPackages: Seq[String] = Seq(
"akka.protobufv3.internal.*",
)
override def includeFromJar : T[Seq[String]]= T { super.includeFromJar() ++ Seq(
"google/protobuf/any.proto",
"google/protobuf/api.proto",
"google/protobuf/compiler/plugin.proto",
"google/protobuf/descriptor.proto",
"google/protobuf/duration.proto",
"google/protobuf/empty.proto",
"google/protobuf/field_mask.proto",
"google/protobuf/source_context.proto",
"google/protobuf/struct.proto",
"google/protobuf/timestamp.proto",
"google/protobuf/type.proto",
"google/protobuf/wrappers.proto"
)}
}
object stream extends AkkaWrapper {
override def artifact : String = "akka-stream"
override def exportPackages = Seq(
"akka.stream.*",
"com.typesafe.sslconfig.akka.*"
)
override def osgiHeaders: T[OsgiHeaders] = T { super.osgiHeaders().copy(
`Fragment-Host` = Some(actor.bundleSymbolicName())
)}
override def includeFromJar: T[Seq[String]] = T {
Seq(
"reference.conf"
)
}
}
object slf4j extends AkkaWrapper {
override def artifact : String = "akka-slf4j"
override def exportPackages = Seq(
"akka.event.slf4j.*",
)
}
}
}
/** Test cases to check integrity of generated OSGi bundles. */
object testsupport extends mill.Cross[testsupport](scalaVersions:_*)
class testsupport(crossScalaVersion : String) extends ScalaModule {
object Deps {
def scalatest = ivy"org.scalatest::scalatest:3.1.1"
def osLib = ivy"com.lihaoyi::os-lib:0.6.3"
}
override def millSourcePath = projectDir / "testsupport"
override def scalaVersion = T { crossScalaVersion }
override def ivyDeps: Target[Loose.Agg[Dep]] = T {
super.ivyDeps() ++ Agg(
Deps.scalatest,
Deps.osLib
)
}
}