From 253ba37b170cd0c9820d5b8dd67cd9bda8d7d4aa Mon Sep 17 00:00:00 2001 From: Nate Finch Date: Sun, 26 Aug 2018 10:12:01 -0400 Subject: [PATCH] move checkdupes into parse (#138) by putting checkdeps into parse, it gets checked by the parse tests and it just a cleaner place to put it. --- mage/main.go | 29 ----------------------------- mg/deps.go | 5 ++++- parse/parse.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/mage/main.go b/mage/main.go index 52140c8..8973cdc 100644 --- a/mage/main.go +++ b/mage/main.go @@ -293,17 +293,6 @@ func Invoke(inv Invocation) int { return 1 } - hasDupes, names := CheckDupes(info) - if hasDupes { - log.Println("Build targets must be case insensitive, thus the follow targets conflict:") - for _, v := range names { - if len(v) > 1 { - log.Println(" " + strings.Join(v, ", ")) - } - } - return 1 - } - main := filepath.Join(inv.Dir, mainfile) if err := GenerateMainfile(main, info); err != nil { log.Println("Error:", err) @@ -331,24 +320,6 @@ func Invoke(inv Invocation) int { return RunCompiled(inv, exePath) } -// CheckDupes checks a package for duplicate target names. -func CheckDupes(info *parse.PkgInfo) (hasDupes bool, names map[string][]string) { - names = map[string][]string{} - lowers := map[string]bool{} - for _, f := range info.Funcs { - low := strings.ToLower(f.Name) - if f.Receiver != "" { - low = strings.ToLower(f.Receiver) + ":" + low - } - if lowers[low] { - hasDupes = true - } - lowers[low] = true - names[low] = append(names[low], f.Name) - } - return hasDupes, names -} - type data struct { Description string Funcs []parse.Function diff --git a/mg/deps.go b/mg/deps.go index 50c75fa..00ad6bc 100644 --- a/mg/deps.go +++ b/mg/deps.go @@ -119,7 +119,10 @@ func checkFns(fns []interface{}) { } } -// Deps runs the given functions with the default runtime context +// Deps runs the given functions in parallel, exactly once. This is a way to +// build up a tree of dependencies with each dependency defining its own +// dependencies. Functions must have the same signature as a Mage target, i.e. +// optional context argument, optional error return. func Deps(fns ...interface{}) { CtxDeps(context.Background(), fns...) } diff --git a/parse/parse.go b/parse/parse.go index 0724eae..7858893 100644 --- a/parse/parse.go +++ b/parse/parse.go @@ -1,6 +1,7 @@ package parse import ( + "errors" "fmt" "go/ast" "go/build" @@ -16,6 +17,8 @@ import ( mgTypes "github.com/magefile/mage/types" ) +// PkgInfo contains inforamtion about a package of files according to mage's +// parsing rules. type PkgInfo struct { Description string Funcs []Function @@ -156,12 +159,41 @@ typeloop: } } + hasDupes, names := checkDupes(pi) + if hasDupes { + msg := "Build targets must be case insensitive, thus the following targets conflict:\n" + for _, v := range names { + if len(v) > 1 { + msg += " " + strings.Join(v, ", ") + "\n" + } + } + return nil, errors.New(msg) + } + setDefault(p, pi, info) setAliases(p, pi, info) return pi, nil } +// checkDupes checks a package for duplicate target names. +func checkDupes(info *PkgInfo) (hasDupes bool, names map[string][]string) { + names = map[string][]string{} + lowers := map[string]bool{} + for _, f := range info.Funcs { + low := strings.ToLower(f.Name) + if f.Receiver != "" { + low = strings.ToLower(f.Receiver) + ":" + low + } + if lowers[low] { + hasDupes = true + } + lowers[low] = true + names[low] = append(names[low], f.Name) + } + return hasDupes, names +} + // sanitizeSynopsis sanitizes function Doc to create a summary. func sanitizeSynopsis(f *doc.Func) string { synopsis := doc.Synopsis(f.Doc)