forked from linkerd/linkerd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBase.scala
338 lines (300 loc) · 12.4 KB
/
Base.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
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
import com.typesafe.sbt.SbtScalariform.{scalariformSettings => baseScalariformSettings, _}
import sbt._
import sbt.Keys._
import complete.Parsers.spaceDelimited
import sbtassembly.AssemblyKeys._
import sbtassembly.AssemblyPlugin.assemblySettings
import sbtassembly.{AssemblyUtils, MergeStrategy}
import sbtdocker._
import sbtdocker.DockerKeys._
import sbtdocker.DockerSettings.baseDockerSettings
import scala.language.implicitConversions
import scalariform.formatter.preferences._
import scoverage.ScoverageKeys._
import scoverage.ScoverageSbtPlugin
object Base {
val developTwitterDeps = settingKey[Boolean]("use SNAPSHOT twitter dependencies")
val doDevelopTwitterDeps = (developTwitterDeps in Global) ?? false
val dockerEnvPrefix = settingKey[String]("prefix to be applied to environment variables")
val dockerJavaImage = settingKey[String]("base docker image, providing java")
val dockerTag = settingKey[String]("docker image tag")
val assemblyExecScript = settingKey[Seq[String]]("script used to execute the application")
val configFile = settingKey[File]("path to config file")
val runtimeConfiguration = settingKey[Configuration]("runtime configuration")
}
/**
* Base project configuration.
*/
class Base extends Build {
import Base._
val headVersion = "1.4.6"
val openJdkVersion = "8u151"
val openJ9Version = "jdk8u162-b12_openj9-0.8.0"
object Git {
def git(arg: String, args: String*) = Process("git" +: arg +: args)
val devnull = new ProcessLogger {
def info (s: => String) {}
def error (s: => String) { }
def buffer[T] (f: => T): T = f
}
val noGit = git("status").!<(devnull) != 0
val version = {
if (noGit) headVersion
else {
val headRevision = git("rev-parse", "--short", "HEAD").!!.trim
git("name-rev", "--tags", "--name-only", headRevision).!!.trim match {
case tag if tag == headVersion || tag == s"${headVersion}^0" => headVersion
case _ => s"$headVersion-SNAPSHOT"
}
}
}
val revision = {
if (noGit) ""
else git("rev-parse", "HEAD").!!.trim
}
}
val baseSettings = Seq(
organization := "io.buoyant",
version := Git.version,
homepage := Some(url("https://linkerd.io")),
scalaVersion in GlobalScope := "2.12.1",
crossScalaVersions in GlobalScope := Seq("2.11.11", "2.12.1"),
ivyScala := ivyScala.value.map(_.copy(overrideScalaVersion = true)),
scalacOptions ++=
Seq("-Xfatal-warnings", "-deprecation", "-Ywarn-value-discard", "-feature"),
// XXX
//conflictManager := ConflictManager.strict,
resolvers ++= Seq(
"twitter-repo" at "https://maven.twttr.com",
Resolver.mavenLocal,
"typesafe" at "https://repo.typesafe.com/typesafe/releases"
),
aggregate in assembly := false,
(developTwitterDeps in Global) := { sys.env.get("TWITTER_DEVELOP") == Some("1") },
// Sonatype publishing
publishArtifact in Test := false,
pomIncludeRepository := { _ => false },
publishMavenStyle := true,
pomExtra :=
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0</url>
</license>
</licenses>
<scm>
<url>[email protected]:BuoyantIO/linkerd.git</url>
<connection>scm:git:[email protected]:BuoyantIO/linkerd.git</connection>
</scm>
<developers>
<developer>
<id>buoyant</id>
<name>Buoyant Inc.</name>
<url>https://buoyant.io/</url>
</developer>
</developers>,
publishTo := {
val nexus = "https://oss.sonatype.org/"
if (version.value.trim.endsWith("SNAPSHOT"))
Some("snapshots" at nexus + "content/repositories/snapshots")
else
Some("releases" at nexus + "service/local/staging/deploy/maven2")
},
mappings in (Compile, packageBin) ~= { (ms: Seq[(File,String)]) => ms filter {
case (file,toPath) => {
file.getPath match {
case nodeModulesRE() => false
case _ => true
}
}
}}
)
val aggregateSettings = Seq(
publishArtifact := false
)
val scalariformSettings = baseScalariformSettings ++ Seq(
ScalariformKeys.preferences := ScalariformKeys.preferences.value
.setPreference(DoubleIndentClassDeclaration, false)
.setPreference(PreserveSpaceBeforeArguments, false)
.setPreference(SpacesAroundMultiImports, false)
.setPreference(SpacesWithinPatternBinders, false)
)
val EndToEndTest =
config("e2e") extend Test
val IntegrationTest =
config("integration") extend Test
val defaultExecScript =
"""|#!/bin/sh
|exec "${JAVA_HOME:-/usr}/bin/java" -XX:+PrintCommandLineFlags $JVM_OPTIONS -server -jar $0 "$@"
|""".stripMargin.split("\n").toSeq
val nodeModulesRE = ".*/node_modules/.*".r
/**
* finagle-thrift includes files copied from libthrift that cause assembly merge conflicts. To
* work around this, we use a merge strategy that uses the files from libthrift in the event of
* a conflict. This should be removed once https://github.com/twitter/finagle/issues/688 is
* resolved.
*/
val libthriftMergeStrategy = new MergeStrategy {
override def name: String = "libthrift-merge-strategy"
override def apply(
tempDir: File,
path: String,
files: Seq[File]
): Either[String, Seq[(File, String)]] = {
Right {
files.find { f =>
AssemblyUtils.sourceOfFileForMerge(tempDir, f)._1.getName == "libthrift-0.10.0.jar"
}.map { f =>
f -> path
}.toSeq
}
}
}
val appAssemblySettings = assemblySettings ++ Seq(
assemblyExecScript := defaultExecScript,
assemblyOption in assembly := (assemblyOption in assembly).value.copy(
prependShellScript = assemblyExecScript.value match {
case Nil => None
case script => Some(script)
}),
assemblyJarName in assembly := s"${name.value}-${version.value}-exec",
assemblyMergeStrategy in assembly := {
case nodeModulesRE() => MergeStrategy.discard
case "BUILD" => MergeStrategy.discard
case "com/twitter/common/args/apt/cmdline.arg.info.txt.1" => MergeStrategy.discard
case "META-INF/io.netty.versions.properties" => MergeStrategy.last
case path if path.startsWith("org/apache/thrift/protocol") => libthriftMergeStrategy
case path => (assemblyMergeStrategy in assembly).value(path)
}
)
val appPackagingSettings = baseDockerSettings ++ appAssemblySettings ++ Seq(
assemblyJarName in assembly := s"${name.value}-${version.value}-${configuration.value}-exec",
docker := (docker dependsOn (assembly in configuration)).value,
dockerEnvPrefix := "",
dockerJavaImage := (dockerJavaImage in Global).?(_.getOrElse(s"openjdk:${openJdkVersion}-jre")).value,
dockerfile in docker := new Dockerfile {
val envPrefix = dockerEnvPrefix.value.toUpperCase
val home = s"/${organization.value}/${name.value}/${version.value}"
val exec = s"$home/${configuration.value}-exec"
from(dockerJavaImage.value)
run("mkdir", "-p", home)
workDir(home)
env(envPrefix+"HOME", home)
env(envPrefix+"EXEC", exec)
copy((assemblyOutputPath in assembly).value, exec)
entryPoint(exec)
},
dockerTag += (dockerTag in Global).or( initialize[String] { _ =>
s"$version-$configuration"
}).value,
imageNames in docker := Seq(ImageName(
namespace = Some("buoyantio"),
repository = name.value,
tag = Some(dockerTag.value)
))
)
// Examples are named by a .yaml config file
def exampleSettings(runtime: Project) = Seq(
// The example config file should match the example configuration name.
configFile := file(s"${runtime.id}/examples/${configuration.value}.yaml"),
// The runtime configuration may be different from the example configuration.
runtimeConfiguration := configuration.value,
run := // call linkerd's run command with a config file
Def.inputTaskDyn {
val path = configFile.value.getPath
val args = spaceDelimited("<args>").parsed.mkString(" ")
(run in runtime in runtimeConfiguration.value).toTask(s" $path $args")
}.evaluated
)
// Helper method for constructing projects from directory structure
def projectDir(dir: String): Project =
project(dir.replaceAll("/", "-"), file(dir))
def aggregateDir(dir: String, projects: ProjectReference*): Project =
projectDir(dir).settings(aggregateSettings).aggregate(projects:_*)
def project(id: String, dir: File): Project = Project(id, dir)
.settings(name := id)
.settings(baseSettings)
.settings(scalariformSettings)
/**
* Test utilities (mostly for dealing with async APIs)
*/
val testUtil = projectDir("test-util")
.settings(coverageExcludedPackages := "io.buoyant.test.*")
.settings(libraryDependencies += Deps.scalatest)
.settings(libraryDependencies += Deps.scalacheck)
.settings(libraryDependencies += Deps.junit)
.settings(libraryDependencies ++= {
val deps = Deps.twitterUtil("core") :: Deps.twitterUtil("logging") :: Nil
if (doDevelopTwitterDeps.value) {
deps.map { dep =>
dep.copy(revision = dep.revision+"-SNAPSHOT")
}
} else deps
})
.settings(libraryDependencies ++= Deps.jackson)
/**
* Extends Project with helpers to reduce boilerplate in project definitions.
*/
case class ProjectHelpers(project: Project) {
def configDependsOn(cfg: Configuration)(deps: ProjectReference*): Project =
project.dependsOn(deps.map(_ % cfg): _*)
def withLib(dep: ModuleID): Project =
project.settings(libraryDependencies += dep)
def withLibs(deps: Seq[ModuleID]): Project =
project.settings(libraryDependencies ++= deps)
def withLibs(dep: ModuleID, deps: ModuleID*): Project =
withLibs(dep +: deps)
def configWithLibs(cfg: Configuration)(dep: ModuleID, deps: ModuleID*): Project =
withLibs((dep +: deps).map(_ % cfg))
def withTwitterLib(dep: ModuleID): Project =
project.settings(libraryDependencies += {
if (doDevelopTwitterDeps.value) {
dep.copy(revision = dep.revision+"-SNAPSHOT")
} else dep
})
def withTwitterLibs(deps: Seq[ModuleID]): Project =
deps.foldLeft(project) { case (project, dep) => project.withTwitterLib(dep) }
def withTwitterLibs(dep: ModuleID, deps: ModuleID*): Project =
withTwitterLibs(dep +: deps)
/** Enable the test config for a project with basic dependencies */
def withTests(): Project = project.dependsOn(testUtil % Test)
.settings(inConfig(Test)(Defaults.testSettings ++ Seq(
fork := true,
baseDirectory := new File(".")
)))
/** Enables e2e test config for a project with basic dependencies */
def withE2e(): Project = project
.configs(EndToEndTest)
.settings(inConfig(EndToEndTest)(Defaults.testSettings ++ ScoverageSbtPlugin.projectSettings ++ Seq(
fork := true,
baseDirectory := new File(".")
)))
.settings(libraryDependencies += "org.scoverage" %% "scalac-scoverage-runtime" % "1.3.0" % EndToEndTest)
.dependsOn(testUtil % EndToEndTest)
def withExamples(runtime: Project, configs: Seq[(Configuration, Configuration)]): Project = {
val settings = exampleSettings(runtime)
configs.foldLeft(project) {
case (project, (egConfig, runConfig)) =>
project.configs(egConfig)
.settings(inConfig(egConfig)(settings :+ (runtimeConfiguration := runConfig)))
.dependsOn(runtime % s"${egConfig}->${runConfig}")
}
}
def withIntegration(): Project = project
.configs(IntegrationTest)
.settings(inConfig(IntegrationTest)(Defaults.testSettings :+ (parallelExecution := false)))
.dependsOn(testUtil % IntegrationTest)
/** Writes build metadata into the projects resources */
def withBuildProperties(path: String): Project = project
.settings((resourceGenerators in Compile) += task[Seq[File]] {
val dir = (resourceManaged in Compile).value
val rev = Git.revision
val build = new java.text.SimpleDateFormat("yyyyMMdd-HHmmss").format(new java.util.Date)
val contents = s"name=${name.value}\nversion=${version.value}\nbuild_revision=$rev\nbuild_name=$build"
val file = dir / path / "build.properties"
IO.write(file, contents)
Seq(file)
})
}
implicit def pimpMyProject(p: Project): ProjectHelpers = ProjectHelpers(p)
}