Skip to content

Commit

Permalink
fix(powershell): due to a bug in PowerShell, we have to cleanup first…
Browse files Browse the repository at this point in the history
… when the shell open.

When powershell is closed via the x button, this event will not be fired.
 See PowerShell/PowerShell#8000
  • Loading branch information
aooohan committed Mar 13, 2024
1 parent 9a77ef5 commit 8ce7be0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
20 changes: 17 additions & 3 deletions internal/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import (
)

const (
pluginIndexUrl = "https://version-fox.github.io/version-fox-plugins/"
pluginIndexUrl = "https://version-fox.github.io/version-fox-plugins/"
cleanupFlagFilename = ".cleanup"
)

type Arg struct {
Expand Down Expand Up @@ -138,7 +139,9 @@ func (m *Manager) Remove(pluginName string) error {
return fmt.Errorf("remove failed, err: %w", err)
}
pterm.Printf("Removing %s sdk...\n", source.InstallPath)
err = os.RemoveAll(source.InstallPath)
if err = os.RemoveAll(source.InstallPath); err != nil {
return err
}
pterm.Printf("Remove %s plugin successfully! \n", pterm.LightGreen(pluginName))
return nil
}
Expand Down Expand Up @@ -335,6 +338,17 @@ func (m *Manager) Available() ([]*Category, error) {
}

func (m *Manager) CleanTmp() {
// once per day
cleanFlagPath := filepath.Join(m.PathMeta.TempPath, cleanupFlagFilename)
if !util.FileExists(cleanFlagPath) {
_ = os.WriteFile(cleanFlagPath, []byte(strconv.FormatInt(util.GetBeginOfToday(), 10)), 0666)
} else {
if str, err := os.ReadFile(cleanFlagPath); err == nil {
if i, err := strconv.ParseInt(string(str), 10, 64); err == nil && !util.IsBeforeToday(i) {
return
}
}
}
dir, err := os.ReadDir(m.PathMeta.TempPath)
if err == nil {
_ = os.RemoveAll(m.PathMeta.CurTmpPath)
Expand All @@ -352,7 +366,7 @@ func (m *Manager) CleanTmp() {
continue
}
if util.IsBeforeToday(i) {
_ = os.Remove(filepath.Join(m.PathMeta.TempPath, file.Name()))
_ = os.RemoveAll(filepath.Join(m.PathMeta.TempPath, file.Name()))
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions internal/shell/powershell.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ var Pwsh Shell = pwsh{}
const hook = `
{{.EnvContent}}
<#
Due to a bug in PowerShell, we have to cleanup first when the shell open.
#>
& '{{.SelfPath}}' env --cleanup 2>$null | Out-Null;
$__VFOX_PID=$pid;
$originalPrompt = $function:prompt;
function prompt {
$export = &"{{.SelfPath}}" env -s pwsh;
if ($export) {
Expand All @@ -42,6 +48,11 @@ function prompt {
&$originalPrompt;
}
<#
There is a bug here.
When powershell is closed via the x button, this event will not be fired.
See https://github.com/PowerShell/PowerShell/issues/8000
#>
Register-EngineEvent -SourceIdentifier PowerShell.Exiting -SupportEvent -Action {
&"{{.SelfPath}}" env --cleanup;
}
Expand Down

0 comments on commit 8ce7be0

Please sign in to comment.