Files
Reims-Go/setup.go
T
千川白浪andClaude 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

187 lines
5.1 KiB
Go

package main
// First launch: the user picks the data folder before anything else is written
// (runtimes, VMs, installers and caches all live under it).
import (
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/Nwflower/reims-go/internal/config"
"github.com/Nwflower/reims-go/internal/host"
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
// DataRootChoice is a suggested data folder on one drive.
type DataRootChoice struct {
Path string `json:"path"`
FreeGB float64 `json:"free_gb"`
System bool `json:"system"`
External bool `json:"external"` // USB disk: may be unplugged while a VM runs
Recommended bool `json:"recommended"`
}
// DataRootCheck describes a folder the user is about to choose.
type DataRootCheck struct {
Path string `json:"path"`
FreeGB float64 `json:"free_gb"`
System bool `json:"system"`
External bool `json:"external"`
HasConfig bool `json:"has_config"` // an earlier reims-go data folder: its VMs are kept
Error string `json:"error,omitempty"`
}
// SuggestDataRoots offers <drive>\Data\reims-go on each fixed drive, the
// non-system drive with the most free space first.
func (a *App) SuggestDataRoots() []DataRootChoice {
var out []DataRootChoice
for _, d := range host.ListDrives() {
out = append(out, DataRootChoice{
Path: filepath.Join(d.Root, "Data", "reims-go"),
FreeGB: d.FreeGB,
System: d.System,
External: d.External,
})
}
// Internal non-system drives first, then the system drive, USB disks last;
// most free space first within each. A USB disk is never recommended.
rank := func(c DataRootChoice) int {
switch {
case c.External:
return 2
case c.System:
return 1
}
return 0
}
sort.SliceStable(out, func(i, j int) bool {
if rank(out[i]) != rank(out[j]) {
return rank(out[i]) < rank(out[j])
}
return out[i].FreeGB > out[j].FreeGB
})
if len(out) > 0 && !out[0].External {
out[0].Recommended = true
}
return out
}
// CheckDataRoot reports free space and whether dir already holds reims-go data.
func (a *App) CheckDataRoot(dir string) DataRootCheck {
dir = strings.TrimSpace(dir)
c := DataRootCheck{Path: dir}
if err := validateDataRoot(dir); err != nil {
c.Error = err.Error()
return c
}
c.FreeGB, _ = host.FreeGB(existingAncestor(dir))
c.System = isSystemVolume(dir)
vol := strings.ToUpper(filepath.VolumeName(dir))
for _, d := range host.ListDrives() {
if strings.EqualFold(d.Root[:2], vol) {
c.External = d.External
}
}
if _, err := os.Stat(filepath.Join(dir, "reims-go.json")); err == nil {
c.HasConfig = true
}
return c
}
// CompleteSetup makes dir the data folder: creates its layout, keeps an
// existing reims-go config there (its runtimes and VMs), records the choice for
// the next launch and leaves first-launch setup.
func (a *App) CompleteSetup(dir string) error {
dir = filepath.Clean(strings.TrimSpace(dir))
if err := validateDataRoot(dir); err != nil {
return err
}
a.mu.Lock()
locale := a.cfg.Locale
a.mu.Unlock()
cfgPath := filepath.Join(dir, "reims-go.json")
cfg, err := config.Load(cfgPath)
if err != nil {
return fmt.Errorf("read %s: %w", cfgPath, err)
}
cfg.DataRoot = dir
if _, statErr := os.Stat(cfgPath); statErr != nil {
cfg.Locale = locale // a new folder keeps the language picked on this screen
}
if err := cfg.EnsureDirs(); err != nil {
return err
}
if err := writable(dir); err != nil {
return err
}
if err := config.Save(cfgPath, cfg); err != nil {
return err
}
if err := config.SaveLocation(dir); err != nil {
return err
}
a.mu.Lock()
a.cfg = cfg
a.needsSetup = false
a.mu.Unlock()
if a.ctx != nil {
wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true)
}
return nil
}
func validateDataRoot(dir string) error {
if dir == "" {
return errors.New("choose a folder")
}
if !filepath.IsAbs(dir) {
return fmt.Errorf("%s is not a full path", dir)
}
if filepath.Clean(dir) == filepath.VolumeName(dir)+string(filepath.Separator) || filepath.Dir(dir) == dir {
return errors.New("choose a folder, not a whole drive")
}
if strings.ContainsAny(dir, ",") {
// reims-ctl passes kext paths to reims-util as "PATH,MIN,MAX" and refuses ','
// (upstream 3731afd reims_util.rs:308-317); the data folder holds them.
return errors.New("the folder path can't contain ','")
}
if info, err := os.Stat(dir); err == nil && !info.IsDir() {
return fmt.Errorf("%s is a file", dir)
}
return nil
}
// existingAncestor is dir or its nearest existing parent (for free-space checks
// before the folder exists).
func existingAncestor(dir string) string {
for d := dir; ; d = filepath.Dir(d) {
if _, err := os.Stat(d); err == nil {
return d
}
if filepath.Dir(d) == d {
return d
}
}
}
func isSystemVolume(dir string) bool {
sys := os.Getenv("SystemDrive")
vol := filepath.VolumeName(dir)
return sys != "" && vol != "" && strings.EqualFold(sys, vol)
}
func writable(dir string) error {
f, err := os.CreateTemp(dir, ".write-test-*")
if err != nil {
return fmt.Errorf("can't write to %s: %w", dir, err)
}
name := f.Name()
f.Close()
return os.Remove(name)
}