Skip to content

Commit

Permalink
tidy path envs for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
moqsien committed Jan 4, 2025
1 parent ee7d9d1 commit 53fe309
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 32 deletions.
31 changes: 0 additions & 31 deletions .github/workflows/go.yml

This file was deleted.

71 changes: 71 additions & 0 deletions internal/installer/post/rust.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package post

import (
"fmt"
"os"
"path/filepath"
"runtime"

"github.com/gvcgo/goutils/pkgs/gutils"
"github.com/gvcgo/version-manager/internal/cnf"
"github.com/gvcgo/version-manager/internal/download"
"github.com/gvcgo/version-manager/internal/utils"
)

func init() {
RegisterPostInstallHandler(RustSdkName, PostInstallForRust)
}

const (
RustSdkName = "rust"
)

var (
InstallRustupCmd []string = []string{
"vmr",
"use",
"rustup@latest",
}
RustupDir = filepath.Join(cnf.GetVersionsDir(), "rustup_versions", "rustup-latest")
)

/*
post-installation handler for Rust.
*/
func PostInstallForRust(versionName string, version download.Item) {
versionDir := cnf.GetVersionsDir()
d := filepath.Join(versionDir, fmt.Sprintf("%s_versions", RustSdkName))
rustInstallDir := filepath.Join(d, fmt.Sprintf("%s-%s", RustSdkName, versionName))
binPath := filepath.Join(rustInstallDir, "Library", "bin")

rustupInitPath := filepath.Join(RustupDir, "rustup-init")
newName := "rustup"
if runtime.GOOS == gutils.Windows {
rustupInitPath = filepath.Join(RustupDir, "rustup-init.exe")
newName = "rustup.exe"
}

homeDir, _ := os.UserHomeDir()

if ok, _ := gutils.PathIsExist(rustupInitPath); !ok {
task := utils.NewSysCommandRunner(
true,
homeDir,
InstallRustupCmd...,
)
if err := task.Run(); err != nil {
return
}
}

// install rustup to cargo bin path.
if _, err := gutils.CopyFile(rustupInitPath, filepath.Join(binPath, newName)); err == nil && runtime.GOOS != gutils.Windows {
gutils.ExecuteSysCommand(
true,
homeDir,
"chmox",
"+x",
filepath.Join(binPath, newName),
)
}
}
2 changes: 1 addition & 1 deletion internal/self/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func InstallSelf() {
sh := shell.NewShell()
sh.WriteVMEnvToShell()
if runtime.GOOS == gutils.Windows {
sh.SetPath(cnf.GetVMRWorkDir())
sh.SetPath(vmrWorkDir)
}

// Generate update script.
Expand Down
27 changes: 27 additions & 0 deletions internal/shell/win.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"unsafe"

"github.com/gvcgo/goutils/pkgs/gutils"
"github.com/gvcgo/version-manager/internal/cnf"
"github.com/gvcgo/version-manager/internal/shell/sh"

"github.com/gvcgo/goutils/pkgs/gtea/gprint"
Expand Down Expand Up @@ -65,6 +66,28 @@ if ( "" -eq "$env:VMR_CD_INIT" )

var _ Sheller = (*Shell)(nil)

var VersionsDir = cnf.GetVersionsDir()

const (
VMR_VERSIONS_ENV = "VMR_VERSIONS"
VMR_VERSIONS_PREFIX = `%VMR_VERSIONS%`
)

func SetVMRVersionsEnv() {
if os.Getenv(VMR_VERSIONS_ENV) == "" {
shell := NewShell()
shell.SetEnv(VMR_VERSIONS_ENV, VersionsDir)
}
}

func TidyWindowsPathEnv(pathStr string) (newPath string) {
newPath = pathStr
if strings.Contains(pathStr, VersionsDir) && os.Getenv(VMR_VERSIONS_ENV) != "" {
newPath = strings.ReplaceAll(pathStr, VersionsDir, VMR_VERSIONS_PREFIX)
}
return
}

type Shell struct {
sh.Sheller
Key registry.Key
Expand Down Expand Up @@ -209,6 +232,8 @@ func (s *Shell) SetPath(path string) {
gprint.PrintError("Windows registry key is closed.")
return
}
SetVMRVersionsEnv()
path = TidyWindowsPathEnv(path)

oldPathValue, _, err := s.Key.GetStringValue(PathEnvName)
if err != nil {
Expand All @@ -231,6 +256,8 @@ func (s *Shell) UnsetPath(path string) {
gprint.PrintError("Windows registry key is closed.")
return
}
SetVMRVersionsEnv()
path = TidyWindowsPathEnv(path)

oldPathValue, _, err := s.Key.GetStringValue(PathEnvName)
if err != nil {
Expand Down

0 comments on commit 53fe309

Please # to comment.