Skip to content

Commit

Permalink
Merge pull request #76 from olafurpg/filter
Browse files Browse the repository at this point in the history
Make --include support single file even for nested directories.
  • Loading branch information
olafurpg authored Sep 17, 2018
2 parents 137b75c + aee0cc6 commit c419558
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 22 deletions.
4 changes: 2 additions & 2 deletions mdoc/src/main/scala/mdoc/MainSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ final class MainSettings private (
}
}
def withExcludePath(excludePath: List[PathMatcher]): MainSettings = {
copy(settings.copy(excludePath = excludePath))
copy(settings.copy(exclude = excludePath))
}
def withIncludePath(includePath: List[PathMatcher]): MainSettings = {
copy(settings.copy(includePath = includePath))
copy(settings.copy(include = includePath))
}
def withSiteVariables(variables: Map[String, String]): MainSettings = {
copy(settings.copy(site = variables))
Expand Down
2 changes: 1 addition & 1 deletion mdoc/src/main/scala/mdoc/internal/cli/MainOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ final class MainOps(

def handleFile(file: InputFile): Exit = {
try {
if (!settings.matches(file.relpath)) Exit.success
if (!settings.isIncluded(file.relpath)) Exit.success
else {
PathIO.extension(file.in.toNIO) match {
case "md" => handleMarkdown(file)
Expand Down
31 changes: 22 additions & 9 deletions mdoc/src/main/scala/mdoc/internal/cli/Settings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,19 @@ case class Settings(
usage: Boolean = false,
@Description("Print out the version number and exit")
version: Boolean = false,
@Description("Glob to filter which files from --in directory to include.")
includePath: List[PathMatcher] = Nil,
@Description("Glob to filter which files from --in directory to exclude.")
excludePath: List[PathMatcher] = Nil,
@Description(
"Glob to filter which files to process. Defaults to all files. " +
"Example: --include **/example.md will process only files with the name example.md."
)
@ExtraName("includePath")
include: List[PathMatcher] = Nil,
@Description(
"Glob to filter which files from exclude from processing. Defaults to no files. " +
"Example: --include users/**.md --exclude **/example.md will process all files in the users/ directory " +
"excluding files named example.md."
)
@ExtraName("excludePath")
exclude: List[PathMatcher] = Nil,
@Description(
"Use relative filenames when reporting error messages. " +
"Useful for producing consistent docs on a local machine and CI. "
Expand All @@ -99,16 +108,19 @@ case class Settings(

def toInputFile(infile: AbsolutePath): Option[InputFile] = {
val relpath = infile.toRelative(in)
if (matches(relpath)) {
if (isIncluded(relpath)) {
val outfile = out.resolve(relpath)
Some(InputFile(relpath, infile, outfile))
} else {
None
}
}
def matches(path: RelativePath): Boolean = {
(includePath.isEmpty || includePath.exists(_.matches(path.toNIO))) &&
!excludePath.exists(_.matches(path.toNIO))
def isExplicitlyExcluded(path: RelativePath): Boolean = {
exclude.exists(_.matches(path.toNIO))
}
def isIncluded(path: RelativePath): Boolean = {
(include.isEmpty || include.exists(_.matches(path.toNIO))) &&
!isExplicitlyExcluded(path)
}
def validate(logger: Reporter): Configured[Context] = {
if (Files.exists(in.toNIO)) {
Expand Down Expand Up @@ -145,7 +157,8 @@ object Settings extends MetaconfigScalametaImplicits {
|Example: mdoc --in <path> --out <path> (customize input/output directories)
| mdoc --watch (watch for file changes)
| mdoc --site.VERSION 1.0.0 (pass in site variables)
| mdoc --exclude-path <glob> (exclude files matching patterns)
| mdoc --include **/example.md (process only files named example.md)
| mdoc --exclude node_modules (don't process node_modules directory)
|""".stripMargin
def description: Doc =
Doc.paragraph(
Expand Down
4 changes: 2 additions & 2 deletions mdoc/src/main/scala/mdoc/internal/io/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ object IO {

override def preVisitDirectory(dir: Path, attrs: BasicFileAttributes): FileVisitResult = {
val relpath = RelativePath(root.relativize(dir))
if (dir == root || settings.matches(relpath)) FileVisitResult.CONTINUE
else FileVisitResult.SKIP_SUBTREE // excluded
if (settings.isExplicitlyExcluded(relpath)) FileVisitResult.SKIP_SUBTREE
else FileVisitResult.CONTINUE
}
}
Files.walkFileTree(root, visitor)
Expand Down
14 changes: 9 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,8 @@ Usage: mdoc [<option> ...]
Example: mdoc --in <path> --out <path> (customize input/output directories)
mdoc --watch (watch for file changes)
mdoc --site.VERSION 1.0.0 (pass in site variables)
mdoc --exclude-path <glob> (exclude files matching patterns)
mdoc --include **/example.md (process only files named example.md)
mdoc --exclude node_modules (don't process node_modules directory)
Mdoc is a documentation tool that interprets Scala code examples within markdown
code fences allowing you to compile and test documentation as part your build.
Expand Down Expand Up @@ -760,11 +761,14 @@ Less common options:
--version
Print out the version number and exit
--include-path [<glob> ...] (default: [])
Glob to filter which files from --in directory to include.
--include [<glob> ...] (default: [])
Glob to filter which files to process. Defaults to all files. Example: --include
**/example.md will process only files with the name example.md.
--exclude-path [<glob> ...] (default: [])
Glob to filter which files from --in directory to exclude.
--exclude [<glob> ...] (default: [])
Glob to filter which files from exclude from processing. Defaults to no files.
Example: --include users/**.md --exclude **/example.md will process all
files in the users/ directory excluding files named example.md.
--report-relative-paths
Use relative filenames when reporting error messages. Useful for producing
Expand Down
25 changes: 22 additions & 3 deletions tests/unit/src/test/scala/tests/cli/CliSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,31 @@ class CliSuite extends BaseCliSuite {
|# Include
""".stripMargin,
extraArgs = Array(
"--include-path",
"--include",
"include.md",
"--exclude-path",
"--exclude",
"exclude.md"
)
)
checkCli(
"include one file",
"""
|/users/install.md
|# Install
|/users/usage.md
|# Usage
|/developers/setup.md
|# Setup
""".stripMargin,
"""
|/users/install.md
|# Install
""".stripMargin,
extraArgs = Array(
"--include",
"**/install.md"
)
)

checkCli(
"exclude-dir",
Expand All @@ -82,7 +101,7 @@ class CliSuite extends BaseCliSuite {
|# Index 1.0.0
""".stripMargin,
extraArgs = Array(
"--exclude-path",
"--exclude",
"src"
)
)
Expand Down

0 comments on commit c419558

Please sign in to comment.