Skip to content

Commit

Permalink
Add g8ScaffoldForce command to overwrite files
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurz committed Mar 6, 2023
1 parent defe178 commit 770e515
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
6 changes: 6 additions & 0 deletions docs/04/00.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ Use TAB completion to discover available templates.
controller global model
```

To overwrite existing files pass the `--force` flag after the template:

```
> g8Scaffold model --force
```

The template plugin will prompt each property that needed to complete the scaffolding process:

```
Expand Down
11 changes: 6 additions & 5 deletions scaffold/src/main/scala/ScaffoldPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,25 @@ object ScaffoldPlugin extends sbt.AutoPlugin {
import complete._
import complete.DefaultParsers._

val parser: Def.Initialize[State => Parser[(String, List[String])]] =
val parser: Def.Initialize[State => Parser[(String, Boolean, List[String])]] =
Def.setting {
val dir = g8ScaffoldTemplatesDirectory.value
(state: State) =>
val templates = Option(dir.listFiles).toList.flatten
.filter(f => f.isDirectory && !f.isHidden)
.map(_.getName: Parser[String])
(Space) ~> token(templates.foldLeft(" ": Parser[String])(_ | _)).examples("<template>") ~
(Space ~> StringBasic.examples("--k=v")).* map { case tmp ~ args =>
(tmp, args.toList)
(Space ~> "--force").? ~
(Space ~> StringBasic.examples("--k=v")).* map { case tmp ~ force ~ args =>
(tmp, force.nonEmpty, args.toList)
}
}

val scaffoldTask =
Def.inputTask {
val (name, args) = parser.parsed
val (name, forceOverwrite, args) = parser.parsed
val folder = g8ScaffoldTemplatesDirectory.value
G8.fromDirectoryRaw(folder / name, baseDirectory.value, args, false)
G8.fromDirectoryRaw(folder / name, baseDirectory.value, args, forceOverwrite)
.fold(
e => sys.error(e),
r => println("Success :)")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package controllers

import play.api._
import play.api.mvc._

object $className$ extends Controller {

// In the scripted test this file should overwrite the existing controlle file

def index = Action {
Ok(views.html.index("This application is awesome."))
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
className=Application
14 changes: 14 additions & 0 deletions scaffold/src/sbt-test/scaffold/simple/Application_forced.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package controllers

import play.api._
import play.api.mvc._

object Application extends Controller {

// In the scripted test this file should overwrite the existing controlle file

def index = Action {
Ok(views.html.index("This application is awesome."))
}

}
5 changes: 5 additions & 0 deletions scaffold/src/sbt-test/scaffold/simple/test
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
> g8Scaffold controller --className=Application

$exists app/controllers/Application.scala

> g8Scaffold controller-forcing --force --className=Application

# If files are identical then app/controllers/Application.scala was successfully overwritten
$must-mirror app/controllers/Application.scala Application_forced.scala

0 comments on commit 770e515

Please sign in to comment.