Skip to content

Commit

Permalink
feat(upgrade): auto upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Jun 14, 2024
1 parent 527eef0 commit 129c41b
Show file tree
Hide file tree
Showing 18 changed files with 87 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand Down
16 changes: 8 additions & 8 deletions src/cli/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -34,12 +29,17 @@ var upgradeCmd = &cobra.Command{
env.Init()
defer env.Close()

if force {
upgrade.Run(env)
return
}

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
}

upgrade.Run()
upgrade.Run(env)
},
}

Expand Down
1 change: 1 addition & 0 deletions src/engine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/shell/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var (
RPrompt bool
CursorPositioning bool
PromptMark bool
AutoUpgrade bool
)

func getExecutablePath(env platform.Environment) (string, error) {
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions src/shell/scripts/omp.bash
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ fi
if [ "::UPGRADE::" == "true" ]; then
echo "::UPGRADENOTICE::"
fi

if [[ "::AUTOUPGRADE::" == "true" ]]; then
::OMP:: upgrade
fi
4 changes: 4 additions & 0 deletions src/shell/scripts/omp.elv
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ set edit:rprompt = {
if (eq '::UPGRADE::' 'true') {
echo '::UPGRADENOTICE::'
}

if (eq '::AUTOUPGRADE::' 'true') {
::OMP:: upgrade
}
4 changes: 4 additions & 0 deletions src/shell/scripts/omp.fish
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,7 @@ end
if test "::UPGRADE::" = "true"
echo "::UPGRADENOTICE::"
end

if test "::AUTOUPGRADE::" = "true"
::OMP:: upgrade
end
5 changes: 5 additions & 0 deletions src/shell/scripts/omp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions src/shell/scripts/omp.nu
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ if "::TRANSIENT::" == "true" {
if "::UPGRADE::" == "true" {
echo "::UPGRADENOTICE::"
}

if "::AUTOUPGRADE::" == "true" {
^::OMP:: upgrade
}
6 changes: 6 additions & 0 deletions src/shell/scripts/omp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,12 @@ Example:
"Enable-PoshLineError"
"Export-PoshTheme"
"Get-PoshThemes"
"Start-Utf8Process"
"prompt"
)
} | Import-Module -Global


if ("::AUTOUPGRADE::" -eq "true") {
& ::OMP:: upgrade
}
3 changes: 3 additions & 0 deletions src/shell/scripts/omp.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ def posh_right():

if "::UPGRADE::" == "true":
print(notice)

if "::AUTOUPGRADE::" == "true":
::OMP:: upgrade
4 changes: 3 additions & 1 deletion src/shell/scripts/omp.tcsh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 4 additions & 0 deletions src/shell/scripts/omp.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,7 @@ fi
if [[ "::UPGRADE::" = "true" ]]; then
echo "::UPGRADENOTICE::"
fi

if [[ "::AUTOUPGRADE::" = "true" ]]; then
::OMP:: upgrade
fi
28 changes: 23 additions & 5 deletions src/upgrade/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,6 +34,10 @@ const (
installing
)

func setState(message state) {
program.Send(stateMsg(message))
}

type stateMsg state

type model struct {
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/upgrade/cli_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -35,7 +35,7 @@ func install() error {

defer data.Close()

program.Send(stateMsg(installing))
setState(installing)

_, err = io.Copy(file, data)
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion src/upgrade/cli_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@

package upgrade

func Run() {}
import "github.com/jandedobbeleer/oh-my-posh/src/platform"

func Run(_ platform.Environment) {}

var (
Supported = false
)
2 changes: 1 addition & 1 deletion src/upgrade/cli_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/upgrade/notice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down

0 comments on commit 129c41b

Please sign in to comment.