diff --git a/Makefile b/Makefile index dcc2a9a..e904b37 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ ################################################################################ -# This Makefile generated by GoMakeGen 0.7.0 using next command: +# This Makefile generated by GoMakeGen 0.7.1 using next command: # gomakegen --metalinter --strip . ################################################################################ diff --git a/gomakegen.go b/gomakegen.go index 1aa4673..d096c82 100644 --- a/gomakegen.go +++ b/gomakegen.go @@ -2,7 +2,7 @@ package main // ////////////////////////////////////////////////////////////////////////////////// // // // -// Copyright (c) 2009-2017 ESSENTIAL KAOS // +// Copyright (c) 2009-2018 ESSENTIAL KAOS // // Essential Kaos Open Source License // // // // ////////////////////////////////////////////////////////////////////////////////// // @@ -35,7 +35,7 @@ import ( // App info const ( APP = "gomakegen" - VER = "0.7.0" + VER = "0.7.1" DESC = "Utility for generating makefiles for Golang applications" ) @@ -64,11 +64,12 @@ type Makefile struct { TestImports []string Binaries []string - HasTests bool - Benchmark bool - VerbTests bool - Metalinter bool - Strip bool + HasTests bool + Benchmark bool + VerbTests bool + Metalinter bool + Strip bool + HasSubpackages bool GlideUsed bool DepUsed bool @@ -208,50 +209,17 @@ func generateMakefile(sources []string, dir string) *Makefile { // collectImports collect import from source files and return imports for // base sources, test sources and slice with binaries func collectImports(sources []string, dir string) *Makefile { - var ( - bImports map[string]bool - tImports map[string]bool - binaries []string - imports []string - isBinary bool - source string - path string - ) - - bSources, tSources := splitSources(sources) - - bImports = make(map[string]bool) - - for _, source = range bSources { - imports, isBinary = extractImports(source, dir) - - for _, path = range imports { - bImports[path] = true - } - - // Append to slice only binaries in root directory - if isBinary && !strings.Contains(source, "/") { - binaries = append(binaries, source) - } - } + baseSources, testSources := splitSources(sources) - if len(tSources) != 0 { - tImports = make(map[string]bool) - - for _, source = range tSources { - imports, _ = extractImports(source, dir) - - for _, path = range imports { - tImports[path] = true - } - } - } + baseImports, binaries, hasSubPkgs := extractBaseImports(baseSources, dir) + testImports := extractTestImports(testSources, dir) return &Makefile{ - BaseImports: importMapToSlice(bImports), - TestImports: importMapToSlice(tImports), - Binaries: binaries, - HasTests: hasTests(sources), + BaseImports: baseImports, + TestImports: testImports, + Binaries: binaries, + HasTests: hasTests(sources), + HasSubpackages: hasSubPkgs, } } @@ -274,6 +242,51 @@ func splitSources(sources []string) ([]string, []string) { return bSources, tSources } +// extractBaseImports extract base imports from given source files +func extractBaseImports(sources []string, dir string) ([]string, []string, bool) { + importsMap := make(map[string]bool) + binaries := make([]string, 0) + hasSubPkgs := false + + for _, source := range sources { + imports, isBinary := extractImports(source, dir) + + for _, path := range imports { + importsMap[path] = true + } + + // Append to slice only binaries in root directory + if isBinary && !strings.Contains(source, "/") { + binaries = append(binaries, source) + } + + if !hasSubPkgs && strings.Contains(source, "/") { + hasSubPkgs = true + } + } + + return importMapToSlice(importsMap), binaries, hasSubPkgs +} + +// extractTestImports extract test imports from given source files +func extractTestImports(sources []string, dir string) []string { + if len(sources) == 0 { + return nil + } + + importsMap := make(map[string]bool) + + for _, source := range sources { + imports, _ := extractImports(source, dir) + + for _, path := range imports { + importsMap[path] = true + } + } + + return importMapToSlice(importsMap) +} + // cleanupImports remove internal packages and local imports func cleanupImports(imports []string, dir string) []string { if len(imports) == 0 { @@ -736,12 +749,18 @@ func (m *Makefile) getTestTarget() string { result += "\n" } + targets := "." + + if m.HasSubpackages { + targets = "./..." + } + result += "test: ## Run tests\n" if m.VerbTests { - result += "\tgo test -v -covermode=count .\n" + result += "\tgo test -v -covermode=count " + targets + "\n" } else { - result += "\tgo test -covermode=count .\n" + result += "\tgo test -covermode=count " + targets + "\n" } result += "\n"