Skip to content

Commit 8a3ead6

Browse files
committed
Add API for getting data about using directives' definitions
1 parent 2353b2c commit 8a3ead6

File tree

3 files changed

+50
-18
lines changed

3 files changed

+50
-18
lines changed

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

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,35 +46,41 @@ trait DirectiveHandler[+T] { self =>
4646

4747
def map[U](f: T => U): DirectiveHandler[U] =
4848
new DirectiveHandler[U] {
49-
def name = self.name
50-
def usage = self.usage
51-
override def usageMd = self.usageMd
52-
def description = self.description
53-
override def descriptionMd = self.descriptionMd
54-
override def examples = self.examples
49+
def name: String = self.name
50+
def usage: String = self.usage
51+
override def usageMd: String = self.usageMd
52+
def description: String = self.description
53+
override def descriptionMd: String = self.descriptionMd
54+
override def examples: Seq[String] = self.examples
5555

56-
def scalaSpecificationLevel = self.scalaSpecificationLevel
56+
def scalaSpecificationLevel: SpecificationLevel = self.scalaSpecificationLevel
5757

58-
def keys = self.keys
58+
def keys: Seq[Key] = self.keys
5959

60-
def handleValues(scopedDirective: ScopedDirective, logger: Logger) =
60+
def handleValues(
61+
scopedDirective: ScopedDirective,
62+
logger: Logger
63+
): Either[BuildException, ProcessedDirective[U]] =
6164
self.handleValues(scopedDirective, logger)
6265
.map(_.map(f))
6366
}
6467
def mapE[U](f: T => Either[BuildException, U]): DirectiveHandler[U] =
6568
new DirectiveHandler[U] {
66-
def name = self.name
67-
def usage = self.usage
68-
override def usageMd = self.usageMd
69-
def description = self.description
70-
override def descriptionMd = self.descriptionMd
71-
override def examples = self.examples
69+
def name: String = self.name
70+
def usage: String = self.usage
71+
override def usageMd: String = self.usageMd
72+
def description: String = self.description
73+
override def descriptionMd: String = self.descriptionMd
74+
override def examples: Seq[String] = self.examples
7275

73-
def scalaSpecificationLevel = self.scalaSpecificationLevel
76+
def scalaSpecificationLevel: SpecificationLevel = self.scalaSpecificationLevel
7477

75-
def keys = self.keys
78+
def keys: Seq[Key] = self.keys
7679

77-
def handleValues(scopedDirective: ScopedDirective, logger: Logger) =
80+
def handleValues(
81+
scopedDirective: ScopedDirective,
82+
logger: Logger
83+
): Either[BuildException, ProcessedDirective[U]] =
7884
self.handleValues(scopedDirective, logger).flatMap(_.mapE(f))
7985
}
8086

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,11 @@ object Directives {
5454
directives.RequireScalaVersionBounds.handler,
5555
directives.RequireScope.handler
5656
).map(_.mapE(_.buildRequirements))
57+
58+
def allDirectiveHandlers: Seq[DirectiveHandler[BuildRequirements | BuildOptions]] =
59+
usingDirectiveHandlers ++ requireDirectiveHandlers
60+
61+
def getDirectiveHandler(key: String): Option[DirectiveHandler[BuildRequirements | BuildOptions]] =
62+
allDirectiveHandlers.find(_.keys.exists(_.nameAliases.contains(key)))
63+
5764
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package scala.build.preprocessing.directives
2+
3+
import com.eed3si9n.expecty.Expecty.expect
4+
5+
class DirectivesTest extends munit.FunSuite {
6+
test("get directive handler by key") {
7+
val key = "python"
8+
val handler = Directives.getDirectiveHandler(key).get
9+
expect(handler.keys.flatMap(_.nameAliases).contains(key))
10+
expect(handler.isExperimental)
11+
expect(handler.name == "Python")
12+
expect(handler.description.nonEmpty)
13+
expect(handler.descriptionMd.nonEmpty)
14+
expect(handler.usage.nonEmpty)
15+
expect(handler.usageMd.nonEmpty)
16+
expect(handler.examples.nonEmpty)
17+
}
18+
19+
}

0 commit comments

Comments
 (0)