251 lines
7.1 KiB
Go
251 lines
7.1 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
|
|
}
|
|
|
|
func (a *App) runtimeSource() string {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
return rt.NormalizeSource(a.cfg.RuntimeSource)
|
|
}
|
|
|
|
// 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
|
|
source string
|
|
list []rt.Remote
|
|
}
|
|
|
|
const remoteCacheTTL = 10 * time.Minute
|
|
|
|
// ListRemoteRuntimes lists published runtimes for the configured channel and source.
|
|
func (a *App) ListRemoteRuntimes() ([]rt.Remote, error) {
|
|
alpha := a.includeAlpha()
|
|
source := a.runtimeSource()
|
|
remoteCache.Lock()
|
|
defer remoteCache.Unlock()
|
|
if remoteCache.list != nil && remoteCache.alpha == alpha && remoteCache.source == source && time.Since(remoteCache.at) < remoteCacheTTL {
|
|
return remoteCache.list, nil
|
|
}
|
|
list, err := rt.DefaultSourceFor(source).ListRemote(a.baseCtx(), alpha)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
remoteCache.at, remoteCache.alpha, remoteCache.source, remoteCache.list = time.Now(), alpha, source, list
|
|
return list, nil
|
|
}
|
|
|
|
// forgetRemoteRuntimes drops the cached list (after an install or a channel/source 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)
|
|
}
|
|
|
|
// SetRuntimeSource picks "auto", "github", or "gitcode" for online runtime and
|
|
// app update lookups.
|
|
func (a *App) SetRuntimeSource(source string) error {
|
|
switch strings.ToLower(strings.TrimSpace(source)) {
|
|
case "", rt.SourceAuto, rt.SourceGitHub, rt.SourceGitCode:
|
|
source = rt.NormalizeSource(source)
|
|
default:
|
|
return fmt.Errorf("unknown runtime source %q", source)
|
|
}
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
a.cfg.RuntimeSource = source
|
|
forgetRemoteRuntimes()
|
|
appUpdateCache.Lock()
|
|
appUpdateCache.info = nil
|
|
appUpdateCache.Unlock()
|
|
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.DefaultSourceFor(cfg.RuntimeSource)
|
|
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
|
|
info *version.UpdateInfo
|
|
}
|
|
|
|
// CheckAppUpdate checks GitHub Releases for a newer stable Reims-Go version.
|
|
// Results are cached for 10 minutes unless force is true.
|
|
func (a *App) CheckAppUpdate(force bool) (version.UpdateInfo, error) {
|
|
source := a.runtimeSource()
|
|
appUpdateCache.Lock()
|
|
defer appUpdateCache.Unlock()
|
|
if !force && appUpdateCache.info != nil && time.Since(appUpdateCache.at) < remoteCacheTTL {
|
|
return *appUpdateCache.info, nil
|
|
}
|
|
info, err := version.DefaultSourceFor(source).CheckUpdate(a.baseCtx(), version.Version)
|
|
if err != nil {
|
|
return info, err
|
|
}
|
|
copyInfo := info
|
|
appUpdateCache.at = time.Now()
|
|
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
|
|
}
|