@@ -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,92 @@ 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 when requested.""" .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
+ |To access the BuildInfo object you need to import it first. It is available in package `scala.cli.build` so use
519
+ |```scala
520
+ |import scala.cli.build.BuildInfo
521
+ |```
522
+ |to import it.
523
+ |
524
+ |Below you can find an example instance of the BuildInfo object, with all fields explained.
525
+ |Some of the values have been shortened for readability.""" .stripMargin
526
+ )
527
+
528
+ val osLibDep = ExportDependencyFormat (" com.lihaoyi" , ArtifactId (" os-lib" , " os-lib_3" ), " 0.9.1" )
529
+ val toolkitDep =
530
+ ExportDependencyFormat (" org.scala-lang" , ArtifactId (" toolkit" , " toolkit_3" ), " latest.release" )
531
+
532
+ val mainScopedBuildInfo = ScopedBuildInfo (BuildOptions (), Seq (" .../Main.scala" )).copy(
533
+ scalacOptions = Seq (" -Werror" ),
534
+ scalaCompilerPlugins = Nil ,
535
+ dependencies = Seq (osLibDep),
536
+ resolvers = Seq (" https://repo1.maven.org/maven2" , " ivy:file:..." ),
537
+ resourceDirs = Seq (" .../resources" ),
538
+ customJarsDecls = Seq (" .../AwesomeJar1.jar" , " .../AwesomeJar2.jar" )
539
+ )
540
+
541
+ val testScopedBuildInfo = ScopedBuildInfo (BuildOptions (), Seq (" .../MyTests.scala" )).copy(
542
+ scalacOptions = Seq (" -Vdebug" ),
543
+ scalaCompilerPlugins = Nil ,
544
+ dependencies = Seq (toolkitDep),
545
+ resolvers = Seq (" https://repo1.maven.org/maven2" , " ivy:file:..." ),
546
+ resourceDirs = Seq (" .../test/resources" ),
547
+ customJarsDecls = Nil
548
+ )
549
+
550
+ val buildInfo = BuildInfo (BuildOptions ()).copy(
551
+ scalaVersion = Some (" 3.3.0" ),
552
+ platform = Some (" JVM" ),
553
+ jvmVersion = Some (" 11" ),
554
+ scalaJsVersion = None ,
555
+ jsEsVersion = None ,
556
+ scalaNativeVersion = None ,
557
+ mainClass = Some (" Main" )
558
+ )
559
+ .withScope(" main" , mainScopedBuildInfo)
560
+ .withScope(" test" , testScopedBuildInfo)
561
+
562
+ b.section(
563
+ s """ ```scala
564
+ | ${buildInfo.generateContents().strip()}
565
+ |``` """ .stripMargin
566
+ )
567
+
568
+ b.mkString
569
+ }
570
+
484
571
def run (options : InternalDocOptions , args : RemainingArgs ): Unit = {
485
572
486
573
val scalaCli = new ScalaCliCommands (
@@ -512,6 +599,7 @@ object GenerateReferenceDoc extends CaseApp[InternalDocOptions] {
512
599
requireDirectiveHandlers,
513
600
onlyRestricted = false
514
601
)
602
+
515
603
val restrictedDirectivesContent = usingContent(
516
604
allUsingDirectiveHandlers.filterNot(_.isRestricted),
517
605
requireDirectiveHandlers.filterNot(_.isRestricted),
@@ -551,6 +639,7 @@ object GenerateReferenceDoc extends CaseApp[InternalDocOptions] {
551
639
maybeWrite(options.outputPath / " cli-options.md" , allCliOptionsContent)
552
640
maybeWrite(options.outputPath / " commands.md" , allCommandsContent)
553
641
maybeWrite(options.outputPath / " directives.md" , allDirectivesContent)
642
+ maybeWrite(options.outputPath / " build-info.md" , buildInfoContent)
554
643
555
644
maybeWrite(
556
645
options.outputPath / restrictedDocsDir / " cli-options.md" ,
0 commit comments