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

Fix - use resourcesDir in package command #519

Merged
merged 7 commits into from
Jan 10, 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
2 changes: 1 addition & 1 deletion .github/scripts/check_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ if [ $# -eq 0 ]
fi

# adding --resource-dirs is a hack to get file watching for free on .md files
scala-cli sclicheck/sclicheck.scala --resource-dirs docs -- "${toCheck[@]}"
scala-cli sclicheck/sclicheck.scala docs -- "${toCheck[@]}"
28 changes: 27 additions & 1 deletion modules/build/src/main/scala/scala/build/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ object Build {
val crossSources = value {
CrossSources.forInputs(
inputs,
Sources.defaultPreprocessors(options.scriptOptions.codeWrapper.getOrElse(CustomCodeWrapper))
Sources.defaultPreprocessors(
options.scriptOptions.codeWrapper.getOrElse(CustomCodeWrapper)
),
logger
)
}
val sharedOptions = crossSources.sharedOptions(options)
Expand Down Expand Up @@ -253,12 +256,35 @@ object Build {
}

val (mainBuild, extraBuilds) = value(buildScope(Scope.Main, None, None))
copyResourceToClassesDir(mainBuild)

val (testBuild, extraTestBuilds) =
value(buildScope(Scope.Test, Some(mainBuild), Some(extraBuilds)))
copyResourceToClassesDir(testBuild)

Builds(Seq(mainBuild, testBuild), Seq(extraBuilds, extraTestBuilds))
}

private def copyResourceToClassesDir(build: Build) = build match {
case b: Build.Successful =>
for {
resourceDirPath <- b.sources.resourceDirs.filter(os.exists(_))
resourceFilePath <- os.walk(resourceDirPath).filter(os.isFile(_))
relativeResourcePath = resourceFilePath.relativeTo(resourceDirPath)
// dismiss files generated by scala-cli
if !relativeResourcePath.startsWith(os.rel / ".scala")
} {
val destPath = b.output / relativeResourcePath
os.copy(
resourceFilePath,
destPath,
replaceExisting = true,
createFolders = true
)
}
case _ =>
}

private def build(
inputs: Inputs,
sources: Sources,
Expand Down
29 changes: 12 additions & 17 deletions modules/build/src/main/scala/scala/build/CrossSources.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final case class CrossSources(
def withVirtualDir(inputs: Inputs, scope: Scope, options: BuildOptions): CrossSources = {

val srcRootPath = inputs.generatedSrcRoot(scope)
val resourceDirs0 = options.classPathOptions.resourceVirtualDir.map { path =>
val resourceDirs0 = options.classPathOptions.resourcesVirtualDir.map { path =>
HasBuildRequirements(BuildRequirements(), srcRootPath / path)
}

Expand Down Expand Up @@ -91,15 +91,16 @@ object CrossSources {

def forInputs(
inputs: Inputs,
preprocessors: Seq[Preprocessor]
preprocessors: Seq[Preprocessor],
logger: Logger
): Either[BuildException, CrossSources] = either {

val preprocessedSources = value {
inputs.flattened()
.map { elem =>
preprocessors
.iterator
.flatMap(p => p.preprocess(elem).iterator)
.flatMap(p => p.preprocess(elem, logger).iterator)
.take(1)
.toList
.headOption
Expand Down Expand Up @@ -142,19 +143,11 @@ object CrossSources {
)
}

val mainClassOpt = value {
inputs.mainClassElement
.collect {
case elem: Inputs.SingleElement =>
preprocessors.iterator
.flatMap(p => p.preprocess(elem).iterator)
.take(1).toList.headOption
.getOrElse(Right(Nil))
.map(_.flatMap(_.mainClassOpt.toSeq).headOption)
}
.sequence
.map(_.flatten)
}
val mainClassOpt = for {
mainClassPath <- inputs.mainClassElement.map(_.path).map(ScopePath.fromPath(_).path)
processedMainClass <- preprocessedSources.find(_.scopePath.path == mainClassPath)
mainClass <- processedMainClass.mainClassOpt
} yield mainClass

val paths = preprocessedSources.collect {
case d: PreprocessedSource.OnDisk =>
Expand All @@ -176,7 +169,9 @@ object CrossSources {
val resourceDirs = inputs.elements.collect {
case r: Inputs.ResourceDirectory =>
HasBuildRequirements(BuildRequirements(), r.path)
}
} ++ preprocessedSources.flatMap(_.options).flatMap(_.classPathOptions.resourcesDir).map(
HasBuildRequirements(BuildRequirements(), _)
)

CrossSources(paths, inMemory, mainClassOpt, resourceDirs, buildOptions)
}
Expand Down
2 changes: 1 addition & 1 deletion modules/build/src/main/scala/scala/build/Inputs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import scala.util.matching.Regex

final case class Inputs(
elements: Seq[Inputs.Element],
mainClassElement: Option[Inputs.Element],
mainClassElement: Option[Inputs.SourceFile],
workspace: os.Path,
baseProjectName: String,
mayAppendHash: Boolean
Expand Down
3 changes: 2 additions & 1 deletion modules/build/src/main/scala/scala/build/bsp/BspImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ final class BspImpl(
inputs,
Sources.defaultPreprocessors(
buildOptions.scriptOptions.codeWrapper.getOrElse(CustomCodeWrapper)
)
),
logger
).left.map((_, Scope.Main))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ final case class ClassPathOptions(
extraSourceJars: Seq[os.Path] = Nil,
fetchSources: Option[Boolean] = None,
extraDependencies: Seq[Positioned[AnyDependency]] = Nil,
resourceVirtualDir: Seq[os.SubPath] = Nil
resourcesDir: Seq[os.Path] = Nil,
resourcesVirtualDir: Seq[os.SubPath] = Nil
)

object ClassPathOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package scala.build.preprocessing

import java.nio.charset.StandardCharsets

import scala.build.Inputs
import scala.build.errors.BuildException
import scala.build.{Inputs, Logger}

case object DataPreprocessor extends Preprocessor {
def preprocess(input: Inputs.SingleElement)
: Option[Either[BuildException, Seq[PreprocessedSource]]] =
def preprocess(
input: Inputs.SingleElement,
logger: Logger
): Option[Either[BuildException, Seq[PreprocessedSource]]] =
input match {
case file: Inputs.VirtualData =>
val content = new String(file.content, StandardCharsets.UTF_8)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package scala.build.preprocessing
import scala.build.Logger
import scala.build.Ops._
import scala.build.errors.{BuildException, CompositeBuildException}
import scala.build.options.ConfigMonoid
Expand All @@ -16,7 +17,8 @@ object DirectivesProcessor {
directives: Seq[StrictDirective],
handlers: Seq[DirectiveHandler[T]],
path: Either[String, os.Path],
cwd: ScopePath
cwd: ScopePath,
logger: Logger
): Either[BuildException, DirectivesProcessorOutput[T]] = {
val configMonoidInstance = implicitly[ConfigMonoid[T]]

Expand All @@ -37,7 +39,7 @@ object DirectivesProcessor {
.iterator
.flatMap {
case d @ StrictDirective(k, _) =>
handlersMap.get(k).iterator.map(_(d, path, cwd))
handlersMap.get(k).iterator.map(_(d, path, cwd, logger))
}
.toVector
.sequence
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package scala.build.preprocessing

import java.nio.charset.StandardCharsets

import scala.build.Inputs
import scala.build.errors.BuildException
import scala.build.{Inputs, Logger}

case object JavaPreprocessor extends Preprocessor {
def preprocess(input: Inputs.SingleElement)
: Option[Either[BuildException, Seq[PreprocessedSource]]] =
def preprocess(
input: Inputs.SingleElement,
logger: Logger
): Option[Either[BuildException, Seq[PreprocessedSource]]] =
input match {
case j: Inputs.JavaFile =>
Some(Right(Seq(PreprocessedSource.OnDisk(j.path, None, None, Nil, None))))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package scala.build.preprocessing

import scala.build.Inputs
import scala.build.errors.BuildException
import scala.build.{Inputs, Logger}

trait Preprocessor {
def preprocess(input: Inputs.SingleElement)
: Option[Either[BuildException, Seq[PreprocessedSource]]]
def preprocess(
input: Inputs.SingleElement,
logger: Logger
): Option[Either[BuildException, Seq[PreprocessedSource]]]
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import scala.build.errors.{
import scala.build.internal.{AmmUtil, Util}
import scala.build.options.{BuildOptions, BuildRequirements, ClassPathOptions}
import scala.build.preprocessing.directives._
import scala.build.{Inputs, Position, Positioned}
import scala.build.{Inputs, Logger, Position, Positioned}
import scala.jdk.CollectionConverters._

case object ScalaPreprocessor extends Preprocessor {
Expand Down Expand Up @@ -78,8 +78,10 @@ case object ScalaPreprocessor extends Preprocessor {
if (os.isFile(f)) Right(os.read(f, defaultCharSet))
else Left(new FileNotFoundException(f))

def preprocess(input: Inputs.SingleElement)
: Option[Either[BuildException, Seq[PreprocessedSource]]] =
def preprocess(
input: Inputs.SingleElement,
logger: Logger
): Option[Either[BuildException, Seq[PreprocessedSource]]] =
input match {
case f: Inputs.ScalaFile =>
val inferredClsName = {
Expand All @@ -89,7 +91,7 @@ case object ScalaPreprocessor extends Preprocessor {
val res = either {
val content = value(maybeRead(f.path))
val scopePath = ScopePath.fromPath(f.path)
val source = value(process(content, Right(f.path), scopePath / os.up)) match {
val source = value(process(content, Right(f.path), scopePath / os.up, logger)) match {
case None =>
PreprocessedSource.OnDisk(f.path, None, None, Nil, Some(inferredClsName))
case Some(ProcessingOutput(
Expand Down Expand Up @@ -127,7 +129,7 @@ case object ScalaPreprocessor extends Preprocessor {
val content = new String(v.content, StandardCharsets.UTF_8)
val (requirements, scopedRequirements, options, updatedContentOpt) =
value(
process(content, Left(v.source), v.scopePath / os.up)
process(content, Left(v.source), v.scopePath / os.up, logger)
).map {
case ProcessingOutput(reqs, scopedReqs, opts, updatedContent) =>
(reqs, scopedReqs, opts, updatedContent)
Expand All @@ -154,11 +156,12 @@ case object ScalaPreprocessor extends Preprocessor {
def process(
content: String,
path: Either[String, os.Path],
scopeRoot: ScopePath
scopeRoot: ScopePath,
logger: Logger
): Either[BuildException, Option[ProcessingOutput]] = either {
val (content0, isSheBang) = SheBang.ignoreSheBangLines(content)
val afterStrictUsing: StrictDirectivesProcessingOutput =
value(processStrictUsing(content0, path, scopeRoot))
value(processStrictUsing(content0, path, scopeRoot, logger))

val afterProcessImports: Option[SpecialImportsProcessingOutput] = value {
processSpecialImports(
Expand Down Expand Up @@ -273,7 +276,8 @@ case object ScalaPreprocessor extends Preprocessor {
private def processStrictUsing(
content: String,
path: Either[String, os.Path],
cwd: ScopePath
cwd: ScopePath,
logger: Logger
): Either[BuildException, StrictDirectivesProcessingOutput] = either {

val processor = {
Expand Down Expand Up @@ -301,7 +305,8 @@ case object ScalaPreprocessor extends Preprocessor {
directives0,
usingDirectiveHandlers,
path,
cwd
cwd,
logger
)
}

Expand All @@ -312,7 +317,8 @@ case object ScalaPreprocessor extends Preprocessor {
directives1,
requireDirectiveHandlers,
path,
cwd
cwd,
logger
)
}

Expand All @@ -336,7 +342,7 @@ case object ScalaPreprocessor extends Preprocessor {
value {
if (directives2.nonEmpty) {
val errors = directives2.map(d =>
new DefaultDirectiveHandler[Nothing].handleValues(d, path, cwd)
new DefaultDirectiveHandler[Nothing].handleValues(d, path, cwd, logger)
).collect {
case Left(e) => e
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ package scala.build.preprocessing
import java.nio.charset.StandardCharsets

import scala.build.EitherCps.{either, value}
import scala.build.Inputs
import scala.build.errors.BuildException
import scala.build.internal.{AmmUtil, CodeWrapper, CustomCodeWrapper, Name}
import scala.build.options.{BuildOptions, BuildRequirements}
import scala.build.preprocessing.ScalaPreprocessor.ProcessingOutput
import scala.build.{Inputs, Logger}

final case class ScriptPreprocessor(codeWrapper: CodeWrapper) extends Preprocessor {
def preprocess(input: Inputs.SingleElement)
: Option[Either[BuildException, Seq[PreprocessedSource]]] =
def preprocess(
input: Inputs.SingleElement,
logger: Logger
): Option[Either[BuildException, Seq[PreprocessedSource]]] =
input match {
case script: Inputs.Script =>
val content = os.read(script.path)
Expand All @@ -23,7 +25,8 @@ final case class ScriptPreprocessor(codeWrapper: CodeWrapper) extends Preprocess
content,
codeWrapper,
script.subPath,
ScopePath.fromPath(script.path)
ScopePath.fromPath(script.path),
logger
)
}
preprocessed
Expand All @@ -40,7 +43,8 @@ final case class ScriptPreprocessor(codeWrapper: CodeWrapper) extends Preprocess
content,
codeWrapper,
script.wrapperPath,
script.scopePath
script.scopePath,
logger
)
}
preprocessed
Expand All @@ -59,15 +63,21 @@ object ScriptPreprocessor {
content: String,
codeWrapper: CodeWrapper,
subPath: os.SubPath,
scopePath: ScopePath
scopePath: ScopePath,
logger: Logger
): Either[BuildException, List[PreprocessedSource.InMemory]] = either {

val (contentIgnoredSheBangLines, _) = SheBang.ignoreSheBangLines(content)

val (pkg, wrapper) = AmmUtil.pathToPackageWrapper(subPath)

val processingOutput =
value(ScalaPreprocessor.process(contentIgnoredSheBangLines, reportingPath, scopePath / os.up))
value(ScalaPreprocessor.process(
contentIgnoredSheBangLines,
reportingPath,
scopePath / os.up,
logger
))
.getOrElse(ProcessingOutput(BuildRequirements(), Nil, BuildOptions(), None))

val (code, topWrapperLen, _) = codeWrapper.wrapCode(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scala.build.preprocessing.directives
import os.Path

import scala.build.Logger
import scala.build.errors.{BuildException, UnusedDirectiveError}
import scala.build.preprocessing.ScopePath

Expand All @@ -16,7 +17,8 @@ class DefaultDirectiveHandler[T] extends DirectiveHandler[T] {
override def handleValues(
directive: StrictDirective,
path: Either[String, Path],
cwd: ScopePath
cwd: ScopePath,
logger: Logger
): Either[BuildException, ProcessedDirective[T]] = {
val values = DirectiveUtil.stringValues(
directive.values,
Expand Down
Loading