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

#345 Conditionals for file creation #432

Merged
merged 1 commit into from Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/03/00.md
Expand Up @@ -127,6 +127,22 @@ src/main/g8

```

If you want to skip a directory from the path, but keep all nested directories and files, use `.` as the name of the directory. For example the next template:

```
src/main/g8
├── parent_folder
│   ├── \$if(cond.truthy)\$skip_folder\$else\$.\$endif\$
| | └── child_file
```

will be processed to

```
├── parent_folder
| └── child_file
```

[conditionals]: https://github.com/antlr/stringtemplate4/blob/master/doc/templates.md#conditionals

### name field
Expand Down
71 changes: 43 additions & 28 deletions library/src/main/scala/g8.scala
Expand Up @@ -95,7 +95,10 @@ object G8 {
def apply(fromMapping: Seq[(File, String)], toPath: File, parameters: Map[String, String]): Seq[File] =
fromMapping filter { !_._1.isDirectory } flatMap {
case (in, relative) =>
apply(in, expandPath(relative, toPath, parameters), toPath, parameters)
expandPath(relative, toPath, parameters) match {
case None => Seq()
case Some(out) => apply(in, out, toPath, parameters)
}
}

def apply(in: File, out: File, base: File, parameters: Map[String, String]) = {
Expand Down Expand Up @@ -193,18 +196,25 @@ object G8 {
rules.isIgnored(file, base)
}

def expandPath(relative: String, toPath: File, parameters: Map[String, String]): File =
def expandPath(relative: String, toPath: File, parameters: Map[String, String]): Option[File] =
try {
val fileSeparator = System.getProperty("file.separator")
val fileParams = Map(parameters.toSeq map {
case (k, v) if k == "package" =>
(k, v.replaceAll("""\.""", System.getProperty("file.separator") match {
(k, v.replaceAll("""\.""", fileSeparator match {
case "\\" => "\\\\"
case sep => sep
}))
case x => x
}: _*)

new File(toPath, applyTemplate(formatize(relative), fileParams))
val ignored = relative
.split(fileSeparator)
.map(part => applyTemplate(formatize(part), fileParams))
.exists(_.isEmpty)

if (ignored) None
else Some(new File(toPath, applyTemplate(formatize(relative), fileParams)))
} catch {
case e: STException =>
// add the current relative path to the exception for debugging purposes
Expand Down Expand Up @@ -475,34 +485,39 @@ object G8 {

templates
.map { in =>
val name = relativize(in, tmpl)
val out = G8.expandPath(name, base, parameters)
(in, out)
val name = relativize(in, tmpl)
val optionOut = G8.expandPath(name, base, parameters)
(in, optionOut)
}
.foreach {
case (in, out) =>
if (out.exists && !forceOverwrite) {
println("Skipping existing file: %s" format out.toString)
} else {
out.getParentFile.mkdirs()
if (G8.verbatim(in, parameters, tmpl))
FileUtils.copyFile(in, out)
else {
catching(classOf[MalformedInputException])
.opt {
Some(G8.write(in, out, parameters /*, append = existingScaffoldingAction.getOrElse(false)*/ ))
}
.getOrElse {
// if (existingScaffoldingAction.getOrElse(false)) {
// val existing = FileUtils.readFileToString(in, UTF_8)
// FileUtils.write(out, existing, UTF_8, true)
// } else {
case (in, optionOut) =>
optionOut match {
case None => println("Skipping ignored file: %s" format in.toString)
case Some(out) => {
if (out.exists && !forceOverwrite) {
println("Skipping existing file: %s" format out.toString)
} else {
out.getParentFile.mkdirs()
if (G8.verbatim(in, parameters, tmpl))
FileUtils.copyFile(in, out)
// }
else {
catching(classOf[MalformedInputException])
.opt {
Some(G8.write(in, out, parameters /*, append = existingScaffoldingAction.getOrElse(false)*/ ))
}
.getOrElse {
// if (existingScaffoldingAction.getOrElse(false)) {
// val existing = FileUtils.readFileToString(in, UTF_8)
// FileUtils.write(out, existing, UTF_8, true)
// } else {
FileUtils.copyFile(in, out)
// }
}
}
}
if (in.canExecute) {
out.setExecutable(true)
if (in.canExecute) {
out.setExecutable(true)
}
}
}
}
}
Expand Down
@@ -0,0 +1 @@
This file should be created
@@ -0,0 +1 @@
This is a simple file
@@ -0,0 +1 @@
This file with parent folder should be created
@@ -0,0 +1 @@
This file should be ignored
@@ -0,0 +1 @@
This file should be ignored
@@ -0,0 +1 @@
This file with parent folder should be created
@@ -0,0 +1 @@
condition=no
@@ -0,0 +1 @@
This file should be created
@@ -0,0 +1 @@
This file should be ignored
@@ -0,0 +1 @@
This is a simple file