Skip to content

Commit

Permalink
Fix subtool detection on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
djdv committed Jan 9, 2018
1 parent 252e3ab commit 41bf6c7
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions gxutil/pm.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,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 @@ -669,20 +674,15 @@ func getSubtoolPath(env string) (string, error) {
return "", nil
}

var suffix string
if runtime.GOOS == "windows" {
suffix = ".exe"
}

binname := "gx-" + env + suffix
binname := "gx-" + env + binarySuffix
_, err := exec.LookPath(binname)
if err != nil {
if !strings.Contains(err.Error(), "file not found") {
return "", err
}

if strings.HasSuffix(os.Args[0], "/gx") {
nearBin := os.Args[0] + "-" + env + suffix
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 @@ -696,6 +696,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

0 comments on commit 41bf6c7

Please sign in to comment.