Skip to content

Commit 4c6bb94

Browse files
committed
shell
1 parent 757bef4 commit 4c6bb94

File tree

6 files changed

+92
-37
lines changed

6 files changed

+92
-37
lines changed

internal/shell/sh/bash.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,20 @@ func (b *BashShell) WriteVMEnvToShell() {
2929
installPath := cnf.GetVMRWorkDir()
3030
vmEnvConfPath := b.VMEnvConfPath()
3131

32-
content, _ := os.ReadFile(vmEnvConfPath)
33-
oldEnvStr := strings.TrimSpace(string(content))
32+
// content, _ := os.ReadFile(vmEnvConfPath)
33+
// oldEnvStr := strings.TrimSpace(string(content))
3434
envStr := fmt.Sprintf(vmEnvZsh, FormatPathString(installPath))
35-
if !strings.Contains(oldEnvStr, envStr) {
36-
if oldEnvStr != "" {
37-
envStr = envStr + "\n" + oldEnvStr
38-
}
39-
_ = os.WriteFile(vmEnvConfPath, []byte(envStr), ModePerm)
40-
}
35+
vmrEnvPath := fmt.Sprintf("export PATH=%s:$PATH", FormatPathString(installPath))
36+
UpdateVMRShellFile(vmEnvConfPath, vmrEnvPath, envStr)
37+
// if !strings.Contains(oldEnvStr, envStr) {
38+
// if oldEnvStr != "" {
39+
// envStr = envStr + "\n" + oldEnvStr
40+
// }
41+
// _ = os.WriteFile(vmEnvConfPath, []byte(envStr), ModePerm)
42+
// }
4143

4244
shellConfig := b.ConfPath()
43-
content, _ = os.ReadFile(shellConfig)
45+
content, _ := os.ReadFile(shellConfig)
4446
data := string(content)
4547

4648
sourceStr := fmt.Sprintf(shellContent, VMDisableEnvName, FormatPathString(vmEnvConfPath))

internal/shell/sh/fish.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,21 @@ func (f *FishShell) WriteVMEnvToShell() {
6262
installPath := cnf.GetVMRWorkDir()
6363
vmEnvConfPath := f.VMEnvConfPath()
6464

65-
content, _ := os.ReadFile(vmEnvConfPath)
66-
oldEnvStr := strings.TrimSpace(string(content))
65+
// content, _ := os.ReadFile(vmEnvConfPath)
66+
// oldEnvStr := strings.TrimSpace(string(content))
6767
envStr := fmt.Sprintf(vmEnvFish, FormatPathString(installPath))
68-
if !strings.Contains(oldEnvStr, envStr) {
69-
if oldEnvStr != "" {
70-
envStr = envStr + "\n" + oldEnvStr
71-
}
72-
_ = os.WriteFile(vmEnvConfPath, []byte(envStr), ModePerm)
73-
}
68+
69+
vmrEnvPath := fmt.Sprintf("fish_add_path --global %s", FormatPathString(installPath))
70+
UpdateVMRShellFile(vmEnvConfPath, vmrEnvPath, envStr)
71+
// if !strings.Contains(oldEnvStr, envStr) {
72+
// if oldEnvStr != "" {
73+
// envStr = envStr + "\n" + oldEnvStr
74+
// }
75+
// _ = os.WriteFile(vmEnvConfPath, []byte(envStr), ModePerm)
76+
// }
7477

7578
shellConfig := f.ConfPath()
76-
content, _ = os.ReadFile(shellConfig)
79+
content, _ := os.ReadFile(shellConfig)
7780
data := string(content)
7881

7982
sourceStr := fmt.Sprintf(fishShellContent, VMDisableEnvName, FormatPathString(vmEnvConfPath))

internal/shell/sh/shell.go

+27
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sh
33
import (
44
"io/fs"
55
"os"
6+
"regexp"
67
"runtime"
78
"strings"
89

@@ -39,3 +40,29 @@ func FormatPathString(p string) (formattedPath string) {
3940
}
4041
return
4142
}
43+
44+
/*
45+
Update vmr.sh or vmr.fish
46+
*/
47+
var ShellRegExp = regexp.MustCompile(`# cd hook start[\w\W]+# cd hook end`)
48+
49+
func UpdateVMRShellFile(fPath, vmrPathEnv, newHookContent string) {
50+
oldData, _ := os.ReadFile(fPath)
51+
oldContent := string(oldData)
52+
if oldContent == "" {
53+
os.WriteFile(fPath, []byte(newHookContent), ModePerm)
54+
return
55+
}
56+
oldHookContent := ShellRegExp.FindString(oldContent)
57+
58+
if !strings.Contains(oldHookContent, vmrPathEnv) {
59+
oldContent = strings.ReplaceAll(oldContent, vmrPathEnv, "")
60+
}
61+
62+
if oldHookContent != "" {
63+
oldContent = strings.ReplaceAll(oldContent, oldHookContent, newHookContent)
64+
} else {
65+
oldContent = newHookContent + "\n" + oldContent
66+
}
67+
_ = os.WriteFile(fPath, []byte(strings.TrimSpace(oldContent)), ModePerm)
68+
}

internal/shell/sh/zsh.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,19 @@ func (z *ZshShell) WriteVMEnvToShell() {
6969
installPath := cnf.GetVMRWorkDir()
7070
vmEnvConfPath := z.VMEnvConfPath()
7171

72-
content, _ := os.ReadFile(vmEnvConfPath)
73-
oldEnvStr := strings.TrimSpace(string(content))
72+
// content, _ := os.ReadFile(vmEnvConfPath)
73+
// oldEnvStr := strings.TrimSpace(string(content))
7474
envStr := fmt.Sprintf(vmEnvZsh, FormatPathString(installPath))
75-
if !strings.Contains(oldEnvStr, envStr) {
76-
if oldEnvStr != "" {
77-
envStr = envStr + "\n" + oldEnvStr
78-
}
79-
_ = os.WriteFile(vmEnvConfPath, []byte(envStr), ModePerm)
80-
}
75+
vmrEnvPath := fmt.Sprintf("export PATH=%s:$PATH", FormatPathString(installPath))
76+
UpdateVMRShellFile(vmEnvConfPath, vmrEnvPath, envStr)
77+
// if !strings.Contains(oldEnvStr, envStr) {
78+
// if oldEnvStr != "" {
79+
// envStr = envStr + "\n" + oldEnvStr
80+
// }
81+
// _ = os.WriteFile(vmEnvConfPath, []byte(envStr), ModePerm)
82+
// }
8183
shellConfig := z.ConfPath()
82-
content, _ = os.ReadFile(shellConfig)
84+
content, _ := os.ReadFile(shellConfig)
8385
data := string(content)
8486

8587
sourceStr := fmt.Sprintf(shellContent, VMDisableEnvName, FormatPathString(vmEnvConfPath))

internal/shell/win.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ const (
2222
PathEnvName string = "path"
2323
)
2424

25+
const oldPwsHook string = `function cdhook {
26+
$TRUE_FALSE=(Test-Path $args[0])
27+
if ( $TRUE_FALSE -eq "True" )
28+
{
29+
chdir $args[0]
30+
vmr use -E
31+
}
32+
}
33+
34+
function vmrsource {
35+
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
36+
}
37+
38+
Set-Alias -Name cd -Option AllScope -Value cdhook
39+
Set-Alias -Name source -Value vmrsource`
40+
2541
// PowershellHook for Powershell
2642
const PowershellHook string = `# cd hook start
2743
function cdhook {
@@ -128,7 +144,9 @@ func (s *Shell) cdHook() {
128144
return
129145
}
130146

131-
if content != "" {
147+
if strings.Contains(content, oldPwsHook) {
148+
content = strings.ReplaceAll(content, oldPwsHook, PowershellHook)
149+
} else if content != "" {
132150
content = fmt.Sprintf("%s\n%s", PowershellHook, content)
133151
} else {
134152
content = PowershellHook

main.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222
package main
2323

2424
import (
25+
"fmt"
2526
"os"
26-
27-
"github.com/gvcgo/version-manager/internal/cnf"
28-
"github.com/gvcgo/version-manager/internal/shell"
29-
"github.com/gvcgo/version-manager/internal/tui/cmds"
27+
"regexp"
3028
)
3129

3230
func main() {
@@ -72,12 +70,17 @@ func main() {
7270

7371
// test vmr
7472

75-
sh := shell.NewShell()
76-
sh.WriteVMEnvToShell()
77-
os.Setenv(cnf.VMRLocalProxyEnv, "http://localhost:2023")
78-
ll := cmds.NewTUI()
79-
ll.ListSDKName()
73+
// sh := shell.NewShell()
74+
// sh.WriteVMEnvToShell()
75+
// os.Setenv(cnf.VMRLocalProxyEnv, "http://localhost:2023")
76+
// ll := cmds.NewTUI()
77+
// ll.ListSDKName()
78+
79+
var ShellRegExp = regexp.MustCompile(`# cd hook start[\w\W]+# cd hook end`)
8080

81+
content, _ := os.ReadFile("/home/moqsien/.vmr/vmr.sh")
82+
s := ShellRegExp.FindString(string(content))
83+
fmt.Println(s)
8184
// _, err := gutils.ExecuteSysCommand(
8285
// true,
8386
// "",

0 commit comments

Comments
 (0)