Skip to content

Commit

Permalink
feat: add a soft link to current version of sdk
Browse files Browse the repository at this point in the history
Colleagues create the current directory. The current directory takes precedence.

path: $VFOX_HOME/cache/<sdk>/current
  • Loading branch information
aooohan committed May 11, 2024
1 parent 030ea23 commit 23b8163
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 114 deletions.
27 changes: 9 additions & 18 deletions cmd/commands/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package commands

import (
"fmt"
"github.com/version-fox/vfox/internal/logger"
"github.com/version-fox/vfox/internal/shim"
"os"
"strings"
"text/template"
Expand Down Expand Up @@ -64,36 +62,29 @@ func activateCmd(ctx *cli.Context) error {
if err != nil {
return err
}
envKeys, err := manager.EnvKeys(toolset.MultiToolVersions{
sdkEnvs, err := manager.EnvKeys(toolset.MultiToolVersions{
workToolVersion,
homeToolVersion,
})
if err != nil {
return err
}

sdkCurrentPaths := sdkEnvs.LinkCurrent(manager.PathMeta.CurTmpPath)

envKeys := sdkEnvs.ToEnvs()

exportEnvs := make(env.Vars)
for k, v := range envKeys.Variables {
exportEnvs[k] = v
}

// generate shims for current shell
if envKeys.Paths.Len() > 0 {
logger.Debugf("Generate shims for current shell, path: %s\n", manager.PathMeta.ShellShimsPath)
bins := envKeys.Paths.ToBinPaths()
for _, bin := range bins.Slice() {
binShim := shim.NewShim(bin, manager.PathMeta.ShellShimsPath)
if err = binShim.Generate(); err != nil {
continue
}
}
}

_ = os.Setenv(env.HookFlag, name)
exportEnvs[env.HookFlag] = &name
osPaths := env.NewPaths(env.OsPaths)
osPaths.AddWithIndex(0, manager.PathMeta.ShellShimsPath)
osPathsStr := osPaths.String()
exportEnvs["PATH"] = &osPathsStr
sdkCurrentPaths.Merge(osPaths)
pathsStr := sdkCurrentPaths.String()
exportEnvs["PATH"] = &pathsStr

path := manager.PathMeta.ExecutablePath
path = strings.Replace(path, "\\", "/", -1)
Expand Down
44 changes: 20 additions & 24 deletions cmd/commands/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/version-fox/vfox/internal/env"
"github.com/version-fox/vfox/internal/logger"
"github.com/version-fox/vfox/internal/shell"
"github.com/version-fox/vfox/internal/shim"
"github.com/version-fox/vfox/internal/toolset"
"path/filepath"
)
Expand Down Expand Up @@ -122,34 +121,32 @@ func envFlag(ctx *cli.Context) error {
manager := internal.NewSdkManager()
defer manager.Close()

envKeys, err := aggregateEnvKeys(manager)
sdkEnvs, err := aggregateEnvKeys(manager)
if err != nil {
return err
}

envKeys := sdkEnvs.ToEnvs()

// link to current directory
sdkCurrentPaths := sdkEnvs.LinkCurrent(manager.PathMeta.CurTmpPath)

exportEnvs := make(env.Vars)
for k, v := range envKeys.Variables {
exportEnvs[k] = v
}

// generate shims for current shell
if envKeys.Paths.Len() > 0 {
logger.Debugf("Generate shims for current shell, path: %s\n", manager.PathMeta.ShellShimsPath)
bins := envKeys.Paths.ToBinPaths()
for _, bin := range bins.Slice() {
binShim := shim.NewShim(bin, manager.PathMeta.ShellShimsPath)
if err = binShim.Generate(); err != nil {
continue
}
}
}
osPaths := env.NewPaths(env.OsPaths)
sdkCurrentPaths.Merge(osPaths)
pathsStr := sdkCurrentPaths.String()
exportEnvs["PATH"] = &pathsStr

exportStr := s.Export(exportEnvs)
fmt.Println(exportStr)
return nil
}

func aggregateEnvKeys(manager *internal.Manager) (*env.Envs, error) {
func aggregateEnvKeys(manager *internal.Manager) (internal.SdkEnvs, error) {
workToolVersion, err := toolset.NewToolVersion(manager.PathMeta.WorkingDirectory)
if err != nil {
return nil, err
Expand All @@ -172,16 +169,14 @@ func aggregateEnvKeys(manager *internal.Manager) (*env.Envs, error) {
// Add the working directory to the first
tvs := toolset.MultiToolVersions{workToolVersion, curToolVersion}

shellEnvs := &env.Envs{
Variables: make(env.Vars),
Paths: env.NewPaths(env.EmptyPaths),
}
flushCache, err := cache.NewFileCache(filepath.Join(manager.PathMeta.CurTmpPath, "flush_env.cache"))
if err != nil {
return nil, err
}
defer flushCache.Close()

var sdkEnvs []*internal.SdkEnv

tvs.FilterTools(func(name, version string) bool {
if lookupSdk, err := manager.LookupSdk(name); err == nil {
vv, ok := flushCache.Get(name)
Expand All @@ -191,13 +186,14 @@ func aggregateEnvKeys(manager *internal.Manager) (*env.Envs, error) {
} else {
logger.Debugf("No hit cache, name: %s cache: %s, expected: %s \n", name, string(vv), version)
}
if keys, err := lookupSdk.EnvKeys(internal.Version(version)); err == nil {
for key, value := range keys.Variables {
shellEnvs.Variables[key] = value
}
shellEnvs.Paths.Merge(keys.Paths)
v := internal.Version(version)
if keys, err := lookupSdk.EnvKeys(v); err == nil {
flushCache.Set(name, cache.Value(version), cache.NeverExpired)

sdkEnvs = append(sdkEnvs, &internal.SdkEnv{
Sdk: lookupSdk, Env: keys,
})

// If we encounter a .tool-versions file, it is valid for the entire shell session,
// unless we encounter the next .tool-versions file or manually switch to the use command.
curToolVersion.Record[name] = version
Expand All @@ -207,5 +203,5 @@ func aggregateEnvKeys(manager *internal.Manager) (*env.Envs, error) {
return false
})

return shellEnvs, nil
return sdkEnvs, nil
}
4 changes: 3 additions & 1 deletion internal/env/windows_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ func (w *windowsEnvManager) Flush() (err error) {
_ = w.Remove(&Envs{
Variables: Vars{
"VERSION_FOX_PATH": nil,
}})
},
Paths: NewPaths(EmptyPaths),
})
}
// user env
oldPath, success := w.Get("PATH")
Expand Down
26 changes: 13 additions & 13 deletions internal/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,28 @@ type Manager struct {
Config *config.Config
}

func (m *Manager) EnvKeys(tvs toolset.MultiToolVersions) (*env.Envs, error) {
shellEnvs := &env.Envs{
Variables: make(env.Vars),
Paths: env.NewPaths(env.EmptyPaths),
}

func (m *Manager) EnvKeys(tvs toolset.MultiToolVersions) (SdkEnvs, error) {
var sdkEnvs SdkEnvs
tools := make(map[string]struct{})
for _, t := range tvs {
for name, version := range t.Record {
if _, ok := tools[name]; ok {
continue
}
if lookupSdk, err := m.LookupSdk(name); err == nil {
if ek, err := lookupSdk.EnvKeys(Version(version)); err == nil {
v := Version(version)
if ek, err := lookupSdk.EnvKeys(v); err == nil {
tools[name] = struct{}{}
for key, value := range ek.Variables {
shellEnvs.Variables[key] = value
}
shellEnvs.Paths.Merge(ek.Paths)

sdkEnvs = append(sdkEnvs, &SdkEnv{
Sdk: lookupSdk,
Env: ek,
})
}
}
}
}
return shellEnvs, nil
return sdkEnvs, nil
}

// LookupSdk lookup sdk by name
Expand Down Expand Up @@ -197,7 +195,9 @@ func (m *Manager) Remove(pluginName string) error {
return err
}

source.clearCurrentEnvConfig()
if err = source.ClearCurrentEnv(); err != nil {
return err
}
pPath := filepath.Join(m.PathMeta.PluginPath, pluginName)
pterm.Printf("Removing %s plugin...\n", pPath)
err = os.RemoveAll(pPath)
Expand Down
4 changes: 0 additions & 4 deletions internal/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ type PathMeta struct {
ExecutablePath string
WorkingDirectory string
GlobalShimsPath string
ShellShimsPath string
}

func newPathMeta() (*PathMeta, error) {
Expand Down Expand Up @@ -67,8 +66,6 @@ func newPathMeta() (*PathMeta, error) {

globalShimsPath := filepath.Join(homePath, "shims")
_ = os.MkdirAll(globalShimsPath, 0777)
shellShimsPath := filepath.Join(curTmpPath, "shims")
_ = os.MkdirAll(shellShimsPath, 0777)

workingDirectory, err := os.Getwd()
if err != nil {
Expand All @@ -84,6 +81,5 @@ func newPathMeta() (*PathMeta, error) {
ExecutablePath: exePath,
WorkingDirectory: workingDirectory,
GlobalShimsPath: globalShimsPath,
ShellShimsPath: shellShimsPath,
}, nil
}
Loading

0 comments on commit 23b8163

Please sign in to comment.