-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.sbt
275 lines (246 loc) · 9.64 KB
/
build.sbt
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
import com.typesafe.sbt.packager.archetypes.TemplateWriter
import sbt.Keys.{sourceGenerators, _}
import sbt._
import sbtcrossproject.CrossPlugin.autoImport.crossProject
import sbt.internal.inc.ReflectUtilities
import sbtassembly.MergeStrategy
enablePlugins(JavaServerAppPackaging, JDebPackaging, SystemdPlugin, GitVersioning)
val versionSource = Def.task {
val versionFile = (Compile / sourceManaged).value / "com" / "ltonetwork" / "Version.scala"
val versionExtractor = """(\d+)\.(\d+)\.(\d+).*""".r
version.value match {
case versionExtractor(ma, mi, pa) =>
val (major, minor, patch) = (ma.toInt, mi.toInt, pa.toInt)
IO.write(
versionFile,
s"""package com.ltonetwork
|
|object Version {
| val VersionString = "${version.value}"
| val VersionTuple = ($major, $minor, $patch)
|}
|""".stripMargin
)
Seq(versionFile)
case _ => Seq()
}
}
val network = SettingKey[Network]("network")
network := { Network(sys.props.get("network")) }
name := "lto"
normalizedName := s"${name.value}${network.value.packageSuffix}"
unmanagedSources / excludeFilter := "Version.scala"
git.useGitDescribe := true
git.uncommittedSignifier := None
logBuffered := false
inThisBuild(
Seq(
scalaVersion := "2.12.9",
organization := "com.ltonetwork",
crossPaths := false,
scalacOptions ++= Seq("-feature", "-deprecation", "-language:higherKinds", "-language:implicitConversions", "-Ywarn-unused:-implicits")
))
resolvers += Resolver.file("local-dependencies", file("dependencies"))(Resolver.ivyStylePatterns)
run / fork := true
run / javaOptions ++= Seq(
"-XX:+IgnoreUnrecognizedVMOptions",
"--add-modules=java.xml.bind"
)
val aopMerge: MergeStrategy = new MergeStrategy {
val name = "aopMerge"
import scala.xml._
import scala.xml.dtd._
import java.io.FileInputStream
import org.xml.sax.InputSource
val parser = {
val factory = javax.xml.parsers.SAXParserFactory.newInstance()
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)
factory.newSAXParser()
}
def apply(tempDir: File, path: String, files: Seq[File]): Either[String, Seq[(File, String)]] = {
val dt = DocType("aspectj", PublicID("-//AspectJ//DTD//EN", "https://www.eclipse.org/aspectj/dtd/aspectj.dtd"), Nil)
val file = MergeStrategy.createMergeTarget(tempDir, path)
val xmls: Seq[Elem] = files.map(f => XML.loadXML(new InputSource(new FileInputStream(f)), parser))
val aspectsChildren: Seq[Node] = xmls.flatMap(_ \\ "aspectj" \ "aspects" \ "_")
val weaverChildren: Seq[Node] = xmls.flatMap(_ \\ "aspectj" \ "weaver" \ "_")
val options: String = xmls.map(x => (x \\ "aspectj" \ "weaver" \ "@options").text).mkString(" ").trim
val weaverAttr = if (options.isEmpty) Null else new UnprefixedAttribute("options", options, Null)
val aspects = new Elem(null, "aspects", Null, TopScope, false, aspectsChildren: _*)
val weaver = new Elem(null, "weaver", weaverAttr, TopScope, false, weaverChildren: _*)
val aspectj = new Elem(null, "aspectj", Null, TopScope, false, aspects, weaver)
XML.save(file.toString, aspectj, "UTF-8", xmlDecl = false, dt)
IO.append(file, IO.Newline.getBytes(IO.defaultCharset))
Right(Seq(file -> path))
}
}
inTask(assembly)(
Seq(
test := {},
assemblyJarName := s"lto-public-all-${version.value}.jar",
assemblyMergeStrategy := {
case PathList("META-INF", "io.netty.versions.properties") => MergeStrategy.concat
case PathList("META-INF", "aop.xml") => aopMerge
case PathList(xs @ _*) if xs.lastOption.fold(false)(_ == "module-info.class") => MergeStrategy.discard
case other => (assembly / assemblyMergeStrategy).value(other)
}
))
inConfig(Compile)(
Seq(
mainClass := Some("com.ltonetwork.Application"),
packageDoc / publishArtifact := false,
packageSrc / publishArtifact := false,
sourceGenerators += versionSource
))
inConfig(Test)(
Seq(
logBuffered := false,
parallelExecution := false,
testOptions += Tests.Argument("-oIDOF", "-u", "target/test-reports"),
testOptions += Tests.Setup(_ => sys.props("sbt-testing") = "true")
))
inConfig(Linux)(
Seq(
maintainer := "lto.network",
packageSummary := "LTO Network node",
packageDescription := "LTO Network node"
))
bashScriptExtraDefines += s"""addJava "-lto.directory=/var/lib/${normalizedName.value}""""
val linuxScriptPattern = "bin/(.+)".r
val batScriptPattern = "bin/([^.]+)\\.bat".r
inConfig(Universal)(
Seq(
mappings += (baseDirectory.value / s"lto-${network.value}.conf" -> "doc/lto.conf.sample"),
mappings := {
val scriptSuffix = network.value.packageSuffix
mappings.value.map {
case m @ (file, batScriptPattern(script)) =>
if (script.endsWith(scriptSuffix)) m else (file, s"bin/$script$scriptSuffix.bat")
case m @ (file, linuxScriptPattern(script)) =>
if (script.endsWith(scriptSuffix)) m else (file, s"bin/$script$scriptSuffix")
case other => other
}
},
mappings += (baseDirectory.value / s"fee-vote.py" -> s"bin/lto${network.value.packageSuffix}-fee-vote"),
javaOptions ++= Seq(
// -J prefix is required by the bash script
"-J-server",
// JVM memory tuning for 2g ram
"-J-Xms128m",
"-J-Xmx2g",
"-J-XX:+ExitOnOutOfMemoryError",
"-J-XX:+IgnoreUnrecognizedVMOptions",
// from https://groups.google.com/d/msg/akka-user/9s4Yl7aEz3E/zfxmdc0cGQAJ
"-J-XX:+UseG1GC",
"-J-XX:+UseNUMA",
"-J-XX:+AlwaysPreTouch",
// probably can't use these with jstack and others tools
"-J-XX:+PerfDisableSharedMem",
"-J-XX:+ParallelRefProcEnabled",
"-J-XX:+UseStringDeduplication"
)
))
val packageSource = Def.setting {
sourceDirectory.value / "package"
}
val upstartScript = Def.task {
val src = packageSource.value / "upstart.conf"
val dest = (Debian / target).value / "upstart" / s"${packageName.value}.conf"
val result = TemplateWriter.generateScript(src.toURI.toURL, linuxScriptReplacements.value)
IO.write(dest, result)
dest
}
linuxPackageMappings ++= Seq(
(upstartScript.value, s"/etc/init/${packageName.value}.conf")
).map(packageMapping(_).withConfig().withPerms("644"))
linuxScriptReplacements += "detect-loader" ->
"""is_systemd() {
| which systemctl >/dev/null 2>&1 && \
| systemctl | grep -- -\.mount >/dev/null 2>&1
|}
|is_upstart() {
| /sbin/init --version | grep upstart >/dev/null 2>&1
|}
|""".stripMargin
inConfig(Debian)(
Seq(
linuxStartScriptTemplate := (packageSource.value / "systemd.service").toURI.toURL,
debianPackageDependencies += "java11-runtime-headless",
serviceAutostart := false,
maintainerScripts := maintainerScriptsFromDirectory(packageSource.value / "debian", Seq("preinst", "postinst", "postrm", "prerm"))
))
commands += Command.command("build") { state =>
"clean" :: "assembly" :: state
}
commands += Command.command("packageAll") { state =>
"assembly" :: "debian:packageBin" :: state
}
// https://stackoverflow.com/a/48592704/4050580
def allProjects: List[ProjectReference] = ReflectUtilities.allVals[Project](this).values.toList map { p =>
p: ProjectReference
}
addCommandAlias("testAll", """; Global / testAllRaw""")
lazy val testAllRaw = taskKey[Unit]("Build a project and run unit tests")
testAllRaw in Global := {
try {
clean.all(ScopeFilter(inProjects(allProjects: _*), inConfigurations(Compile))).value
} finally {
test.all(ScopeFilter(inProjects(langJVM, node), inConfigurations(Test))).value
}
}
lazy val lang =
crossProject(JSPlatform, JVMPlatform)
.withoutSuffixFor(JVMPlatform)
.settings(
version := "0.0.1",
// the following line forces scala version across all dependencies
scalaModuleInfo ~= (_.map(_.withOverrideScalaVersion(true))),
assembly / test := {},
addCompilerPlugin(Dependencies.kindProjector),
libraryDependencies ++=
Dependencies.cats ++
Dependencies.fp ++
Dependencies.scalacheck ++
Dependencies.scorex ++
Dependencies.scalatest ++
Dependencies.scalactic ++
Dependencies.monix.value ++
Dependencies.scodec.value ++
Dependencies.fastparse.value ++
Dependencies.seasalt,
resolvers += Resolver.bintrayIvyRepo("portable-scala", "sbt-plugins")
)
.jsSettings(
scalaJSLinkerConfig ~= {
_.withModuleKind(ModuleKind.CommonJSModule)
}
)
.jvmSettings(
libraryDependencies ++= Seq(
"org.scala-js" %% "scalajs-stubs" % "0.6.22" % "provided"
) ++ Dependencies.logging.map(_ % "test") // scrypto logs an error if a signature verification was failed
)
lazy val langJS = lang.js
lazy val langJVM = lang.jvm
lazy val node = project
.in(file("."))
.settings(
addCompilerPlugin(Dependencies.kindProjector),
libraryDependencies ++=
Dependencies.network ++
Dependencies.db ++
Dependencies.http ++
Dependencies.akka ++
Dependencies.serialization ++
Dependencies.testKit ++
Dependencies.logging ++
Dependencies.matcher ++
Dependencies.metrics ++
Dependencies.fp ++
Dependencies.meta ++
Dependencies.ficus ++
Dependencies.scorex ++
Dependencies.commons_net ++
Dependencies.jaxb_api ++
Dependencies.monix.value
)
.dependsOn(langJVM)