-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.sbt
217 lines (199 loc) · 7.18 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
import sbtcrossproject.CrossType
import scala.collection.JavaConverters._
import java.lang.management.ManagementFactory
import Common.isScala3
lazy val nobox = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.crossType(CustomCrossType)
.in(file("."))
.enablePlugins(BuildInfoPlugin)
.settings(
Common.commonSettings,
scalapropsCoreSettings,
libraryDependencies ++= (
("com.github.scalaprops" %%% "scalaprops" % "0.9.1" % "test") ::
Nil
),
(Compile / unmanagedResources) += (LocalRootProject / baseDirectory).value / "LICENSE.txt",
name := "nobox",
licenses := Seq("MIT" -> url("http://opensource.org/licenses/MIT")),
console / initialCommands := "import nobox._",
startYear := Some(2013),
description := "immutable primitive array wrapper for Scala",
pomExtra := (
<url>https://github.com/xuwei-k/nobox</url>
<developers>
<developer>
<id>xuwei-k</id>
<name>Kenji Yoshida</name>
<url>https://github.com/xuwei-k</url>
</developer>
</developers>
<scm>
<url>[email protected]:xuwei-k/nobox.git</url>
<connection>scm:git:[email protected]:xuwei-k/nobox.git</connection>
<tag>{gitTagOrHash.value}</tag>
</scm>
),
printInfo := printInfo.dependsOn(compile).evaluated,
printInfo := {
val srcs = (Compile / scalaSource).value
val files = (srcs ** "*.scala").get.map(f => f -> IO.readLines(f)).sortBy(_._1)
println("all lines " + files.map(_._2.size).sum)
files.foreach{ case (file, lines) =>
println(file.getName + " " + lines.size)
}
(Test / runMain).fullInput(" nobox.Info").evaluated
},
pomPostProcess := { node =>
import scala.xml._
import scala.xml.transform._
def stripIf(f: Node => Boolean) = new RewriteRule {
override def transform(n: Node) =
if (f(n)) NodeSeq.Empty else n
}
val stripTestScope = stripIf { n => n.label == "dependency" && (n \ "scope").text == "test" }
new RuleTransformer(stripTestScope).transform(node)(0)
},
credentials ++= PartialFunction.condOpt(sys.env.get("SONATYPE_USER") -> sys.env.get("SONATYPE_PASS")){
case (Some(user), Some(pass)) =>
Credentials("Sonatype Nexus Repository Manager", "oss.sonatype.org", user, pass)
}.toList,
buildInfoKeys := Seq[BuildInfoKey](
organization,
name,
version,
scalaVersion,
sbtVersion,
licenses
),
buildInfoPackage := "nobox",
buildInfoObject := "BuildInfoNobox",
(Compile / sourceGenerators) += Def.sequential(
Def.task {
IO.delete(generateDir.value)
},
Def.task {
val cp = (generator / Compile / fullClasspath).value
val _ = (generator / Compile / runner).value.run(
mainClass = "nobox.Generate",
classpath = Attributed.data(cp),
options = Seq(generateDir.value.toString),
log = streams.value.log
)
},
Def.task {
(generateDir.value ** "*.scala").get
}
),
(Compile / packageSrc / mappings) ++= (Compile / managedSources).value.map{ f =>
// to merge generated sources into sources.jar as well
(f, f.relativeTo((Compile / sourceManaged).value).get.getPath.replace(generateDirName, "nobox").replace("sbt-buildinfo", "nobox"))
},
checkPackage := {
println(IO.read(makePom.value))
println()
IO.withTemporaryDirectory{ dir =>
IO.unzip((Compile / packageSrc).value, dir).map(f => f.getName -> f.length) foreach println
}
},
benchmark := {
val args = benchmarkArgsParser.parsed
val ((classes, names), sizes) = args
val cp = (Test / fullClasspath).value
val r = (Test / runner).value
classes.foreach{ clazz =>
def run(s: Option[String]) = r.run(
"nobox."+clazz,
Attributed.data(cp),
s.toList ++ names,
streams.value.log
)
if(sizes.isEmpty) run(None)
else sizes.foreach(run)
}
}
).platformsSettings(JVMPlatform, JSPlatform)(
(Test / unmanagedSourceDirectories) += {
baseDirectory.value.getParentFile / "jvm_js/src/test/scala/"
}
).jsSettings(
scalacOptions ++= {
val a = (LocalRootProject / baseDirectory).value.toURI.toString
val g = "https://raw.githubusercontent.com/xuwei-k/nobox/" + gitTagOrHash.value
if (isScala3.value) {
Seq(s"-scalajs-mapSourceURI:$a->$g/")
} else {
Seq(s"-P:scalajs:mapSourceURI:$a->$g/")
}
}
).jvmSettings(
javaOptions ++= "-Djava.awt.headless=true" +: ManagementFactory.getRuntimeMXBean.getInputArguments.asScala.toList.filter(
a => Seq("-Xmx","-Xms","-XX").exists(a.startsWith)
)
).nativeSettings(
scalapropsNativeSettings,
)
lazy val noboxJVM = nobox.jvm
lazy val noboxJS = nobox.js
lazy val noboxNative = nobox.native
lazy val notPublish = Seq(
publishArtifact := false,
publish := {},
publishLocal := {},
PgpKeys.publishSigned := {},
PgpKeys.publishLocalSigned := {}
)
lazy val root = project.in(file(".")).aggregate(
noboxJVM, noboxJS // exclude noboxNative on purpose
).settings(
Common.commonSettings,
notPublish,
Compile / scalaSource := baseDirectory.value / "dummy",
Test / scalaSource := baseDirectory.value / "dummy"
)
lazy val gitTagOrHash = Def.setting {
if(isSnapshot.value) {
sys.process.Process("git rev-parse HEAD").lineStream_!.head
} else {
"v" + version.value
}
}
lazy val benchmark = inputKey[Unit]("benchmark")
lazy val benchmarkClasses = Set("IntBenchmark", "RefBenchmark")
lazy val seqMethods = classOf[Seq[_]].getMethods.map(_.getName).filterNot(_ contains '$').toSet
lazy val benchmarkArgsParser = {
import sbt.complete.Parser._
import sbt.complete.Parsers._
val classes0 = (token(Space) ~> benchmarkClasses.map(token(_)).reduceLeft(_ | _)).* !!! "please input Benchmark classes"
// run all benchmark when does not specified any benchmark class name
val classes = classes0.map(c => if(c.isEmpty) benchmarkClasses else c)
val names = (token(Space) ~> ScalaID.examples(seqMethods)).* !!! "please input method names"
val size = (token(Space) ~> NatBasic.examples().map(s => Option(s.toString))).* !!! "please input array size"
(classes ~ names ~ size)
}
lazy val printInfo = inputKey[Unit]("print each file line counts")
lazy val checkPackage = taskKey[Unit]("show pom.xml and sources.jar")
lazy val generateDirName = "generate"
lazy val generateDir = Def.setting {
(Compile / sourceManaged).value / generateDirName
}
lazy val generator = (project in file("generator")).settings(
Common.commonSettings,
notPublish
)
lazy val CustomCrossType = new sbtcrossproject.CrossType {
override def projectDir(crossBase: File, projectType: String) =
crossBase / projectType
override def projectDir(crossBase: File, projectType: sbtcrossproject.Platform) = {
val dir = projectType match {
case JVMPlatform => "jvm"
case JSPlatform => "js"
case NativePlatform => "native"
}
crossBase / dir
}
def shared(projectBase: File, conf: String) =
projectBase.getParentFile / "src" / conf / "scala"
override def sharedSrcDir(projectBase: File, conf: String) =
Some(shared(projectBase, conf))
}