- Add stable, alpha, and rolling dev release channel resolution in internal/version (including non-monotonic dev commit SHA detection) - Persist app_update_channel in config and expose App.SetAppUpdateChannel - Preserve previous executables (.old*) and downloaded update installers on startup (and avoid overwriting existing .old backups during swap) when alpha or dev channel is selected
259 lines
7.3 KiB
Go
259 lines
7.3 KiB
Go
package app
|
|
|
|
// Online runtime install from GitHub Releases (internal/runtime/remote.go).
|
|
// Downloads run as "runtime-install" tasks, so progress shows in the status bar.
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/installer"
|
|
rt "github.com/Nwflower/reims-go/internal/runtime"
|
|
"github.com/Nwflower/reims-go/internal/version"
|
|
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
|
|
)
|
|
|
|
func (a *App) baseCtx() context.Context {
|
|
if a.ctx != nil {
|
|
return a.ctx
|
|
}
|
|
return context.Background()
|
|
}
|
|
|
|
func (a *App) includeAlpha() bool {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
return rt.NormalizeChannel(a.cfg.RuntimeChannel) == rt.ChannelAlpha
|
|
}
|
|
|
|
// remoteCache keeps the release list for a while: GitHub allows 60
|
|
// unauthenticated API requests an hour, and the host page, first launch and
|
|
// the CLI all ask for it.
|
|
var remoteCache struct {
|
|
sync.Mutex
|
|
at time.Time
|
|
alpha bool
|
|
list []rt.Remote
|
|
}
|
|
|
|
const remoteCacheTTL = 10 * time.Minute
|
|
|
|
// ListRemoteRuntimes lists published runtimes for the configured channel.
|
|
func (a *App) ListRemoteRuntimes() ([]rt.Remote, error) {
|
|
alpha := a.includeAlpha()
|
|
remoteCache.Lock()
|
|
defer remoteCache.Unlock()
|
|
if remoteCache.list != nil && remoteCache.alpha == alpha && time.Since(remoteCache.at) < remoteCacheTTL {
|
|
return remoteCache.list, nil
|
|
}
|
|
list, err := rt.DefaultSource().ListRemote(a.baseCtx(), alpha)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
remoteCache.at, remoteCache.alpha, remoteCache.list = time.Now(), alpha, list
|
|
return list, nil
|
|
}
|
|
|
|
// forgetRemoteRuntimes drops the cached list (after an install or a channel change).
|
|
func forgetRemoteRuntimes() {
|
|
remoteCache.Lock()
|
|
remoteCache.list = nil
|
|
remoteCache.Unlock()
|
|
}
|
|
|
|
// SetRuntimeChannel picks "stable" or "alpha" for online installs
|
|
// ("candidate", the old name for alpha, is still accepted).
|
|
func (a *App) SetRuntimeChannel(channel string) error {
|
|
switch channel {
|
|
case rt.ChannelStable, rt.ChannelAlpha, "candidate":
|
|
channel = rt.NormalizeChannel(channel)
|
|
default:
|
|
return fmt.Errorf("unknown channel %q", channel)
|
|
}
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
a.cfg.RuntimeChannel = channel
|
|
forgetRemoteRuntimes()
|
|
return config.Save("", a.cfg)
|
|
}
|
|
|
|
// InstallRuntimeOnline downloads, verifies and installs a published runtime
|
|
// ("" or "latest" for the newest on the channel) and makes it active. It runs
|
|
// in the background and returns the task ID; CancelTask pauses the download,
|
|
// which resumes on the next install.
|
|
func (a *App) InstallRuntimeOnline(commit string) (string, error) {
|
|
a.mu.Lock()
|
|
if a.needsSetup {
|
|
a.mu.Unlock()
|
|
return "", errors.New("choose a data folder first")
|
|
}
|
|
cfg := *a.cfg
|
|
a.mu.Unlock()
|
|
|
|
subject := strings.TrimSpace(commit)
|
|
if subject == "" {
|
|
subject = "latest"
|
|
}
|
|
for _, t := range tasks.list() {
|
|
if t.Kind == "runtime-install" && t.Status == TaskRunning {
|
|
return t.ID, nil
|
|
}
|
|
}
|
|
a.ensureTaskEmitter()
|
|
ctx, cancel := context.WithCancel(a.baseCtx())
|
|
id := tasks.start("runtime-install", subject, "", cancel)
|
|
go func() {
|
|
defer cancel()
|
|
res, err := a.installRuntime(ctx, &cfg, commit, id)
|
|
switch {
|
|
case err == nil:
|
|
tasks.finish(id, res, nil)
|
|
case ctx.Err() != nil:
|
|
a.emitLog("runtime", "Runtime download paused; installing again continues where it stopped")
|
|
tasks.finish(id, "", context.Canceled)
|
|
default:
|
|
a.emitLog("runtime", "Runtime install error: "+err.Error())
|
|
tasks.finish(id, "", err)
|
|
}
|
|
if a.ctx != nil {
|
|
wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true)
|
|
}
|
|
}()
|
|
return id, nil
|
|
}
|
|
|
|
func (a *App) installRuntime(ctx context.Context, cfg *config.Config, commit, taskID string) (string, error) {
|
|
src := rt.DefaultSource()
|
|
step := func(msg string) {
|
|
a.emitLog("runtime", msg)
|
|
tasks.update(taskID, installer.Update{Kind: installer.KindStep, Message: msg, Percent: -1})
|
|
}
|
|
step("Looking up runtime releases")
|
|
// An explicit commit may be an alpha even on the stable channel: the
|
|
// user asked for it by name.
|
|
list, err := src.ListRemote(ctx, rt.NormalizeChannel(cfg.RuntimeChannel) == rt.ChannelAlpha || (commit != "" && commit != "latest"))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
r, err := rt.Pick(list, commit)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if !rt.IsInstalled(cfg, r.Commit) {
|
|
step("Downloading runtime " + r.Commit)
|
|
path, err := src.Download(ctx, *r, rt.DownloadsDir(cfg), func(done, total int64) {
|
|
tasks.update(taskID, installer.Update{
|
|
Kind: installer.KindProgress, Step: "downloading runtime " + r.Commit,
|
|
Done: done, Total: total, Percent: percent(done, total),
|
|
})
|
|
})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
step("Installing runtime " + r.Commit)
|
|
inst, err := rt.InstallFromArchive(cfg, path)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
_ = os.Remove(path) // the unpacked runtime is what's kept
|
|
r.Commit = inst.Commit
|
|
}
|
|
|
|
a.mu.Lock()
|
|
a.cfg.ActiveRuntime = r.Commit
|
|
err = config.Save("", a.cfg)
|
|
a.mu.Unlock()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
a.emitLog("runtime", fmt.Sprintf("Runtime %s (%s) installed and active", r.Commit, r.Channel))
|
|
return r.Commit, nil
|
|
}
|
|
|
|
func percent(done, total int64) float64 {
|
|
if total <= 0 {
|
|
return -1
|
|
}
|
|
return float64(done) * 100 / float64(total)
|
|
}
|
|
|
|
var appUpdateCache struct {
|
|
sync.Mutex
|
|
at time.Time
|
|
channel string
|
|
info *version.UpdateInfo
|
|
}
|
|
|
|
// SetAppUpdateChannel picks "stable", "alpha", or "dev" for Reims-Go client
|
|
// updates. Selecting "alpha" or "dev" also preserves old executables and
|
|
// downloaded installers on startup so users can roll back safely.
|
|
func (a *App) SetAppUpdateChannel(channel string) error {
|
|
switch strings.ToLower(strings.TrimSpace(channel)) {
|
|
case version.ChannelStable, version.ChannelAlpha, version.ChannelDev:
|
|
channel = version.NormalizeChannel(channel)
|
|
default:
|
|
return fmt.Errorf("unknown app update channel %q", channel)
|
|
}
|
|
a.mu.Lock()
|
|
a.cfg.AppUpdateChannel = channel
|
|
err := config.Save("", a.cfg)
|
|
a.mu.Unlock()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
appUpdateCache.Lock()
|
|
appUpdateCache.info = nil
|
|
appUpdateCache.channel = ""
|
|
appUpdateCache.Unlock()
|
|
appUpdateReady.Lock()
|
|
appUpdateReady.version, appUpdateReady.path, appUpdateReady.sha256 = "", "", ""
|
|
appUpdateReady.Unlock()
|
|
return nil
|
|
}
|
|
|
|
// CheckAppUpdate checks GitHub Releases for a newer Reims-Go version on the
|
|
// configured app update channel. Results are cached for 10 minutes unless force is true.
|
|
func (a *App) CheckAppUpdate(force bool) (version.UpdateInfo, error) {
|
|
a.mu.Lock()
|
|
ch := version.ChannelStable
|
|
if a.cfg != nil {
|
|
ch = version.NormalizeChannel(a.cfg.AppUpdateChannel)
|
|
}
|
|
a.mu.Unlock()
|
|
|
|
appUpdateCache.Lock()
|
|
defer appUpdateCache.Unlock()
|
|
if !force && appUpdateCache.info != nil && appUpdateCache.channel == ch && time.Since(appUpdateCache.at) < remoteCacheTTL {
|
|
return *appUpdateCache.info, nil
|
|
}
|
|
info, err := version.DefaultSource().CheckUpdateChannel(a.baseCtx(), version.Version, ch)
|
|
if err != nil {
|
|
return info, err
|
|
}
|
|
copyInfo := info
|
|
appUpdateCache.at = time.Now()
|
|
appUpdateCache.channel = ch
|
|
appUpdateCache.info = ©Info
|
|
return info, nil
|
|
}
|
|
|
|
// OpenURL opens an HTTPS link in the host's default browser.
|
|
func (a *App) OpenURL(raw string) error {
|
|
trimmed := strings.TrimSpace(raw)
|
|
if !strings.HasPrefix(trimmed, "https://") {
|
|
return fmt.Errorf("refusing to open non-HTTPS URL")
|
|
}
|
|
if a.ctx == nil {
|
|
return errors.New("window context not ready")
|
|
}
|
|
wailsRuntime.BrowserOpenURL(a.ctx, trimmed)
|
|
return nil
|
|
}
|