Skip to content

Commit

Permalink
Support a custom includes path (#25)
Browse files Browse the repository at this point in the history
* Support a custom include path
  • Loading branch information
codekoala authored and zph committed Sep 21, 2019
1 parent 480af4f commit 5261f65
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/mmake/mmake.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func main() {
// ensure deps are installed
i := installer.New(installer.Config{
Resolver: resolver.NewUniversalResolver(),
Destination: "/usr/local/include",
Destination: resolver.GetIncludePath(os.Args[1:]),
Log: log.Log,
ForceUpdate: cmd == "update",
})
Expand Down
5 changes: 3 additions & 2 deletions help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/pkg/errors"

"github.com/tj/mmake/parser"
"github.com/tj/mmake/resolver"
)

// OutputAllShort outputs all short help representations to the given writer.
Expand Down Expand Up @@ -57,8 +58,8 @@ func OutputAllLong(r io.Reader, w io.Writer, targets []string) error {

// getComments parses, filters, and sorts all comment nodes.
func getComments(r io.Reader, targets []string) ([]parser.Comment, error) {
nodes, err := parser.ParseRecursive(r, "/usr/local/include")

nodes, err := parser.ParseRecursive(r, resolver.IncludePath)
if err != nil {
return nil, errors.Wrap(err, "parsing")
}
Expand Down
24 changes: 24 additions & 0 deletions resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"io"
)

// DefaultIncludePath is the default directory where make includes reside and
// to which mmake will download remote includes.
const DefaultIncludePath = "/usr/local/include"

// Errors.
var (
// ErrNotFound is returned when a non-local lookup fails.
Expand All @@ -15,10 +19,30 @@ var (
ErrNotSupported = errors.New("unsupported")
)

// IncludePath is the directory where make includes reside and to which mmake
// will download remote includes for the duration of a particular execution
// of mmake.
var IncludePath = DefaultIncludePath

// Interface for resolving and fetching includes.
type Interface interface {
// Get must return the contents of the file,
// ErrNotFound when the file is not found,
// or an error.
Get(path string) (io.ReadCloser, error)
}

// GetIncludePath allows the include path to be overridden.
func GetIncludePath(args []string) string {
for idx, arg := range args {
if arg == "-I" && len(args) > idx+1 {
IncludePath = args[idx+1]
break
} else if arg[:2] == "-I" && len(arg) > 2 {
IncludePath = arg[2:]
break
}
}

return IncludePath
}
27 changes: 27 additions & 0 deletions resolver/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,33 @@ import (
"github.com/tj/mmake/resolver"
)

func TestGetIncludePath(t *testing.T) {
var cases = []struct {
Args []string
Expected string
}{
{[]string{}, resolver.DefaultIncludePath},
{[]string{"update", "-I", "./relative/path"}, "./relative/path"},
{[]string{"update", "-I./other/path"}, "./other/path"},
{[]string{"update", "-I./other/path", "-I", "multiple/"}, "./other/path"},
{[]string{"-I", "multiple/"}, "multiple/"},
{[]string{"-I"}, resolver.DefaultIncludePath},
{[]string{"update"}, resolver.DefaultIncludePath},
}

for _, c := range cases {
t.Run(c.Expected, func(t *testing.T) {
// always reset the global IncludePath to the default for consistent test results
resolver.IncludePath = resolver.DefaultIncludePath

out := resolver.GetIncludePath(c.Args)
if out != c.Expected {
t.Errorf("expected %q, got %q", c.Expected, out)
}
})
}
}

func TestGithub(t *testing.T) {
resolver := resolver.NewGithubResolver()

Expand Down

0 comments on commit 5261f65

Please sign in to comment.