feat(selfupdate): follow release filename on in-app update
This commit is contained in:
1 parent
8969c53644
commit
354450ddcb
4 files changed
+101
-23
No files matched your search
@@ -98,7 +98,7 @@ flowchart TB
|
||||
### 4.5 应用内更新
|
||||
- 界面启动后检查一次新版本,之后每 6 小时检查一次(后端缓存 10 分钟),只看 `vX.Y.Z` 正式版,忽略草稿与预发布。有新版本时侧栏顶部显示更新胶囊卡片,关于页提供同样的操作。
|
||||
- 下载作为 `app-update` 任务进行,落在 `<DataRoot>\downloads\app\`,支持取消后续传。发布页没有 `SHA256SUMS.txt`、或文件与其中记录不符时拒绝安装;安装前再次校验,防止下载后被改动。
|
||||
- 安装不依赖独立的更新程序:Windows 不允许覆盖运行中的 exe,但允许重命名,因此把运行中的程序改名为 `<exe>.old`,把新文件放到原路径(失败即回滚),再以 `--wait-pid=<旧进程 PID>` 拉起新程序后退出。新进程先等旧进程退出再占用单实例锁,启动后清理 `.old` 与下载文件。文件名保持不变,快捷方式和自启动项继续有效。
|
||||
- 安装不依赖独立的更新程序:Windows 不允许覆盖运行中的 exe,但允许重命名,因此把运行中的程序改名为 `<exe>.old`,把新文件放到原路径(失败即回滚),再以 `--wait-pid=<旧进程 PID>` 拉起新程序后退出。新进程先等旧进程退出再占用单实例锁,启动后清理 `.old` 与下载文件。程序文件名若仍是发布时的带版本号名称(`reims-go-vX.Y.Z-windows-amd64.exe`),更新后改为新版本的名称;其他名称(如 `reims-go.exe`)保持不变,指向它的快捷方式继续有效。
|
||||
- 虚拟机是独立进程,更新期间继续运行;Guest Tools 桥接与输入法桥接在新界面启动后自动恢复。Win10 冷重启监视不会接回已在运行的虚拟机,这些虚拟机下次客体重启后需要手动启动,确认对话框会提示。
|
||||
- 开发构建(`wails dev`,`dev` 标签)与非 Windows 平台不支持应用内更新;程序所在目录不可写(如未提权的 `Program Files`)时提示改为手动更新。
|
||||
|
||||
|
||||
@@ -167,16 +167,18 @@ func (a *App) InstallAppUpdate() error {
|
||||
if err := selfupdate.Writable(filepath.Dir(exe)); err != nil {
|
||||
return a.updateErr("err.update_not_writable", filepath.Dir(exe))
|
||||
}
|
||||
if err := selfupdate.Swap(exe, path); err != nil {
|
||||
// A file still named after a release takes the new release's name.
|
||||
target := selfupdate.Target(exe, filepath.Base(path))
|
||||
if err := selfupdate.Swap(exe, target, path); err != nil {
|
||||
return err
|
||||
}
|
||||
a.emitLog("app", fmt.Sprintf("Installed update %s at %s; restarting", ver, exe))
|
||||
a.emitLog("app", fmt.Sprintf("Installed update %s at %s; restarting", ver, target))
|
||||
|
||||
a.uiHealth.mu.Lock()
|
||||
hidden := a.uiHealth.windowHidden
|
||||
a.uiHealth.mu.Unlock()
|
||||
args := cli.WindowArgs{Background: hidden, WaitPID: uint32(os.Getpid())}.Args()
|
||||
if err := selfupdate.Launch(exe, args); err != nil {
|
||||
if err := selfupdate.Launch(target, args); err != nil {
|
||||
a.emitLog("app", "Could not start the updated Reims-Go: "+err.Error())
|
||||
return a.updateErr("err.update_relaunch")
|
||||
}
|
||||
|
||||
@@ -2,11 +2,12 @@
|
||||
// that is running, without a separate updater process.
|
||||
//
|
||||
// Windows won't overwrite a running .exe but will rename it, so Swap moves the
|
||||
// running file aside to "<exe>.old" and puts the new one at the original
|
||||
// path; the running process keeps its mapped image. The caller then launches
|
||||
// the new file with WaitPIDFlag, which waits for this process to exit before
|
||||
// the window takes the single-instance lock, and quits. The next start
|
||||
// removes the ".old" file (Cleanup).
|
||||
// running file aside to "<exe>.old" and puts the new one at the target path
|
||||
// (Target: the original path, or the new release's file name when the running
|
||||
// file still has a release name); the running process keeps its mapped image.
|
||||
// The caller then launches the new file with --wait-pid, which waits for this
|
||||
// process to exit before the window takes the single-instance lock, and quits.
|
||||
// The next start removes the ".old" file (Cleanup).
|
||||
package selfupdate
|
||||
|
||||
import (
|
||||
@@ -15,11 +16,27 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// releaseName matches the file name release assets carry,
|
||||
// "reims-go-v0.2.6-windows-amd64.exe".
|
||||
var releaseName = regexp.MustCompile(`(?i)^reims-go-v\d+\.\d+\.\d+-windows-amd64\.exe$`)
|
||||
|
||||
// Target is where the update goes. A running file that still has a release
|
||||
// name ("reims-go-v0.2.5-windows-amd64.exe") takes the new release's name, so
|
||||
// the name keeps telling the version; any other name (say "reims-go.exe") is
|
||||
// kept, so shortcuts to it keep working.
|
||||
func Target(exe, assetName string) string {
|
||||
if releaseName.MatchString(filepath.Base(exe)) && releaseName.MatchString(assetName) {
|
||||
return filepath.Join(filepath.Dir(exe), assetName)
|
||||
}
|
||||
return exe
|
||||
}
|
||||
|
||||
// ErrDevBuild is returned by Supported for `wails dev` builds, whose
|
||||
// executable is the developer's build output, not an installed copy.
|
||||
var ErrDevBuild = errors.New("in-app update is disabled in development builds")
|
||||
@@ -59,11 +76,12 @@ func Writable(dir string) error {
|
||||
return os.Remove(name)
|
||||
}
|
||||
|
||||
// Swap installs newExe at exe. The running file becomes "<exe>.old" (or a
|
||||
// timestamped name when an earlier one is still locked). On any failure the
|
||||
// original executable is left in place.
|
||||
func Swap(exe, newExe string) error {
|
||||
staged := exe + ".new"
|
||||
// Swap installs newExe at target (see Target; usually exe itself) and moves
|
||||
// the running exe aside to "<exe>.old" (or a timestamped name when an earlier
|
||||
// one is still locked). A file already at target is replaced. On any failure
|
||||
// the original executable is left in place.
|
||||
func Swap(exe, target, newExe string) error {
|
||||
staged := target + ".new"
|
||||
if err := copyFile(newExe, staged); err != nil {
|
||||
os.Remove(staged)
|
||||
return fmt.Errorf("stage new executable: %w", err)
|
||||
@@ -76,7 +94,7 @@ func Swap(exe, newExe string) error {
|
||||
os.Remove(staged)
|
||||
return fmt.Errorf("move running executable aside: %w", err)
|
||||
}
|
||||
if err := os.Rename(staged, exe); err != nil {
|
||||
if err := os.Rename(staged, target); err != nil {
|
||||
if rerr := os.Rename(old, exe); rerr != nil {
|
||||
return fmt.Errorf("install new executable: %w (restoring the old one also failed: %v; it is at %s)", err, rerr, old)
|
||||
}
|
||||
@@ -86,14 +104,22 @@ func Swap(exe, newExe string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Cleanup removes what Swap leaves behind next to exe. Files still locked by
|
||||
// an exiting process are left for the next start.
|
||||
// Cleanup removes what Swap leaves behind next to exe, including the moved
|
||||
// aside file of an earlier release name. Files still locked by an exiting
|
||||
// process are left for the next start.
|
||||
func Cleanup(exe string) {
|
||||
_ = os.Remove(exe + ".new")
|
||||
_ = os.Remove(exe + ".old")
|
||||
if extra, err := filepath.Glob(exe + ".old-*"); err == nil {
|
||||
for _, p := range extra {
|
||||
_ = os.Remove(p)
|
||||
dir := filepath.Dir(exe)
|
||||
patterns := []string{exe + ".old", exe + ".old-*", filepath.Join(dir, "reims-go-v*-windows-amd64.exe.old*")}
|
||||
for _, pat := range patterns {
|
||||
matches, err := filepath.Glob(pat)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
for _, p := range matches {
|
||||
if p != exe {
|
||||
_ = os.Remove(p)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ func TestSwapAndCleanup(t *testing.T) {
|
||||
write(t, newExe, "new build")
|
||||
write(t, exe+".old", "left over from an earlier update")
|
||||
|
||||
if err := Swap(exe, newExe); err != nil {
|
||||
if err := Swap(exe, exe, newExe); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := read(t, exe); got != "new build" {
|
||||
@@ -56,7 +56,7 @@ func TestSwapLeavesExeOnFailure(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
exe := filepath.Join(dir, "reims-go.exe")
|
||||
write(t, exe, "old build")
|
||||
if err := Swap(exe, filepath.Join(dir, "missing.exe")); err == nil {
|
||||
if err := Swap(exe, exe, filepath.Join(dir, "missing.exe")); err == nil {
|
||||
t.Fatal("a missing download must fail")
|
||||
}
|
||||
if got := read(t, exe); got != "old build" {
|
||||
@@ -77,3 +77,53 @@ func TestWritable(t *testing.T) {
|
||||
t.Fatal("a missing folder is not writable")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTarget(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
asset := "reims-go-v0.2.6-windows-amd64.exe"
|
||||
cases := []struct{ running, want string }{
|
||||
{"reims-go-v0.2.5-windows-amd64.exe", asset}, // release name: follow the version
|
||||
{"Reims-Go-V0.2.5-Windows-AMD64.exe", asset}, // case does not matter
|
||||
{"reims-go.exe", "reims-go.exe"}, // own name: keep it for shortcuts
|
||||
{"reims-go-v0.2.5-windows-amd64 (1).exe", "reims-go-v0.2.5-windows-amd64 (1).exe"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := Target(filepath.Join(dir, c.running), asset); got != filepath.Join(dir, c.want) {
|
||||
t.Errorf("Target(%s) = %s, want %s", c.running, filepath.Base(got), c.want)
|
||||
}
|
||||
}
|
||||
if got := Target(filepath.Join(dir, "reims-go-v0.2.5-windows-amd64.exe"), "odd.exe"); filepath.Base(got) != "reims-go-v0.2.5-windows-amd64.exe" {
|
||||
t.Errorf("an asset without a release name must not rename, got %s", filepath.Base(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSwapRenamesReleaseFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
exe := filepath.Join(dir, "reims-go-v0.2.5-windows-amd64.exe")
|
||||
newExe := filepath.Join(t.TempDir(), "reims-go-v0.2.6-windows-amd64.exe")
|
||||
write(t, exe, "v0.2.5")
|
||||
write(t, newExe, "v0.2.6")
|
||||
target := Target(exe, filepath.Base(newExe))
|
||||
|
||||
if err := Swap(exe, target, newExe); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := read(t, target); got != "v0.2.6" {
|
||||
t.Fatalf("new name holds %q", got)
|
||||
}
|
||||
if _, err := os.Stat(exe); !os.IsNotExist(err) {
|
||||
t.Fatal("the old release name must be gone, not left beside the new one")
|
||||
}
|
||||
if got := read(t, exe+".old"); got != "v0.2.5" {
|
||||
t.Fatalf("old build moved aside = %q", got)
|
||||
}
|
||||
|
||||
// The new version's first start removes the old release's .old file.
|
||||
Cleanup(target)
|
||||
if _, err := os.Stat(exe + ".old"); !os.IsNotExist(err) {
|
||||
t.Fatal("Cleanup must remove the earlier release's .old file")
|
||||
}
|
||||
if got := read(t, target); got != "v0.2.6" {
|
||||
t.Fatal("Cleanup must not touch the running executable")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user