Skip to content

Commit

Permalink
Prepend to path should be in one place
Browse files Browse the repository at this point in the history
One version of PrependToPath function, used by buildpacks.
  • Loading branch information
Ridai Govinda Pombo committed Jun 2, 2020
1 parent be189e7 commit 4cceb0b
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 27 deletions.
4 changes: 2 additions & 2 deletions buildpacks/anaconda.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"path/filepath"

. "github.com/yourbase/yb/plumbing"
"github.com/yourbase/yb/plumbing/log"
"github.com/yourbase/yb/runtime"
. "github.com/yourbase/yb/types"
Expand Down Expand Up @@ -127,7 +126,8 @@ func (bt AnacondaBuildTool) DownloadUrl() string {

func (bt AnacondaBuildTool) Setup() error {
installDir := bt.InstallDir()
PrependToPath(filepath.Join(installDir, "bin"))
t := bt.spec.InstallTarget
t.PrependToPath(filepath.Join(installDir, "bin"))
setupDir := bt.spec.PackageDir

for _, cmd := range []string{
Expand Down
4 changes: 2 additions & 2 deletions buildpacks/dart.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"path/filepath"
"strings"

. "github.com/yourbase/yb/plumbing"
"github.com/yourbase/yb/plumbing/log"
. "github.com/yourbase/yb/types"
)
Expand Down Expand Up @@ -80,7 +79,8 @@ func (bt DartBuildTool) DartDir() string {
func (bt DartBuildTool) Setup() error {
dartDir := bt.DartDir()
cmdPath := filepath.Join(dartDir, "bin")
PrependToPath(cmdPath)
t := bt.spec.InstallTarget
t.PrependToPath(cmdPath)

return nil
}
Expand Down
6 changes: 4 additions & 2 deletions buildpacks/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ func (bt GolangBuildTool) Setup() error {
cmdPath := filepath.Join(golangDir, "bin")
t.PrependToPath(cmdPath)
for _, pathElement := range goPathElements {
pathBinDir := filepath.Join(pathElement, "bin")
t.PrependToPath(pathBinDir)
if pathElement != "" {
pathBinDir := filepath.Join(pathElement, "bin")
t.PrependToPath(pathBinDir)
}
}

log.Infof("Setting GOROOT to %s", golangDir)
Expand Down
10 changes: 6 additions & 4 deletions buildpacks/homebrew.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ func (bt HomebrewBuildTool) PackagePrefix(packageString string) (string, error)

func (bt HomebrewBuildTool) PackageInstalled() bool {
prefix, err := bt.PackagePrefix(bt.PackageVersionString())
t := bt.spec.InstallTarget
if err != nil {
return false
}

if prefix != "" {
return PathExists(prefix)
return t.PathExists(prefix)
}

return false
Expand Down Expand Up @@ -228,6 +229,7 @@ func (bt HomebrewBuildTool) InstallLinux() error {
}

func (bt HomebrewBuildTool) Setup() error {
t := bt.spec.InstallTarget
if bt.IsPackage() {
prefixPath, err := bt.PackagePrefix(bt.PackageVersionString())
if err != nil {
Expand All @@ -236,12 +238,12 @@ func (bt HomebrewBuildTool) Setup() error {
binDir := filepath.Join(prefixPath, "bin")
sbinDir := filepath.Join(prefixPath, "sbin")

PrependToPath(binDir)
PrependToPath(sbinDir)
t.PrependToPath(binDir)
t.PrependToPath(sbinDir)
} else {
brewDir := bt.HomebrewDir()
brewBinDir := filepath.Join(brewDir, "bin")
PrependToPath(brewBinDir)
t.PrependToPath(brewBinDir)
brewLibDir := filepath.Join(brewDir, "lib")
runtime.SetEnv("LD_LIBRARY_PATH", brewLibDir)
}
Expand Down
4 changes: 2 additions & 2 deletions buildpacks/protoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"path/filepath"
"strings"

. "github.com/yourbase/yb/plumbing"
"github.com/yourbase/yb/plumbing/log"
. "github.com/yourbase/yb/types"
)
Expand Down Expand Up @@ -81,9 +80,10 @@ func (bt ProtocBuildTool) ProtocDir() string {

func (bt ProtocBuildTool) Setup() error {
protocDir := bt.ProtocDir()
t := bt.spec.InstallTarget

cmdPath := filepath.Join(protocDir, "bin")
PrependToPath(cmdPath)
t.PrependToPath(cmdPath)
return nil
}

Expand Down
3 changes: 1 addition & 2 deletions buildpacks/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ func (bt PythonBuildTool) Setup() error {
condaDir := bt.AnacondaInstallDir()
envDir := bt.EnvironmentDir()
t := bt.spec.InstallTarget
t.PrependToPath(filepath.Join(condaDir, "bin"))

if t.PathExists(envDir) {
log.Infof("environment installed in %s", envDir)
} else {
newPath := fmt.Sprintf("PATH=%s:%s", filepath.Join(condaDir, "bin"), t.GetDefaultPath())
setupDir := bt.spec.PackageDir
condaBin := filepath.Join(condaDir, "bin", "conda")

Expand All @@ -146,7 +146,6 @@ func (bt PythonBuildTool) Setup() error {
Command: cmd,
Interactive: false,
Directory: setupDir,
Environment: []string{newPath},
}

if err := t.Run(p); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions buildpacks/yarn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"path/filepath"

. "github.com/yourbase/yb/plumbing"
"github.com/yourbase/yb/plumbing/log"
. "github.com/yourbase/yb/types"
)
Expand Down Expand Up @@ -75,9 +74,10 @@ func (bt YarnBuildTool) Install() error {
}

func (bt YarnBuildTool) Setup() error {
t := bt.spec.InstallTarget
yarnDir := bt.YarnDir()
cmdPath := filepath.Join(yarnDir, "bin")
PrependToPath(cmdPath)
t.PrependToPath(cmdPath)

return nil
}
11 changes: 0 additions & 11 deletions plumbing/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,6 @@ import (

const SNIFFLEN = 8000

func PrependToPath(dir string) {
currentPath := os.Getenv("PATH")
// Only prepend if it's not already the head; presume that
// whomever asked for this wants to be at the front so it's okay if it's
// duplicated later
if !strings.HasPrefix(currentPath, dir) {
newPath := fmt.Sprintf("%s:%s", dir, currentPath)
os.Setenv("PATH", newPath)
}
}

func ConfigFilePath(filename string) string {
u, _ := user.Current()
configDir := filepath.Join(u.HomeDir, ".config", "yb")
Expand Down

0 comments on commit 4cceb0b

Please sign in to comment.