Skip to content

Commit 7cd8ad7

Browse files
committed
#345 Conditionals for file creation
1 parent 55a4520 commit 7cd8ad7

File tree

12 files changed

+69
-28
lines changed

12 files changed

+69
-28
lines changed

docs/03/00.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ src/main/g8
127127

128128
```
129129

130+
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:
131+
132+
```
133+
src/main/g8
134+
├── parent_folder
135+
│   ├── \$if(cond.truthy)\$skip_folder\$else\$.\$endif\$
136+
| | └── child_file
137+
```
138+
139+
will be processed to
140+
141+
```
142+
├── parent_folder
143+
| └── child_file
144+
```
145+
130146
[conditionals]: https://github.com/antlr/stringtemplate4/blob/master/doc/templates.md#conditionals
131147

132148
### name field

library/src/main/scala/g8.scala

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ object G8 {
9595
def apply(fromMapping: Seq[(File, String)], toPath: File, parameters: Map[String, String]): Seq[File] =
9696
fromMapping filter { !_._1.isDirectory } flatMap {
9797
case (in, relative) =>
98-
apply(in, expandPath(relative, toPath, parameters), toPath, parameters)
98+
expandPath(relative, toPath, parameters) match {
99+
case None => Seq()
100+
case Some(out) => apply(in, out, toPath, parameters)
101+
}
99102
}
100103

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

196-
def expandPath(relative: String, toPath: File, parameters: Map[String, String]): File =
199+
def expandPath(relative: String, toPath: File, parameters: Map[String, String]): Option[File] =
197200
try {
201+
val fileSeparator = System.getProperty("file.separator")
198202
val fileParams = Map(parameters.toSeq map {
199203
case (k, v) if k == "package" =>
200-
(k, v.replaceAll("""\.""", System.getProperty("file.separator") match {
204+
(k, v.replaceAll("""\.""", fileSeparator match {
201205
case "\\" => "\\\\"
202206
case sep => sep
203207
}))
204208
case x => x
205209
}: _*)
206210

207-
new File(toPath, applyTemplate(formatize(relative), fileParams))
211+
val ignored = relative
212+
.split(fileSeparator)
213+
.map(part => applyTemplate(formatize(part), fileParams))
214+
.exists(_.isEmpty)
215+
216+
if (ignored) None
217+
else Some(new File(toPath, applyTemplate(formatize(relative), fileParams)))
208218
} catch {
209219
case e: STException =>
210220
// add the current relative path to the exception for debugging purposes
@@ -475,34 +485,39 @@ object G8 {
475485

476486
templates
477487
.map { in =>
478-
val name = relativize(in, tmpl)
479-
val out = G8.expandPath(name, base, parameters)
480-
(in, out)
488+
val name = relativize(in, tmpl)
489+
val optionOut = G8.expandPath(name, base, parameters)
490+
(in, optionOut)
481491
}
482492
.foreach {
483-
case (in, out) =>
484-
if (out.exists && !forceOverwrite) {
485-
println("Skipping existing file: %s" format out.toString)
486-
} else {
487-
out.getParentFile.mkdirs()
488-
if (G8.verbatim(in, parameters, tmpl))
489-
FileUtils.copyFile(in, out)
490-
else {
491-
catching(classOf[MalformedInputException])
492-
.opt {
493-
Some(G8.write(in, out, parameters /*, append = existingScaffoldingAction.getOrElse(false)*/ ))
494-
}
495-
.getOrElse {
496-
// if (existingScaffoldingAction.getOrElse(false)) {
497-
// val existing = FileUtils.readFileToString(in, UTF_8)
498-
// FileUtils.write(out, existing, UTF_8, true)
499-
// } else {
493+
case (in, optionOut) =>
494+
optionOut match {
495+
case None => println("Skipping ignored file: %s" format in.toString)
496+
case Some(out) => {
497+
if (out.exists && !forceOverwrite) {
498+
println("Skipping existing file: %s" format out.toString)
499+
} else {
500+
out.getParentFile.mkdirs()
501+
if (G8.verbatim(in, parameters, tmpl))
500502
FileUtils.copyFile(in, out)
501-
// }
503+
else {
504+
catching(classOf[MalformedInputException])
505+
.opt {
506+
Some(G8.write(in, out, parameters /*, append = existingScaffoldingAction.getOrElse(false)*/ ))
507+
}
508+
.getOrElse {
509+
// if (existingScaffoldingAction.getOrElse(false)) {
510+
// val existing = FileUtils.readFileToString(in, UTF_8)
511+
// FileUtils.write(out, existing, UTF_8, true)
512+
// } else {
513+
FileUtils.copyFile(in, out)
514+
// }
515+
}
502516
}
503-
}
504-
if (in.canExecute) {
505-
out.setExecutable(true)
517+
if (in.canExecute) {
518+
out.setExecutable(true)
519+
}
520+
}
506521
}
507522
}
508523
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file should be created
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a simple file
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file with parent folder should be created
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file should be ignored
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file should be ignored
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file with parent folder should be created
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
condition=no
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file should be created

0 commit comments

Comments
 (0)