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

Fix/windows subtool suffix #150

Merged
merged 6 commits into from
Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ os:
language: go

go:
- 1.7
- 1.9

env:
- TEST_VERBOSE=1
Expand Down
37 changes: 33 additions & 4 deletions gxutil/pm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os/exec"
"os/user"
"path/filepath"
"runtime"
"strings"
"time"

Expand All @@ -23,9 +24,14 @@ const GxVersion = "0.12.1"
const PkgFileName = "package.json"

var installPathsCache map[string]string
var binarySuffix string

func init() {
installPathsCache = make(map[string]string)

if runtime.GOOS == "windows" {
binarySuffix = ".exe"
}
}

type PM struct {
Expand Down Expand Up @@ -668,15 +674,19 @@ func getSubtoolPath(env string) (string, error) {
return "", nil
}

binname := "gx-" + env
binname := "gx-" + env + binarySuffix
_, err := exec.LookPath(binname)
if err != nil {
if !strings.Contains(err.Error(), "file not found") {
if eErr, ok := err.(*exec.Error); ok {
if eErr.Err != exec.ErrNotFound {
return "", err
}
} else {
return "", err
}

if strings.HasSuffix(os.Args[0], "/gx") {
nearBin := os.Args[0] + "-" + env
if calledWithPathSeparator() {
nearBin := strings.TrimSuffix(os.Args[0], binarySuffix) + "-" + env + binarySuffix
if _, err := os.Stat(nearBin); err != nil {
VLog("subtool_exec: No gx helper tool found for", env)
return "", nil
Expand All @@ -690,6 +700,25 @@ func getSubtoolPath(env string) (string, error) {
return binname, nil
}

func calledWithPathSeparator() bool {
trimmedArg := strings.TrimSuffix(os.Args[0], binarySuffix)
if trimmedArg == "gx" {
return false
}

if runtime.GOOS == "windows" {
if strings.HasSuffix(trimmedArg, (string(os.PathSeparator)+"gx")) || strings.HasSuffix(trimmedArg, "/gx") {
return true
}
} else {
if strings.HasSuffix(trimmedArg, (string(os.PathSeparator) + "gx")) {
return true
}
}

return false
}

func TryRunHook(hook, env string, req bool, args ...string) error {
binname, err := getSubtoolPath(env)
if err != nil {
Expand Down