Skip to content

Commit 7b7b9ff

Browse files
author
Andres Almiray
committed
Support goal/task aliases. Fixes #9
1 parent a6a5ea8 commit 7b7b9ff

File tree

6 files changed

+111
-11
lines changed

6 files changed

+111
-11
lines changed

.goreleaser.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ snapcrafts:
6363
* **-gm** force Maven build
6464
* **-gn** executes nearest build file
6565
* **-gq** run gm in quiet mode
66+
* **-gr** do not replace goals/tasks
6667
* **-gv** displays version information
6768
6869
Gum will execute the build based on the root build file unless **-gn** is specified, in which case the nearest build file

README.adoc

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,42 @@ Gum supports the following flags
2929
* *-gm* force Maven build
3030
* *-gn* executes nearest build file
3131
* *-gq* run gm in quiet mode
32+
* *-gr* do not replace goals/tasks
3233
* *-gv* displays version information
3334

3435
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.
3536
If a specific build file is given (*-b*, *--build-file* for Gradle; *-f*, *--file* for Maven) then that file will be used instead.
3637

37-
Gum works by passing the given arguments to the resolved tool, thus you can invoke it with a Maven or Gradle build like so
38+
Gum works by passing the given arguments to the resolved tool; it will replace common goal/task names following these mappings
39+
40+
|===
41+
| Maven | Gradle
42+
| compile | classes
43+
| package | assemble
44+
| verify | build
45+
| install | publishToMavenLocal
46+
|===
47+
48+
You can skip these replacements by defining the *-gr* flag.
49+
50+
Gum can be used to run Maven and Gradle builds like so:
3851

3952
.Maven
4053
[source]
4154
----
42-
$ gm verify
55+
$ gm build
4356
----
4457

58+
Which results in the invocation of either *mvnw* or *mvn* with the *verify* goal.
59+
4560
.Gradle
4661
[source]
4762
----
48-
$ gm build
63+
$ gm verify
4964
----
5065

66+
Which results in the invocation of either *gradlew* or *gradle* with the *build* goal.
67+
5168
== Installation
5269

5370
=== Homebrew

gm.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func main() {
3333
fmt.Println(" -gm\tforce Maven build")
3434
fmt.Println(" -gn\texecutes nearest build file")
3535
fmt.Println(" -gq\trun gm in quiet mode")
36+
fmt.Println(" -gr\tdo not replace goals/tasks")
3637
fmt.Println(" -gv\tdisplays version information")
3738
os.Exit(-1)
3839
}

gum/flag.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,20 @@ func findFlag(flag string, args []string) (bool, string) {
5151

5252
return false, ""
5353
}
54+
55+
func replaceArgs(args []string, replacements map[string]string) []string {
56+
nargs := make([]string, 0)
57+
58+
for i := range args {
59+
key := args[i]
60+
val := replacements[key]
61+
62+
if len(val) > 0 {
63+
nargs = append(nargs, val)
64+
} else {
65+
nargs = append(nargs, key)
66+
}
67+
}
68+
69+
return nargs
70+
}

gum/gradle.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,24 @@ func (c GradleCommand) Execute() {
2828

2929
banner := make([]string, 0)
3030
banner = append(banner, "Using gradle at '"+c.executable+"'")
31-
nearest, nargs := GrabFlag("-gn", c.args)
32-
debug, nargs := GrabFlag("-gd", nargs)
31+
nearest, oargs := GrabFlag("-gn", c.args)
32+
debug, oargs := GrabFlag("-gd", oargs)
33+
skipReplace, oargs := GrabFlag("-gr", oargs)
34+
35+
nargs := replaceGradleTasks(skipReplace, oargs)
3336

3437
if debug {
3538
fmt.Println("nearest = ", nearest)
36-
fmt.Println("args = ", nargs)
3739
fmt.Println("rootBuildFile = ", c.rootBuildFile)
3840
fmt.Println("buildFile = ", c.buildFile)
3941
fmt.Println("settingsFile = ", c.settingsFile)
4042
fmt.Println("explicitBuildFile = ", c.explicitBuildFile)
4143
fmt.Println("explicitSettingsFile = ", c.explicitSettingsFile)
4244
fmt.Println("explicitProjectDir = ", c.explicitProjectDir)
43-
fmt.Println("")
45+
fmt.Println("original args = ", oargs)
46+
if !skipReplace {
47+
fmt.Println("replaced args = ", nargs)
48+
}
4449
}
4550

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

87+
if debug {
88+
fmt.Println("actual args = ", args)
89+
fmt.Println("")
90+
}
91+
8292
if !c.context.IsQuiet() {
8393
fmt.Println(strings.Join(banner, " "))
8494
}
@@ -89,6 +99,22 @@ func (c GradleCommand) Execute() {
8999
cmd.Run()
90100
}
91101

102+
func replaceGradleTasks(skipReplace bool, args []string) []string {
103+
var nargs []string = args
104+
105+
if !skipReplace {
106+
replacements := map[string]string{
107+
"compile": "classes",
108+
"package": "assemble",
109+
"verify": "build",
110+
"install": "publishToMavenLocal"}
111+
112+
nargs = replaceArgs(args, replacements)
113+
}
114+
115+
return nargs
116+
}
117+
92118
// FindGradle finds and executes gradlew/gradle
93119
func FindGradle(context Context, args []string) *GradleCommand {
94120
pwd := context.GetWorkingDir()

gum/maven.go

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,32 @@ func (c MavenCommand) Execute() {
2525

2626
banner := make([]string, 0)
2727
banner = append(banner, "Using maven at '"+c.executable+"'")
28-
nearest, nargs := GrabFlag("-gn", c.args)
29-
debug, nargs := GrabFlag("-gd", nargs)
28+
nearest, oargs := GrabFlag("-gn", c.args)
29+
debug, oargs := GrabFlag("-gd", oargs)
30+
skipReplace, oargs := GrabFlag("-gr", oargs)
31+
32+
var nargs []string = oargs
33+
34+
if !skipReplace {
35+
replacements := map[string]string{
36+
"classes": "compile",
37+
"assemble": "package",
38+
"build": "verify",
39+
"publishToMavenLocal": "install",
40+
"puTML": "install"}
41+
42+
nargs = replaceArgs(oargs, replacements)
43+
}
3044

3145
if debug {
3246
fmt.Println("nearest = ", nearest)
33-
fmt.Println("args = ", nargs)
3447
fmt.Println("rootBuildFile = ", c.rootBuildFile)
3548
fmt.Println("buildFile = ", c.buildFile)
3649
fmt.Println("explicitBuildFile = ", c.explicitBuildFile)
37-
fmt.Println("")
50+
fmt.Println("original args = ", oargs)
51+
if !skipReplace {
52+
fmt.Println("replaced args = ", nargs)
53+
}
3854
}
3955

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

72+
if debug {
73+
fmt.Println("actual args = ", args)
74+
fmt.Println("")
75+
}
76+
5677
if !c.context.IsQuiet() {
5778
fmt.Println(strings.Join(banner, " "))
5879
}
@@ -63,6 +84,23 @@ func (c MavenCommand) Execute() {
6384
cmd.Run()
6485
}
6586

87+
func replaceMavenGoals(skipReplace bool, args []string) []string {
88+
var nargs []string = args
89+
90+
if !skipReplace {
91+
replacements := map[string]string{
92+
"classes": "compile",
93+
"assemble": "package",
94+
"build": "verify",
95+
"publishToMavenLocal": "install",
96+
"puTML": "install"}
97+
98+
nargs = replaceArgs(args, replacements)
99+
}
100+
101+
return nargs
102+
}
103+
66104
// FindMaven finds and executes mvnw/mvn
67105
func FindMaven(context Context, args []string) *MavenCommand {
68106
pwd := context.GetWorkingDir()

0 commit comments

Comments
 (0)