Skip to content

Commit

Permalink
Merge pull request #198 from alexarchambault/tweaking
Browse files Browse the repository at this point in the history
Tweak build / address compiler warnings
  • Loading branch information
alexarchambault committed Oct 8, 2021
2 parents ecb1a6e + e332537 commit a62e63e
Show file tree
Hide file tree
Showing 44 changed files with 482 additions and 314 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Scala CLI is an experimental tool to run/compile/test Scala that aims at being a

Building Scala CLI requires:
- a JVM (>= 8)
- [`cs`](https://get-coursier.io/docs/cli-installation#native-launcher) in the `PATH` (running just `cs` in the terminal needs to work)

The Scala CLI sources ship with Mill launchers, so that Mill itself doesn't need to be installed on your system.

Expand Down
45 changes: 26 additions & 19 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,21 @@ object dummy extends Module {
def ivyDeps = Agg(
Deps.ammonite
)
def compile = T {
resolvedRunIvyDeps()
null: mill.scalalib.api.CompilationResult
}
}
object scalafmt extends ScalaModule with Bloop.Module {
def skipBloop = true
def scalaVersion = Scala.defaultInternal
def ivyDeps = Agg(
Deps.scalafmtCli
)
def compile = T {
resolvedRunIvyDeps()
null: mill.scalalib.api.CompilationResult
}
}
}

Expand All @@ -144,7 +152,7 @@ class Build(val crossScalaVersion: String)
`tasty-lib`()
)
def scalacOptions = T {
super.scalacOptions() ++ Seq("-Xasync", "-Ywarn-unused")
super.scalacOptions() ++ Seq("-Xasync", "-Ywarn-unused", "-deprecation")
}
def repositories = super.repositories ++ Seq(
coursier.Repositories.sonatype("snapshots")
Expand All @@ -155,6 +163,7 @@ class Build(val crossScalaVersion: String)
def ivyDeps = super.ivyDeps() ++ Agg(
Deps.asm,
Deps.bloopConfig,
Deps.collectionCompat,
Deps.coursierJvm
// scalaJsEnvNodeJs brings a guava version that conflicts with this
.exclude(("com.google.collections", "google-collections")),
Expand All @@ -173,16 +182,11 @@ class Build(val crossScalaVersion: String)
Deps.swoval
)

private def vcsState = {
val isCI = System.getenv("CI") != null
if (isCI)
T.persistent {
VcsVersion.vcsState()
}
else
T {
VcsVersion.vcsState()
}
private def vcsState = T.persistent {
val isCI = System.getenv("CI") != null
val state = VcsVersion.vcsState().format()
if (isCI) state
else state + "-maybe-stale"
}
def constantsFile = T.persistent {
val dest = T.dest / "Constants.scala"
Expand All @@ -203,7 +207,7 @@ class Build(val crossScalaVersion: String)
|/** Build-time constants. Generated by mill. */
|object Constants {
| def version = "${publishVersion()}"
| def detailedVersion = "${vcsState().format()}"
| def detailedVersion = "${vcsState()}"
|
| def scalaJsVersion = "${Deps.scalaJsLinker.dep.version}"
| def scalaNativeVersion = "${Deps.nativeTools.dep.version}"
Expand All @@ -229,7 +233,6 @@ class Build(val crossScalaVersion: String)
| def semanticDbPluginVersion = "${Deps.scalametaTrees.dep.version}"
|
| def localRepoResourcePath = "$localRepoResourcePath"
| def localRepoVersion = "${vcsState().format()}"
|
| def jmhVersion = "1.29"
|
Expand Down Expand Up @@ -264,7 +267,7 @@ trait Cli extends SbtModule with CliLaunchers with ScalaCliPublishModule with Fo
with HasTests with HasMacroAnnotations with ScalaCliScalafixModule {
def scalaVersion = Scala.defaultInternal
def scalacOptions = T {
super.scalacOptions() ++ Seq("-Xasync", "-Ywarn-unused")
super.scalacOptions() ++ Seq("-Xasync", "-Ywarn-unused", "-deprecation")
}
def moduleDeps = Seq(
build(Scala.defaultInternal),
Expand Down Expand Up @@ -308,7 +311,7 @@ trait CliIntegrationBase extends SbtModule with ScalaCliPublishModule with HasTe
PathRef(T.dest / "working-dir")
}
def scalacOptions = T {
super.scalacOptions() ++ Seq("-Ywarn-unused")
super.scalacOptions() ++ Seq("-Ywarn-unused", "-deprecation")
}

def sources = T.sources {
Expand Down Expand Up @@ -369,6 +372,7 @@ trait CliIntegrationBase extends SbtModule with ScalaCliPublishModule with HasTe
| def munitVersion = "${TestDeps.munit.dep.version}"
| def dockerTestImage = "${Docker.testImage}"
| def dockerAlpineTestImage = "${Docker.alpineTestImage}"
| def cs = "${settings.cs().replace("\\", "\\\\")}"
|}
|""".stripMargin
if (!os.isFile(dest) || os.read(dest) != code)
Expand Down Expand Up @@ -463,12 +467,13 @@ class TestRunner(val crossScalaVersion: String) extends CrossSbtModule with Scal
with ScalaCliScalafixModule {
def scalacOptions = T {
super.scalacOptions() ++ {
if (scalaVersion().startsWith("2.")) Seq("-Ywarn-unused")
if (scalaVersion().startsWith("2.")) Seq("-Ywarn-unused", "-deprecation")
else Nil
}
}
def ivyDeps = super.ivyDeps() ++ Agg(
Deps.asm,
Deps.collectionCompat,
Deps.testInterface
)
def mainClass = Some("scala.build.testrunner.DynamicTestRunner")
Expand All @@ -477,10 +482,11 @@ class TestRunner(val crossScalaVersion: String) extends CrossSbtModule with Scal
class BloopRifle(val crossScalaVersion: String) extends CrossSbtModule with ScalaCliPublishModule
with ScalaCliScalafixModule {
def scalacOptions = T {
super.scalacOptions() ++ Seq("-Ywarn-unused")
super.scalacOptions() ++ Seq("-Ywarn-unused", "-deprecation")
}
def ivyDeps = super.ivyDeps() ++ Agg(
Deps.bsp4j,
Deps.collectionCompat,
Deps.ipcSocket,
Deps.snailgun
)
Expand Down Expand Up @@ -854,7 +860,8 @@ object ci extends Module {
.filter(os.isDir(_))
.map(_ / distName)
.filter(os.isFile(_))
.toStream
.take(1)
.toList
.headOption
.getOrElse {
sys.error(
Expand Down Expand Up @@ -891,7 +898,7 @@ object ci extends Module {
.iterator
} yield path

candidatesIt.toStream.headOption.getOrElse {
candidatesIt.take(1).toList.headOption.getOrElse {
sys.error(s"$fileNamePrefix*$fileNameSuffix not found")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import scala.annotation.tailrec
import scala.build.bloop.bloop4j.BloopExtraBuildParams
import scala.build.blooprifle.internal.Constants
import scala.build.blooprifle.{BloopRifle, BloopRifleConfig, BloopRifleLogger, BspConnection}
import scala.collection.JavaConverters._
import scala.concurrent.Await
import scala.concurrent.duration.{Duration, FiniteDuration}
import scala.jdk.CollectionConverters._

trait BloopServer {
def server: BuildServer
Expand Down
2 changes: 1 addition & 1 deletion modules/build/src/main/scala/scala/build/Bloop.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import java.io.File
import scala.build.EitherCps.{either, value}
import scala.build.blooprifle.BloopRifleConfig
import scala.build.errors.{BuildException, ModuleFormatError}
import scala.collection.JavaConverters._
import scala.concurrent.duration.FiniteDuration
import scala.jdk.CollectionConverters._
import scala.util.Properties

object Bloop {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import java.io.{File, PrintStream}
import java.net.URI
import java.nio.file.Paths

import scala.collection.JavaConverters._
import scala.collection.mutable
import scala.jdk.CollectionConverters._

class ConsoleBloopBuildClient(
logger: Logger,
Expand Down
9 changes: 7 additions & 2 deletions modules/build/src/main/scala/scala/build/CrossSources.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ object CrossSources {
val preprocessedSources = value {
inputs.flattened()
.map { elem =>
preprocessors.iterator.flatMap(p => p.preprocess(elem).iterator).toStream.headOption
preprocessors
.iterator
.flatMap(p => p.preprocess(elem).iterator)
.take(1)
.toList
.headOption
.getOrElse(Right(Nil)) // FIXME Warn about unprocessed stuff?
}
.sequence
Expand Down Expand Up @@ -107,7 +112,7 @@ object CrossSources {
case elem: Inputs.SingleElement =>
preprocessors.iterator
.flatMap(p => p.preprocess(elem).iterator)
.toStream.headOption
.take(1).toList.headOption
.getOrElse(Right(Nil))
.map(_.flatMap(_.mainClassOpt.toSeq).headOption)
}
Expand Down
2 changes: 1 addition & 1 deletion modules/build/src/main/scala/scala/build/Directories.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package scala.build

import dev.dirs.{GetWinDirs, ProjectDirectories}
import coursier.cache.shaded.dirs.{GetWinDirs, ProjectDirectories}

trait Directories {
def localRepoDir: os.Path
Expand Down
112 changes: 77 additions & 35 deletions modules/build/src/main/scala/scala/build/LocalRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package scala.build

import coursier.paths.Util

import java.io.{BufferedInputStream, ByteArrayOutputStream, InputStream}
import java.io.{BufferedInputStream, ByteArrayOutputStream, Closeable, InputStream}
import java.nio.channels.{FileChannel, FileLock}
import java.nio.charset.StandardCharsets
import java.nio.file.{Path, StandardOpenOption}
import java.util.zip.{ZipEntry, ZipInputStream}

Expand All @@ -12,7 +13,62 @@ import scala.build.internal.Constants
object LocalRepo {

private def resourcePath = Constants.localRepoResourcePath
private def version = Constants.localRepoVersion

private def using[S <: Closeable, T](is: => S)(f: S => T): T = {
var is0 = Option.empty[S]
try {
is0 = Some(is)
f(is0.get)
}
finally if (is0.nonEmpty) is0.get.close()
}

private def readContent(is: InputStream): Array[Byte] = {
val baos = new ByteArrayOutputStream

val buf = Array.ofDim[Byte](16 * 1024)
var read = -1
while ({
read = is.read(buf)
read >= 0
})
baos.write(buf, 0, read)

baos.toByteArray
}

private def extractZip(zis: ZipInputStream, dest: os.Path): Unit = {
var ent: ZipEntry = null
while ({
ent = zis.getNextEntry()
ent != null
})
if (!ent.isDirectory) {
val content = readContent(zis)
zis.closeEntry()
os.write(
dest / ent.getName.split('/').toSeq,
content,
createFolders = true
)
}
}

private def entryContent(zis: ZipInputStream, entryPath: String): Option[Array[Byte]] = {
var ent: ZipEntry = null
while ({
ent = zis.getNextEntry()
ent != null && (ent.isDirectory || ent.getName != entryPath)
}) {}
if (ent == null) None
else {
assert(ent.getName == entryPath)

val content = readContent(zis)
zis.closeEntry()
Some(content)
}
}

def localRepo(
baseDir: os.Path,
Expand All @@ -22,46 +78,32 @@ object LocalRepo {

if (archiveUrl == null) None
else {
val repoDir = baseDir / version
val tmpRepoDir = repoDir / os.up / s".$version.tmp"
val repo = "ivy:" + repoDir.toNIO.toUri.toASCIIString + "/[defaultPattern]"

val version =
using(archiveUrl.openStream()) { is =>
using(new ZipInputStream(new BufferedInputStream(is))) { zis =>
val b = entryContent(zis, "version").getOrElse {
sys.error(s"Malformed local repo JAR $archiveUrl (no version file)")
}
new String(b, StandardCharsets.UTF_8)
}
}

val repoDir = baseDir / version

if (!os.exists(repoDir))
withLock((repoDir / os.up).toNIO, version) {
val tmpRepoDir = repoDir / os.up / s".$version.tmp"
os.remove.all(tmpRepoDir)
var is: InputStream = null
var zis: ZipInputStream = null
try {
is = archiveUrl.openStream()
zis = new ZipInputStream(new BufferedInputStream(is))
var ent: ZipEntry = null
val buf = Array.ofDim[Byte](16 * 1024)
while ({
ent = zis.getNextEntry()
ent != null
})
if (!ent.isDirectory) {
val baos = new ByteArrayOutputStream
var read = -1
while ({
read = zis.read(buf)
read >= 0
})
baos.write(buf, 0, read)
zis.closeEntry()
os.write(
tmpRepoDir / ent.getName.split('/').toSeq,
baos.toByteArray,
createFolders = true
)
}
}
finally {
if (zis != null) zis.close()
if (is != null) is.close()
using(archiveUrl.openStream()) { is =>
using(new ZipInputStream(new BufferedInputStream(is))) { zis =>
extractZip(zis, tmpRepoDir)
}
}
os.move(tmpRepoDir, repoDir)
}

val repo = "ivy:" + repoDir.toNIO.toUri.toASCIIString + "/[defaultPattern]"
Some(repo)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import java.util.concurrent.ExecutorService

import scala.build.postprocessing.LineConversion
import scala.build.{BloopBuildClient, GeneratedSource, Logger}
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._

class BspClient(
readFilesEs: ExecutorService,
Expand Down
2 changes: 1 addition & 1 deletion modules/build/src/main/scala/scala/build/bsp/BspImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import scala.build.blooprifle.BloopRifleConfig
import scala.build.errors.BuildException
import scala.build.internal.{Constants, CustomCodeWrapper}
import scala.build.options.{BuildOptions, Scope}
import scala.collection.JavaConverters._
import scala.collection.mutable.ListBuffer
import scala.concurrent.{ExecutionContext, Future, Promise}
import scala.jdk.CollectionConverters._
import scala.util.{Failure, Success}

final class BspImpl(
Expand Down
Loading

0 comments on commit a62e63e

Please sign in to comment.