fix(build): improve Windows support based on PR #13 review feedback

Unify BUILD_PLATFORM macro with optional suffix parameter to eliminate
  duplication, centralize IS_WINDOWS/BINARY_NAME exports in install.js,
  add Windows-safe rename-then-replace strategy in update.js, and add
  PATH guidance in README for Windows users.
This commit is contained in:
kite 2026-06-02 22:37:27 +08:00
parent 89effb6b22
commit 558ffe9a91
6 changed files with 42 additions and 20 deletions

View file

@ -21,7 +21,7 @@ LD_FLAGS := -s -w \
define BUILD_PLATFORM
GOOS=$(1) GOARCH=$(2) CGO_ENABLED=0 $(GO) build -ldflags "$(LD_FLAGS)" \
-o $(DIST_DIR)/$(BINARY_NAME)-$(1)-$(2) \
-o $(DIST_DIR)/$(BINARY_NAME)-$(1)-$(2)$(3) \
./cmd/opencodereview
endef
@ -55,14 +55,10 @@ build-darwin-arm64:
$(call BUILD_PLATFORM,darwin,arm64)
build-windows-amd64:
GOOS=windows GOARCH=amd64 CGO_ENABLED=0 $(GO) build -ldflags "$(LD_FLAGS)" \
-o $(DIST_DIR)/$(BINARY_NAME)-windows-amd64.exe \
./cmd/opencodereview
$(call BUILD_PLATFORM,windows,amd64,.exe)
build-windows-arm64:
GOOS=windows GOARCH=arm64 CGO_ENABLED=0 $(GO) build -ldflags "$(LD_FLAGS)" \
-o $(DIST_DIR)/$(BINARY_NAME)-windows-arm64.exe \
./cmd/opencodereview
$(call BUILD_PLATFORM,windows,arm64,.exe)
build-all: build-linux-amd64 build-linux-arm64 build-darwin-amd64 build-darwin-arm64 build-windows-amd64 build-windows-arm64

View file

@ -90,10 +90,10 @@ chmod +x ocr && sudo mv ocr /usr/local/bin/ocr
curl -Lo ocr https://github.com/alibaba/open-code-review/releases/latest/download/opencodereview-linux-arm64
chmod +x ocr && sudo mv ocr /usr/local/bin/ocr
# Windows (x86_64)
# Windows (x86_64) — move ocr.exe to a directory in your PATH
curl -Lo ocr.exe https://github.com/alibaba/open-code-review/releases/latest/download/opencodereview-windows-amd64.exe
# Windows (ARM64)
# Windows (ARM64) — move ocr.exe to a directory in your PATH
curl -Lo ocr.exe https://github.com/alibaba/open-code-review/releases/latest/download/opencodereview-windows-arm64.exe
```

View file

@ -90,10 +90,10 @@ chmod +x ocr && sudo mv ocr /usr/local/bin/ocr
curl -Lo ocr https://github.com/alibaba/open-code-review/releases/latest/download/opencodereview-linux-arm64
chmod +x ocr && sudo mv ocr /usr/local/bin/ocr
# Windows (x86_64)
# Windows (x86_64) — 将 ocr.exe 移动到 PATH 目录中
curl -Lo ocr.exe https://github.com/alibaba/open-code-review/releases/latest/download/opencodereview-windows-amd64.exe
# Windows (ARM64)
# Windows (ARM64) — 将 ocr.exe 移动到 PATH 目录中
curl -Lo ocr.exe https://github.com/alibaba/open-code-review/releases/latest/download/opencodereview-windows-arm64.exe
```

View file

@ -141,23 +141,29 @@ func TestMakefileBuildMatchesURLPattern(t *testing.T) {
}
outputTemplate := m[1]
platforms := []struct{ os, arch string }{
{"linux", "amd64"},
{"linux", "arm64"},
{"darwin", "amd64"},
{"darwin", "arm64"},
platforms := []struct{ os, arch, ext string }{
{"linux", "amd64", ""},
{"linux", "arm64", ""},
{"darwin", "amd64", ""},
{"darwin", "arm64", ""},
{"windows", "amd64", ".exe"},
{"windows", "arm64", ".exe"},
}
for _, p := range platforms {
rendered := outputTemplate
rendered = strings.ReplaceAll(rendered, "$(BINARY_NAME)", "opencodereview")
rendered = strings.ReplaceAll(rendered, "$(1)", p.os)
rendered = strings.ReplaceAll(rendered, "$(2)", p.arch)
rendered = strings.ReplaceAll(rendered, "$(3)", p.ext)
url := pkg.OcrConfig.URLPattern
url = strings.ReplaceAll(url, "{version}", "1.0.7")
url = strings.ReplaceAll(url, "{os}", p.os)
url = strings.ReplaceAll(url, "{arch}", p.arch)
expected := extractFilenameFromURL(url)
if p.ext != "" {
expected += p.ext
}
if rendered != expected {
t.Errorf("Makefile produces %q, urlPattern expects %q", rendered, expected)

View file

@ -240,6 +240,8 @@ if (require.main === module) {
});
} else {
module.exports = {
IS_WINDOWS,
BINARY_NAME,
detectPlatform,
loadPackageJson,
buildUrl,

View file

@ -9,6 +9,8 @@ const https = require("https");
const { spawnSync } = require("child_process");
const {
IS_WINDOWS,
BINARY_NAME,
detectPlatform,
loadPackageJson,
buildUrl,
@ -16,9 +18,6 @@ const {
downloadBinary,
computeChecksum,
} = require("./install.js");
const IS_WINDOWS = process.platform === "win32";
const BINARY_NAME = IS_WINDOWS ? "opencodereview.exe" : "opencodereview";
const packageRoot = path.join(__dirname, "..");
const binDir = path.join(packageRoot, "bin");
const binaryPath = path.join(binDir, BINARY_NAME);
@ -197,7 +196,26 @@ async function main() {
}
}
fs.renameSync(tempPath, binaryPath);
if (IS_WINDOWS) {
const oldPath = binaryPath + ".old";
try { fs.unlinkSync(oldPath); } catch (_) {}
try {
fs.renameSync(binaryPath, oldPath);
} catch (e) {
if (fs.existsSync(binaryPath)) {
throw e;
}
}
try {
fs.renameSync(tempPath, binaryPath);
} catch (e) {
try { fs.renameSync(oldPath, binaryPath); } catch (_) {}
throw e;
}
try { fs.unlinkSync(oldPath); } catch (_) {}
} else {
fs.renameSync(tempPath, binaryPath);
}
} catch (_) {
cleanupTemp();
} finally {