Skip to content

Commit

Permalink
Support goal/task aliases. Fixes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
Andres Almiray committed Jun 5, 2020
1 parent a6a5ea8 commit 7b7b9ff
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 11 deletions.
1 change: 1 addition & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ snapcrafts:
* **-gm** force Maven build
* **-gn** executes nearest build file
* **-gq** run gm in quiet mode
* **-gr** do not replace goals/tasks
* **-gv** displays version information
Gum will execute the build based on the root build file unless **-gn** is specified, in which case the nearest build file
Expand Down
23 changes: 20 additions & 3 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,42 @@ Gum supports the following flags
* *-gm* force Maven build
* *-gn* executes nearest build file
* *-gq* run gm in quiet mode
* *-gr* do not replace goals/tasks
* *-gv* displays version information

Gum will execute the build based on the root build file unless *-gn* is specified, in which case the nearest build file will be selected.
If a specific build file is given (*-b*, *--build-file* for Gradle; *-f*, *--file* for Maven) then that file will be used instead.

Gum works by passing the given arguments to the resolved tool, thus you can invoke it with a Maven or Gradle build like so
Gum works by passing the given arguments to the resolved tool; it will replace common goal/task names following these mappings

|===
| Maven | Gradle
| compile | classes
| package | assemble
| verify | build
| install | publishToMavenLocal
|===

You can skip these replacements by defining the *-gr* flag.

Gum can be used to run Maven and Gradle builds like so:

.Maven
[source]
----
$ gm verify
$ gm build
----

Which results in the invocation of either *mvnw* or *mvn* with the *verify* goal.

.Gradle
[source]
----
$ gm build
$ gm verify
----

Which results in the invocation of either *gradlew* or *gradle* with the *build* goal.

== Installation

=== Homebrew
Expand Down
1 change: 1 addition & 0 deletions gm.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func main() {
fmt.Println(" -gm\tforce Maven build")
fmt.Println(" -gn\texecutes nearest build file")
fmt.Println(" -gq\trun gm in quiet mode")
fmt.Println(" -gr\tdo not replace goals/tasks")
fmt.Println(" -gv\tdisplays version information")
os.Exit(-1)
}
Expand Down
17 changes: 17 additions & 0 deletions gum/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,20 @@ func findFlag(flag string, args []string) (bool, string) {

return false, ""
}

func replaceArgs(args []string, replacements map[string]string) []string {
nargs := make([]string, 0)

for i := range args {
key := args[i]
val := replacements[key]

if len(val) > 0 {
nargs = append(nargs, val)
} else {
nargs = append(nargs, key)
}
}

return nargs
}
34 changes: 30 additions & 4 deletions gum/gradle.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,24 @@ func (c GradleCommand) Execute() {

banner := make([]string, 0)
banner = append(banner, "Using gradle at '"+c.executable+"'")
nearest, nargs := GrabFlag("-gn", c.args)
debug, nargs := GrabFlag("-gd", nargs)
nearest, oargs := GrabFlag("-gn", c.args)
debug, oargs := GrabFlag("-gd", oargs)
skipReplace, oargs := GrabFlag("-gr", oargs)

nargs := replaceGradleTasks(skipReplace, oargs)

if debug {
fmt.Println("nearest = ", nearest)
fmt.Println("args = ", nargs)
fmt.Println("rootBuildFile = ", c.rootBuildFile)
fmt.Println("buildFile = ", c.buildFile)
fmt.Println("settingsFile = ", c.settingsFile)
fmt.Println("explicitBuildFile = ", c.explicitBuildFile)
fmt.Println("explicitSettingsFile = ", c.explicitSettingsFile)
fmt.Println("explicitProjectDir = ", c.explicitProjectDir)
fmt.Println("")
fmt.Println("original args = ", oargs)
if !skipReplace {
fmt.Println("replaced args = ", nargs)
}
}

if len(c.explicitProjectDir) > 0 {
Expand Down Expand Up @@ -79,6 +84,11 @@ func (c GradleCommand) Execute() {
args = append(args, nargs[i])
}

if debug {
fmt.Println("actual args = ", args)
fmt.Println("")
}

if !c.context.IsQuiet() {
fmt.Println(strings.Join(banner, " "))
}
Expand All @@ -89,6 +99,22 @@ func (c GradleCommand) Execute() {
cmd.Run()
}

func replaceGradleTasks(skipReplace bool, args []string) []string {
var nargs []string = args

if !skipReplace {
replacements := map[string]string{
"compile": "classes",
"package": "assemble",
"verify": "build",
"install": "publishToMavenLocal"}

nargs = replaceArgs(args, replacements)
}

return nargs
}

// FindGradle finds and executes gradlew/gradle
func FindGradle(context Context, args []string) *GradleCommand {
pwd := context.GetWorkingDir()
Expand Down
46 changes: 42 additions & 4 deletions gum/maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,32 @@ func (c MavenCommand) Execute() {

banner := make([]string, 0)
banner = append(banner, "Using maven at '"+c.executable+"'")
nearest, nargs := GrabFlag("-gn", c.args)
debug, nargs := GrabFlag("-gd", nargs)
nearest, oargs := GrabFlag("-gn", c.args)
debug, oargs := GrabFlag("-gd", oargs)
skipReplace, oargs := GrabFlag("-gr", oargs)

var nargs []string = oargs

if !skipReplace {
replacements := map[string]string{
"classes": "compile",
"assemble": "package",
"build": "verify",
"publishToMavenLocal": "install",
"puTML": "install"}

nargs = replaceArgs(oargs, replacements)
}

if debug {
fmt.Println("nearest = ", nearest)
fmt.Println("args = ", nargs)
fmt.Println("rootBuildFile = ", c.rootBuildFile)
fmt.Println("buildFile = ", c.buildFile)
fmt.Println("explicitBuildFile = ", c.explicitBuildFile)
fmt.Println("")
fmt.Println("original args = ", oargs)
if !skipReplace {
fmt.Println("replaced args = ", nargs)
}
}

if len(c.explicitBuildFile) > 0 {
Expand All @@ -53,6 +69,11 @@ func (c MavenCommand) Execute() {
args = append(args, nargs[i])
}

if debug {
fmt.Println("actual args = ", args)
fmt.Println("")
}

if !c.context.IsQuiet() {
fmt.Println(strings.Join(banner, " "))
}
Expand All @@ -63,6 +84,23 @@ func (c MavenCommand) Execute() {
cmd.Run()
}

func replaceMavenGoals(skipReplace bool, args []string) []string {
var nargs []string = args

if !skipReplace {
replacements := map[string]string{
"classes": "compile",
"assemble": "package",
"build": "verify",
"publishToMavenLocal": "install",
"puTML": "install"}

nargs = replaceArgs(args, replacements)
}

return nargs
}

// FindMaven finds and executes mvnw/mvn
func FindMaven(context Context, args []string) *MavenCommand {
pwd := context.GetWorkingDir()
Expand Down

0 comments on commit 7b7b9ff

Please sign in to comment.