Skip to content

Commit

Permalink
improve lowercasing of target names: YAMLGenerate -> yamlGenerate (ma…
Browse files Browse the repository at this point in the history
…gefile#225)

Previously lowercasing caused awkwardly-looking yAMLGenerate and pC targets.
  • Loading branch information
Mikhail Gusarov authored and natefinch committed Mar 14, 2019
1 parent 8c9aa25 commit 8dce728
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
4 changes: 2 additions & 2 deletions mage/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ func TestMageImportsList(t *testing.T) {
expected := `
Targets:
buildSubdir Builds stuff.
nS:deploy deploys stuff.
ns:deploy deploys stuff.
root
zz:buildSubdir2 Builds stuff.
zz:nS:deploy2* deploys stuff.
zz:ns:deploy2* deploys stuff.
* default target
`[1:]
Expand Down
21 changes: 18 additions & 3 deletions mage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"sort"
"strings"
"text/template"
"time"
"unicode"

"github.com/magefile/mage/internal"
"github.com/magefile/mage/mg"
Expand All @@ -30,13 +30,28 @@ import (
// change the inputs to the compiling process.
const magicRebuildKey = "v0.3"

// (Aaaa)(Bbbb) -> aaaaBbbb
var firstWordRx = regexp.MustCompile(`^([[:upper:]][^[:upper:]]+)([[:upper:]].*)$`)

// (AAAA)(Bbbb) -> aaaaBbbb
var firstAbbrevRx = regexp.MustCompile(`^([[:upper:]]+)([[:upper:]][^[:upper:]].*)$`)

func lowerFirstWord(s string) string {
if match := firstWordRx.FindStringSubmatch(s); match != nil {
return strings.ToLower(match[1]) + match[2]
}
if match := firstAbbrevRx.FindStringSubmatch(s); match != nil {
return strings.ToLower(match[1]) + match[2]
}
return strings.ToLower(s)
}

var mainfileTemplate = template.Must(template.New("").Funcs(map[string]interface{}{
"lower": strings.ToLower,
"lowerFirst": func(s string) string {
parts := strings.Split(s, ":")
for i, t := range parts {
r := []rune(t)
parts[i] = string(unicode.ToLower(r[0])) + string(r[1:])
parts[i] = lowerFirstWord(t)
}
return strings.Join(parts, ":")
},
Expand Down

0 comments on commit 8dce728

Please sign in to comment.