Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set magefiles working directory #277

Merged
merged 7 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ mage_output_file.go
# Goland
.idea
*.iml

# Vim
*.sw[op]
Session.vim
*~

# GNU Screen
.screenrc
14 changes: 12 additions & 2 deletions mage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func Main() int {
type Invocation struct {
Debug bool // turn on debug messages
Dir string // directory to read magefiles from
WorkDir string // directory where magefiles will run
Force bool // forces recreation of the compiled binary
Verbose bool // tells the magefile to print out log statements
List bool // tells the magefile to print out a list of targets
Expand Down Expand Up @@ -176,7 +177,8 @@ func Parse(stderr, stdout io.Writer, args []string) (inv Invocation, cmd Command
fs.BoolVar(&inv.Help, "h", false, "show this help")
fs.DurationVar(&inv.Timeout, "t", 0, "timeout in duration parsable format (e.g. 5m30s)")
fs.BoolVar(&inv.Keep, "keep", false, "keep intermediate mage files around after running")
fs.StringVar(&inv.Dir, "d", ".", "run magefiles in the given directory")
fs.StringVar(&inv.Dir, "d", ".", "directory to read magefiles from")
fs.StringVar(&inv.WorkDir, "C", inv.Dir, "working directory where magefiles will run")
fs.StringVar(&inv.GoCmd, "gocmd", mg.GoCmd(), "use the given go binary to compile the output")
fs.StringVar(&inv.GOOS, "goos", "", "set GOOS for binary produced with -compile")
fs.StringVar(&inv.GOARCH, "goarch", "", "set GOARCH for binary produced with -compile")
Expand Down Expand Up @@ -210,7 +212,9 @@ Commands:

Options:
-d <string>
run magefiles in the given directory (default ".")
directory to read magefiles from (default ".")
-C <string>
working directory where magefiles will run (default -d value)
-debug turn on debug messages
-h show description of a target
-f force recreation of compiled magefile
Expand Down Expand Up @@ -297,6 +301,9 @@ func Invoke(inv Invocation) int {
if inv.Dir == "" {
inv.Dir = "."
}
if inv.WorkDir == "" {
inv.WorkDir = inv.Dir
}
if inv.CacheDir == "" {
inv.CacheDir = mg.CacheDir()
}
Expand Down Expand Up @@ -617,6 +624,9 @@ func RunCompiled(inv Invocation, exePath string, errlog *log.Logger) int {
c.Stdout = inv.Stdout
c.Stdin = inv.Stdin
c.Dir = inv.Dir
if inv.WorkDir != inv.Dir {
c.Dir = inv.WorkDir
}
// intentionally pass through unaltered os.Environ here.. your magefile has
// to deal with it.
c.Env = os.Environ()
Expand Down
24 changes: 24 additions & 0 deletions mage/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,30 @@ func TestSetDir(t *testing.T) {
}
}

func TestSetWorkingDir(t *testing.T) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
code := Invoke(Invocation{
Dir: "testdata/setworkdir",
WorkDir: "testdata/setworkdir/data",
Stdout: stdout,
Stderr: stderr,
Args: []string{"TestWorkingDir"},
})

if code != 0 {
t.Errorf(
"expected code 0, but got %d. Stdout:\n%s\nStderr:\n%s",
code, stdout, stderr,
)
}

expected := "file1.txt, file2.txt\n"
if out := stdout.String(); out != expected {
t.Fatalf("expected list of files to be %q, but was %q", expected, out)
}
}

// Test the timeout option
func TestTimeout(t *testing.T) {
stderr := &bytes.Buffer{}
Expand Down
1 change: 1 addition & 0 deletions mage/testdata/setworkdir/data/file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lorem ipsum 1
1 change: 1 addition & 0 deletions mage/testdata/setworkdir/data/file2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lorem ipsum 2
23 changes: 23 additions & 0 deletions mage/testdata/setworkdir/magefile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//+build mage

package main

import (
"fmt"
"io/ioutil"
"strings"
)

func TestWorkingDir() error {
files, err := ioutil.ReadDir(".")
if err != nil {
return err
}
var out []string
for _, f := range files {
out = append(out, f.Name())
}

fmt.Println(strings.Join(out, ", "))
return nil
}
4 changes: 3 additions & 1 deletion site/content/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ Commands:

Options:
-d <string>
run magefiles in the given directory (default ".")
directory to read magefiles from (default ".")
-C <string>
working directory where magefiles will run (default -d value)
-debug turn on debug messages
-h show description of a target
-f force recreation of compiled magefile
Expand Down