Skip to content

Commit

Permalink
minor cleanup (#22)
Browse files Browse the repository at this point in the history
* some minor cleanup
  • Loading branch information
natefinch authored Sep 29, 2017
1 parent 84e8285 commit d674cfa
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 29 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,11 @@ run mage.go <target>` and it'll work just as if you ran `mage <target>`
package main

import (
"os"
"github.com/magefile/mage/mage"
)

func main() { mage.Main() }
func main() { os.Exit(mage.Main()) }
```

Note that because of the peculiarities of `go run`, if you run this way, go run
Expand Down
48 changes: 29 additions & 19 deletions mage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ const mageVer = "v0.3"
var output = template.Must(template.New("").Funcs(map[string]interface{}{
"lower": strings.ToLower,
}).Parse(tpl))
var mageTemplateOutput = template.Must(template.New("").Parse(mageTpl))
var initOutput = template.Must(template.New("").Parse(mageTpl))

const mainfile = "mage_output_file.go"
const mageTemplate = "magefile.go"
const initFile = "magefile.go"

var (
force, verbose, list, help, mageInit bool
Expand All @@ -54,39 +54,48 @@ func init() {
// Main is the entrypoint for running mage. It exists external to mage's main
// function to allow it to be used from other programs, specifically so you can
// go run a simple file that run's mage's Main.
func Main() {
func Main() int {
log.SetFlags(0)
flag.Parse()
if help && len(flag.Args()) == 0 {
flag.Usage()
return
return 0
}

files := magefiles()
files, err := magefiles()
if err != nil {
fmt.Println(err)
return 1
}
if len(files) == 0 && mageInit {
if err := generateInit(); err != nil {
log.Fatalf("%+v", err)
log.Printf("%+v", err)
return 1
}
files = magefiles()
log.Println(initFile, "created")
return 0
} else if len(files) == 0 {
log.Fatal("No files marked with the mage build tag in this directory.")
log.Print("No .go files marked with the mage build tag in this directory.")
return 1
}

out, err := ExeName(files)

if err != nil {
log.Fatalf("%+v", err)
log.Printf("%+v", err)
return 1
}

if !force {
if _, err := os.Stat(out); err == nil {
os.Exit(run(out, flag.Args()...))
return run(out, flag.Args()...)
}
}

info, err := parse.Package(".", files)
if err != nil {
log.Fatalf("%v", err)
log.Printf("%v", err)
return 1
}

names := map[string][]string{}
Expand All @@ -111,9 +120,10 @@ func Main() {
}

if err := compile(out, info, files); err != nil {
log.Fatal(err)
log.Print(err)
return 1
}
os.Exit(run(out, flag.Args()...))
return run(out, flag.Args()...)
}

type data struct {
Expand All @@ -122,18 +132,18 @@ type data struct {
Default string
}

func magefiles() []string {
func magefiles() ([]string, error) {
ctx := build.Default
ctx.RequiredTags = []string{"mage"}
ctx.BuildTags = []string{"mage"}
p, err := ctx.ImportDir(".", 0)
if err != nil {
if _, ok := err.(*build.NoGoError); ok {
return p.GoFiles
return []string{}, nil
}
log.Fatalf("%+v", err)
return nil, err
}
return p.GoFiles
return p.GoFiles, nil
}

func compile(out string, info *parse.PkgInfo, gofiles []string) error {
Expand Down Expand Up @@ -226,13 +236,13 @@ func confDir() string {
}

func generateInit() error {
f, err := os.Create(mageTemplate)
f, err := os.Create(initFile)
if err != nil {
return errors.WithMessage(err, "could not create mage template")
}
defer f.Close()

if err := mageTemplateOutput.Execute(f, nil); err != nil {
if err := initOutput.Execute(f, nil); err != nil {
return errors.WithMessage(err, "can't execute magefile template")
}

Expand Down
4 changes: 2 additions & 2 deletions mage/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ package main
import (
"fmt"
"github.com/magefile/mage/mg" // mg contains helpful utility functions, like Deps
"os"
"os/exec"
"github.com/magefile/mage/mg" // mg contains helpful utility functions, like Deps
)
// Default target to run when none is specified
Expand Down Expand Up @@ -152,5 +153,4 @@ func Clean() {
fmt.Println("Cleaning...")
os.RemoveAll("MyApp")
}
`
8 changes: 6 additions & 2 deletions mage/testdata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

package main

import "github.com/magefile/mage/mage"
import (
"os"

"github.com/magefile/mage/mage"
)

func main() {
mage.Main()
os.Exit(mage.Main())
}
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package main

import "github.com/magefile/mage/mage"
import (
"os"

"github.com/magefile/mage/mage"
)

func main() {
mage.Main()
os.Exit(mage.Main())
}
5 changes: 2 additions & 3 deletions parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ func Package(path string, files []string) (*PkgInfo, error) {
}
}

getDefault(p, pi, info)
setDefault(p, pi, info)

return pi, nil
}

func getDefault(p *doc.Package, pi *PkgInfo, info types.Info) {
func setDefault(p *doc.Package, pi *PkgInfo, info types.Info) {
for _, v := range p.Vars {
for x, name := range v.Names {
if name != "Default" {
Expand All @@ -92,7 +92,6 @@ func getDefault(p *doc.Package, pi *PkgInfo, info types.Info) {
log.Println("warning: default declaration does not reference a mage target")
}
}
log.Println("no default target found")
}

// getPackage returns the non-test package at the given path.
Expand Down

0 comments on commit d674cfa

Please sign in to comment.