Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target a single file for --in and --out #322

Merged
merged 6 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ inThisBuild(
scalaVersion := scala212,
crossScalaVersions := List(scala212, scala211, scala213),
scalacOptions ++= List(
"-Yrangepos",
"-Xexperimental",
"-deprecation"
),
Expand Down Expand Up @@ -36,7 +37,8 @@ inThisBuild(
// faster publishLocal:
publishArtifact.in(packageDoc) := "true" == System.getenv("CI"),
publishArtifact.in(packageSrc) := "true" == System.getenv("CI"),
turbo := true
turbo := true,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL'd about turbo 🤔

useSuperShell := false // overlaps with MUnit test failure reports.
)
)

Expand Down
22 changes: 19 additions & 3 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ The sbt-mdoc plugin supports the following settings.

```


## Command-line

Use [coursier](https://github.com/coursier/coursier/#command-line) to launch
Expand Down Expand Up @@ -151,6 +150,14 @@ contained, by default the `docs/` directory is used.
+ --in mydocs
```

The `--in` flag doesn't have to be a directory, it also supports individual
files.

```diff
coursier launch org.scalameta:mdoc_@SCALA_BINARY_VERSION@:@VERSION@ -- \
+ --in mydocs/readme.md
```

Use `--site.VARIABLE=value` to add site variables that can be referenced from
markdown as `@@VARIABLE@`.

Expand All @@ -159,14 +166,23 @@ markdown as `@@VARIABLE@`.
+ --site.SCALA_VERSION @SCALA_VERSION@
```

Use `--out` to customize the directory where markdown sources are generated, by
default the `out/` directory is used.
Use `--out` to customize where your markdown sources are generated, by default
the `out/` directory is used.

```diff
coursier launch org.scalameta:mdoc_@SCALA_BINARY_VERSION@:@VERSION@ -- \
+ --out target/docs
```

The `--out` flag doesn't have to be a directory, it can also be an individual
file. However, this assumes that your `--in` was also an individual file.

```diff
coursier launch org.scalameta:mdoc_@SCALA_BINARY_VERSION@:@VERSION@ -- \
+ --in mydocs/readme.template.md \
+ --out readme.md
```

Use `--watch` to start the file watcher with livereload. It's recommended to use
`--watch` while writing documentation to enjoy 3-4x faster compilation
performance.
Expand Down
7 changes: 4 additions & 3 deletions mdoc-docs/src/main/scala/mdoc/docs/MdocModifier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import mdoc.internal.markdown.GitHubIdGenerator
import mdoc.internal.markdown.LinkHygiene
import mdoc.internal.pos.PositionSyntax._
import mdoc.internal.markdown.MarkdownFile
import mdoc.internal.cli.InputFile

class MdocModifier(context: Context) extends StringModifier {
private val myStdout = new ByteArrayOutputStream()
Expand All @@ -24,16 +25,16 @@ class MdocModifier(context: Context) extends StringModifier {
myStdout.reset()
myReporter.reset()
val cleanInput = Input.VirtualFile(code.filename, code.text)
val relpath = RelativePath(code.filename)
val file = InputFile.fromSettings(code.filename, context.settings)
val markdown = Markdown.toMarkdown(
cleanInput,
myContext,
relpath,
file,
myContext.settings.site,
myReporter,
myContext.settings
)
val links = DocumentLinks.fromMarkdown(GitHubIdGenerator, relpath, cleanInput)
val links = DocumentLinks.fromMarkdown(GitHubIdGenerator, file.relpath, cleanInput)
LinkHygiene.lint(List(links), myReporter, verbose = false)
val stdout = fansi.Str(myStdout.toString()).plainText
if (myReporter.hasErrors || myReporter.hasWarnings) {
Expand Down
13 changes: 4 additions & 9 deletions mdoc-js/src/main/scala-2.12/mdoc/modifiers/JsConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ case class JsConfig(
libraries: List[AbsolutePath] = Nil,
mountNode: String = "node",
minLevel: Level = Level.Info,
outDirectory: AbsolutePath = PathIO.workingDirectory,
outDirectories: List[AbsolutePath] = List(PathIO.workingDirectory),
outPrefix: Option[String] = None,
fullOpt: Boolean = true,
htmlPrefix: String = "",
relativeLinkPrefix: String = ""
Expand Down Expand Up @@ -65,14 +66,8 @@ object JsConfig {
ctx.site.getOrElse("js-html-header", ""),
Classpath(ctx.site.getOrElse("js-libraries", "")).entries,
mountNode = ctx.site.getOrElse("js-mount-node", base.mountNode),
outDirectory = ctx.site.get("js-out-prefix") match {
case Some(value) =>
// This is needed for Docusaurus that requires assets (non markdown) files to live under
// `docs/assets/`: https://docusaurus.io/docs/en/doc-markdown#linking-to-images-and-other-assets
ctx.settings.out.resolve(value)
case None =>
ctx.settings.out
},
outDirectories = ctx.settings.out,
outPrefix = ctx.site.get("js-out-prefix"),
minLevel = ctx.site.get("js-level") match {
case None => Level.Info
case Some("info") => Level.Info
Expand Down
47 changes: 35 additions & 12 deletions mdoc-js/src/main/scala-2.12/mdoc/modifiers/JsModifier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import scala.meta.Term
import scala.meta.inputs.Input
import scala.meta.io.Classpath
import scala.reflect.io.VirtualDirectory
import mdoc.internal.cli.InputFile
import scala.meta.io.AbsolutePath

class JsModifier extends mdoc.PreModifier {
override val name = "js"
Expand Down Expand Up @@ -153,19 +155,40 @@ class JsModifier extends mdoc.PreModifier {
} else {
val output = WritableMemVirtualJSFile("output.js")
linker.link(virtualIrFiles ++ sjsir, Nil, output, sjsLogger)
val outjsfile = config.outDirectory.resolve(ctx.relativePath.resolveSibling(_ + ".js"))
outjsfile.write(output.content)
val outmdoc = outjsfile.resolveSibling(_ => "mdoc.js")
outmdoc.write(Resources.readPath("/mdoc.js"))
val relfile = outjsfile.toRelativeLinkFrom(ctx.outputFile, config.relativeLinkPrefix)
val relmdoc = outmdoc.toRelativeLinkFrom(ctx.outputFile, config.relativeLinkPrefix)
new CodeBuilder()
.println(config.htmlHeader)
.lines(config.libraryScripts(outjsfile, ctx))
.println(s"""<script type="text/javascript" src="$relfile" defer></script>""")
.println(s"""<script type="text/javascript" src="$relmdoc" defer></script>""")
.toString
ctx.settings.toInputFile(ctx.inputFile) match {
case None =>
ctx.reporter.error(
s"unable to find output file matching the input file '${ctx.inputFile}'. " +
s"To fix this problem, make sure that --in points to a directory that contains the file ${ctx.inputFile}."
)
""
case Some(inputFile) =>
val outjsfile = resolveOutputJsFile(inputFile)
outjsfile.write(output.content)
val outmdoc = outjsfile.resolveSibling(_ => "mdoc.js")
outmdoc.write(Resources.readPath("/mdoc.js"))
val relfile = outjsfile.toRelativeLinkFrom(ctx.outputFile, config.relativeLinkPrefix)
val relmdoc = outmdoc.toRelativeLinkFrom(ctx.outputFile, config.relativeLinkPrefix)
new CodeBuilder()
.println(config.htmlHeader)
.lines(config.libraryScripts(outjsfile, ctx))
.println(s"""<script type="text/javascript" src="$relfile" defer></script>""")
.println(s"""<script type="text/javascript" src="$relmdoc" defer></script>""")
.toString
}
}
}

private def resolveOutputJsFile(file: InputFile): AbsolutePath = {
val outputDirectory = config.outPrefix match {
case None =>
file.outputDirectory
case Some(prefix) =>
// This is needed for Docusaurus that requires assets (non markdown) files to live under
// `docs/assets/`: https://docusaurus.io/docs/en/doc-markdown#linking-to-images-and-other-assets
file.outputDirectory.resolve(prefix)
}
outputDirectory.resolve(file.relpath).resolveSibling(_ + ".js")
}

override def process(ctx: PreModifierContext): String = {
Expand Down
7 changes: 4 additions & 3 deletions mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ object MdocPlugin extends AutoPlugin {
)
val mdocIn =
settingKey[File](
"Input directory containing markdown sources to be processed by mdoc. " +
"Input directory or source file containing markdown to be processed by mdoc. " +
"Defaults to the toplevel docs/ directory."
)
val mdocOut =
settingKey[File](
"Output directory for mdoc generated markdown. " +
"Defaults to the target/mdoc directory of this project."
"Output directory or output file name for mdoc generated markdown. " +
"Defaults to the target/mdoc directory of this project. " +
"If this is a file name, it assumes your `in` was also an indidivual file"
olafurpg marked this conversation as resolved.
Show resolved Hide resolved
)
val mdocExtraArguments =
settingKey[Seq[String]](
Expand Down
10 changes: 8 additions & 2 deletions mdoc/src/main/scala/mdoc/MainSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,16 @@ final class MainSettings private (
copy(settings.withWorkingDirectory(AbsolutePath(cwd)))
}
def withOut(out: Path): MainSettings = {
copy(settings.copy(out = AbsolutePath(out)))
withOutputPaths(List(out))
}
def withOutputPaths(out: List[Path]): MainSettings = {
copy(settings.copy(out = out.map(AbsolutePath(_)(settings.cwd))))
}
def withIn(in: Path): MainSettings = {
copy(settings.copy(in = AbsolutePath(in)))
withInputPaths(List(in))
}
def withInputPaths(in: List[Path]): MainSettings = {
copy(settings.copy(in = in.map(AbsolutePath(_)(settings.cwd))))
}
def withClasspath(classpath: String): MainSettings = {
copy(settings.copy(classpath = classpath))
Expand Down
10 changes: 10 additions & 0 deletions mdoc/src/main/scala/mdoc/OnLoadContext.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package mdoc

import mdoc.internal.cli.Settings

final class OnLoadContext private[mdoc] (
val reporter: Reporter,
private[mdoc] val settings: Settings
) {
def site: Map[String, String] = settings.site
}
12 changes: 7 additions & 5 deletions mdoc/src/main/scala/mdoc/PostModifier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import scala.meta.inputs.Input
import scala.meta.io.AbsolutePath
import scala.collection.JavaConverters._
import scala.meta.io.RelativePath
import mdoc.internal.cli.InputFile

trait PostModifier {
val name: String
Expand All @@ -33,12 +34,13 @@ final class PostModifierContext private[mdoc] (
val outputCode: String,
val variables: List[Variable],
val reporter: Reporter,
val relativePath: RelativePath,
private[mdoc] val file: InputFile,
private[mdoc] val settings: Settings
) {
def inputFile: AbsolutePath = inDirectory.resolve(relativePath)
def outputFile: AbsolutePath = outDirectory.resolve(relativePath)
def lastValue: Any = variables.lastOption.map(_.runtimeValue).orNull
def inDirectory: AbsolutePath = settings.in
def outDirectory: AbsolutePath = settings.out
def relativePath: RelativePath = file.relpath
def inputFile: AbsolutePath = file.inputFile
def outputFile: AbsolutePath = file.outputFile
def inDirectory: AbsolutePath = file.inputDirectory
def outDirectory: AbsolutePath = file.outputDirectory
}
18 changes: 18 additions & 0 deletions mdoc/src/main/scala/mdoc/PostProcessContext.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package mdoc

import scala.meta.io.AbsolutePath
import scala.meta.io.RelativePath
import mdoc.internal.cli.Settings
import mdoc.internal.cli.InputFile

final class PostProcessContext private[mdoc] (
val reporter: Reporter,
private[mdoc] val file: InputFile,
private[mdoc] val settings: Settings
) {
def relativePath: RelativePath = file.relpath
def inputFile: AbsolutePath = file.inputFile
def outputFile: AbsolutePath = file.outputFile
def inDirectory: AbsolutePath = file.inputDirectory
def outDirectory: AbsolutePath = file.outputDirectory
}
37 changes: 0 additions & 37 deletions mdoc/src/main/scala/mdoc/PreModifier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,3 @@ object PreModifier {
implicit val encoder: ConfEncoder[PreModifier] =
ConfEncoder.StringEncoder.contramap(mod => s"<${mod.name}>")
}

final class OnLoadContext private[mdoc] (
val reporter: Reporter,
private[mdoc] val settings: Settings
) {
def site: Map[String, String] = settings.site
}

final class PostProcessContext private[mdoc] (
val reporter: Reporter,
val relativePath: RelativePath,
private[mdoc] val settings: Settings
) {
def inputFile: AbsolutePath = inDirectory.resolve(relativePath)
def outputFile: AbsolutePath = outDirectory.resolve(relativePath)
def inDirectory: AbsolutePath = settings.in
def outDirectory: AbsolutePath = settings.out
}

final class PreModifierContext private[mdoc] (
val info: String,
val originalCode: Input,
val reporter: Reporter,
val relativePath: RelativePath,
private[mdoc] val settings: Settings
) {
def infoInput: Input = {
val cpos = originalCode.toPosition.toUnslicedPosition
val start = cpos.start - info.length - 1
val end = cpos.start - 1
Input.Slice(cpos.input, start, end)
}
def inputFile: AbsolutePath = inDirectory.resolve(relativePath)
def outputFile: AbsolutePath = outDirectory.resolve(relativePath)
def inDirectory: AbsolutePath = settings.in
def outDirectory: AbsolutePath = settings.out
}
28 changes: 28 additions & 0 deletions mdoc/src/main/scala/mdoc/PreModifierContext.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package mdoc

import scala.meta.inputs.Input
import scala.meta.io.RelativePath
import mdoc.internal.cli.Settings
import mdoc.internal.pos.PositionSyntax._
import scala.meta.io.AbsolutePath
import mdoc.internal.cli.InputFile

final class PreModifierContext private[mdoc] (
val info: String,
val originalCode: Input,
val reporter: Reporter,
private[mdoc] val file: InputFile,
private[mdoc] val settings: Settings
) {
def infoInput: Input = {
val cpos = originalCode.toPosition.toUnslicedPosition
val start = cpos.start - info.length - 1
val end = cpos.start - 1
Input.Slice(cpos.input, start, end)
}
def relativePath: RelativePath = file.relpath
def inputFile: AbsolutePath = file.inputFile
def outputFile: AbsolutePath = file.outputFile
def inDirectory: AbsolutePath = file.inputDirectory
def outDirectory: AbsolutePath = file.outputDirectory
}
21 changes: 21 additions & 0 deletions mdoc/src/main/scala/mdoc/internal/cli/Feedback.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mdoc.internal.cli

import java.nio.file.Path
import scala.meta.io.AbsolutePath

object Feedback {
def outSubdirectoryOfIn(in: Path, out: Path): String = {
Expand All @@ -9,4 +10,24 @@ object Feedback {
s" --in=$in\n" +
s" --out=$out"
}
def mustBeNonEmpty(what: String): String = {
s"--$what must be non-empty. To fix this problem, add an argument for the `--$what <value>` argument."
olafurpg marked this conversation as resolved.
Show resolved Hide resolved
}
def inputDifferentLengthOutput(input: List[AbsolutePath], output: List[AbsolutePath]): String = {
val diff = math.abs(input.length - output.length)
val toFix = if (input.length > output.length) "out" else "in"
s"--in and --out must have the same length but found ${input.length} --in argument(s) and ${output.length} --out argument(s). " +
s"To fix this problem, add $diff more $toFix arguments."
}
def outputCannotBeRegularFile(input: AbsolutePath, output: AbsolutePath): String = {
s"--out argument '$output' cannot be a regular file when --in argument '$input' is a directory."
}
def outputCannotBeDirectory(input: AbsolutePath, output: AbsolutePath): String = {
s"--out argument '$output' cannot be a directory when --in argument '$input' is a regular file. " +
"To fix this problem, change the --out argument to point to a regular file or an empty path."
}
def inputEqualOutput(input: AbsolutePath): String = {
s"--in and --out cannot be the same path '$input'. " +
"To fix this problem, change the --out argument to another path."
}
}
Loading