@@ -11,6 +11,7 @@ import shapeless.tag
11
11
import java .nio .charset .StandardCharsets
12
12
import java .util
13
13
14
+ import scala .build .info .{ArtifactId , BuildInfo , ExportDependencyFormat , ScopedBuildInfo }
14
15
import scala .build .options .{BuildOptions , BuildRequirements , WithBuildRequirements }
15
16
import scala .build .preprocessing .directives .DirectiveHandler
16
17
import scala .build .preprocessing .directives .DirectivesPreprocessingUtils .*
@@ -481,6 +482,93 @@ object GenerateReferenceDoc extends CaseApp[InternalDocOptions] {
481
482
b.toString
482
483
}
483
484
485
+ private def buildInfoContent : String = {
486
+ val b = new StringBuilder
487
+
488
+ b.section(
489
+ """ ---
490
+ |title: BuildInfo
491
+ |sidebar_position: 6
492
+ |---""" .stripMargin
493
+ )
494
+ b.section(
495
+ """ :::caution
496
+ |BuildInfo is a restricted feature and requires setting the `--power` option to be used.
497
+ |You can pass it explicitly or set it globally by running:
498
+ |
499
+ | scala-cli config power true
500
+ |:::""" .stripMargin
501
+ )
502
+
503
+ b.section(
504
+ """ During the building process Scala CLI collects information about the project's configuration,
505
+ |both from the console options and `using directives` found in the project's sources.
506
+ |You can access this information from your code using the `BuildInfo` object, that's automatically generated for your
507
+ |build on compile when that information changes.""" .stripMargin
508
+ )
509
+
510
+ b.section(
511
+ """ To enable BuildInfo generation pass the `--build-info` option to Scala CLI or use a
512
+ |`//> using buildInfo` directive.""" .stripMargin
513
+ )
514
+
515
+ b.section(
516
+ """ ## Usage
517
+ |
518
+ |The generated BuildInfo object is available on the project's classpath. To access it you need to import it first.
519
+ |It is available in the package `scala.cli.build` so use
520
+ |```scala
521
+ |import scala.cli.build.BuildInfo
522
+ |```
523
+ |to import it.
524
+ |
525
+ |Below you can find an example instance of the BuildInfo object, with all fields explained.
526
+ |Some of the values have been shortened for readability.""" .stripMargin
527
+ )
528
+
529
+ val osLibDep = ExportDependencyFormat (" com.lihaoyi" , ArtifactId (" os-lib" , " os-lib_3" ), " 0.9.1" )
530
+ val toolkitDep =
531
+ ExportDependencyFormat (" org.scala-lang" , ArtifactId (" toolkit" , " toolkit_3" ), " latest.release" )
532
+
533
+ val mainScopedBuildInfo = ScopedBuildInfo (BuildOptions (), Seq (" .../Main.scala" )).copy(
534
+ scalacOptions = Seq (" -Werror" ),
535
+ scalaCompilerPlugins = Nil ,
536
+ dependencies = Seq (osLibDep),
537
+ resolvers = Seq (" https://repo1.maven.org/maven2" , " ivy:file:..." ),
538
+ resourceDirs = Seq (" .../resources" ),
539
+ customJarsDecls = Seq (" .../AwesomeJar1.jar" , " .../AwesomeJar2.jar" )
540
+ )
541
+
542
+ val testScopedBuildInfo = ScopedBuildInfo (BuildOptions (), Seq (" .../MyTests.scala" )).copy(
543
+ scalacOptions = Seq (" -Vdebug" ),
544
+ scalaCompilerPlugins = Nil ,
545
+ dependencies = Seq (toolkitDep),
546
+ resolvers = Seq (" https://repo1.maven.org/maven2" , " ivy:file:..." ),
547
+ resourceDirs = Seq (" .../test/resources" ),
548
+ customJarsDecls = Nil
549
+ )
550
+
551
+ val buildInfo = BuildInfo (BuildOptions ()).copy(
552
+ scalaVersion = Some (" 3.3.0" ),
553
+ platform = Some (" JVM" ),
554
+ jvmVersion = Some (" 11" ),
555
+ scalaJsVersion = None ,
556
+ jsEsVersion = None ,
557
+ scalaNativeVersion = None ,
558
+ mainClass = Some (" Main" )
559
+ )
560
+ .withScope(" main" , mainScopedBuildInfo)
561
+ .withScope(" test" , testScopedBuildInfo)
562
+
563
+ b.section(
564
+ s """ ```scala
565
+ | ${buildInfo.generateContents().strip()}
566
+ |``` """ .stripMargin
567
+ )
568
+
569
+ b.mkString
570
+ }
571
+
484
572
def run (options : InternalDocOptions , args : RemainingArgs ): Unit = {
485
573
486
574
val scalaCli = new ScalaCliCommands (
@@ -512,6 +600,7 @@ object GenerateReferenceDoc extends CaseApp[InternalDocOptions] {
512
600
requireDirectiveHandlers,
513
601
onlyRestricted = false
514
602
)
603
+
515
604
val restrictedDirectivesContent = usingContent(
516
605
allUsingDirectiveHandlers.filterNot(_.isRestricted),
517
606
requireDirectiveHandlers.filterNot(_.isRestricted),
@@ -524,6 +613,7 @@ object GenerateReferenceDoc extends CaseApp[InternalDocOptions] {
524
613
(os.rel / " cli-options.md" ) -> allCliOptionsContent,
525
614
(os.rel / " commands.md" ) -> allCommandsContent,
526
615
(os.rel / " directives.md" ) -> allDirectivesContent,
616
+ (os.rel / " build-info.md" ) -> buildInfoContent,
527
617
(os.rel / restrictedDocsDir / " cli-options.md" ) -> restrictedCliOptionsContent,
528
618
(os.rel / restrictedDocsDir / " commands.md" ) -> restrictedCommandsContent,
529
619
(os.rel / restrictedDocsDir / " directives.md" ) -> restrictedDirectivesContent,
@@ -551,6 +641,7 @@ object GenerateReferenceDoc extends CaseApp[InternalDocOptions] {
551
641
maybeWrite(options.outputPath / " cli-options.md" , allCliOptionsContent)
552
642
maybeWrite(options.outputPath / " commands.md" , allCommandsContent)
553
643
maybeWrite(options.outputPath / " directives.md" , allDirectivesContent)
644
+ maybeWrite(options.outputPath / " build-info.md" , buildInfoContent)
554
645
555
646
maybeWrite(
556
647
options.outputPath / restrictedDocsDir / " cli-options.md" ,
0 commit comments