-
Notifications
You must be signed in to change notification settings - Fork 22
/
generateBloopFiles.sc
217 lines (181 loc) · 7.2 KB
/
generateBloopFiles.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
/**
* Generates bloop project files for all libraries which compiles
* for the current version of ScalablyTyped.
*
* After generation you can say something like this:
* `ls .bloop/ -1|cut -d. -f1|xargs bloop compile --pipeline`
* to compile all libraries.
*
* Note: This script is written with ammonite 1.6.9 for scala *2.12*.
*
* 2.12 is necessary to use bloop libraries. If you follow the default
* instructions on ammonite.io you'll get 2.13, you'll need to change
* the version number in the installation command yourself.
*/
import $ivy.`ch.epfl.scala::bloop-backend:1.3.2`, bloop.io.AbsolutePath
import $ivy.`ch.epfl.scala::bloop-config:1.3.2`, bloop.config
import $ivy.`ch.epfl.scala::bloop-frontend:1.3.2`, bloop._
import ammonite.ops._
import concurrent.ExecutionContext.Implicits.global
import collection.mutable
object utils {
val Quote = '"'.toString
def unquote(str: String) =
if (str.startsWith(Quote) && str.endsWith(Quote)) str.drop(1).dropRight(1) else str
def quote(str: String) =
if (str.startsWith(Quote) && str.endsWith(Quote)) str else s"$Quote$str$Quote"
}
import utils._
class FileLayout(val base: os.Path) {
val bloop = base / ".bloop"
/* luckily it's not as if we're leaking implementation details in the public json file */
val allLibraries: Seq[os.Path] = {
val arr = ujson.read(os.read(os.pwd / "summary.json"))("successes").arr
arr.map {
case ujson.Obj(x) =>
if (x.get("TsIdentLibrarySimple").isDefined) x("TsIdentLibrarySimple")("value").str
else s"${x("TsIdentLibraryScoped")("scope").str}__${x("TsIdentLibraryScoped")("nameOpt").str}"
}.map(x => x.replaceAllLiterally(".", "_dot_")).sorted.map(pathFor)
}
def libNameFor(p: os.Path): String =
if (p.segments.contains("facades")) s"${p.last}-facade" else p.last
def pathFor(libName: String) = {
if (libName.endsWith("-facade")) base / 'facades / libName.dropRight("-facade".length)
else base / libName.take(1) / libName
}
}
trait Versions {
val scalaVersion: String
val binVersion: String
val scalaJsVersion: String
val scalaJsBinVersion: String
final def s(artifact: String): String =
s"${artifact}_$binVersion"
final def sjs(artifact: String): String =
s"${artifact}_sjs${scalaJsBinVersion}_$binVersion"
lazy val scala = Dep("org.scala-lang", "scala-compiler", "scala-compiler", scalaVersion)
lazy val scalaJs = Dep("org.scala-js", "scalajs-library", s("scalajs-library"), scalaJsVersion)
lazy val scalaJsCompiler = Dep("org.scala-js", "scalajs-compiler", s"scalajs-compiler_${scalaVersion}", scalaJsVersion)
lazy val runtime = Dep("com.olvind", "scalablytyped-runtime", sjs("scalablytyped-runtime"), "2.1.0")
val scalacOptions: List[String]
}
object version212 extends Versions {
val scalaVersion = "2.12.8"
val scalaJsVersion = "0.6.28"
val binVersion = "2.12"
val scalaJsBinVersion = "0.6"
val scalacOptions = List("-P:scalajs:sjsDefinedByDefault", "-g:notailcalls")
}
/* everything except facades should compile under 2.12 and scala.js 1.0.0-M8 */
object version212_scalajs_1 extends Versions {
val scalaVersion = "2.12.8"
val scalaJsVersion = "1.0.0-M8"
val binVersion = "2.12"
val scalaJsBinVersion = "1.0.0-M8"
val scalacOptions = List("-g:notailcalls")
}
/* everything except japgolly facades should compile under 2.13 */
object version213 extends Versions {
val scalaVersion = "2.13.0"
val scalaJsVersion = "0.6.28"
val binVersion = "2.13"
val scalaJsBinVersion = "0.6"
val scalacOptions = List("-P:scalajs:sjsDefinedByDefault", "-g:notailcalls")
}
val version = version212
case class Dep(org: String, name: String, artifact: String, version: String)
/**
* @param scalaVersion not used
* @param scalacOptions not used
*/
case class Project(libDir: os.Path, self: Dep, scalaVersion: String, deps: Seq[Dep], scalacOptions: Seq[String])
object Project {
def parseSbtProjectIn(libDir: os.Path): Project = {
val lines = os.read(libDir / "build.sbt").split("\n")
val v = lines.map(_.split(":=|\\+\\+=|\\+=")).collect { case Array(key, value) => key.trim -> unquote(value.trim) }.toMap
val deps = lines.map(_.split("%").filter(_.nonEmpty)).collect {
case Array(org, n, v) =>
val name = unquote(n.trim)
Dep(unquote(org.trim), name, version.sjs(name), unquote(v.trim.dropRight(1)))
}
val scalacOptions = v("scalacOptions").dropWhile(_ != '"').dropRight(1).split(",").map(str => unquote(str.trim))
val name = v("name")
Project(libDir, Dep(v("organization"), name, version.sjs(name), v("version")), v("scalaVersion"), deps, scalacOptions)
}
}
object Resolve {
val bloopLogger = logging.BloopLogger.at(
name = "logger",
out = System.out,
err = System.err,
isVerbose = true,
colorOutput = true,
filter = logging.DebugFilter.All
)
private val cache = mutable.Map.empty[Dep, Array[AbsolutePath]]
def apply(dep: Dep) =
cache.getOrElseUpdate(
dep,
DependencyResolution.resolve(dep.org, dep.artifact, dep.version, bloopLogger)
)
}
val scalaCompilerJars: Array[AbsolutePath] =
Resolve(version.scala)
val globalClasspath: Array[AbsolutePath] =
Array(
scalaCompilerJars.collect { case path if path.underlying.toString.contains("scala-library") => path },
Resolve(version.scalaJs),
Resolve(version.runtime)
).flatten
val scalaJsCompilerJar =
Resolve(version.scalaJsCompiler).collectFirst { case f if f.syntax.contains("scalajs-compiler") => f }.head
def bloopFileFor(layout: FileLayout, p: Project): config.Config.File = {
def classesDirFor(path: os.Path) =
(path / 'target / s"scala-${version.binVersion}" / 'classes).toNIO
val (internal, external) = p.deps.partition(_.org == "org.scalablytyped")
val classpath = {
val fromGlobal = globalClasspath.map(_.underlying)
val fromExternal = external.flatMap(Resolve.apply).map(_.underlying)
val fromInternal = internal.map(d => classesDirFor(layout.pathFor(d.name)))
fromGlobal.toList ++ fromExternal ++ fromInternal
}
config.Config.File(
"1.3.2",
config.Config.Project(
name = p.self.name,
directory = p.libDir.toNIO,
sources = (p.libDir / 'src / 'main / 'scala).toNIO :: Nil,
dependencies = internal.map(_.name).toList,
classpath = classpath,
out = (p.libDir / 'target).toNIO,
classesDir = classesDirFor(p.libDir),
resources = None,
scala = Some(
config.Config.Scala(
organization = version.scala.org,
name = version.scala.name,
version = version.scala.version,
options = List("-Xplugin:" + scalaJsCompilerJar.syntax) ++ version.scalacOptions,
jars = scalaCompilerJars.toList.map(_.underlying),
analysis = None,
setup = None,
),
),
java = None,
sbt = None,
test = None,
platform = None,
resolution = None,
),
)
}
val layout = new FileLayout(os.pwd)
println(s"Generating files for ${layout.allLibraries.length} libraries")
os.remove.all(layout.bloop)
os.makeDir(layout.bloop)
layout.allLibraries.map {
libPath =>
val project = Project.parseSbtProjectIn(libPath)
val bloopConfig = bloopFileFor(layout, project)
config.write(bloopConfig, (layout.bloop / (layout.libNameFor(libPath) + ".json")).toNIO)
}