Skip to content

Commit

Permalink
[gradle] allow path prefixed task substitutions. Fixes #20
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Sep 19, 2020
1 parent 9fc73e2 commit d2dc49d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
25 changes: 21 additions & 4 deletions gum/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,32 @@ func findFlagValue(flag string, args []string) (bool, string) {
return false, ""
}

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

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

if len(val) > 0 {
nargs = append(nargs, val)
subMatch := ""

if allowsSubMatch {
semicolon := strings.LastIndex(key, ":")
if semicolon > -1 {
prefix := key[0:(semicolon + 1)]
suffix := key[(semicolon + 1):]
match := replacements[suffix]

if len(match) > 0 {
subMatch = prefix + match
}
}
}

if len(exactMatch) > 0 {
nargs = append(nargs, exactMatch)
} else if allowsSubMatch && len(subMatch) > 0 {
nargs = append(nargs, subMatch)
} else {
nargs = append(nargs, key)
}
Expand Down
2 changes: 1 addition & 1 deletion gum/gradle.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func replaceGradleTasks(config *Config, args []string) []string {
var nargs []string = args

if config.gradle.replace {
nargs = replaceArgs(args, config.gradle.mappings)
nargs = replaceArgs(args, config.gradle.mappings, true)
}

return nargs
Expand Down
52 changes: 52 additions & 0 deletions gum/gradle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,55 @@ func TestGradleWithoutExecutables(t *testing.T) {
t.Error("Expected a nil command but got something")
}
}

func TestGradleReplaceWithExactMatch(t *testing.T) {
// given:
bin, _ := filepath.Abs(filepath.Join("..", "tests", "gradle", "bin"))
pwd, _ := filepath.Abs(filepath.Join("..", "tests", "gradle", "single-with-wrapper"))

context := testContext{
quiet: true,
explicit: true,
windows: false,
workingDir: pwd,
paths: []string{bin}}

// when:
cmd := FindGradle(context, []string{"verify"})

// then:
if cmd == nil {
t.Error("Expected a command but got nil")
}

cmd.doConfigureGradle()
if len(cmd.args) != 2 && cmd.args[len(cmd.args)-1] != "build" {
t.Errorf("args: got verify, want build")
}
}

func TestGradleReplaceWithSubMatch(t *testing.T) {
// given:
bin, _ := filepath.Abs(filepath.Join("..", "tests", "gradle", "bin"))
pwd, _ := filepath.Abs(filepath.Join("..", "tests", "gradle", "single-with-wrapper"))

context := testContext{
quiet: true,
explicit: true,
windows: false,
workingDir: pwd,
paths: []string{bin}}

// when:
cmd := FindGradle(context, []string{":subproject:verify"})

// then:
if cmd == nil {
t.Error("Expected a command but got nil")
}

cmd.doConfigureGradle()
if len(cmd.args) != 2 && cmd.args[len(cmd.args)-1] != ":subproject:build" {
t.Errorf("args: got :subproject:verify, want b:subproject:build")
}
}
2 changes: 1 addition & 1 deletion gum/maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func replaceMavenGoals(config *Config, args []string) []string {
var nargs []string = args

if config.maven.replace {
nargs = replaceArgs(args, config.maven.mappings)
nargs = replaceArgs(args, config.maven.mappings, false)
}

return nargs
Expand Down

0 comments on commit d2dc49d

Please sign in to comment.