Skip to content

Commit 988bc96

Browse files
committed
add ability to specify an out file
1 parent 5a5111c commit 988bc96

File tree

5 files changed

+100
-15
lines changed

5 files changed

+100
-15
lines changed

mdoc/src/main/scala/mdoc/internal/cli/MainOps.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ final class MainOps(
121121
}
122122

123123
def generateCompleteSite(): Exit = {
124-
val isFile = settings.in.toFile.isFile
124+
val isFile = Files.isRegularFile(settings.in.toNIO)
125125
val files = if (isFile) {
126126
settings.toInputFile(settings.in).toList
127127
} else {

mdoc/src/main/scala/mdoc/internal/cli/Settings.scala

+18-10
Original file line numberDiff line numberDiff line change
@@ -161,21 +161,27 @@ case class Settings(
161161
def isFileWatching: Boolean = watch && !check
162162

163163
def toInputFile(infile: AbsolutePath): Option[InputFile] = {
164-
val relpath = if (infile == in) {
164+
val relativeIn = if (infile == in) {
165165
RelativePath(in.toNIO.getFileName.toString)
166166
} else {
167167
infile.toRelative(in)
168168
}
169-
if (isIncluded(relpath)) {
170-
val outfile = out.resolve(relpath)
171-
Some(InputFile(relpath, infile, outfile))
169+
if (isIncluded(relativeIn)) {
170+
val outfile = if (assumedRegularFile(out)) {
171+
out
172+
} else {
173+
out.resolve(relativeIn)
174+
}
175+
Some(InputFile(relativeIn, infile, outfile))
172176
} else {
173177
None
174178
}
175179
}
180+
176181
def isExplicitlyExcluded(path: RelativePath): Boolean = {
177182
exclude.exists(_.matches(path.toNIO))
178183
}
184+
179185
def isIncluded(path: RelativePath): Boolean = {
180186
(include.isEmpty || include.exists(_.matches(path.toNIO))) &&
181187
!isExplicitlyExcluded(path)
@@ -185,10 +191,13 @@ case class Settings(
185191
val ctx = new OnLoadContext(reporter, this)
186192
preModifiers.foreach(_.onLoad(ctx))
187193
}
194+
188195
def validate(logger: Reporter): Configured[Context] = {
189196
if (Files.exists(in.toNIO)) {
190-
if (out.toNIO.startsWith(in.toNIO)) {
197+
if (out.toNIO.startsWith(in.toNIO) && !assumedRegularFile(out)) {
191198
Configured.error(Feedback.outSubdirectoryOfIn(in.toNIO, out.toNIO))
199+
} else if (assumedRegularFile(out) && Files.isDirectory(in.toNIO)) {
200+
Configured.error("your 'in' must be a file if 'out' is a file")
192201
} else {
193202
val compiler = MarkdownCompiler.fromClasspath(classpath, scalacOptions)
194203
onLoad(logger)
@@ -202,13 +211,12 @@ case class Settings(
202211
ConfError.fileDoesNotExist(in.toNIO).notOk
203212
}
204213
}
205-
def resolveIn(relpath: RelativePath): AbsolutePath = {
206-
in.resolve(relpath)
207-
}
208214

209-
def resolveOut(relpath: RelativePath): AbsolutePath = {
210-
out.resolve(relpath)
215+
private def assumedRegularFile(absPath: AbsolutePath): Boolean = {
216+
val extension = PathIO.extension(absPath.toNIO)
217+
markdownExtensions.toSet.contains(extension)
211218
}
219+
212220
def withWorkingDirectory(dir: AbsolutePath): Settings = {
213221
copy(
214222
in = dir.resolve("docs"),

tests/unit/src/test/scala/tests/cli/BaseCliSuite.scala

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ abstract class BaseCliSuite extends FunSuite {
1919
expected: String,
2020
extraArgs: Array[String] = Array.empty,
2121
setup: CliFixture => Unit = _ => (),
22-
configureInFlag: AbsolutePath => String = _.toString(),
22+
configureInFlag: AbsolutePath => String = _.toString,
23+
configureOutFlag: Path => String = _.toString,
2324
expectedExitCode: Int = 0,
2425
onStdout: String => Unit = _ => ()
2526
): Unit = {
@@ -32,7 +33,7 @@ abstract class BaseCliSuite extends FunSuite {
3233
"--in",
3334
configureInFlag(in),
3435
"--out",
35-
out.toString,
36+
configureOutFlag(out),
3637
"--cwd",
3738
in.toString,
3839
"--site.version",

tests/unit/src/test/scala/tests/cli/CliArgsSuite.scala

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ class CliArgsSuite extends FunSuite {
3636
s"File $tmp does not exist."
3737
)
3838

39+
private val anotherTemp = Files.createTempDirectory("mdoc")
40+
checkError(
41+
"--in" :: anotherTemp.toString :: "--out" :: "fake.md" :: Nil,
42+
"your 'in' must be a file if 'out' is a file"
43+
)
44+
3945
checkOk(
4046
"--site.VERSION" :: "1.0" :: Nil,
4147
_.site == Map("VERSION" -> "1.0")

tests/unit/src/test/scala/tests/cli/CliSuite.scala

+72-2
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class CliSuite extends BaseCliSuite {
190190
)
191191

192192
checkCli(
193-
"single-file",
193+
"single-in",
194194
"""
195195
|/index.md
196196
|# Single file
@@ -206,7 +206,77 @@ class CliSuite extends BaseCliSuite {
206206
|// one file
207207
|```
208208
|""".stripMargin,
209-
configureInFlag = { in => in + "/index.md" }
209+
configureInFlag = { in => in.resolve("index.md").toString }
210+
)
211+
212+
checkCli(
213+
"single-in-only",
214+
"""
215+
|/index.md
216+
|# Single file
217+
|```scala mdoc
218+
|println("one file")
219+
|```
220+
|/second.md
221+
|```scala mdoc
222+
|println("second file")
223+
|```
224+
|""".stripMargin,
225+
"""
226+
|/index.md
227+
|# Single file
228+
|```scala
229+
|println("one file")
230+
|// one file
231+
|```
232+
|""".stripMargin,
233+
configureInFlag = { in => in.resolve("index.md").toString }
234+
)
235+
236+
checkCli(
237+
"single-in-single-out",
238+
"""
239+
|/index.md
240+
|# Single file
241+
|```scala mdoc
242+
|println("one file")
243+
|```
244+
|""".stripMargin,
245+
"""
246+
|/out.md
247+
|# Single file
248+
|```scala
249+
|println("one file")
250+
|// one file
251+
|```
252+
|""".stripMargin,
253+
configureInFlag = { in => in.resolve("index.md").toString },
254+
configureOutFlag = { out => out.resolve("out.md").toString }
255+
)
256+
257+
checkCli(
258+
"single-in-single-out-only",
259+
"""
260+
|/index.md
261+
|# Single file
262+
|```scala mdoc
263+
|println("one file")
264+
|```
265+
|/second.md
266+
|```scala mdoc
267+
|println("second file")
268+
|```
269+
|""".stripMargin,
270+
"""
271+
|/out.md
272+
|# Single file
273+
|```scala
274+
|println("one file")
275+
|// one file
276+
|```
277+
|""".stripMargin,
278+
configureInFlag = { in => in.resolve("index.md").toString },
279+
configureOutFlag = { out => out.resolve("out.md").toString }
210280
)
211281

212282
}

0 commit comments

Comments
 (0)