Skip to content

Commit 8dce728

Browse files
Mikhail Gusarovnatefinch
authored andcommitted
improve lowercasing of target names: YAMLGenerate -> yamlGenerate (#225)
Previously lowercasing caused awkwardly-looking yAMLGenerate and pC targets.
1 parent 8c9aa25 commit 8dce728

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

mage/import_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ func TestMageImportsList(t *testing.T) {
2323
expected := `
2424
Targets:
2525
buildSubdir Builds stuff.
26-
nS:deploy deploys stuff.
26+
ns:deploy deploys stuff.
2727
root
2828
zz:buildSubdir2 Builds stuff.
29-
zz:nS:deploy2* deploys stuff.
29+
zz:ns:deploy2* deploys stuff.
3030
3131
* default target
3232
`[1:]

mage/main.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
"os"
1212
"os/exec"
1313
"path/filepath"
14+
"regexp"
1415
"runtime"
1516
"sort"
1617
"strings"
1718
"text/template"
1819
"time"
19-
"unicode"
2020

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

33+
// (Aaaa)(Bbbb) -> aaaaBbbb
34+
var firstWordRx = regexp.MustCompile(`^([[:upper:]][^[:upper:]]+)([[:upper:]].*)$`)
35+
36+
// (AAAA)(Bbbb) -> aaaaBbbb
37+
var firstAbbrevRx = regexp.MustCompile(`^([[:upper:]]+)([[:upper:]][^[:upper:]].*)$`)
38+
39+
func lowerFirstWord(s string) string {
40+
if match := firstWordRx.FindStringSubmatch(s); match != nil {
41+
return strings.ToLower(match[1]) + match[2]
42+
}
43+
if match := firstAbbrevRx.FindStringSubmatch(s); match != nil {
44+
return strings.ToLower(match[1]) + match[2]
45+
}
46+
return strings.ToLower(s)
47+
}
48+
3349
var mainfileTemplate = template.Must(template.New("").Funcs(map[string]interface{}{
3450
"lower": strings.ToLower,
3551
"lowerFirst": func(s string) string {
3652
parts := strings.Split(s, ":")
3753
for i, t := range parts {
38-
r := []rune(t)
39-
parts[i] = string(unicode.ToLower(r[0])) + string(r[1:])
54+
parts[i] = lowerFirstWord(t)
4055
}
4156
return strings.Join(parts, ":")
4257
},

0 commit comments

Comments
 (0)