forked from scalacenter/scastie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSbtShared.scala
202 lines (165 loc) · 5.3 KB
/
SbtShared.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
import sbt._
import Keys._
import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport._
import org.scalajs.sbtplugin.cross.CrossProject
import sbtbuildinfo.BuildInfoPlugin
import sbtbuildinfo.BuildInfoPlugin.autoImport._
import java.util.Properties
import java.nio.file._
import java.io.FileInputStream
/*
This code is shared between the build and the "build-build".
it allows us to use the api project at the build level
to generate the docker image for the different
configuration matrix
*/
object SbtShared {
val sbt210 = "2.10.7"
val latest211 = "2.11.12"
val latest212 = "2.12.8"
val latest213 = "2.13.0-RC1"
val currentScalaVersion = latest212
val latestScalaJs = "0.6.26"
val latestDotty = "0.7.0-RC1"
val latestCoursier = "1.0.3"
val sbtVersion = "0.13.18"
val runtimeProjectName = "runtime-scala"
def gitIsDirty(): Boolean = {
Process("git diff-files --quiet").! == 1
}
def gitHash(): String = {
import sys.process._
if (!sys.env.contains("CI")) {
val indexState =
if (gitIsDirty()) "-dirty"
else ""
Process("git rev-parse --verify HEAD").lines.mkString("") + indexState
} else "CI"
}
val gitHashNow = gitHash()
val gitIsDirtyNow = gitIsDirty()
val versionNow = {
val base = "0.27.0"
if (gitIsDirtyNow)
base + "-SNAPSHOT"
else {
val hash = gitHashNow
s"$base+$hash"
}
}
val playJsonVersion = "2.6.9"
val scalajsDomVersion = "0.9.3"
val playJson =
libraryDependencies += toScalaJSGroupID("com.typesafe.play") %%% "play-json" % playJsonVersion
lazy val baseSettings = Seq(
// skip scaladoc
publishArtifact in (Compile, packageDoc) := false,
publishArtifact in packageDoc := false,
publishArtifact in packageSrc := false,
sources in (Compile, doc) := Seq.empty,
parallelExecution in Test := false,
scalaVersion := currentScalaVersion,
scalacOptions ++= {
val scalaV = scalaVersion.value
val base =
Seq(
"-deprecation",
"-encoding",
"UTF-8",
"-feature",
"-unchecked"
)
if (scalaV == sbt210) base
else {
base ++
Seq(
"-Yrangepos",
"-Ywarn-unused-import",
"-Ywarn-adapted-args"
)
}
},
console := (console in Test).value,
scalacOptions in (Test, console) -= "-Ywarn-unused-import",
scalacOptions in (Compile, consoleQuick) -= "-Ywarn-unused-import"
) ++ orgSettings
lazy val orgSettings = Seq(
organization := "org.scastie",
version := versionNow
)
val baseJsSettings = Seq(
scalacOptions += "-P:scalajs:sjsDefinedByDefault"
)
def crossDir(projectId: String) = file(".cross/" + projectId)
def dash(name: String) = name.replaceAllLiterally(".", "-")
/* api is for the communication between sbt <=> server <=> frontend */
def apiProject(scalaV: String, fromSbt: Boolean = false) = {
val projectName = "api"
val projectId0 =
if (scalaV != currentScalaVersion) {
s"$projectName-${dash(scalaV)}"
} else projectName
val extra =
if (fromSbt) "-sbt"
else ""
val projectId = projectId0 + extra
def src(config: String): Def.Initialize[File] = Def.setting {
val base0 = (baseDirectory in ThisBuild).value
val base =
if (fromSbt) base0.getParentFile
else base0
base / projectName / "src" / config / "scala"
}
def readSbtVersion(base: Path): String = {
val sbtPropertiesFileName = "build.properties"
val projectFolder = "project"
val to = Paths.get(projectFolder, sbtPropertiesFileName)
val guess1 = base.resolve(to)
val guess2 = base.getParent.resolve(to)
val sbtPropertiesFile =
if (Files.isRegularFile(guess1)) guess1
else if (Files.isRegularFile(guess2)) guess2
else {
sys.error(
s"cannot find $sbtPropertiesFileName in $guess1 and $guess2"
)
}
val prop = new Properties()
val input = new FileInputStream(sbtPropertiesFile.toFile)
prop.load(input)
input.close()
val res = prop.getProperty("sbt.version")
assert(res != null)
res
}
CrossProject(id = projectId, base = crossDir(projectId), crossType = CrossType.Pure)
.settings(baseSettings)
.settings(
buildInfoKeys := Seq[BuildInfoKey](
organization,
version,
"runtimeProjectName" -> runtimeProjectName,
"defaultScalaVersion" -> latest212,
"defaultScalaJsVersion" -> latestScalaJs,
"defaultDottyVersion" -> latestDotty,
"latestCoursier" -> latestCoursier,
"sbtVersion" -> readSbtVersion(
(baseDirectory in ThisBuild).value.toPath
),
BuildInfoKey.action("gitHash") { gitHashNow }
),
buildInfoPackage := "com.olegych.scastie.buildinfo",
scalaVersion := scalaV,
moduleName := projectName,
unmanagedSourceDirectories in Compile += src("main").value,
unmanagedSourceDirectories in Test += src("test").value
)
.settings(playJson)
.jsSettings(baseJsSettings)
.jsSettings(
test := {},
libraryDependencies += toScalaJSGroupID("org.scala-js") %%% "scalajs-dom" % scalajsDomVersion
)
.enablePlugins(BuildInfoPlugin)
}
}