Skip to content

Commit

Permalink
NIT Refactor Inputs
Browse files Browse the repository at this point in the history
  - remove dead code
  - fix formatting
  - add missing type declarations
  - apply Scala 3 syntax
  - misc fixes
  • Loading branch information
Gedochao committed Nov 15, 2022
1 parent 5b8f760 commit 8bf026f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 38 deletions.
37 changes: 8 additions & 29 deletions modules/build/src/main/scala/scala/build/input/Inputs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ final case class Inputs(
allowRestrictedFeatures: Boolean
) {

def isEmpty: Boolean =
elements.isEmpty
def isEmpty: Boolean = elements.isEmpty

def singleFiles(): Seq[SingleFile] =
elements.flatMap {
Expand All @@ -43,12 +42,6 @@ final case class Inputs(
case f: SourceFile => f
}

def virtualSourceFiles(): Seq[Virtual] =
elements.flatMap {
case v: Virtual => Seq(v)
case _ => Nil
}

def flattened(): Seq[SingleElement] =
elements.flatMap {
case f: SingleFile => Seq(f)
Expand All @@ -63,17 +56,14 @@ final case class Inputs(
case Seq(d: Directory) => d.path != workspace
case _ => true
})
if (needsSuffix) baseProjectName + "-" + inputsHash
else baseProjectName
if needsSuffix then s"$baseProjectName-$inputsHash" else baseProjectName
}

def scopeProjectName(scope: Scope): String =
if (scope == Scope.Main) projectName
else projectName + "-" + scope.name
if scope == Scope.Main then projectName else s"$projectName-${scope.name}"

def add(extraElements: Seq[Element]): Inputs =
if (elements.isEmpty) this
else copy(elements = (elements ++ extraElements).distinct)
if elements.isEmpty then this else copy(elements = (elements ++ extraElements).distinct)

def generatedSrcRoot(scope: Scope): os.Path =
workspace / Constants.workspaceDirName / projectName / "src_generated" / scope.name
Expand All @@ -85,8 +75,7 @@ final case class Inputs(
workspaceOrigin = Some(WorkspaceOrigin.HomeDir)
)
def avoid(forbidden: Seq[os.Path], directories: Directories): Inputs =
if (forbidden.exists(workspace.startsWith)) inHomeDir(directories)
else this
if forbidden.exists(workspace.startsWith) then inHomeDir(directories) else this
def checkAttributes(directories: Directories): Inputs = {
@tailrec
def existingParent(p: os.Path): Option[os.Path] =
Expand All @@ -100,8 +89,7 @@ final case class Inputs(
os.owner(p) == os.owner(os.home) &&
p.toIO.canWrite
val canWrite = existingParent(workspace).exists(reallyOwnedByUser)
if (canWrite) this
else inHomeDir(directories)
if canWrite then this else inHomeDir(directories)
}
def sourceHash(): String = {
def bytes(s: String): Array[Byte] = s.getBytes(StandardCharsets.UTF_8)
Expand Down Expand Up @@ -136,22 +124,18 @@ object Inputs {
private def forValidatedElems(
validElems: Seq[Element],
baseProjectName: String,
directories: Directories,
forcedWorkspace: Option[os.Path],
enableMarkdown: Boolean,
allowRestrictedFeatures: Boolean,
extraClasspathWasPassed: Boolean
): Inputs = {

assert(extraClasspathWasPassed || validElems.nonEmpty)

val (inferredWorkspace, inferredNeedsHash, workspaceOrigin) = {
val settingsFiles = validElems.projectSettingsFiles
val dirsAndFiles = validElems.collect {
case d: Directory => d
case f: SourceFile => f
}

settingsFiles.headOption.map { s =>
if (settingsFiles.length > 1)
System.err.println(
Expand Down Expand Up @@ -287,7 +271,7 @@ object Inputs {
}
else if (arg.contains("://")) {
val url =
if (githubGistsArchiveRegex.findFirstMatchIn(arg).nonEmpty) s"$arg/download" else arg
if githubGistsArchiveRegex.findFirstMatchIn(arg).nonEmpty then s"$arg/download" else arg
download(url).map { content =>
if (githubGistsArchiveRegex.findFirstMatchIn(arg).nonEmpty)
resolveZipArchive(content)
Expand Down Expand Up @@ -318,7 +302,6 @@ object Inputs {
private def forNonEmptyArgs(
args: Seq[String],
cwd: os.Path,
directories: Directories,
baseProjectName: String,
download: String => Either[String, Array[Byte]],
stdinOpt: => Option[Array[Byte]],
Expand Down Expand Up @@ -348,7 +331,6 @@ object Inputs {
Right(forValidatedElems(
validElems,
baseProjectName,
directories,
forcedWorkspace,
enableMarkdown,
allowRestrictedFeatures,
Expand All @@ -362,7 +344,6 @@ object Inputs {
def apply(
args: Seq[String],
cwd: os.Path,
directories: Directories,
baseProjectName: String = "project",
defaultInputs: () => Option[Inputs] = () => None,
download: String => Either[String, Array[Byte]] = _ => Left("URL not supported"),
Expand All @@ -386,7 +367,6 @@ object Inputs {
forNonEmptyArgs(
args,
cwd,
directories,
baseProjectName,
download,
stdinOpt,
Expand All @@ -400,8 +380,7 @@ object Inputs {
extraClasspathWasPassed
)

def default(): Option[Inputs] =
None
def default(): Option[Inputs] = None

def empty(workspace: os.Path, enableMarkdown: Boolean): Inputs =
Inputs(
Expand Down
13 changes: 7 additions & 6 deletions modules/build/src/test/scala/scala/build/tests/InputsTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scala.build.tests
import com.eed3si9n.expecty.Expecty.expect

import scala.build.Build
import scala.build.blooprifle.BloopRifleConfig
import scala.build.input.Inputs
import scala.build.input.ElementsUtils.*
import scala.build.options.{BuildOptions, InternalOptions, MaybeScalaVersion}
Expand All @@ -11,11 +12,11 @@ import scala.build.{BuildThreads, Directories, LocalRepo}
import scala.build.internal.Constants

class InputsTests extends munit.FunSuite {
val buildThreads = BuildThreads.create()
val extraRepoTmpDir = os.temp.dir(prefix = "scala-cli-tests-extra-repo-")
val directories = Directories.under(extraRepoTmpDir)
def bloopConfigOpt = Some(BloopServer.bloopConfig)
val buildOptions = BuildOptions(
val buildThreads: BuildThreads = BuildThreads.create()
val extraRepoTmpDir: os.Path = os.temp.dir(prefix = "scala-cli-tests-extra-repo-")
val directories: Directories = Directories.under(extraRepoTmpDir)
def bloopConfigOpt: Option[BloopRifleConfig] = Some(BloopServer.bloopConfig)
val buildOptions: BuildOptions = BuildOptions(
internal = InternalOptions(
localRepository = LocalRepo.localRepo(directories.localRepoDir),
keepDiagnostics = true
Expand Down Expand Up @@ -57,7 +58,7 @@ class InputsTests extends munit.FunSuite {
(root, _, buildMaybe) =>
val javaOptsCheck = buildMaybe match {
case Right(build: Build.Successful) =>
build.options.javaOptions.javaOpts.toSeq(0).value.value == "-Dfoo=bar"
build.options.javaOptions.javaOpts.toSeq.head.value.value == "-Dfoo=bar"
case _ => false
}
assert(javaOptsCheck)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ final case class TestInputs(
val res = Inputs(
inputArgs0,
tmpDir,
Directories.under(tmpDir / ".data"),
forcedWorkspace = forcedWorkspaceOpt.map(_.resolveFrom(tmpDir)),
allowRestrictedFeatures = true,
extraClasspathWasPassed = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ object Clean extends ScalaCommand[CleanOptions] {
val inputs = Inputs(
args.all,
Os.pwd,
options.directories.directories,
defaultInputs = () => Inputs.default(),
forcedWorkspace = options.workspace.forcedWorkspaceOpt,
allowRestrictedFeatures = ScalaCli.allowRestrictedFeatures,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ object SharedOptionsUtil extends CommandHelpers {
val maybeInputs = Inputs(
args,
Os.pwd,
directories,
defaultInputs = defaultInputs,
download = downloadInputs(cache),
stdinOpt = readStdin(logger = logger),
Expand Down

0 comments on commit 8bf026f

Please sign in to comment.