Skip to content

Commit ced87c3

Browse files
committed
Move script to sbt task
1 parent 633d3bf commit ced87c3

File tree

7 files changed

+83
-66
lines changed

7 files changed

+83
-66
lines changed

.github/workflows/ci.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -490,9 +490,6 @@ jobs:
490490
- name: Add SBT proxy repositories
491491
run: cp -vf .github/workflows/repositories /root/.sbt/ ; true
492492

493-
- name: Copy docs
494-
run: ./bin/scala project/scripts/processDocs.sc
495-
496493
- name: Generate Website
497494
run: |
498495
./project/scripts/genDocs -doc-snapshot

.github/workflows/scaladoc.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ jobs:
5252
- name: Generate testcases documentation
5353
run: ./project/scripts/sbt scaladoc/generateTestcasesDocumentation
5454

55-
- name: Copy docs
56-
run: ./bin/scala project/scripts/processDocs.sc
57-
5855
- name: Generate Scala 3 documentation
5956
run: ./project/scripts/sbt scaladoc/generateScalaDocumentation
6057

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,6 @@ cs
102102

103103
# Coursier test product
104104
compiler/test-coursier/run/*.jar
105+
106+
# Docs
107+
docs-for-dotty-page/*

docs/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
This directory is for keeping all the compiler related docs.
22

33
For serving this files, one should notice that files under `docs/docs/reference` and `docs/docs/usage/scaladoc` are designed to be compatible with `docs.scala-lang` jekyll page.
4-
If one wants to generate static site with these files, should firstly run the script from `project/scripts/processDocs.sc`, for example with:
5-
`bin/scala project/scripts/processDocs.sc` and then using sbt task `sbt scaladoc/generateScalaDocumentation` generate the whole scaladoc for dotty.
4+
Preprocessing the sources to be compatible with scaladoc static site is done via script at `project/CopyDocs.scala`

project/Build.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,7 @@ object Build {
13881388
val dest = file(extraArgs.headOption.getOrElse("scaladoc/output/scala3")).getAbsoluteFile
13891389
val justAPI = extraArgs.drop(1).headOption == Some("--justAPI")
13901390
val majorVersion = (LocalProject("scala3-library-bootstrapped") / scalaBinaryVersion).value
1391-
1391+
CopyDocs.copyDocs() // invoke copying function form `project/CopyDocs.scala`
13921392
val dottyJars: Seq[java.io.File] = Seq(
13931393
(`stdlib-bootstrapped`/Compile/products).value,
13941394
(`scala3-interfaces`/Compile/products).value,

project/CopyDocs.scala

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import scala.io.{ Codec, Source }
2+
import java.nio.file.{ FileSystems, Files, Path, StandardCopyOption }
3+
import java.io.PrintStream
4+
import collection.JavaConverters._
5+
6+
7+
/**
8+
* Object used for copying docs from docs to docs-for-dotty-page. More explenation can be found and [readme](../docs/README.md)
9+
*/
10+
object CopyDocs {
11+
12+
/**
13+
* Input directory from which we will take all the docs.
14+
*/
15+
val inputDir = FileSystems.getDefault.getPath("docs")
16+
17+
/**
18+
* Output directory. Most of the sources will be copied as they are, but some of the files will have headers and links processed.
19+
*/
20+
val outputDir = FileSystems.getDefault.getPath("docs-for-dotty-page")
21+
22+
implicit def stringToFun(s: String): MyParams => String = _ => s
23+
24+
// Patterns, for convenience
25+
val titlePattern = "(?s)^---\n.*title: ([^\n]*).*---"
26+
val jekyllLinkPattern = """\{\% link _overviews/scala3-scaladoc(.*) %\}"""
27+
val jekyllLinkSubstitution = "..$1"
28+
val jekyllLinkPattern2 = """\{\% link |_overviews/scala3-scaladoc/(.*) %\}"""
29+
val jekyllLinkSubstitution2 = "$1"
30+
val localLinkPattern = """\((?!http|www)(.*).html\)"""
31+
val localLinkSubstitution = "($1.md)"
32+
33+
case class MyParams(newPath: String)
34+
35+
/**
36+
* Structure for holding which transformations should be applied to which directories.
37+
* The outer map is holding morphism `directory prefix` -> `set of transformations`.
38+
* The inner set is a collection of pairs `regex pattern` -> `substitution value`.
39+
*/
40+
val transformationMap: Map[String, Set[(String, MyParams => String)]] = Map(
41+
"docs/docs/reference/" -> Set(
42+
titlePattern -> ((p) => s"---\nlayout: doc-page\ntitle: $$1\nmovedTo: https://docs.scala-lang.org/scala3/reference/${p.newPath}.html\n---"),
43+
jekyllLinkPattern -> jekyllLinkSubstitution,
44+
jekyllLinkPattern2 -> jekyllLinkSubstitution2,
45+
localLinkPattern -> localLinkSubstitution,
46+
),
47+
48+
"docs/docs/usage/scaladoc/" -> Set(
49+
titlePattern -> s"---\ntitle: $$1\n---",
50+
jekyllLinkPattern -> jekyllLinkSubstitution,
51+
jekyllLinkPattern2 -> jekyllLinkSubstitution2,
52+
localLinkPattern -> localLinkSubstitution,
53+
),
54+
)
55+
56+
def copyDocs() = {
57+
def copyFile(path: Path): Unit = {
58+
val newPath = outputDir.resolve(inputDir.relativize(path))
59+
Files.createDirectories(newPath.getParent())
60+
61+
path.toString match {
62+
case s if s.startsWith("docs/docs/") =>
63+
val inputStream = Source.fromFile(path.toFile)(Codec.UTF8)
64+
val fileContent = inputStream.getLines().mkString("\n")
65+
66+
new PrintStream(newPath.toFile) {
67+
val patterns = transformationMap.find { case (k, v) => path.toString.startsWith(k) }.map(_._2).getOrElse(Set.empty)
68+
val params = MyParams(newPath = s.stripPrefix("docs/docs/reference/").stripSuffix(".md"))
69+
val transformed = patterns.foldLeft(fileContent) { case (res, (pattern, substitution)) => res.replaceAll(pattern, substitution(params)) }
70+
write(transformed.getBytes("UTF8"))
71+
}
72+
case s =>
73+
Files.copy(path, newPath, StandardCopyOption.REPLACE_EXISTING);
74+
}
75+
}
76+
Files.walk(inputDir).iterator().asScala.filter(Files.isRegularFile(_)).foreach(copyFile)
77+
}
78+
}

project/scripts/processDocs.sc

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)