Files
Reims-Go/runtime_bindings.go
T
NwflowerandClaude Opus 5.5 05cc34cbaa feat(ui): declutter the host page; cache the runtime release list
- Four stat tiles always on one row; drop the P/E-core line, os/arch and
  the data-folder path from the tiles. The WHPX tile carries "Enable" when
  WHPX is off, replacing the duplicate WHPX row.
- Runtime: one list with the current runtime (QEMU version, "Up to date" or
  "Update") and the candidate switch; no install path, no separate
  online-install row once a runtime is installed.
- "System" and "Preferences" merge into one "Settings" section: data folder
  (shown once), P-core pinning (no hex mask), language.
- Graphics cards show name and badges; the Vulkan ICD path moves to a tooltip.
- ListRemoteRuntimes caches for 10 minutes (GitHub allows 60 anonymous API
  requests an hour); a channel change clears it.
- Roadmap: M2.2 done; M2.1 and M2.3 progress for v0.2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 05:12:48 -07:00

181 lines
5.0 KiB
Go

package main
// 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"
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) includeCandidate() bool {
a.mu.Lock()
defer a.mu.Unlock()
return a.cfg.RuntimeChannel == rt.ChannelCandidate
}
// 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
candidate bool
list []rt.Remote
}
const remoteCacheTTL = 10 * time.Minute
// ListRemoteRuntimes lists published runtimes for the configured channel.
func (a *App) ListRemoteRuntimes() ([]rt.Remote, error) {
cand := a.includeCandidate()
remoteCache.Lock()
defer remoteCache.Unlock()
if remoteCache.list != nil && remoteCache.candidate == cand && time.Since(remoteCache.at) < remoteCacheTTL {
return remoteCache.list, nil
}
list, err := rt.DefaultSource().ListRemote(a.baseCtx(), cand)
if err != nil {
return nil, err
}
remoteCache.at, remoteCache.candidate, remoteCache.list = time.Now(), cand, 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 "candidate" for online installs.
func (a *App) SetRuntimeChannel(channel string) error {
if channel != rt.ChannelStable && channel != rt.ChannelCandidate {
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 on GitHub")
// An explicit commit may be a candidate even on the stable channel: the
// user asked for it by name.
list, err := src.ListRemote(ctx, cfg.RuntimeChannel == rt.ChannelCandidate || (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)
}