Skip to content

Decompose & refactor Inputs #1565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Extract Inputs.Element to a separate source file
  • Loading branch information
Gedochao committed Nov 15, 2022
commit d88df10ce8aa91b516a88feca48e73d8aa6d79a9
24 changes: 12 additions & 12 deletions modules/build/src/main/scala/scala/build/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import scala.build.Ops.*
import scala.build.actionable.ActionablePreprocessor
import scala.build.compiler.{ScalaCompiler, ScalaCompilerMaker}
import scala.build.errors.*
import scala.build.input.Inputs
import scala.build.input.Inputs.VirtualScript.VirtualScriptNameRegex
import scala.build.input.VirtualScript.VirtualScriptNameRegex
import scala.build.input.*
import scala.build.internal.resource.ResourceMapper
import scala.build.internal.{Constants, CustomCodeWrapper, MainClass, Util}
import scala.build.options.ScalaVersionUtil.asVersion
Expand Down Expand Up @@ -514,8 +514,8 @@ object Build {
else if (scalaVersion.startsWith("2.12"))
if (
inputs.sourceFiles().forall {
case _: Inputs.AnyScript => snNumeralVer >= SNNumeralVersion(0, 4, 3)
case _ => true
case _: AnyScript => snNumeralVer >= SNNumeralVersion(0, 4, 3)
case _ => true
}
) Right(snNumeralVer)
else snCompatError
Expand Down Expand Up @@ -667,7 +667,7 @@ object Build {
val watcher = new Watcher(ListBuffer(), threads.fileWatcher, run(), compiler.shutdown())

def doWatch(): Unit = {
val elements: Seq[Inputs.Element] =
val elements: Seq[Element] =
if (res == null) inputs.elements
else
res.map { builds =>
Expand All @@ -677,11 +677,11 @@ object Build {
}.getOrElse(inputs.elements)
for (elem <- elements) {
val depth = elem match {
case _: Inputs.SingleFile => -1
case _ => Int.MaxValue
case _: SingleFile => -1
case _ => Int.MaxValue
}
val eventFilter: PathWatchers.Event => Boolean = elem match {
case d: Inputs.Directory =>
case d: Directory =>
// Filtering event for directories, to ignore those related to the .bloop directory in particular
event =>
val p = os.Path(event.getTypedPath.getPath.toAbsolutePath)
Expand All @@ -695,9 +695,9 @@ object Build {

val watcher0 = watcher.newWatcher()
elem match {
case d: Inputs.OnDisk =>
case d: OnDisk =>
watcher0.register(d.path.toNIO, depth)
case _: Inputs.Virtual =>
case _: Virtual =>
}
watcher0.addObserver {
onChangeBufferedObserver { event =>
Expand Down Expand Up @@ -1214,8 +1214,8 @@ object Build {
// hash of the underlying project if needed is already in jmhProjectName
mayAppendHash = false,
elements = inputs.elements ++ Seq(
Inputs.Directory(jmhSourceDir),
Inputs.ResourceDirectory(jmhResourceDir)
Directory(jmhSourceDir),
ResourceDirectory(jmhResourceDir)
)
)
val updatedOptions = build.options.copy(
Expand Down
20 changes: 10 additions & 10 deletions modules/build/src/main/scala/scala/build/CrossSources.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import scala.build.EitherCps.{either, value}
import scala.build.Ops.*
import scala.build.Positioned
import scala.build.errors.{BuildException, CompositeBuildException, MalformedDirectiveError}
import scala.build.input.Inputs
import scala.build.input.*
import scala.build.options.{
BuildOptions,
BuildRequirements,
Expand Down Expand Up @@ -109,7 +109,7 @@ object CrossSources {
p.root.exists { path =>
val fullPath = path / p.path
inputs.elements.exists {
case Inputs.Directory(path) =>
case Directory(path) =>
// Is this file subdirectory of given dir and if we have a subdiretory 'test' on the way
fullPath.startsWith(path) &&
fullPath.relativeTo(path).segments.contains("test")
Expand All @@ -127,7 +127,7 @@ object CrossSources {
maybeRecoverOnError: BuildException => Option[BuildException] = e => Some(e)
): Either[BuildException, (CrossSources, Inputs)] = either {

def preprocessSources(elems: Seq[Inputs.SingleElement])
def preprocessSources(elems: Seq[SingleElement])
: Either[BuildException, Seq[PreprocessedSource]] =
elems
.map { elem =>
Expand Down Expand Up @@ -221,7 +221,7 @@ object CrossSources {
}

val resourceDirs = allInputs.elements.collect {
case r: Inputs.ResourceDirectory =>
case r: ResourceDirectory =>
HasBuildRequirements(BuildRequirements(), r.path)
} ++ preprocessedSources.flatMap(_.options).flatMap(_.classPathOptions.resourcesDir).map(
HasBuildRequirements(BuildRequirements(), _)
Expand All @@ -236,13 +236,13 @@ object CrossSources {
lazy val dir = sourcePath / os.up
lazy val subPath = sourcePath.subRelativeTo(dir)
if (os.isDir(sourcePath))
Right(Inputs.singleFilesFromDirectory(Inputs.Directory(sourcePath), enableMarkdown))
Right(Inputs.singleFilesFromDirectory(Directory(sourcePath), enableMarkdown))
else if (sourcePath == os.sub / "project.scala")
Right(Seq(Inputs.ProjectScalaFile(dir, subPath)))
else if (sourcePath.ext == "scala") Right(Seq(Inputs.SourceScalaFile(dir, subPath)))
else if (sourcePath.ext == "sc") Right(Seq(Inputs.Script(dir, subPath)))
else if (sourcePath.ext == "java") Right(Seq(Inputs.JavaFile(dir, subPath)))
else if (sourcePath.ext == "md") Right(Seq(Inputs.MarkdownFile(dir, subPath)))
Right(Seq(ProjectScalaFile(dir, subPath)))
else if (sourcePath.ext == "scala") Right(Seq(SourceScalaFile(dir, subPath)))
else if (sourcePath.ext == "sc") Right(Seq(Script(dir, subPath)))
else if (sourcePath.ext == "java") Right(Seq(JavaFile(dir, subPath)))
else if (sourcePath.ext == "md") Right(Seq(MarkdownFile(dir, subPath)))
else {
val msg =
if (os.exists(sourcePath))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.util.concurrent.atomic.AtomicReference

import scala.build.Build
import scala.build.compiler.BloopCompiler
import scala.build.input.Inputs
import scala.build.input.{Inputs, OnDisk, SingleFile, Virtual}

final class BloopSession(
val inputs: Inputs,
Expand All @@ -17,9 +17,9 @@ final class BloopSession(
def resetDiagnostics(localClient: BspClient): Unit =
for (targetId <- bspServer.targetIds)
inputs.flattened().foreach {
case f: Inputs.SingleFile =>
case f: SingleFile =>
localClient.resetDiagnostics(f.path, targetId)
case _: Inputs.Virtual =>
case _: Virtual =>
}
def dispose(): Unit = {
watcher.dispose()
Expand All @@ -28,7 +28,7 @@ final class BloopSession(

def registerWatchInputs(): Unit =
inputs.elements.foreach {
case elem: Inputs.OnDisk =>
case elem: OnDisk =>
val eventFilter: PathWatchers.Event => Boolean = { event =>
val newOrDeletedFile =
event.getKind == PathWatchers.Event.Kind.Create ||
Expand Down
107 changes: 107 additions & 0 deletions modules/build/src/main/scala/scala/build/input/Element.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package scala.build.input

import scala.build.preprocessing.ScopePath
import scala.util.matching.Regex

sealed abstract class Element extends Product with Serializable

sealed trait SingleElement extends Element

sealed trait AnyScript extends Element

sealed abstract class OnDisk extends Element {
def path: os.Path
}

sealed abstract class Virtual extends SingleElement {
def content: Array[Byte]

def source: String

def subPath: os.SubPath = {
val idx = source.lastIndexOf('/')
os.sub / source.drop(idx + 1)
}

def scopePath: ScopePath =
ScopePath(Left(source), subPath)
}

sealed abstract class VirtualSourceFile extends Virtual {
def isStdin: Boolean = source.startsWith("<stdin>")

def isSnippet: Boolean = source.startsWith("<snippet>")

protected def generatedSourceFileName(fileSuffix: String): String =
if (isStdin) s"stdin$fileSuffix"
else if (isSnippet) s"${source.stripPrefix("<snippet>-")}$fileSuffix"
else s"virtual$fileSuffix"
}

sealed trait SingleFile extends OnDisk with SingleElement

sealed trait SourceFile extends SingleFile {
def subPath: os.SubPath
}

sealed trait Compiled extends Element

sealed trait AnyScalaFile extends Compiled

sealed trait ScalaFile extends AnyScalaFile {
def base: os.Path

def subPath: os.SubPath

def path: os.Path = base / subPath
}

final case class Script(base: os.Path, subPath: os.SubPath)
extends OnDisk with SourceFile with AnyScalaFile with AnyScript {
lazy val path: os.Path = base / subPath
}

final case class SourceScalaFile(base: os.Path, subPath: os.SubPath)
extends OnDisk with SourceFile with ScalaFile

final case class ProjectScalaFile(base: os.Path, subPath: os.SubPath)
extends OnDisk with SourceFile with ScalaFile

final case class JavaFile(base: os.Path, subPath: os.SubPath)
extends OnDisk with SourceFile with Compiled {
lazy val path: os.Path = base / subPath
}

final case class CFile(base: os.Path, subPath: os.SubPath)
extends OnDisk with SourceFile with Compiled {
lazy val path = base / subPath
}

final case class MarkdownFile(base: os.Path, subPath: os.SubPath)
extends OnDisk with SourceFile {
lazy val path: os.Path = base / subPath
}

final case class Directory(path: os.Path) extends OnDisk with Compiled

final case class ResourceDirectory(path: os.Path) extends OnDisk

final case class VirtualScript(content: Array[Byte], source: String, wrapperPath: os.SubPath)
extends Virtual with AnyScalaFile with AnyScript

object VirtualScript {
val VirtualScriptNameRegex: Regex = "(^stdin$|^snippet\\d*$)".r
}

final case class VirtualScalaFile(content: Array[Byte], source: String)
extends VirtualSourceFile with AnyScalaFile {
def generatedSourceFileName: String = generatedSourceFileName(".scala")
}

final case class VirtualJavaFile(content: Array[Byte], source: String)
extends VirtualSourceFile with Compiled {
def generatedSourceFileName: String = generatedSourceFileName(".java")
}

final case class VirtualData(content: Array[Byte], source: String)
extends Virtual
Loading