Skip to content

Commit

Permalink
Merge pull request #18 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 0.7.1
  • Loading branch information
andyone authored Jan 23, 2018
2 parents e377de5 + df810b9 commit 87e5c30
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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 .

################################################################################
Expand Down
119 changes: 69 additions & 50 deletions gomakegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2017 ESSENTIAL KAOS //
// Copyright (c) 2009-2018 ESSENTIAL KAOS //
// Essential Kaos Open Source License <https://essentialkaos.com/ekol> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
}
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 87e5c30

Please sign in to comment.