Files
Reims-Go/runtime_bindings.go
T
NwflowerandClaude Opus 5.5 f6f6bfce34 feat: online runtime install, first-launch data folder picker, source packages
- Runtime: list and fetch runtimes from this repo's GitHub Releases
  (stable by default, candidate on request), with SHA256 check and resume.
  CLI gains `runtime available` and `runtime fetch`; the host page gets an
  online install row and a "receive candidates" switch.
- First launch: pick a data folder (non-system drives first, USB never
  recommended); the choice is recorded in %APPDATA%\reims-go\location.json.
  Existing installs in the default folder are adopted without asking.
- pack-runtime.ps1: also builds the matching source package, records every
  patch with its SHA256 and the channel, and refuses undeclared changes in
  the build trees.
- patches: declare reims-vgpu-cargo-lock.patch (Cargo.lock source lines).
- UI: sidebar and VM page restyle, new design tokens.

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

148 lines
4.1 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"
"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
}
// ListRemoteRuntimes lists published runtimes for the configured channel.
func (a *App) ListRemoteRuntimes() ([]rt.Remote, error) {
return rt.DefaultSource().ListRemote(a.baseCtx(), a.includeCandidate())
}
// 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
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)
}