Skip to content

Commit 77bde06

Browse files
Merge pull request #96 from alexarchambault/develop
Minor refactoring / adjustments
2 parents 0a687c0 + 79f12cf commit 77bde06

File tree

11 files changed

+192
-168
lines changed

11 files changed

+192
-168
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ jobs:
189189
- uses: coursier/[email protected]
190190
with:
191191
jvm: 8
192-
- run: ./mill -i ci.copyVcRedist || true
192+
- run: ./mill -i ci.copyVcRedist
193193
- uses: actions/[email protected]
194194
with:
195195
name: launchers

modules/build/src/main/scala/scala/build/BloopBuildClient.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ch.epfl.scala.bsp4j
55
import java.io.PrintStream
66

77
trait BloopBuildClient extends bsp4j.BuildClient {
8+
def setProjectParams(newParams: Seq[String]): Unit
89
def setGeneratedSources(newGeneratedSources: Seq[GeneratedSource]): Unit
910
def diagnostics: Option[Seq[(Either[String, os.Path], bsp4j.Diagnostic)]]
1011
def clear(): Unit

modules/build/src/main/scala/scala/build/Build.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ object Build {
124124

125125
val generatedSources = sources.generateSources(inputs0.generatedSrcRoot)
126126

127+
buildClient.setProjectParams(options0.projectParams)
127128
build(
128129
inputs0,
129130
sources,
@@ -301,7 +302,7 @@ object Build {
301302

302303
val classesDir0 = classesDir(inputs.workspace, inputs.projectName)
303304

304-
val artifacts = options.artifacts(params, logger)
305+
val artifacts = options.artifacts(logger)
305306

306307
val pluginScalacOptions = artifacts.compilerPlugins.map {
307308
case (_, _, path) =>

modules/build/src/main/scala/scala/build/ConsoleBloopBuildClient.scala

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ class ConsoleBloopBuildClient(
1616
var generatedSources: Seq[GeneratedSource] = Nil
1717
) extends BloopBuildClient {
1818

19+
private var projectParams = Seq.empty[String]
20+
21+
private def projectNameSuffix =
22+
if (projectParams.isEmpty) ""
23+
else " (" + projectParams.mkString(", ") + ")"
24+
25+
private def projectName = "project" + projectNameSuffix
26+
1927
private var printedStart = false
2028
private val gray = "\u001b[90m"
2129
private val reset = Console.RESET
@@ -25,6 +33,9 @@ class ConsoleBloopBuildClient(
2533
def setGeneratedSources(newGeneratedSources: Seq[GeneratedSource]) = {
2634
generatedSources = newGeneratedSources
2735
}
36+
def setProjectParams(newParams: Seq[String]): Unit = {
37+
projectParams = newParams
38+
}
2839
def diagnostics: Option[Seq[(Either[String, os.Path], bsp4j.Diagnostic)]] =
2940
if (keepDiagnostics) Some(diagnostics0.result())
3041
else None
@@ -115,7 +126,10 @@ class ConsoleBloopBuildClient(
115126
logger.debug("Received onBuildTaskStart from bloop: " + params)
116127
for (msg <- Option(params.getMessage) if !msg.contains(" no-op compilation")) {
117128
printedStart = true
118-
out.println(gray + msg + reset)
129+
val msg0 =
130+
if (params.getDataKind == "compile-task") s"Compiling $projectName"
131+
else msg
132+
out.println(gray + msg0 + reset)
119133
}
120134
}
121135

@@ -125,8 +139,18 @@ class ConsoleBloopBuildClient(
125139
override def onBuildTaskFinish(params: bsp4j.TaskFinishParams): Unit = {
126140
logger.debug("Received onBuildTaskFinish from bloop: " + params)
127141
if (printedStart)
128-
for (msg <- Option(params.getMessage))
129-
out.println(gray + msg + reset)
142+
for (msg <- Option(params.getMessage)) {
143+
val msg0 =
144+
if (params.getDataKind == "compile-report")
145+
params.getStatus match {
146+
case bsp4j.StatusCode.OK => s"Compiled $projectName"
147+
case bsp4j.StatusCode.ERROR => s"Error compiling $projectName"
148+
case bsp4j.StatusCode.CANCELLED => s"Compilation cancelled$projectNameSuffix"
149+
case _ => s"Compiled $projectName" // ???
150+
}
151+
else msg
152+
out.println(gray + msg0 + reset)
153+
}
130154
}
131155

132156
override def onConnectWithServer(server: bsp4j.BuildServer): Unit = {}

modules/build/src/main/scala/scala/build/Sources.scala

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -35,81 +35,12 @@ final case class Sources(
3535

3636
object Sources {
3737

38-
def process(path: os.Path): Option[(Seq[String], String)] = {
39-
val printablePath =
40-
if (path.startsWith(Os.pwd)) path.relativeTo(Os.pwd).toString
41-
else path.toString
42-
val content = os.read(path)
43-
process(content, printablePath)
44-
}
45-
def process(content: String, printablePath: String): Option[(Seq[String], String)] = {
46-
47-
import fastparse._
48-
import scalaparse._
49-
import scala.build.internal.ScalaParse._
50-
51-
val res = parse(content, Header(_))
52-
53-
val indicesOrFailingIdx0 = res.fold((_, idx, _) => Left(idx), (value, _) => Right(value))
54-
55-
val indicesOrErrorMsg = indicesOrFailingIdx0 match {
56-
case Left(failingIdx) =>
57-
val newCode = content.take(failingIdx)
58-
val res1 = parse(newCode, Header(_))
59-
res1 match {
60-
case f: Parsed.Failure =>
61-
val msg = formatFastparseError(printablePath, content, f)
62-
Left(msg)
63-
case s: Parsed.Success[Seq[(Int, Int)]] =>
64-
Right(s.value)
65-
}
66-
case Right(ind) =>
67-
Right(ind)
68-
}
69-
70-
// TODO Report error if indicesOrErrorMsg.isLeft?
71-
72-
val importTrees = indicesOrErrorMsg.right.toSeq.iterator.flatMap(_.iterator).flatMap {
73-
case (start, end) =>
74-
val code = content.substring(start, end)
75-
// .trim // meh
76-
val importRes = parse(code, ImportSplitter(_))
77-
importRes.fold((_, _, _) => Iterator.empty, (trees, _) => trees.iterator).map { tree =>
78-
tree.copy(start = start + tree.start, end = start + tree.end)
79-
}
80-
}.toVector
81-
82-
val dependencyTrees = importTrees.filter { t =>
83-
val firstSegmentOpt = t.prefix.headOption
84-
firstSegmentOpt.contains("$ivy") || firstSegmentOpt.contains("$dep")
85-
}
86-
87-
if (dependencyTrees.isEmpty) None
88-
else {
89-
// replace statements like
90-
// import $ivy.`foo`,
91-
// by
92-
// import $ivy.A ,
93-
// Ideally, we should just wipe those statements, and take care of keeping 'import' and ','
94-
// for standard imports.
95-
val buf = content.toCharArray
96-
for (t <- dependencyTrees) {
97-
val substitute = (t.prefix(0) + ".A").padTo(t.end - t.start, ' ')
98-
assert(substitute.length == (t.end - t.start))
99-
System.arraycopy(substitute.toArray, 0, buf, t.start, substitute.length)
100-
}
101-
val newCode = new String(buf)
102-
Some((dependencyTrees.map(_.prefix.drop(1).mkString(".")), newCode))
103-
}
104-
}
105-
106-
10738
def defaultPreprocessors(codeWrapper: CodeWrapper): Seq[Preprocessor] =
10839
Seq(
10940
ScriptPreprocessor(codeWrapper),
11041
JavaPreprocessor,
11142
ConfigPreprocessor,
112-
ScalaFilePreprocessor
43+
ScalaPreprocessor
11344
)
11445

11546
def forInputs(

modules/build/src/main/scala/scala/build/bsp/BspClient.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class BspClient(
8686
}
8787
}
8888

89+
def setProjectParams(newParams: Seq[String]): Unit = {}
8990
def diagnostics: Option[Seq[(Either[String, os.Path], b.Diagnostic)]] = None
9091
def clear(): Unit = {}
9192
}

modules/build/src/main/scala/scala/build/bsp/BspImpl.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ object BspImpl {
274274
def underlying = actualLocalClient
275275
def clear() = underlying.clear()
276276
def diagnostics = underlying.diagnostics
277+
def setProjectParams(newParams: Seq[String]) =
278+
underlying.setProjectParams(newParams)
277279
def setGeneratedSources(newGeneratedSources: Seq[GeneratedSource]) =
278280
underlying.setGeneratedSources(newGeneratedSources)
279281
}

modules/build/src/main/scala/scala/build/options/BuildOptions.scala

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,46 @@ final case class BuildOptions(
3030
replOptions: ReplOptions = ReplOptions()
3131
) {
3232

33+
lazy val projectParams: Seq[String] = {
34+
val platform =
35+
if (scalaJsOptions.enable) "Scala.JS"
36+
else if (scalaNativeOptions.enable) "Scala Native"
37+
else "JVM"
38+
Seq(s"Scala ${scalaParams.scalaVersion}", platform)
39+
}
40+
3341
def addRunnerDependency: Option[Boolean] =
3442
internalDependencies.addRunnerDependencyOpt
3543
.orElse(if (scalaJsOptions.enable || scalaNativeOptions.enable) Some(false) else None)
3644

37-
private def scalaLibraryDependencies(params: ScalaParameters): Seq[AnyDependency] =
45+
private def scalaLibraryDependencies: Seq[AnyDependency] =
3846
if (scalaOptions.addScalaLibrary.getOrElse(true)) {
3947
val lib =
40-
if (params.scalaVersion.startsWith("3."))
41-
dep"org.scala-lang::scala3-library::${params.scalaVersion}"
48+
if (scalaParams.scalaVersion.startsWith("3."))
49+
dep"org.scala-lang::scala3-library::${scalaParams.scalaVersion}"
4250
else
43-
dep"org.scala-lang:scala-library:${params.scalaVersion}"
51+
dep"org.scala-lang:scala-library:${scalaParams.scalaVersion}"
4452
Seq(lib)
4553
}
4654
else Nil
4755

48-
def dependencies(params: ScalaParameters): Seq[AnyDependency] =
49-
scalaJsOptions.jsDependencies(params.scalaVersion) ++
56+
private def dependencies: Seq[AnyDependency] =
57+
scalaJsOptions.jsDependencies(scalaParams.scalaVersion) ++
5058
scalaNativeOptions.nativeDependencies ++
51-
scalaLibraryDependencies(params) ++
59+
scalaLibraryDependencies ++
5260
classPathOptions.extraDependencies
5361

54-
private def semanticDbPlugins(params: ScalaParameters): Seq[AnyDependency] =
55-
if (scalaOptions.generateSemanticDbs.getOrElse(false) && params.scalaVersion.startsWith("2."))
62+
private def semanticDbPlugins: Seq[AnyDependency] =
63+
if (scalaOptions.generateSemanticDbs.getOrElse(false) && scalaParams.scalaVersion.startsWith("2."))
5664
Seq(
5765
dep"$semanticDbPluginOrganization:::$semanticDbPluginModuleName:$semanticDbPluginVersion"
5866
)
5967
else Nil
6068

61-
def compilerPlugins(params: ScalaParameters): Seq[AnyDependency] =
62-
scalaJsOptions.compilerPlugins(params.scalaVersion) ++
69+
def compilerPlugins: Seq[AnyDependency] =
70+
scalaJsOptions.compilerPlugins(scalaParams.scalaVersion) ++
6371
scalaNativeOptions.compilerPlugins ++
64-
semanticDbPlugins(params)
72+
semanticDbPlugins
6573

6674
def allExtraJars: Seq[Path] =
6775
classPathOptions.extraJars.map(_.toNIO)
@@ -163,19 +171,19 @@ final case class BuildOptions(
163171
(sv, sbv)
164172
}
165173

166-
def scalaParams: ScalaParameters = {
174+
lazy val scalaParams: ScalaParameters = {
167175
val (scalaVersion, scalaBinaryVersion) = computeScalaVersions(scalaOptions.scalaVersion, scalaOptions.scalaBinaryVersion)
168176
val maybePlatformSuffix =
169177
scalaJsOptions.platformSuffix
170178
.orElse(scalaNativeOptions.platformSuffix)
171179
ScalaParameters(scalaVersion, scalaBinaryVersion, maybePlatformSuffix)
172180
}
173181

174-
def artifacts(params: ScalaParameters, logger: Logger): Artifacts =
182+
def artifacts(logger: Logger): Artifacts =
175183
Artifacts(
176-
params = params,
177-
compilerPlugins = compilerPlugins(params),
178-
dependencies = dependencies(params),
184+
params = scalaParams,
185+
compilerPlugins = compilerPlugins,
186+
dependencies = dependencies,
179187
extraJars = allExtraJars,
180188
extraCompileOnlyJars = allExtraCompileOnlyJars,
181189
extraSourceJars = allExtraSourceJars,

modules/build/src/main/scala/scala/build/preprocessing/ScalaFilePreprocessor.scala

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)