Skip to content
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

Decompose & refactor Inputs #1565

Merged
merged 6 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 12 additions & 11 deletions modules/build/src/main/scala/scala/build/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import java.util.concurrent.{ScheduledExecutorService, ScheduledFuture}

import scala.annotation.tailrec
import scala.build.EitherCps.{either, value}
import scala.build.Inputs.VirtualScript.VirtualScriptNameRegex
import scala.build.Ops.*
import scala.build.actionable.ActionablePreprocessor
import scala.build.compiler.{ScalaCompiler, ScalaCompilerMaker}
import scala.build.errors.*
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 @@ -513,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 @@ -666,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 @@ -676,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 @@ -694,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 @@ -1213,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: 11 additions & 9 deletions modules/build/src/main/scala/scala/build/CrossSources.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ 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.ElementsUtils.*
import scala.build.input.*
import scala.build.options.{
BuildOptions,
BuildRequirements,
Expand Down Expand Up @@ -108,7 +110,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 @@ -126,7 +128,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 @@ -220,7 +222,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 @@ -235,13 +237,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(Directory(sourcePath).singleFilesFromDirectory(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
1 change: 1 addition & 0 deletions modules/build/src/main/scala/scala/build/Sources.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scala.build
import coursier.cache.ArchiveCache
import coursier.util.Task

import scala.build.input.Inputs
import scala.build.internal.CodeWrapper
import scala.build.options.{BuildOptions, Scope}
import scala.build.preprocessing.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import com.swoval.files.PathWatchers

import java.util.concurrent.atomic.AtomicReference

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

final class BloopSession(
val inputs: Inputs,
Expand All @@ -16,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 @@ -27,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
2 changes: 1 addition & 1 deletion modules/build/src/main/scala/scala/build/bsp/Bsp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package scala.build.bsp

import java.io.{InputStream, OutputStream}

import scala.build.Inputs
import scala.build.errors.BuildException
import scala.build.input.Inputs
import scala.concurrent.Future

trait Bsp {
Expand Down
1 change: 1 addition & 0 deletions modules/build/src/main/scala/scala/build/bsp/BspImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import scala.build.actionable.ActionablePreprocessor
import scala.build.bloop.BloopServer
import scala.build.compiler.BloopCompiler
import scala.build.errors.{BuildException, Diagnostic, ParsingInputsException}
import scala.build.input.Inputs
import scala.build.internal.{Constants, CustomCodeWrapper}
import scala.build.options.{BuildOptions, Scope}
import scala.collection.mutable.ListBuffer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import ch.epfl.scala.{bsp4j => b}

import java.util.concurrent.CompletableFuture

import scala.build.GeneratedSource
import scala.build.input.Inputs
import scala.build.options.Scope
import scala.build.{GeneratedSource, Inputs}

class BuildServerProxy(
bspServer: () => BspServer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package scala.build.bsp

import ch.epfl.scala.{bsp4j => b}

import scala.build.GeneratedSource
import scala.build.input.Inputs
import scala.build.internal.Constants
import scala.build.options.Scope
import scala.build.{GeneratedSource, Inputs}

trait HasGeneratedSources {
def targetIds: List[b.BuildTargetIdentifier]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package scala.build.bsp

import ch.epfl.scala.{bsp4j => b}

import scala.build.GeneratedSource
import scala.build.input.Inputs
import scala.build.internal.Constants
import scala.build.options.Scope
import scala.build.{GeneratedSource, Inputs}
import scala.collection.mutable

trait HasGeneratedSourcesImpl extends HasGeneratedSources {
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