Skip to content

Commit 85709e8

Browse files
Pass a root to resolve things from to directives
1 parent d7d9fc8 commit 85709e8

18 files changed

+99
-36
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ object DirectivesProcessor {
4545

4646
def process(
4747
directives: Map[Path, Seq[Value[_]]],
48-
handlers: Seq[DirectiveHandler]
48+
handlers: Seq[DirectiveHandler],
49+
cwd: ScopePath
4950
): Either[BuildException, BuildOptions] = {
5051

5152
val values = directives.map {
@@ -63,7 +64,7 @@ object DirectivesProcessor {
6364
.iterator
6465
.flatMap {
6566
case (k, v) =>
66-
handlersMap.get(k).iterator.map(_(v))
67+
handlersMap.get(k).iterator.map(_(v, cwd))
6768
}
6869
.toVector
6970
.sequence

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ case object ScalaPreprocessor extends Preprocessor {
124124
Option[String]
125125
)]] = either {
126126

127-
val afterStrictUsing = value(processStrictUsing(content))
127+
val afterStrictUsing = value(processStrictUsing(content, scopeRoot))
128128
val afterUsing = value {
129129
processUsing(path, afterStrictUsing.map(_._2).getOrElse(content), scopeRoot)
130130
.sequence
@@ -153,14 +153,16 @@ case object ScalaPreprocessor extends Preprocessor {
153153
}
154154
}
155155

156-
private def directivesBuildOptions(directives: Seq[Directive])
157-
: Either[BuildException, BuildOptions] = {
156+
private def directivesBuildOptions(
157+
directives: Seq[Directive],
158+
cwd: ScopePath
159+
): Either[BuildException, BuildOptions] = {
158160
val results = directives
159161
.filter(_.tpe == Directive.Using)
160162
.map { dir =>
161163
val fromHandlersOpt = usingDirectiveHandlers
162164
.iterator
163-
.flatMap(_.handle(dir).iterator)
165+
.flatMap(_.handle(dir, cwd).iterator)
164166
.take(1)
165167
.toList
166168
.headOption
@@ -190,7 +192,7 @@ case object ScalaPreprocessor extends Preprocessor {
190192
.map { dir =>
191193
val fromHandlersOpt = requireDirectiveHandlers
192194
.iterator
193-
.flatMap(_.handle(dir).iterator)
195+
.flatMap(_.handle(dir, scopeRoot).iterator)
194196
.take(1)
195197
.toList
196198
.headOption
@@ -240,7 +242,7 @@ case object ScalaPreprocessor extends Preprocessor {
240242
case (directives, updatedContentOpt) =>
241243
val tuple = (
242244
directivesBuildRequirements(directives, scopeRoot),
243-
directivesBuildOptions(directives),
245+
directivesBuildOptions(directives, scopeRoot),
244246
Right(updatedContentOpt)
245247
)
246248
tuple
@@ -339,7 +341,8 @@ case object ScalaPreprocessor extends Preprocessor {
339341
}
340342

341343
private def processStrictUsing(
342-
content: String
344+
content: String,
345+
cwd: ScopePath
343346
): Either[BuildException, Option[(BuildOptions, String)]] = either {
344347

345348
val processor = {
@@ -364,7 +367,8 @@ case object ScalaPreprocessor extends Preprocessor {
364367
val updatedOptions = value {
365368
DirectivesProcessor.process(
366369
directives0,
367-
usingDirectiveHandlers ++ requireDirectiveHandlers
370+
usingDirectiveHandlers ++ requireDirectiveHandlers,
371+
cwd
368372
)
369373
}
370374
val codeOffset = directives.getCodeOffset()

modules/build/src/main/scala/scala/build/preprocessing/directives/DirectiveHandler.scala

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

33
import scala.build.errors.BuildException
44
import scala.build.options.BuildOptions
5+
import scala.build.preprocessing.ScopePath
56

67
trait DirectiveHandler {
78
def name: String
@@ -13,7 +14,7 @@ trait DirectiveHandler {
1314

1415
// Strict / using_directives-based directives
1516
def keys: Seq[String] = Nil
16-
def handleValues(values: Seq[Any]): Either[BuildException, BuildOptions] =
17+
def handleValues(values: Seq[Any], cwd: ScopePath): Either[BuildException, BuildOptions] =
1718
if (keys.isEmpty)
1819
sys.error("Cannot happen")
1920
else

modules/build/src/main/scala/scala/build/preprocessing/directives/RequireDirectiveHandler.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ package scala.build.preprocessing.directives
22

33
import scala.build.errors.BuildException
44
import scala.build.options.BuildRequirements
5+
import scala.build.preprocessing.ScopePath
56

67
trait RequireDirectiveHandler extends DirectiveHandler {
7-
def handle(directive: Directive): Option[Either[BuildException, BuildRequirements]]
8+
def handle(
9+
directive: Directive,
10+
cwd: ScopePath
11+
): Option[Either[BuildException, BuildRequirements]]
812
}

modules/build/src/main/scala/scala/build/preprocessing/directives/RequirePlatformsDirectiveHandler.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package scala.build.preprocessing.directives
22

33
import scala.build.errors.BuildException
44
import scala.build.options.{BuildRequirements, Platform}
5+
import scala.build.preprocessing.ScopePath
56

67
case object RequirePlatformsDirectiveHandler extends RequireDirectiveHandler {
78
def name = "Platform"
@@ -14,7 +15,10 @@ case object RequirePlatformsDirectiveHandler extends RequireDirectiveHandler {
1415
"using target jvm"
1516
)
1617

17-
def handle(directive: Directive): Option[Either[BuildException, BuildRequirements]] =
18+
def handle(
19+
directive: Directive,
20+
cwd: ScopePath
21+
): Option[Either[BuildException, BuildRequirements]] =
1822
Platform.parseSpec(directive.values.map(Platform.normalize)) match {
1923
case Some(platforms) =>
2024
val reqs = BuildRequirements(

modules/build/src/main/scala/scala/build/preprocessing/directives/RequireScalaVersionDirectiveHandler.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package scala.build.preprocessing.directives
22

33
import scala.build.errors.BuildException
44
import scala.build.options.BuildRequirements
5+
import scala.build.preprocessing.ScopePath
56

67
case object RequireScalaVersionDirectiveHandler extends RequireDirectiveHandler {
78
def name = "Scala version"
@@ -14,7 +15,10 @@ case object RequireScalaVersionDirectiveHandler extends RequireDirectiveHandler
1415
"using target scala 3.0.2"
1516
)
1617

17-
def handle(directive: Directive): Option[Either[BuildException, BuildRequirements]] =
18+
def handle(
19+
directive: Directive,
20+
cwd: ScopePath
21+
): Option[Either[BuildException, BuildRequirements]] =
1822
directive.values match {
1923
case Seq("scala", ">=", minVer) =>
2024
val req = BuildRequirements(

modules/build/src/main/scala/scala/build/preprocessing/directives/RequireScopeDirectiveHandler.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package scala.build.preprocessing.directives
22

33
import scala.build.errors.BuildException
44
import scala.build.options.{BuildRequirements, Scope}
5+
import scala.build.preprocessing.ScopePath
56

67
case object RequireScopeDirectiveHandler extends RequireDirectiveHandler {
78
def name = "Scope"
@@ -14,7 +15,10 @@ case object RequireScopeDirectiveHandler extends RequireDirectiveHandler {
1415

1516
private val scopesByName = Scope.all.map(s => s.name -> s).toMap
1617

17-
def handle(directive: Directive): Option[Either[BuildException, BuildRequirements]] =
18+
def handle(
19+
directive: Directive,
20+
cwd: ScopePath
21+
): Option[Either[BuildException, BuildRequirements]] =
1822
directive.values match {
1923
case Seq(name) if scopesByName.contains(name) =>
2024
val scope = scopesByName(name)

modules/build/src/main/scala/scala/build/preprocessing/directives/UsingCustomJarDirectiveHandler.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package scala.build.preprocessing.directives
33
import scala.build.Os
44
import scala.build.errors.BuildException
55
import scala.build.options.{BuildOptions, ClassPathOptions}
6+
import scala.build.preprocessing.ScopePath
67

78
case object UsingCustomJarDirectiveHandler extends UsingDirectiveHandler {
89
def name = "Custom JAR"
@@ -14,7 +15,7 @@ case object UsingCustomJarDirectiveHandler extends UsingDirectiveHandler {
1415
"using jar \"/Users/alexandre/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/com/chuusai/shapeless_2.13/2.3.7/shapeless_2.13-2.3.7.jar\""
1516
)
1617

17-
def handle(directive: Directive): Option[Either[BuildException, BuildOptions]] =
18+
def handle(directive: Directive, cwd: ScopePath): Option[Either[BuildException, BuildOptions]] =
1819
directive.values match {
1920
case Seq("jar" | "jars", paths @ _*) =>
2021
val paths0 = paths.map(os.Path(_, Os.pwd))
@@ -29,7 +30,10 @@ case object UsingCustomJarDirectiveHandler extends UsingDirectiveHandler {
2930
}
3031

3132
override def keys = Seq("jar", "jars")
32-
override def handleValues(values: Seq[Any]): Either[BuildException, BuildOptions] = {
33+
override def handleValues(
34+
values: Seq[Any],
35+
cwd: ScopePath
36+
): Either[BuildException, BuildOptions] = {
3337

3438
val extraJars = DirectiveUtil.stringValues(values).map { p =>
3539
// FIXME Not the right cwd

modules/build/src/main/scala/scala/build/preprocessing/directives/UsingDependencyDirectiveHandler.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import scala.build.Ops._
88
import scala.build.Positioned
99
import scala.build.errors.{BuildException, DependencyFormatError}
1010
import scala.build.options.{BuildOptions, ClassPathOptions}
11+
import scala.build.preprocessing.ScopePath
1112

1213
case object UsingDependencyDirectiveHandler extends UsingDirectiveHandler {
1314
def name = "Dependency"
@@ -19,7 +20,7 @@ case object UsingDependencyDirectiveHandler extends UsingDirectiveHandler {
1920
"using dev.zio::zio:1.0.12"
2021
)
2122

22-
def handle(directive: Directive): Option[Either[BuildException, BuildOptions]] =
23+
def handle(directive: Directive, cwd: ScopePath): Option[Either[BuildException, BuildOptions]] =
2324
directive.values match {
2425
case Seq(depStr) if depStr.split(":").count(_.trim.nonEmpty) == 3 =>
2526
val res =
@@ -39,7 +40,10 @@ case object UsingDependencyDirectiveHandler extends UsingDirectiveHandler {
3940
.left.map(err => new DependencyFormatError(depStr, err))
4041

4142
override def keys = Seq("lib")
42-
override def handleValues(values: Seq[Any]): Either[BuildException, BuildOptions] = either {
43+
override def handleValues(
44+
values: Seq[Any],
45+
cwd: ScopePath
46+
): Either[BuildException, BuildOptions] = either {
4347

4448
val extraDependencies = value {
4549
DirectiveUtil.stringValues(values)

modules/build/src/main/scala/scala/build/preprocessing/directives/UsingDirectiveHandler.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package scala.build.preprocessing.directives
22

33
import scala.build.errors.BuildException
44
import scala.build.options.BuildOptions
5+
import scala.build.preprocessing.ScopePath
56

67
trait UsingDirectiveHandler extends DirectiveHandler {
78
// Loose / fastparse-based directives
8-
def handle(directive: Directive): Option[Either[BuildException, BuildOptions]]
9+
def handle(directive: Directive, cwd: ScopePath): Option[Either[BuildException, BuildOptions]]
910
}

0 commit comments

Comments
 (0)