From 8dce728c572d87ada67f1222282d55ca687899e9 Mon Sep 17 00:00:00 2001 From: Mikhail Gusarov Date: Thu, 14 Mar 2019 15:23:16 +0100 Subject: [PATCH] improve lowercasing of target names: YAMLGenerate -> yamlGenerate (#225) Previously lowercasing caused awkwardly-looking yAMLGenerate and pC targets. --- mage/import_test.go | 4 ++-- mage/main.go | 21 ++++++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/mage/import_test.go b/mage/import_test.go index c24b8529..259a2804 100644 --- a/mage/import_test.go +++ b/mage/import_test.go @@ -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:] diff --git a/mage/main.go b/mage/main.go index 0db67e50..a8adfb29 100644 --- a/mage/main.go +++ b/mage/main.go @@ -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" @@ -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, ":") },