Skip to content

Commit 38fd674

Browse files
committed
NIT Refactor
- remove redundant braces - apply Scala 3 syntax where applicable - remove redundant conversions - simplify transformations - fill-in missing type declarations - explicit annotate @tailrec functions - remove redundant regex escapes
1 parent bed324e commit 38fd674

File tree

7 files changed

+37
-36
lines changed

7 files changed

+37
-36
lines changed

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import java.io.File
99
import java.nio.file.{FileSystemException, Path}
1010
import java.util.concurrent.{ScheduledExecutorService, ScheduledFuture}
1111

12+
import scala.annotation.tailrec
1213
import scala.build.EitherCps.{either, value}
13-
import scala.build.Ops._
14+
import scala.build.Ops.*
1415
import scala.build.compiler.{ScalaCompiler, ScalaCompilerMaker}
15-
import scala.build.errors._
16+
import scala.build.errors.*
1617
import scala.build.internal.{Constants, CustomCodeWrapper, MainClass, Util}
17-
import scala.build.options._
18+
import scala.build.options.*
1819
import scala.build.options.validation.ValidationException
19-
import scala.build.postprocessing._
20+
import scala.build.postprocessing.*
2021
import scala.collection.mutable.ListBuffer
2122
import scala.concurrent.duration.DurationInt
2223
import scala.util.Properties
@@ -329,7 +330,7 @@ object Build {
329330
builds
330331
}
331332

332-
private def copyResourceToClassesDir(build: Build) = build match {
333+
private def copyResourceToClassesDir(build: Build): Unit = build match {
333334
case b: Build.Successful =>
334335
for {
335336
resourceDirPath <- b.sources.resourceDirs.filter(os.exists(_))
@@ -502,7 +503,7 @@ object Build {
502503
logger: Logger,
503504
options: BuildOptions
504505
): Either[BuildException, Unit] = {
505-
val (errors, otherDiagnostics) = options.validate.toSeq.partition(_.severity == Severity.Error)
506+
val (errors, otherDiagnostics) = options.validate.partition(_.severity == Severity.Error)
506507
logger.log(otherDiagnostics)
507508
if (errors.nonEmpty)
508509
Left(CompositeBuildException(errors.map(new ValidationException(_))))
@@ -541,7 +542,7 @@ object Build {
541542
logger
542543
))
543544

544-
def run() = {
545+
def run(): Unit = {
545546
try {
546547
val res = build(
547548
inputs,
@@ -940,6 +941,7 @@ object Build {
940941
def onError(t: Throwable): Unit = {
941942
// TODO Log that properly
942943
System.err.println("got error:")
944+
@tailrec
943945
def printEx(t: Throwable): Unit =
944946
if (t != null) {
945947
System.err.println(t)
@@ -973,7 +975,7 @@ object Build {
973975
}
974976

975977
private val lock = new Object
976-
private var f: ScheduledFuture[_] = null
978+
private var f: ScheduledFuture[?] = _
977979
private val waitFor = 50.millis
978980
private val runnable: Runnable = { () =>
979981
lock.synchronized {
@@ -1080,7 +1082,7 @@ object Build {
10801082
command.iterator.map(_ + System.lineSeparator()).mkString
10811083
)
10821084

1083-
new ProcessBuilder(command: _*)
1085+
new ProcessBuilder(command*)
10841086
.inheritIO()
10851087
.start()
10861088
.waitFor()

modules/build/src/main/scala/scala/build/CrossSources.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package scala.build
22

33
import scala.build.EitherCps.{either, value}
4-
import scala.build.Ops._
4+
import scala.build.Ops.*
55
import scala.build.errors.{BuildException, CompositeBuildException}
66
import scala.build.options.{BuildOptions, BuildRequirements, HasBuildRequirements, Scope}
7-
import scala.build.preprocessing._
7+
import scala.build.preprocessing.*
88

99
final case class CrossSources(
1010
paths: Seq[HasBuildRequirements[(os.Path, os.RelPath)]],

modules/build/src/main/scala/scala/build/Inputs.scala

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ final case class Inputs(
5757

5858
private lazy val inputsHash: String =
5959
Inputs.inputsHash(elements)
60-
lazy val projectName = {
60+
lazy val projectName: String = {
6161
val needsSuffix = mayAppendHash && (elements match {
6262
case Seq(d: Inputs.Directory) => d.path != workspace
6363
case _ => true
@@ -87,19 +87,18 @@ final case class Inputs(
8787
if (forbidden.exists(workspace.startsWith)) inHomeDir(directories)
8888
else this
8989
def checkAttributes(directories: Directories): Inputs = {
90+
@tailrec
9091
def existingParent(p: os.Path): Option[os.Path] =
9192
if (os.exists(p)) Some(p)
9293
else if (p.segmentCount <= 0) None
9394
else existingParent(p / os.up)
9495
def reallyOwnedByUser(p: os.Path): Boolean =
9596
if (Properties.isWin)
96-
p.toIO.canWrite() // Wondering if there's a better way to do that…
97+
p.toIO.canWrite // Wondering if there's a better way to do that…
9798
else
9899
os.owner(p) == os.owner(os.home) &&
99-
p.toIO.canWrite()
100-
val canWrite = existingParent(workspace)
101-
.map(reallyOwnedByUser)
102-
.getOrElse(false)
100+
p.toIO.canWrite
101+
val canWrite = existingParent(workspace).exists(reallyOwnedByUser)
103102
if (canWrite) this
104103
else inHomeDir(directories)
105104
}
@@ -123,7 +122,7 @@ final case class Inputs(
123122
Iterator(v.content, bytes("\n"))
124123
}
125124
val md = MessageDigest.getInstance("SHA-1")
126-
it.foreach(md.update(_))
125+
it.foreach(md.update)
127126
val digest = md.digest()
128127
val calculatedSum = new BigInteger(1, digest)
129128
String.format(s"%040x", calculatedSum)
@@ -188,15 +187,15 @@ object Inputs {
188187

189188
final case class Script(base: os.Path, subPath: os.SubPath)
190189
extends OnDisk with SourceFile with AnyScalaFile with AnyScript {
191-
lazy val path = base / subPath
190+
lazy val path: os.Path = base / subPath
192191
}
193192
final case class ScalaFile(base: os.Path, subPath: os.SubPath)
194193
extends OnDisk with SourceFile with AnyScalaFile {
195-
lazy val path = base / subPath
194+
lazy val path: os.Path = base / subPath
196195
}
197196
final case class JavaFile(base: os.Path, subPath: os.SubPath)
198197
extends OnDisk with SourceFile with Compiled {
199-
lazy val path = base / subPath
198+
lazy val path: os.Path = base / subPath
200199
}
201200
final case class Directory(path: os.Path) extends OnDisk with Compiled
202201
final case class ResourceDirectory(path: os.Path) extends OnDisk
@@ -226,13 +225,13 @@ object Inputs {
226225
Iterator(bytes("virtual:"), v.content, bytes("\n"))
227226
}
228227
val md = MessageDigest.getInstance("SHA-1")
229-
it.foreach(md.update(_))
228+
it.foreach(md.update)
230229
val digest = md.digest()
231230
val calculatedSum = new BigInteger(1, digest)
232231
String.format(s"%040x", calculatedSum).take(10)
233232
}
234233

235-
def homeWorkspace(elements: Seq[Element], directories: Directories) = {
234+
def homeWorkspace(elements: Seq[Element], directories: Directories): os.Path = {
236235
val hash0 = inputsHash(elements)
237236
val dir = directories.virtualProjectsDir / hash0.take(2) / s"project-${hash0.drop(2)}"
238237
os.makeDir.all(dir)
@@ -292,7 +291,7 @@ object Inputs {
292291
}
293292

294293
private val githubGistsArchiveRegex: Regex =
295-
s""":\\/\\/gist\\.github\\.com\\/[^\\/]*?\\/[^\\/]*$$""".r
294+
s"""://gist\\.github\\.com/[^/]*?/[^/]*$$""".r
296295

297296
private def resolve(path: String, content: Array[Byte]): Element =
298297
if (path.endsWith(".scala")) VirtualScalaFile(content, path)
@@ -307,7 +306,7 @@ object Inputs {
307306
val zipInputStream = new ZipInputStream(new ByteArrayInputStream(content))
308307
@tailrec
309308
def readArchive(acc: Seq[Element]): Seq[Element] =
310-
Option(zipInputStream.getNextEntry()) match {
309+
Option(zipInputStream.getNextEntry) match {
311310
case Some(entry) if entry.isDirectory => readArchive(acc)
312311
case Some(entry) =>
313312
val content = zipInputStream.readAllBytes()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package scala.build
22

33
import scala.build.internal.CodeWrapper
44
import scala.build.options.{BuildOptions, Scope}
5-
import scala.build.preprocessing._
5+
import scala.build.preprocessing.*
66

77
final case class Sources(
88
paths: Seq[(os.Path, os.RelPath)],

modules/build/src/main/scala/scala/build/internal/NativeBuilderHelper.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ object NativeBuilderHelper {
4949
dest: os.Path,
5050
nativeWorkDir: os.Path,
5151
currentProjectSha: String
52-
) = {
52+
): Unit = {
5353
val projectShaPath = resolveProjectShaPath(nativeWorkDir)
5454
os.write.over(projectShaPath, currentProjectSha, createFolders = true)
5555

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import dependency.parser.DependencyParser
77
import java.nio.charset.StandardCharsets
88

99
import scala.build.EitherCps.{either, value}
10-
import scala.build.Ops._
11-
import scala.build.errors._
10+
import scala.build.Ops.*
11+
import scala.build.errors.*
1212
import scala.build.internal.Util
1313
import scala.build.options.{BuildOptions, BuildRequirements, ClassPathOptions, ShadowingSeq}
14-
import scala.build.preprocessing.directives._
14+
import scala.build.preprocessing.directives.*
1515
import scala.build.{Inputs, Logger, Position, Positioned}
1616

1717
case object ScalaPreprocessor extends Preprocessor {
@@ -177,9 +177,9 @@ case object ScalaPreprocessor extends Preprocessor {
177177
path: Either[String, os.Path]
178178
): Either[BuildException, Option[SpecialImportsProcessingOutput]] = either {
179179

180-
import fastparse._
180+
import fastparse.*
181181

182-
import scala.build.internal.ScalaParse._
182+
import scala.build.internal.ScalaParse.*
183183

184184
val res = parse(content, Header(_))
185185

@@ -315,7 +315,7 @@ case object ScalaPreprocessor extends Preprocessor {
315315
updatedRequirements.scoped,
316316
updatedContentOpt
317317
))
318-
case Seq(h, t @ _*) =>
318+
case Seq(h, t*) =>
319319
val errors = ::(
320320
handleUnusedValues(ScopedDirective(h, path, cwd)),
321321
t.map(d => handleUnusedValues(ScopedDirective(d, path, cwd))).toList

modules/cli/src/main/scala/scala/cli/commands/Package.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ object Package extends ScalaCommand[PackageOptions] {
195195
def alreadyExistsCheck(): Unit = {
196196
val alreadyExists = !force &&
197197
os.exists(destPath) &&
198-
expectedModifyEpochSecondOpt.forall(exp => os.mtime(destPath) != exp)
198+
!expectedModifyEpochSecondOpt.contains(os.mtime(destPath))
199199
if (alreadyExists) {
200200
val msg =
201201
if (expectedModifyEpochSecondOpt.isEmpty) s"$printableDest already exists"
@@ -685,7 +685,7 @@ object Package extends ScalaCommand[PackageOptions] {
685685
noOpt: Boolean,
686686
logger: Logger
687687
): Either[BuildException, os.Path] =
688-
Library.withLibraryJar(build, dest.last.toString.stripSuffix(".jar")) { mainJar =>
688+
Library.withLibraryJar(build, dest.last.stripSuffix(".jar")) { mainJar =>
689689
val classPath = os.Path(mainJar, os.pwd) +: build.artifacts.classPath
690690
val linkingDir = os.temp.dir(prefix = "scala-cli-js-linking")
691691
either {
@@ -771,7 +771,7 @@ object Package extends ScalaCommand[PackageOptions] {
771771
if (cacheData.changed)
772772
Library.withLibraryJar(build, dest.last.stripSuffix(".jar")) { mainJar =>
773773

774-
val classpath = build.fullClassPath.map(_.toString) :+ mainJar.toString()
774+
val classpath = build.fullClassPath.map(_.toString) :+ mainJar.toString
775775
val args =
776776
cliOptions ++
777777
logger.scalaNativeCliInternalLoggerOptions ++

0 commit comments

Comments
 (0)