Skip to content

Commit

Permalink
fix: clean up unused sdk symbolic link in time when refreshing the en…
Browse files Browse the repository at this point in the history
…vironment
  • Loading branch information
aooohan committed May 23, 2024
1 parent 15a0b74 commit 385686b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 27 deletions.
14 changes: 14 additions & 0 deletions cmd/commands/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
22 changes: 22 additions & 0 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
51 changes: 24 additions & 27 deletions internal/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ import (
"github.com/version-fox/vfox/internal/util"
)

var (
localSdkPackageCache = make(map[Version]*Package)
)

type Version string

type SdkEnv struct {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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))
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

0 comments on commit 385686b

Please sign in to comment.