@@ -2554,47 +2554,54 @@ object Build {
2554
2554
config : GenerationConfig => config.add(OutputDir (newDir))
2555
2555
})
2556
2556
val justAPI = extraArgs.contains(" --justAPI" )
2557
- def setupContentOverrides (config : GenerationConfig ): GenerationConfig = {
2558
- if (justAPI) {
2557
+ def justAPIOverride (config : GenerationConfig ): GenerationConfig = {
2558
+ if (! justAPI) config
2559
+ else {
2559
2560
val siteRoot = IO .createTemporaryDirectory.getAbsolutePath()
2560
2561
config.add(SiteRoot (siteRoot))
2561
2562
}
2562
- else patchSources(config)
2563
- }
2564
-
2565
- def patchSources (config : GenerationConfig ): GenerationConfig = {
2566
- // Create a copy of docs and apply patches
2567
- val docs = IO .createTemporaryDirectory
2568
- IO .copyDirectory(file(" docs" ), docs)
2563
+ }
2569
2564
2565
+ // It would be the easiest to create a temp directory and apply patches there, but this task would be used frequently during development
2566
+ // If we'd build using copy the emitted warnings would point developers to copies instead of original sources. Any fixes made in there would be lost.
2567
+ // Instead let's apply revertable patches to the files as part snapshot doc generation process
2568
+ abstract class SourcePatch (val file : File ) {
2569
+ def apply (): Unit
2570
+ def revert (): Unit
2571
+ }
2572
+ val docs = file(" docs" )
2573
+ val sourcePatches = if (justAPI) Nil else Seq (
2570
2574
// Generate full sidebar.yml based on template and reference content
2571
- locally {
2572
- val outputFile = docs / " sidebar.yml"
2573
- val yaml = new org.yaml.snakeyaml.Yaml ()
2574
- type YamlObject = java.util.Map [String , AnyRef ]
2575
- type YamlList [T ] = java.util.List [T ]
2576
- def loadYaml (file : File ): YamlObject = {
2577
- val reader = Files .newBufferedReader(file.toPath)
2578
- try yaml.load(reader).asInstanceOf [YamlObject ]
2579
- finally reader.close()
2575
+ new SourcePatch (docs / " sidebar.yml" ) {
2576
+ override def apply (): Unit = {
2577
+ val yaml = new org.yaml.snakeyaml.Yaml ()
2578
+ type YamlObject = java.util.Map [String , AnyRef ]
2579
+ type YamlList [T ] = java.util.List [T ]
2580
+ def loadYaml (file : File ): YamlObject = {
2581
+ val reader = Files .newBufferedReader(file.toPath)
2582
+ try yaml.load(reader).asInstanceOf [YamlObject ]
2583
+ finally reader.close()
2584
+ }
2585
+ // Ensure to always operate on original (Map, List) instances
2586
+ val template = loadYaml(docs / " sidebar.nightly.template.yml" )
2587
+ template.get(" subsection" )
2588
+ .asInstanceOf [YamlList [YamlObject ]]
2589
+ .stream()
2590
+ .filter(_.get(" title" ) == " Reference" )
2591
+ .findFirst()
2592
+ .orElseThrow(() => new IllegalStateException (" Reference subsection not found in sidebar.nightly.template.yml" ))
2593
+ .putAll(loadYaml(docs / " sidebar.reference.yml" ))
2594
+
2595
+ val sidebarWriter = Files .newBufferedWriter(this .file.toPath)
2596
+ try yaml.dump(template, sidebarWriter)
2597
+ finally sidebarWriter.close()
2580
2598
}
2581
- // Ensure to always operate on original (Map, List) instances
2582
- val template = loadYaml(docs / " sidebar.nightly.template.yml" )
2583
- template.get(" subsection" )
2584
- .asInstanceOf [YamlList [YamlObject ]]
2585
- .stream()
2586
- .filter(_.get(" title" ) == " Reference" )
2587
- .findFirst()
2588
- .orElseThrow(() => new IllegalStateException (" Reference subsection not found in sidebar.nightly.template.yml" ))
2589
- .putAll(loadYaml(docs / " sidebar.reference.yml" ))
2590
-
2591
- val sidebarWriter = Files .newBufferedWriter(outputFile.toPath)
2592
- try yaml.dump(template, sidebarWriter)
2593
- finally sidebarWriter.close()
2594
- }
2595
-
2599
+ override def revert (): Unit = IO .delete(file)
2600
+ },
2596
2601
// Add patch about nightly version usage
2597
- locally {
2602
+ new SourcePatch (docs / " _layouts" / " static-site-main.html" ) {
2603
+ lazy val originalContent = IO .read(file)
2604
+
2598
2605
val warningMessage = """ {% if page.nightlyOf %}
2599
2606
| <aside class="warning">
2600
2607
| <div class='icon'></div>
@@ -2604,21 +2611,21 @@ object Build {
2604
2611
| </div>
2605
2612
| </aside>
2606
2613
|{% endif %}""" .stripMargin
2607
- val mainStaticSiteLayout = docs / " _layouts" / " static-site-main.html"
2608
- IO .write(
2609
- mainStaticSiteLayout,
2610
- IO .read(mainStaticSiteLayout)
2611
- .replace(" {{ content }}" , s " $warningMessage {{ content }} " )
2612
- .ensuring(_.contains(warningMessage), " patch to static-site-main layout not applied!" )
2613
- )
2614
- }
2615
2614
2616
- config.add(SiteRoot (docs.getAbsolutePath))
2617
- }
2615
+ override def apply (): Unit = {
2616
+ IO .write(file,
2617
+ originalContent
2618
+ .replace(" {{ content }}" , s " $warningMessage {{ content }} " )
2619
+ .ensuring(_.contains(warningMessage), " patch to static-site-main layout not applied!" )
2620
+ )
2621
+ }
2622
+ override def revert (): Unit = IO .write(file, originalContent)
2623
+ }
2624
+ )
2618
2625
2619
2626
val config = Def .task {
2620
2627
outputDirOverride
2621
- .andThen(setupContentOverrides )
2628
+ .andThen(justAPIOverride )
2622
2629
.apply(Scala3 .value)
2623
2630
}
2624
2631
@@ -2630,8 +2637,21 @@ object Build {
2630
2637
IO .write(dest / " CNAME" , " dotty.epfl.ch" )
2631
2638
}
2632
2639
}
2640
+ val applyPatches = Def .task {
2641
+ streams.value.log.info(s " Generating snapshot scaladoc, would apply patches to ${sourcePatches.map(_.file)}" )
2642
+ sourcePatches.foreach(_.apply())
2643
+ }
2644
+ val revertPatches = Def .task {
2645
+ streams.value.log.info(s " Generated snapshot scaladoc, reverting changes made to ${sourcePatches.map(_.file)}" )
2646
+ sourcePatches.foreach(_.revert())
2647
+ }
2633
2648
2634
- writeAdditionalFiles.dependsOn(generateDocumentation(config))
2649
+ writeAdditionalFiles.dependsOn(
2650
+ revertPatches.dependsOn(
2651
+ generateDocumentation(config)
2652
+ .dependsOn(applyPatches)
2653
+ )
2654
+ )
2635
2655
}.evaluated,
2636
2656
2637
2657
generateStableScala3Documentation := Def .inputTaskDyn {
@@ -2696,7 +2716,7 @@ object Build {
2696
2716
.add(ProjectVersion (baseVersion))
2697
2717
.remove[VersionsDictionaryUrl ]
2698
2718
.add(SourceLinks (List (
2699
- s " ${docs.getAbsolutePath}=github://scala/scala3/language-reference-stable "
2719
+ s " ${docs.getParentFile(). getAbsolutePath}=github://scala/scala3/language-reference-stable "
2700
2720
)))
2701
2721
.withTargets(List (" ___fake___.scala" ))
2702
2722
}
0 commit comments