/
GenerateReferenceDoc.scala
565 lines (492 loc) · 19.5 KB
/
GenerateReferenceDoc.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
package scala.cli.doc
import caseapp.*
import caseapp.core.Arg
import caseapp.core.Scala3Helpers.*
import caseapp.core.util.Formatter
import dotty.tools.dotc.ScalacCommand
import munit.internal.difflib.Diff
import shapeless.tag
import java.nio.charset.StandardCharsets
import java.util
import scala.build.options.{BuildOptions, BuildRequirements}
import scala.build.preprocessing.ScalaPreprocessor
import scala.build.preprocessing.directives.DirectiveHandler
import scala.cli.commands.{ScalaCommand, SpecificationLevel, tags}
import scala.cli.doc.ReferenceDocUtils.*
import scala.cli.util.ArgHelpers.*
import scala.cli.{ScalaCli, ScalaCliCommands}
object GenerateReferenceDoc extends CaseApp[InternalDocOptions] {
implicit class PBUtils(sb: StringBuilder) {
def section(t: String*): StringBuilder =
sb.append(t.mkString("", "\n", "\n\n"))
}
private def cleanUpOrigin(origin: String): String = {
val origin0 = origin.takeWhile(_ != '[').stripSuffix("Options")
val actualOrigin = if (origin0 == "WithFullHelp" || origin0 == "WithHelp") "Help" else origin0
if (actualOrigin == "Shared") actualOrigin
else actualOrigin.stripPrefix("Shared")
}
private def formatOrigin(origin: String, keepCapitalization: Boolean = true): String = {
val l = origin.head :: origin.toList.tail.flatMap { c =>
if (c.isUpper) " " :: c.toLower :: Nil
else c :: Nil
}
val value = l.mkString
.replace("Scala native", "Scala Native")
.replace("Scala js", "Scala.js")
.split("\\s+")
.map(w => if (w == "ide") "IDE" else w)
.mkString(" ")
val valueNeedsLowerCasing = keepCapitalization ||
(value.startsWith("Scala") && !value.startsWith("Scalac")) ||
!value.head.isUpper
if (valueNeedsLowerCasing) value
else value.head.toLower +: value.tail
}
private def prettyPath(path: os.Path): String =
if (path.startsWith(os.pwd)) path.relativeTo(os.pwd).toString
else path.toString
private def maybeWrite(dest: os.Path, content: String): Unit = {
val content0 = content.getBytes(StandardCharsets.UTF_8)
val needsUpdate = !os.exists(dest) || {
val currentContent = os.read.bytes(dest)
!util.Arrays.equals(content0, currentContent)
}
if (needsUpdate) {
os.write.over(dest, content0, createFolders = true)
System.err.println(s"Wrote ${prettyPath(dest)}")
}
else
System.err.println(s"${prettyPath(dest)} doesn't need updating")
}
private def maybeDiff(dest: os.Path, content: String): Option[Diff] = {
val currentContentOpt =
if (os.exists(dest)) Some(new String(os.read.bytes(dest), StandardCharsets.UTF_8))
else None
currentContentOpt.filter(_ != content).map { currentContent =>
new Diff(currentContent, content)
}
}
private def actualHelp(command: Command[_]): Help[_] =
command match {
case ext: scala.cli.commands.pgp.ExternalCommand =>
ext.actualHelp
case _ =>
command.finalHelp
}
private def scalacOptionForwarding =
"""## Scalac options forwarding
|
| All options that start with:
|
|
|- `-g`
|- `-language`
|- `-opt`
|- `-P`
|- `-target`
|- `-V`
|- `-W`
|- `-X`
|- `-Y`
|
|are assumed to be Scala compiler options and will be propagated to Scala Compiler. This applies to all commands that uses compiler directly or indirectly.
|
|
| ## Scalac options that are directly supported in scala CLI (so can be provided as is, without any prefixes etc.):
|
| - `-encoding`
| - `-release`
| - `-color`
| - `-nowarn`
| - `-feature`
| - `-deprecation`
|
|""".stripMargin
private def cliOptionsContent(
commands: Seq[Command[_]],
allArgs: Seq[Arg],
nameFormatter: Formatter[Name],
onlyRestricted: Boolean = false
): String = {
val argsToShow = if (!onlyRestricted) allArgs
else
allArgs.filterNot(_.isExperimentalOrRestricted)
val argsByOrigin = argsToShow.groupBy(arg => cleanUpOrigin(arg.origin.getOrElse("")))
val commandOrigins = for {
command <- commands
origin <- actualHelp(command).args.map(_.origin.getOrElse("")).map(cleanUpOrigin)
} yield origin -> command
val commandOriginsMap = commandOrigins.groupBy(_._1)
.map {
case (k, v) =>
(k, v.map(_._2).distinct.sortBy(_.name))
}
val mainOptionsContent = new StringBuilder
val hiddenOptionsContent = new StringBuilder
mainOptionsContent.append(
s"""---
|title: Command-line options
|sidebar_position: 1
|---
|
|${
if (onlyRestricted)
"**This document describes as scala-cli behaves if run as `scala` command. See more information in [SIP-46](https://github.com/scala/improvement-proposals/pull/46)**"
else ""
}
|
|This is a summary of options that are available for each subcommand of the `${ScalaCli.baseRunnerName}` command.
|
|""".stripMargin
)
mainOptionsContent.section(scalacOptionForwarding)
for ((origin, originArgs) <- argsByOrigin.toVector.sortBy(_._1)) {
val distinctArgs = originArgs.map(_.withOrigin(None)).distinct
val originCommands = commandOriginsMap.getOrElse(origin, Nil)
val onlyForHiddenCommands = originCommands.nonEmpty && originCommands.forall(_.hidden)
val allArgsHidden = distinctArgs.forall(_.noHelp)
val isInternal = onlyForHiddenCommands || allArgsHidden
val b = if (isInternal) hiddenOptionsContent else mainOptionsContent
if (originCommands.nonEmpty) {
val formattedOrigin = formatOrigin(origin)
val formattedCommands = originCommands.map { c =>
// https://scala-cli.virtuslab.org/docs/reference/commands#install-completions
val names = c.names.map(_.mkString(" "))
val text = names.map("`" + _ + "`").mkString(" , ")
s"[$text](./commands.md#${names.head.replace(" ", "-")})"
}
val availableIn = "Available in commands:\n\n" + formattedCommands.mkString(", ")
val header = if (isInternal) "###" else "##"
b.append(
s"""$header $formattedOrigin options
|
|$availableIn
|
|<!-- Automatically generated, DO NOT EDIT MANUALLY -->
|
|""".stripMargin
)
for (arg <- distinctArgs) {
import caseapp.core.util.NameOps._
arg.name.option(nameFormatter)
val names = (arg.name +: arg.extraNames).map(_.option(nameFormatter))
b.append(s"### `${names.head}`\n\n")
if (names.tail.nonEmpty)
b.append(
names
.tail
.sortBy(_.dropWhile(_ == '-'))
.map(n => s"`$n`")
.mkString("Aliases: ", ", ", "\n\n")
)
if (onlyRestricted)
b.section(s"`${arg.level.md}` per Scala Runner specification")
else if (isInternal || arg.noHelp) b.append("[Internal]\n")
for (desc <- arg.helpMessage.map(_.referenceDocMessage))
b.append(
s"""$desc
|
|""".stripMargin
)
}
}
}
mainOptionsContent.append("## Internal options \n")
mainOptionsContent.append(hiddenOptionsContent.toString)
mainOptionsContent.toString
}
private def optionsReference(
commands: Seq[Command[_]],
nameFormatter: Formatter[Name]
): String = {
val b = new StringBuilder
b.section(
"""---
|title: Scala Runner specification
|sidebar_position: 1
|---
""".stripMargin
)
b.section(
"**This document describes proposed specification for Scala runner based on Scala CLI documentation as requested per [SIP-46](https://github.com/scala/improvement-proposals/pull/46)**"
)
b.section(
"Commands and options are marked with MUST and SHOULD (in the RFC style) for ones applicable for Scala Runner.",
"Options and commands marked as **Implementation** are needed for smooth running of Scala CLI.",
"We recommend for those options and commands to be supported by the `scala` command (when based on Scala CLI) but not to be a part of the Scala Runner specification."
)
b.section(
"The proposed Scala runner specification should also contain supported `Using directives` defined in the dedicated [document](./directives.md)]"
)
b.section(scalacOptionForwarding)
def optionsForCommand(command: Command[_]): Unit = {
val supportedArgs = actualHelp(command).args
val argsByLevel = supportedArgs.groupBy(_.level)
import caseapp.core.util.NameOps._
(SpecificationLevel.inSpecification :+ SpecificationLevel.IMPLEMENTATION).foreach { level =>
val args = argsByLevel.getOrElse(level, Nil)
if (args.nonEmpty) {
if (level == SpecificationLevel.IMPLEMENTATION) b.section(
"<details><summary>",
s"\n### Implementantation specific options\n",
"</summary>"
)
else b.section(s"### ${level.md} options")
args.foreach { arg =>
val names = (arg.name +: arg.extraNames).map(_.option(nameFormatter))
b.section(s"**${names.head}**")
b.section(arg.helpMessage.fold("")(_.referenceDocMessage))
if (names.tail.nonEmpty) b.section(names.tail.mkString("Aliases: `", "` ,`", "`"))
}
if (level == SpecificationLevel.IMPLEMENTATION) b.section("</details>")
}
}
}
(SpecificationLevel.inSpecification :+ SpecificationLevel.IMPLEMENTATION).foreach { level =>
val levelCommands =
commands.collect { case s: ScalaCommand[_] if s.scalaSpecificationLevel == level => s }
if (levelCommands.nonEmpty) b.section(s"# ${level.md} commands")
levelCommands.foreach { command =>
b.section(
s"## `${command.name}` command",
s"**${level.md} for Scala Runner specification.**"
)
if (command.names.tail.nonEmpty)
b.section(command.names.map(_.mkString(" ")).tail.mkString("Aliases: `", "`, `", "`"))
for (desc <- command.messages.helpMessage.map(_.referenceDocDetailedMessage))
b.section(desc)
optionsForCommand(command)
b.section("---")
}
}
b.toString
}
private def commandsContent(commands: Seq[Command[_]], onlyRestricted: Boolean): String = {
val b = new StringBuilder
b.append(
s"""---
|title: Commands
|sidebar_position: 3
|---
|
|${
if (onlyRestricted)
"**This document describes as scala-cli behaves if run as `scala` command. See more information in [SIP-46](https://github.com/scala/improvement-proposals/pull/46)**"
else ""
}
|
|
|""".stripMargin
)
val (hiddenCommands, mainCommands) = commands.partition(_.hidden)
def addCommand(c: Command[_], additionalIndentation: Int = 0): Unit = {
val origins = c.parser0.args.flatMap(_.origin.toSeq).map(cleanUpOrigin).distinct.sorted
val headerPrefix = "#" * additionalIndentation
val names = c.names.map(_.mkString(" "))
b.append(s"$headerPrefix## ${names.head}\n\n")
if (names.tail.nonEmpty) b.append(names.tail.sorted.mkString("Aliases: `", "`, `", "`\n\n"))
for (desc <- c.messages.helpMessage.map(_.referenceDocDetailedMessage)) b.section(desc)
if (origins.nonEmpty) {
val links = origins.map { origin =>
val cleanedUp = formatOrigin(origin, keepCapitalization = false)
val linkPart = cleanedUp
.split("\\s+")
.map(_.toLowerCase(util.Locale.ROOT).filter(_ != '.'))
.mkString("-")
s"[$cleanedUp](./cli-options.md#$linkPart-options)"
}
b.append(
s"""Accepts option groups: ${links.mkString(", ")}
|""".stripMargin
)
b.append("\n")
}
}
if (onlyRestricted) {
val scalaCommands = commands.collect { case s: ScalaCommand[_] => s }
b.section("# `scala` commands")
// TODO add links to RFC
b.section(
"This document is a specification of the `scala` runner.",
"For now it uses documentation specific to Scala CLI but at some point it may be refactored to provide more abstract documentation.",
"Documentation is split into sections in the spirit of RFC keywords (`MUST`, `SHOULD`, `NICE TO HAVE`) including the `IMPLEMENTATION` category,",
"that is reserved for commands that need to be present for Scala CLI to work properly but should not be a part of the official API."
)
SpecificationLevel.inSpecification.foreach { level =>
val commands = scalaCommands.filter(_.scalaSpecificationLevel == level)
if (commands.nonEmpty) {
b.section(s"## ${level.md.capitalize} commands:")
commands.foreach(addCommand(_, additionalIndentation = 1))
}
}
b.section("## Implementation-specific commands")
b.section(
"Commands which are used within Scala CLI and should be a part of the `scala` command but aren't a part of the specification."
)
scalaCommands
.filter(_.scalaSpecificationLevel == SpecificationLevel.IMPLEMENTATION)
.foreach(c => addCommand(c, additionalIndentation = 1))
}
else {
mainCommands.foreach(addCommand(_))
b.section("## Hidden commands")
hiddenCommands.foreach(c => addCommand(c, additionalIndentation = 1))
}
b.toString
}
private def usingContent(
usingHandlers: Seq[DirectiveHandler[BuildOptions]],
requireHandlers: Seq[DirectiveHandler[BuildRequirements]],
onlyRestricted: Boolean
): String = {
val b = new StringBuilder
b.section(
"""---
|title: Directives
|sidebar_position: 2
|---""".stripMargin
)
b.section(
if (onlyRestricted)
"**This document describes as scala-cli behaves if run as `scala` command. See more information in [SIP-46](https://github.com/scala/improvement-proposals/pull/46)**"
else "## using directives"
)
def addHandlers(handlers: Seq[DirectiveHandler[_]]): Unit =
for (handler <- handlers.sortBy(_.name)) {
b.append(
s"""### ${handler.name}
|
|${handler.descriptionMd}
|
|${handler.usageMd}
|
|""".stripMargin
)
val examples = handler.examples
if (examples.nonEmpty) {
b.append(
"""#### Examples
|""".stripMargin
)
for (ex <- examples)
b.append(
s"""`$ex`
|
|""".stripMargin
)
}
}
if (onlyRestricted) {
// TODO add links to RFC
b.section(
"This document is a specification of the `scala` runner.",
"For now it uses documentation specific to Scala CLI but at some point it may be refactored to provide more abstract documentation.",
"Documentation is split into sections in the spirit of RFC keywords (`MUST`, `SHOULD`)."
)
SpecificationLevel.inSpecification.foreach { level =>
val handlers = usingHandlers.filter(_.scalaSpecificationLevel == level)
if (handlers.nonEmpty) {
b.section(s"## ${level.md.capitalize} directives:")
addHandlers(handlers)
}
}
val implHandlers =
usingHandlers.filter(_.scalaSpecificationLevel == SpecificationLevel.IMPLEMENTATION)
if (implHandlers.nonEmpty) {
b.section("## Implementation-specific directices")
b.section(
"Directives which are used within Scala CLI and should be a part of the the `scala` command but aren't a part of the specification."
)
addHandlers(implHandlers)
}
}
else {
addHandlers(usingHandlers)
b.append(
"""
|## target directives
|
|""".stripMargin
)
addHandlers(requireHandlers)
}
b.toString
}
def run(options: InternalDocOptions, args: RemainingArgs): Unit = {
val scalaCli = new ScalaCliCommands(
"scala-cli",
ScalaCli.baseRunnerName,
ScalaCli.fullRunnerName,
isSipScala = false
)
val commands = scalaCli.commands
val restrictedCommands =
commands.iterator.collect { case s: ScalaCommand[_] if !s.isRestricted => s }.toSeq
val allArgs = commands.flatMap(actualHelp(_).args)
val nameFormatter = scalaCli.actualDefaultCommand.nameFormatter
val allCliOptionsContent = cliOptionsContent(commands, allArgs, nameFormatter)
val restrictedCliOptionsContent =
cliOptionsContent(restrictedCommands, allArgs, nameFormatter, onlyRestricted = true)
val allCommandsContent = commandsContent(commands, onlyRestricted = false)
val restrictedCommandsContent = commandsContent(restrictedCommands, onlyRestricted = true)
val scalaOptionsReference = optionsReference(restrictedCommands, nameFormatter)
val allDirectivesContent = usingContent(
ScalaPreprocessor.usingDirectiveHandlers,
ScalaPreprocessor.requireDirectiveHandlers,
onlyRestricted = false
)
val restrictedDirectivesContent = usingContent(
ScalaPreprocessor.usingDirectiveHandlers.filterNot(_.isRestricted),
ScalaPreprocessor.requireDirectiveHandlers.filterNot(_.isRestricted),
onlyRestricted = true
)
val restrictedDocsDir = os.rel / "scala-command"
if (options.check) {
val content = Seq(
(os.rel / "cli-options.md") -> allCliOptionsContent,
(os.rel / "commands.md") -> allCommandsContent,
(os.rel / "directives.md") -> allDirectivesContent,
(os.rel / restrictedDocsDir / "cli-options.md") -> restrictedCliOptionsContent,
(os.rel / restrictedDocsDir / "commands.md") -> restrictedCommandsContent,
(os.rel / restrictedDocsDir / "directives.md") -> restrictedDirectivesContent,
(os.rel / restrictedDocsDir / "runner-specification") -> scalaOptionsReference
)
var anyDiff = false
for ((dest, content0) <- content) {
val dest0 = options.outputPath / dest
val diffOpt = maybeDiff(options.outputPath / dest, content0)
diffOpt match {
case Some(diff) =>
anyDiff = true
System.err.println(Console.RED + prettyPath(dest0) + Console.RESET + " differs:")
System.err.println(diff.unifiedDiff)
case None =>
System.err.println(
Console.GREEN + prettyPath(dest0) + Console.RESET + " is up-to-date."
)
}
}
if (anyDiff)
sys.exit(1)
}
else {
maybeWrite(options.outputPath / "cli-options.md", allCliOptionsContent)
maybeWrite(options.outputPath / "commands.md", allCommandsContent)
maybeWrite(options.outputPath / "directives.md", allDirectivesContent)
maybeWrite(
options.outputPath / restrictedDocsDir / "cli-options.md",
restrictedCliOptionsContent
)
maybeWrite(options.outputPath / restrictedDocsDir / "commands.md", restrictedCommandsContent)
maybeWrite(
options.outputPath / restrictedDocsDir / "directives.md",
restrictedDirectivesContent
)
maybeWrite(
options.outputPath / restrictedDocsDir / "runner-specification.md",
scalaOptionsReference
)
}
}
}