feat(upgrade): auto upgrade

This commit is contained in:
Jan De Dobbeleer 2024-06-03 11:26:05 +02:00 committed by Jan De Dobbeleer
parent 527eef0526
commit 129c41ba34
18 changed files with 88 additions and 22 deletions

View file

@ -73,6 +73,7 @@ func runInit(shellName string) {
shell.Tooltips = len(cfg.Tooltips) > 0 shell.Tooltips = len(cfg.Tooltips) > 0
shell.ShellIntegration = cfg.ShellIntegration shell.ShellIntegration = cfg.ShellIntegration
shell.PromptMark = shellName == shell.FISH && cfg.ITermFeatures != nil && cfg.ITermFeatures.Contains(ansi.PromptMark) shell.PromptMark = shellName == shell.FISH && cfg.ITermFeatures != nil && cfg.ITermFeatures.Contains(ansi.PromptMark)
shell.AutoUpgrade = cfg.AutoUpgrade && upgrade.Supported
for i, block := range cfg.Blocks { for i, block := range cfg.Blocks {
// only fetch cursor position when relevant // only fetch cursor position when relevant
@ -85,7 +86,7 @@ func runInit(shellName string) {
} }
// allow overriding the upgrade notice from the config // allow overriding the upgrade notice from the config
if cfg.DisableNotice { if cfg.DisableNotice || cfg.AutoUpgrade {
env.Cache().Set(upgrade.CACHEKEY, "disabled", -1) env.Cache().Set(upgrade.CACHEKEY, "disabled", -1)
} }

View file

@ -23,23 +23,23 @@ var upgradeCmd = &cobra.Command{
return return
} }
if force {
upgrade.Run()
return
}
env := &platform.Shell{ env := &platform.Shell{
CmdFlags: &platform.Flags{}, CmdFlags: &platform.Flags{},
} }
env.Init() env.Init()
defer env.Close() defer env.Close()
if force {
upgrade.Run(env)
return
}
if _, hasNotice := upgrade.Notice(env, true); !hasNotice { if _, hasNotice := upgrade.Notice(env, true); !hasNotice {
fmt.Print("\n✅ no new version available\n\n") fmt.Print("\n✅ no new version available\n\n")
return return
} }
upgrade.Run() upgrade.Run(env)
}, },
} }

View file

@ -52,6 +52,7 @@ type Config struct {
DisableCursorPositioning bool `json:"disable_cursor_positioning,omitempty" toml:"disable_cursor_positioning,omitempty"` DisableCursorPositioning bool `json:"disable_cursor_positioning,omitempty" toml:"disable_cursor_positioning,omitempty"`
PatchPwshBleed bool `json:"patch_pwsh_bleed,omitempty" toml:"patch_pwsh_bleed,omitempty"` PatchPwshBleed bool `json:"patch_pwsh_bleed,omitempty" toml:"patch_pwsh_bleed,omitempty"`
DisableNotice bool `json:"disable_notice,omitempty" toml:"disable_notice,omitempty"` DisableNotice bool `json:"disable_notice,omitempty" toml:"disable_notice,omitempty"`
AutoUpgrade bool `json:"auto_upgrade,omitempty" toml:"auto_upgrade,omitempty"`
ITermFeatures ansi.ITermFeatures `json:"iterm_features,omitempty" toml:"iterm_features,omitempty"` ITermFeatures ansi.ITermFeatures `json:"iterm_features,omitempty" toml:"iterm_features,omitempty"`
// Deprecated // Deprecated

View file

@ -53,6 +53,7 @@ var (
RPrompt bool RPrompt bool
CursorPositioning bool CursorPositioning bool
PromptMark bool PromptMark bool
AutoUpgrade bool
) )
func getExecutablePath(env platform.Environment) (string, error) { func getExecutablePath(env platform.Environment) (string, error) {
@ -283,6 +284,7 @@ func PrintInit(env platform.Environment) string {
"::CURSOR::", strconv.FormatBool(CursorPositioning), "::CURSOR::", strconv.FormatBool(CursorPositioning),
"::UPGRADE::", strconv.FormatBool(hasNotice), "::UPGRADE::", strconv.FormatBool(hasNotice),
"::UPGRADENOTICE::", notice, "::UPGRADENOTICE::", notice,
"::AUTOUPGRADE::", strconv.FormatBool(AutoUpgrade),
"::PROMPT_MARK::", promptMark(), "::PROMPT_MARK::", promptMark(),
).Replace(script) ).Replace(script)
} }

View file

@ -76,3 +76,7 @@ fi
if [ "::UPGRADE::" == "true" ]; then if [ "::UPGRADE::" == "true" ]; then
echo "::UPGRADENOTICE::" echo "::UPGRADENOTICE::"
fi fi
if [[ "::AUTOUPGRADE::" == "true" ]]; then
::OMP:: upgrade
fi

View file

@ -34,3 +34,7 @@ set edit:rprompt = {
if (eq '::UPGRADE::' 'true') { if (eq '::UPGRADE::' 'true') {
echo '::UPGRADENOTICE::' echo '::UPGRADENOTICE::'
} }
if (eq '::AUTOUPGRADE::' 'true') {
::OMP:: upgrade
}

View file

@ -160,3 +160,7 @@ end
if test "::UPGRADE::" = "true" if test "::UPGRADE::" = "true"
echo "::UPGRADENOTICE::" echo "::UPGRADENOTICE::"
end end
if test "::AUTOUPGRADE::" = "true"
::OMP:: upgrade
end

View file

@ -335,3 +335,8 @@ end
if tooltips_enabled and rl.setbinding then if tooltips_enabled and rl.setbinding then
rl.setbinding(' ', [["luafunc:ohmyposh_space"]], 'emacs') rl.setbinding(' ', [["luafunc:ohmyposh_space"]], 'emacs')
end end
if '::AUTOUPGRADE::' == 'true' then
local prompt_exe = string.format('%s upgrade', omp_exe())
os.execute(prompt_exe)
end

View file

@ -44,3 +44,7 @@ if "::TRANSIENT::" == "true" {
if "::UPGRADE::" == "true" { if "::UPGRADE::" == "true" {
echo "::UPGRADENOTICE::" echo "::UPGRADENOTICE::"
} }
if "::AUTOUPGRADE::" == "true" {
^::OMP:: upgrade
}

View file

@ -486,6 +486,12 @@ Example:
"Enable-PoshLineError" "Enable-PoshLineError"
"Export-PoshTheme" "Export-PoshTheme"
"Get-PoshThemes" "Get-PoshThemes"
"Start-Utf8Process"
"prompt" "prompt"
) )
} | Import-Module -Global } | Import-Module -Global
if ("::AUTOUPGRADE::" -eq "true") {
& ::OMP:: upgrade
}

View file

@ -27,3 +27,6 @@ notice = """::UPGRADENOTICE::"""
if "::UPGRADE::" == "true": if "::UPGRADE::" == "true":
print(notice) print(notice)
if "::AUTOUPGRADE::" == "true":
::OMP:: upgrade

View file

@ -10,4 +10,6 @@ alias precmd "$POSH_PRECMD;$USER_PRECMD";
alias postcmd "$POSH_POSTCMD;$USER_POSTCMD"; alias postcmd "$POSH_POSTCMD;$USER_POSTCMD";
set POSH_START_TIME = `::OMP:: get millis`; set POSH_START_TIME = `::OMP:: get millis`;
if ("::UPGRADE::" == "true") echo "::UPGRADENOTICE::" if ("::UPGRADE::" == "true") echo "::UPGRADENOTICE::";
if ("::AUTOUPGRADE::" == "true") ::OMP:: upgrade;

View file

@ -165,3 +165,7 @@ fi
if [[ "::UPGRADE::" = "true" ]]; then if [[ "::UPGRADE::" = "true" ]]; then
echo "::UPGRADENOTICE::" echo "::UPGRADENOTICE::"
fi fi
if [[ "::AUTOUPGRADE::" = "true" ]]; then
::OMP:: upgrade
fi

View file

@ -12,12 +12,16 @@ import (
"github.com/charmbracelet/bubbles/spinner" "github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
"github.com/jandedobbeleer/oh-my-posh/src/build"
"github.com/jandedobbeleer/oh-my-posh/src/platform" "github.com/jandedobbeleer/oh-my-posh/src/platform"
) )
var ( var (
program *tea.Program program *tea.Program
textStyle = lipgloss.NewStyle().Margin(1, 0, 2, 4) textStyle = lipgloss.NewStyle().Margin(1, 0, 2, 0)
title string
Supported = true
) )
type resultMsg string type resultMsg string
@ -30,6 +34,10 @@ const (
installing installing
) )
func setState(message state) {
program.Send(stateMsg(message))
}
type stateMsg state type stateMsg state
type model struct { type model struct {
@ -88,7 +96,7 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m *model) View() string { func (m *model) View() string {
if len(m.message) > 0 { if len(m.message) > 0 {
return textStyle.Render(m.message) return title + textStyle.Render(m.message)
} }
var message string var message string
@ -103,10 +111,20 @@ func (m *model) View() string {
message = "Installing" message = "Installing"
} }
return textStyle.Render(fmt.Sprintf("%s %s", m.spinner.View(), message)) return title + textStyle.Render(fmt.Sprintf("%s %s", m.spinner.View(), message))
} }
func Run() { func Run(env platform.Environment) {
titleStyle := lipgloss.NewStyle().Margin(1, 0, 1, 0)
title = "📦 Upgrading Oh My Posh"
version, err := Latest(env)
if err == nil {
title = fmt.Sprintf("%s from %s to %s", title, build.Version, version)
}
title = titleStyle.Render(title)
program = tea.NewProgram(initialModel()) program = tea.NewProgram(initialModel())
if _, err := program.Run(); err != nil { if _, err := program.Run(); err != nil {
fmt.Println(err) fmt.Println(err)
@ -128,7 +146,7 @@ func downloadAsset(asset string) (io.ReadCloser, error) {
} }
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Failed to download installer: %s", url) return nil, fmt.Errorf("failed to download installer: %s", url)
} }
return resp.Body, nil return resp.Body, nil

View file

@ -8,10 +8,10 @@ import (
"runtime" "runtime"
) )
var successMsg = "Upgrade successful, restart your shell to take full advantage of the new functionality." var successMsg = "🚀 Upgrade successful, restart your shell to take full advantage of the new functionality."
func install() error { func install() error {
program.Send(stateMsg(validating)) setState(validating)
executable, err := os.Executable() executable, err := os.Executable()
if err != nil { if err != nil {
return err return err
@ -24,7 +24,7 @@ func install() error {
defer file.Close() defer file.Close()
program.Send(stateMsg(downloading)) setState(downloading)
asset := fmt.Sprintf("posh-%s-%s", runtime.GOOS, runtime.GOARCH) asset := fmt.Sprintf("posh-%s-%s", runtime.GOOS, runtime.GOARCH)
@ -35,7 +35,7 @@ func install() error {
defer data.Close() defer data.Close()
program.Send(stateMsg(installing)) setState(installing)
_, err = io.Copy(file, data) _, err = io.Copy(file, data)
if err != nil { if err != nil {

View file

@ -2,4 +2,10 @@
package upgrade package upgrade
func Run() {} import "github.com/jandedobbeleer/oh-my-posh/src/platform"
func Run(_ platform.Environment) {}
var (
Supported = false
)

View file

@ -13,7 +13,7 @@ import (
var successMsg = "Oh My Posh is installing in the background.\nRestart your shell in a minute to take full advantage of the new functionality." var successMsg = "Oh My Posh is installing in the background.\nRestart your shell in a minute to take full advantage of the new functionality."
func install() error { func install() error {
program.Send(stateMsg(downloading)) setState(downloading)
temp := os.Getenv("TEMP") temp := os.Getenv("TEMP")
if len(temp) == 0 { if len(temp) == 0 {

View file

@ -34,6 +34,8 @@ const (
upgradeNotice = ` upgradeNotice = `
A new release of Oh My Posh is available: %s %s A new release of Oh My Posh is available: %s %s
To upgrade, run: 'oh-my-posh upgrade' To upgrade, run: 'oh-my-posh upgrade'
To enable automated upgrades, set 'auto_upgrade' to 'true' in your configuration.
` `
CACHEKEY = "upgrade_check" CACHEKEY = "upgrade_check"
) )