Skip to content

Commit 2b9fbb1

Browse files
committed
Ensure the warnings emitted during generateScalaDocumentation are pointing to original sources
1 parent 197df22 commit 2b9fbb1

File tree

1 file changed

+67
-47
lines changed

1 file changed

+67
-47
lines changed

project/Build.scala

Lines changed: 67 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2554,47 +2554,54 @@ object Build {
25542554
config: GenerationConfig => config.add(OutputDir(newDir))
25552555
})
25562556
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 {
25592560
val siteRoot = IO.createTemporaryDirectory.getAbsolutePath()
25602561
config.add(SiteRoot(siteRoot))
25612562
}
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+
}
25692564

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(
25702574
// 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()
25802598
}
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+
},
25962601
// 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+
25982605
val warningMessage = """{% if page.nightlyOf %}
25992606
| <aside class="warning">
26002607
| <div class='icon'></div>
@@ -2604,21 +2611,21 @@ object Build {
26042611
| </div>
26052612
| </aside>
26062613
|{% 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-
}
26152614

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+
)
26182625

26192626
val config = Def.task {
26202627
outputDirOverride
2621-
.andThen(setupContentOverrides)
2628+
.andThen(justAPIOverride)
26222629
.apply(Scala3.value)
26232630
}
26242631

@@ -2630,8 +2637,21 @@ object Build {
26302637
IO.write(dest / "CNAME", "dotty.epfl.ch")
26312638
}
26322639
}
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+
}
26332648

2634-
writeAdditionalFiles.dependsOn(generateDocumentation(config))
2649+
writeAdditionalFiles.dependsOn(
2650+
revertPatches.dependsOn(
2651+
generateDocumentation(config)
2652+
.dependsOn(applyPatches)
2653+
)
2654+
)
26352655
}.evaluated,
26362656

26372657
generateStableScala3Documentation := Def.inputTaskDyn {
@@ -2696,7 +2716,7 @@ object Build {
26962716
.add(ProjectVersion(baseVersion))
26972717
.remove[VersionsDictionaryUrl]
26982718
.add(SourceLinks(List(
2699-
s"${docs.getAbsolutePath}=github://scala/scala3/language-reference-stable"
2719+
s"${docs.getParentFile().getAbsolutePath}=github://scala/scala3/language-reference-stable"
27002720
)))
27012721
.withTargets(List("___fake___.scala"))
27022722
}

0 commit comments

Comments
 (0)