Skip to content

Commit

Permalink
feat: support GitBash shell
Browse files Browse the repository at this point in the history
  • Loading branch information
aooohan authored and ShizheChang committed Mar 14, 2024
1 parent 464672d commit 16ca585
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 40 deletions.
2 changes: 1 addition & 1 deletion cmd/commands/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ func activateCmd(ctx *cli.Context) error {
exportEnvs[k] = v
}

os.Setenv(env.HookFlag, name)
exportEnvs[env.HookFlag] = &name
originPath := os.Getenv("PATH")
exportEnvs[env.PathFlag] = &originPath

sdkPaths := envKeys.Paths
if len(sdkPaths) != 0 {
paths := manager.EnvManager.Paths(append(sdkPaths[:], originPath))
Expand Down
4 changes: 3 additions & 1 deletion cmd/commands/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ package commands
import (
"encoding/json"
"fmt"
"os"

"github.com/urfave/cli/v2"
"github.com/version-fox/vfox/internal"
"github.com/version-fox/vfox/internal/env"
"github.com/version-fox/vfox/internal/shell"
"os"
)

var Env = &cli.Command{
Expand All @@ -50,6 +51,7 @@ var Env = &cli.Command{
}

func envCmd(ctx *cli.Context) error {
// TODO env not effect for git bash
if ctx.IsSet("json") {
type SDKs map[string]map[string]*string
data := struct {
Expand Down
14 changes: 13 additions & 1 deletion internal/env/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@

package env

import "os"
import (
"os"
"strconv"
)

const (
HookFlag = "__VFOX_SHELL"
PathFlag = "__VFOX_ORIG_PATH"
PidFlag = "__VFOX_PID"
)

func IsHookEnv() bool {
Expand All @@ -30,3 +34,11 @@ func IsHookEnv() bool {
func GetOrigPath() string {
return os.Getenv(PathFlag)
}

func GetPid() int {
if pid := os.Getenv(PidFlag); pid != "" {
p, _ := strconv.Atoi(pid) // Convert pid from string to int
return p
}
return os.Getppid()
}
39 changes: 29 additions & 10 deletions internal/env/windows_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ package env

import (
"errors"
"fmt"
"os"
"strings"
"syscall"
"unsafe"

"github.com/version-fox/vfox/internal/util"
"golang.org/x/sys/windows/registry"
)

Expand Down Expand Up @@ -72,11 +74,18 @@ func (w *windowsEnvManager) Flush() (err error) {
customPathSet[path] = struct{}{}
}
pathValue := strings.Join(customPaths, ";")
if err = w.Load("VERSION_FOX_PATH", pathValue); err != nil {
if err = w.Load((&Envs{
Variables: Vars{
"VERSION_FOX_PATH": &pathValue,
},
})); err != nil {
return err
}
} else {
_ = w.Remove("VERSION_FOX_PATH")
_ = w.Remove(&Envs{
Variables: Vars{
"VERSION_FOX_PATH": nil,
}})
}
// user env
oldPath, success := w.Get("PATH")
Expand Down Expand Up @@ -119,20 +128,20 @@ func (w *windowsEnvManager) Flush() (err error) {

func (w *windowsEnvManager) Load(envs *Envs) error {
for k, v := range envs.Variables {
err := os.Setenv(key, value)
err := os.Setenv(k, *v)
if err != nil {
return err
}
err = w.key.SetStringValue(key, value)
err = w.key.SetStringValue(k, *v)
if err != nil {
return err
}
}
for _, path := range envs.Paths {
_, ok := w.pathMap[k]
_, ok := w.pathMap[path]
if !ok {
w.pathMap[k] = struct{}{}
w.paths = append(w.paths, k)
w.pathMap[path] = struct{}{}
w.paths = append(w.paths, path)
}
}
return nil
Expand All @@ -148,7 +157,7 @@ func (w *windowsEnvManager) Get(key string) (string, bool) {

func (w *windowsEnvManager) Remove(envs *Envs) error {
for k, _ := range envs.Variables {
if key == "PATH" {
if k == "PATH" {
return fmt.Errorf("can not remove PATH variable")
}
_ = w.key.DeleteValue(k)
Expand Down Expand Up @@ -187,8 +196,18 @@ func (w *windowsEnvManager) broadcastEnvironment() error {
}

func (w *windowsEnvManager) Paths(paths []string) string {
set := util.NewSortedSetWithSlice[string](paths)
return strings.Join(set.Slice(), ";")
if os.Getenv(HookFlag) == "bash" {
set := util.NewSortedSet[string]()
for _, p := range paths {
for _, pp := range strings.Split(p, ";") {
set.Add(pp)
}
}
return strings.Join(set.Slice(), ":")
} else {
set := util.NewSortedSetWithSlice(paths)
return strings.Join(set.Slice(), ";")
}
}

func NewEnvManager(vfConfigPath string) (Manager, error) {
Expand Down
3 changes: 2 additions & 1 deletion internal/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"
"path/filepath"

"github.com/version-fox/vfox/internal/env"
"github.com/version-fox/vfox/internal/util"
)

Expand Down Expand Up @@ -59,7 +60,7 @@ func newPathMeta() (*PathMeta, error) {
if err != nil {
return nil, err
}
pid := os.Getppid()
pid := env.GetPid()
timestamp := util.GetBeginOfToday()
name := fmt.Sprintf("%d-%d", timestamp, pid)
curTmpPath := filepath.Join(tmpPath, name)
Expand Down
3 changes: 3 additions & 0 deletions internal/shell/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import (
// Based on https://github.com/direnv/direnv/blob/master/internal/cmd/shell_bash.go
const bashHook = `
{{.EnvContent}}
export __VFOX_PID=$$;
_vfox_hook() {
local previous_exit_status=$?;
trap -- '' SIGINT;
Expand Down
54 changes: 28 additions & 26 deletions internal/shell/fish.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,35 @@ var Fish Shell = fish{}

const fishHook = `
{{.EnvContent}}
function __vfox_export_eval --on-event fish_prompt;
"{{.SelfPath}}" env -s fish | source;
if test "$vfox_fish_mode" != "disable_arrow";
function __vfox_cd_hook --on-variable PWD;
if test "$vfox_fish_mode" = "eval_after_arrow";
set -g __vfox_export_again 0;
else;
"{{.SelfPath}}" env -s fish | source;
end;
end;
end;
end;
function __vfox_export_eval_2 --on-event fish_preexec;
if set -q __vfox_export_again;
set -e __vfox_export_again;
"{{.SelfPath}}" env -s fish | source;
echo;
end;
functions --erase __vfox_cd_hook;
end;
function cleanup_on_exit --on-process-exit %self
"{{.SelfPath}}" env --cleanup
set __VFOX_PID %self;
function __vfox_export_eval --on-event fish_prompt;
"{{.SelfPath}}" env -s fish | source;
if test "$vfox_fish_mode" != "disable_arrow";
function __vfox_cd_hook --on-variable PWD;
if test "$vfox_fish_mode" = "eval_after_arrow";
set -g __vfox_export_again 0;
else;
"{{.SelfPath}}" env -s fish | source;
end;
end;
end;
end;
function __vfox_export_eval_2 --on-event fish_preexec;
if set -q __vfox_export_again;
set -e __vfox_export_again;
"{{.SelfPath}}" env -s fish | source;
echo;
end;
functions --erase __vfox_cd_hook;
end;
function cleanup_on_exit --on-process-exit %self
"{{.SelfPath}}" env --cleanup
end;
`

func (sh fish) Activate() (string, error) {
Expand Down
2 changes: 2 additions & 0 deletions internal/shell/powershell.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ var Pwsh Shell = pwsh{}

const hook = `
{{.EnvContent}}
$__VFOX_PID=$pid;
function prompt {
$export = &"{{.SelfPath}}" env -s pwsh;
if ($export) {
Expand Down
3 changes: 3 additions & 0 deletions internal/shell/zsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ var Zsh = zsh{}

const zshHook = `
{{.EnvContent}}
export __VFOX_PID=$$;
_vfox_hook() {
trap -- '' SIGINT;
eval "$("{{.SelfPath}}" env -s zsh)";
Expand Down

0 comments on commit 16ca585

Please sign in to comment.