Skip to content

Commit

Permalink
Expand environment variables on "dir", "sources" and "generates"
Browse files Browse the repository at this point in the history
So a path like this works: $GOPATH/src/github.com/go-task/task

Allowing of "~" was also implemented. See #74 and baac067

Fixes #116
  • Loading branch information
andreynering committed Jun 16, 2018
1 parent a830dba commit 102f8ab
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ vars:
./internal/compiler/v2
./internal/execext
./internal/logger
./internal/osext
./internal/output
./internal/status
./internal/taskfile
Expand Down
22 changes: 22 additions & 0 deletions internal/osext/osext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package osext

import (
"os"

"github.com/mitchellh/go-homedir"
)

// Expand is an improved version of os.ExpandEnv,
// that not only expand enrionment variable ($GOPATH/src/github.com/...)
// but also expands "~" as the home directory.
func Expand(s string) (string, error) {

This comment has been minimized.

Copy link
@mvdan

mvdan Jun 16, 2018

In case you didn't know, you can replace this with mvdan.cc/sh/shell.Expand(s, nil) :) It's like os.ExpandEnv, but it supports all the shell expansions you're used to. This includes complex parameter substitutions like ${foo:-bar}, but also ~/foo.

This comment has been minimized.

Copy link
@andreynering

andreynering Jun 16, 2018

Author Member

Nice!

I'll look at it. Thanks!

This comment has been minimized.

Copy link
@andreynering

andreynering Jun 24, 2018

Author Member

@mvdan

Hmm... Seems that it's not working as expected?

could not stat: stat /Users/andrey/repos/GOPATH/src/github.com/go-task/task/~: no such file or directory

While trying to switch the current implementation with shell.Expand.

This comment has been minimized.

Copy link
@mvdan

mvdan Jun 24, 2018

That's unexpected - will have a look, thanks.

This comment has been minimized.

Copy link
@mvdan

mvdan Jun 24, 2018

Should be fixed now - please see mvdan/sh@1b81de4.

s = os.ExpandEnv(s)

var err error
s, err = homedir.Expand(s)
if err != nil {
return "", err
}

return s, nil
}
5 changes: 3 additions & 2 deletions internal/status/glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"path/filepath"
"sort"

"github.com/go-task/task/internal/osext"

"github.com/mattn/go-zglob"
"github.com/mitchellh/go-homedir"
)

func glob(dir string, globs []string) (files []string, err error) {
for _, g := range globs {
if !filepath.IsAbs(g) {
g = filepath.Join(dir, g)
}
g, err = homedir.Expand(g)
g, err = osext.Expand(g)
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package task
import (
"path/filepath"

"github.com/go-task/task/internal/osext"
"github.com/go-task/task/internal/taskfile"
"github.com/go-task/task/internal/templater"

"github.com/mitchellh/go-homedir"
)

var (
Expand Down Expand Up @@ -41,7 +40,7 @@ func (e *Executor) CompiledTask(call taskfile.Call) (*taskfile.Task, error) {
Method: r.Replace(origTask.Method),
Prefix: r.Replace(origTask.Prefix),
}
new.Dir, err = homedir.Expand(new.Dir)
new.Dir, err = osext.Expand(new.Dir)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 102f8ab

Please sign in to comment.