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

Add --force flag to g8Scaffold command #771

Merged
merged 1 commit into from
Mar 7, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 7 additions & 6 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 folder = g8ScaffoldTemplatesDirectory.value
G8.fromDirectoryRaw(folder / name, baseDirectory.value, args, false)
mkurz marked this conversation as resolved.
Show resolved Hide resolved
val (name, forceOverwrite, args) = parser.parsed
val folder = g8ScaffoldTemplatesDirectory.value
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