From 385686b2a175c2b516d6da57116251310e74bd04 Mon Sep 17 00:00:00 2001 From: lihan Date: Thu, 23 May 2024 19:15:18 +0800 Subject: [PATCH] fix: clean up unused sdk symbolic link in time when refreshing the environment --- cmd/commands/env.go | 14 +++++++++++ internal/cache/cache.go | 22 ++++++++++++++++++ internal/sdk.go | 51 +++++++++++++++++++---------------------- 3 files changed, 60 insertions(+), 27 deletions(-) diff --git a/cmd/commands/env.go b/cmd/commands/env.go index d1827810..9b30ec8d 100644 --- a/cmd/commands/env.go +++ b/cmd/commands/env.go @@ -27,6 +27,7 @@ import ( "github.com/version-fox/vfox/internal/shell" "github.com/version-fox/vfox/internal/toolset" "github.com/version-fox/vfox/internal/util" + "os" "path/filepath" ) @@ -174,6 +175,7 @@ func aggregateEnvKeys(manager *internal.Manager) (internal.SdkEnvs, error) { var ( sdkEnvs []*internal.SdkEnv finalSdks = make(map[string]struct{}) + cacheLen = flushCache.Len() ) for _, tv := range multiToolVersions { @@ -205,5 +207,17 @@ func aggregateEnvKeys(manager *internal.Manager) (internal.SdkEnvs, error) { return nil, err } } + + // Remove the old cache + if cacheLen != len(finalSdks) { + for _, sdkname := range flushCache.Keys() { + if _, ok := finalSdks[sdkname]; !ok { + linkPath := filepath.Join(manager.PathMeta.CurTmpPath, sdkname) + logger.Debugf("Remove unused sdk link: %s\n", linkPath) + _ = os.Remove(linkPath) + flushCache.Remove(sdkname) + } + } + } return sdkEnvs, nil } diff --git a/internal/cache/cache.go b/internal/cache/cache.go index fc84b0e9..a88ee0ba 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -101,6 +101,28 @@ func (c *FileCache) Get(key string) (Value, bool) { return item.Val, true } +func (c *FileCache) Keys() []string { + c.mu.RLock() + defer c.mu.RUnlock() + keys := make([]string, 0, len(c.items)) + for k := range c.items { + keys = append(keys, k) + } + return keys +} + +func (c *FileCache) Len() int { + return len(c.items) +} + +func (c *FileCache) Clear() int { + c.mu.Lock() + defer c.mu.Unlock() + len := len(c.items) + c.items = make(map[string]Item) + return len +} + // Remove a key from the cache func (c *FileCache) Remove(key string) { c.mu.Lock() diff --git a/internal/sdk.go b/internal/sdk.go index 4fe94ab6..6accdd19 100644 --- a/internal/sdk.go +++ b/internal/sdk.go @@ -40,10 +40,6 @@ import ( "github.com/version-fox/vfox/internal/util" ) -var ( - localSdkPackageCache = make(map[Version]*Package) -) - type Version string type SdkEnv struct { @@ -72,7 +68,8 @@ type Sdk struct { sdkManager *Manager Plugin *LuaPlugin // current sdk install path - InstallPath string + InstallPath string + localSdkPackageCache map[Version]*Package } func (b *Sdk) Install(version Version) error { @@ -420,11 +417,11 @@ func (b *Sdk) Use(version Version, scope UseScope) error { func (b *Sdk) useInHook(version Version, scope UseScope) error { var ( - multiToolVersion toolset.MultiToolVersions + toolVersion toolset.ToolVersion ) if scope == Global { - toolVersion, err := toolset.NewToolVersion(b.sdkManager.PathMeta.HomePath) + tv, err := toolset.NewToolVersion(b.sdkManager.PathMeta.HomePath) if err != nil { return fmt.Errorf("failed to read tool versions, err:%w", err) } @@ -446,7 +443,7 @@ func (b *Sdk) useInHook(version Version, scope UseScope) error { // clear global env logger.Debugf("Clear global env: %s\n", b.Plugin.SdkName) - if oldVersion, ok := toolVersion.Get(b.Plugin.SdkName); ok { + if oldVersion, ok := tv.Get(b.Plugin.SdkName); ok { b.clearGlobalEnv(Version(oldVersion)) } @@ -457,29 +454,28 @@ func (b *Sdk) useInHook(version Version, scope UseScope) error { if err != nil { return err } - multiToolVersion = append(multiToolVersion, toolVersion) + toolVersion = tv } else if scope == Project { - toolVersion, err := toolset.NewToolVersion(b.sdkManager.PathMeta.WorkingDirectory) + tv, err := toolset.NewToolVersion(b.sdkManager.PathMeta.WorkingDirectory) if err != nil { return fmt.Errorf("failed to read tool versions, err:%w", err) } - logger.Debugf("Load project toolchain versions: %v\n", toolVersion.Keys()) - multiToolVersion = append(multiToolVersion, toolVersion) - } + toolVersion = tv + } else { + tv, err := toolset.NewToolVersion(b.sdkManager.PathMeta.CurTmpPath) + if err != nil { + return fmt.Errorf("failed to read tool versions, err:%w", err) + } + toolVersion = tv - // It must also be saved once at the session level. - toolVersion, err := toolset.NewToolVersion(b.sdkManager.PathMeta.CurTmpPath) - if err != nil { - return fmt.Errorf("failed to read tool versions, err:%w", err) } - multiToolVersion = append(multiToolVersion, toolVersion) - - multiToolVersion.Add(b.Plugin.SdkName, string(version)) + toolVersion.Set(b.Plugin.SdkName, string(version)) - if err = multiToolVersion.Save(); err != nil { + if err := toolVersion.Save(); err != nil { return fmt.Errorf("failed to save tool versions, err:%w", err) } - if err = b.ToLinkPackage(version, ShellLocation); err != nil { + + if err := b.ToLinkPackage(version, ShellLocation); err != nil { return err } @@ -572,7 +568,7 @@ func (b *Sdk) clearGlobalEnv(version Version) { } func (b *Sdk) GetLocalSdkPackage(version Version) (*Package, error) { - p, ok := localSdkPackageCache[version] + p, ok := b.localSdkPackageCache[version] if ok { return p, nil } @@ -610,7 +606,7 @@ func (b *Sdk) GetLocalSdkPackage(version Version) (*Package, error) { Main: main, Additions: additions, } - localSdkPackageCache[version] = p2 + b.localSdkPackageCache[version] = p2 return p2, nil } @@ -760,8 +756,9 @@ func NewSdk(manager *Manager, pluginPath string) (*Sdk, error) { return nil, fmt.Errorf("failed to create lua plugin: %w", err) } return &Sdk{ - sdkManager: manager, - InstallPath: filepath.Join(manager.PathMeta.SdkCachePath, strings.ToLower(luaPlugin.SdkName)), - Plugin: luaPlugin, + sdkManager: manager, + InstallPath: filepath.Join(manager.PathMeta.SdkCachePath, strings.ToLower(luaPlugin.SdkName)), + Plugin: luaPlugin, + localSdkPackageCache: make(map[Version]*Package), }, nil }