diff --git a/Makefile b/Makefile index 0d638cc..383bbc7 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index 8121e3f..32e073b 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/README.zh-CN.md b/README.zh-CN.md index 571ee75..04b2b3a 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -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 ``` diff --git a/internal/release/asset_naming_test.go b/internal/release/asset_naming_test.go index 1d298d1..807c63c 100644 --- a/internal/release/asset_naming_test.go +++ b/internal/release/asset_naming_test.go @@ -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) diff --git a/scripts/install.js b/scripts/install.js index ec76985..d54b8fc 100644 --- a/scripts/install.js +++ b/scripts/install.js @@ -240,6 +240,8 @@ if (require.main === module) { }); } else { module.exports = { + IS_WINDOWS, + BINARY_NAME, detectPlatform, loadPackageJson, buildUrl, diff --git a/scripts/update.js b/scripts/update.js index 7bc3f9e..9b3a634 100644 --- a/scripts/update.js +++ b/scripts/update.js @@ -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 {