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.ShellIntegration = cfg.ShellIntegration
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 {
// only fetch cursor position when relevant
@ -85,7 +86,7 @@ func runInit(shellName string) {
}
// allow overriding the upgrade notice from the config
if cfg.DisableNotice {
if cfg.DisableNotice || cfg.AutoUpgrade {
env.Cache().Set(upgrade.CACHEKEY, "disabled", -1)
}

View file

@ -19,12 +19,7 @@ var upgradeCmd = &cobra.Command{
Args: cobra.NoArgs,
Run: func(_ *cobra.Command, _ []string) {
if runtime.GOOS == platform.LINUX {
fmt.Print("\n ⚠️ upgrade is not supported on this platform\n\n")
return
}
if force {
upgrade.Run()
fmt.Print("\n⚠ upgrade is not supported on this platform\n\n")
return
}
@ -34,12 +29,17 @@ var upgradeCmd = &cobra.Command{
env.Init()
defer env.Close()
if _, hasNotice := upgrade.Notice(env, true); !hasNotice {
fmt.Print("\n ✅ no new version available\n\n")
if force {
upgrade.Run(env)
return
}
upgrade.Run()
if _, hasNotice := upgrade.Notice(env, true); !hasNotice {
fmt.Print("\n✅ no new version available\n\n")
return
}
upgrade.Run(env)
},
}

View file

@ -52,6 +52,7 @@ type Config struct {
DisableCursorPositioning bool `json:"disable_cursor_positioning,omitempty" toml:"disable_cursor_positioning,omitempty"`
PatchPwshBleed bool `json:"patch_pwsh_bleed,omitempty" toml:"patch_pwsh_bleed,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"`
// Deprecated

View file

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

View file

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

View file

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

View file

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

View file

@ -335,3 +335,8 @@ end
if tooltips_enabled and rl.setbinding then
rl.setbinding(' ', [["luafunc:ohmyposh_space"]], 'emacs')
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" {
echo "::UPGRADENOTICE::"
}
if "::AUTOUPGRADE::" == "true" {
^::OMP:: upgrade
}

View file

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

View file

@ -27,3 +27,6 @@ notice = """::UPGRADENOTICE::"""
if "::UPGRADE::" == "true":
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";
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
echo "::UPGRADENOTICE::"
fi
if [[ "::AUTOUPGRADE::" = "true" ]]; then
::OMP:: upgrade
fi

View file

@ -12,12 +12,16 @@ import (
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/jandedobbeleer/oh-my-posh/src/build"
"github.com/jandedobbeleer/oh-my-posh/src/platform"
)
var (
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
@ -30,6 +34,10 @@ const (
installing
)
func setState(message state) {
program.Send(stateMsg(message))
}
type stateMsg state
type model struct {
@ -88,7 +96,7 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m *model) View() string {
if len(m.message) > 0 {
return textStyle.Render(m.message)
return title + textStyle.Render(m.message)
}
var message string
@ -103,10 +111,20 @@ func (m *model) View() string {
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())
if _, err := program.Run(); err != nil {
fmt.Println(err)
@ -128,7 +146,7 @@ func downloadAsset(asset string) (io.ReadCloser, error) {
}
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

View file

@ -8,10 +8,10 @@ import (
"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 {
program.Send(stateMsg(validating))
setState(validating)
executable, err := os.Executable()
if err != nil {
return err
@ -24,7 +24,7 @@ func install() error {
defer file.Close()
program.Send(stateMsg(downloading))
setState(downloading)
asset := fmt.Sprintf("posh-%s-%s", runtime.GOOS, runtime.GOARCH)
@ -35,7 +35,7 @@ func install() error {
defer data.Close()
program.Send(stateMsg(installing))
setState(installing)
_, err = io.Copy(file, data)
if err != nil {

View file

@ -2,4 +2,10 @@
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."
func install() error {
program.Send(stateMsg(downloading))
setState(downloading)
temp := os.Getenv("TEMP")
if len(temp) == 0 {

View file

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