Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update macOS asset matching #169

Merged
merged 16 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/asset-platform.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const { PLATFORM_ARCH } = require("./constants");

const assetPlatform = (fileName) => {
if (/.*(mac|darwin|osx).*(-arm).*\.zip/i.test(fileName)) {
return PLATFORM_ARCH.DARWIN_ARM64;
}

if (/.*(mac|darwin|osx).*\.zip/i.test(fileName) && !/arm64/.test(fileName)) {
if (/.*(mac|darwin|osx).*\.zip$/i.test(fileName)) {
setchy marked this conversation as resolved.
Show resolved Hide resolved
if (/universal/.test(fileName)) return PLATFORM_ARCH.DARWIN_UNIVERSAL;
if (/arm64/.test(fileName)) return PLATFORM_ARCH.DARWIN_ARM64;
setchy marked this conversation as resolved.
Show resolved Hide resolved
return PLATFORM_ARCH.DARWIN_X64;
}

Expand Down
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const PLATFORM = {
const PLATFORM_ARCH = {
DARWIN_X64: "darwin-x64",
DARWIN_ARM64: "darwin-arm64",
DARWIN_UNIVERSAL: "darwin-universal",
WIN_X64: "win32-x64",
WIN_IA32: "win32-ia32",
WIN_ARM64: "win32-arm64",
Expand Down
21 changes: 19 additions & 2 deletions src/updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,33 @@ class Updates {
}

async handleUpdate(res, account, repository, platform, version) {
const latest = await this.cachedGetLatest(
let latest = await this.cachedGetLatest(
account,
repository,
platform,
version
);

if (platform.includes(PLATFORM.DARWIN)) {
const latestUniversal = await this.cachedGetLatest(
account,
repository,
PLATFORM_ARCH.DARWIN_UNIVERSAL,
version
);

if (
latestUniversal &&
semver.gt(latestUniversal.version, latest.version)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarshallOfSound - moved the semver check here to be part of the universal replacement logic

) {
log.info("Falling back to universal build for darwin");
latest = latestUniversal;
}
}

if (!latest) {
const message = platform.includes(PLATFORM.DARWIN)
? "No updates found (needs asset matching *(mac|darwin|osx).*(-arm).*.zip in public repository)"
? "No updates found (needs asset matching *(mac|darwin|osx).*.zip in public repository)"
: "No updates found (needs asset containing win32-{x64,ia32,arm64} or .exe in public repository)";
notFound(res, message);
} else if (semver.lte(latest.version, version)) {
Expand Down
16 changes: 16 additions & 0 deletions test/asset-platform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ test("assetPlatform() matches the right platform", (t) => {
name: "Electron.Fiddle-darwin-x64-0.27.3.zip",
platform: PLATFORM_ARCH.DARWIN_X64,
},
{
name: "Electron-Builder-1.2.3-mac.zip",
platform: PLATFORM_ARCH.DARWIN_X64,
},
{
name: "Electron-Builder-1.2.3-universal-mac.zip",
platform: PLATFORM_ARCH.DARWIN_UNIVERSAL,
},
{
name: "Electron-Builder-1.2.3-arm64-mac.zip",
platform: PLATFORM_ARCH.DARWIN_ARM64,
},
{
name: "Electron.Fiddle-1.62.6-mac.zip.blockmap",
platform: false,
},
];

for (const release of releases) {
Expand Down
87 changes: 83 additions & 4 deletions test/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ nock("https://api.github.com")
name: "mac-arm64.zip",
browser_download_url: "mac-arm64.zip",
},
{
name: "universal-mac.zip",
browser_download_url: "universal-mac.zip",
},
{
name: "electron-fiddle-1.0.0-win32-x64-setup.exe",
browser_download_url: "electron-fiddle-1.0.0-win32-x64-setup.exe",
Expand Down Expand Up @@ -77,6 +81,43 @@ nock("https://api.github.com")
name: "darwin-arm64.zip",
browser_download_url: "darwin-arm64.zip",
},
{
name: "universal-mac.zip",
browser_download_url: "universal-mac.zip",
},
],
},
])
.get("/repos/owner/repo-universal/releases?per_page=100")
.reply(200, [
{
name: "name",
tag_name: "v2.0.0",
body: "notes",
assets: [
{
name: "universal-mac.zip",
browser_download_url: "universal-mac.zip",
},
{
name: "arm64-mac.zip",
browser_download_url: "arm64-mac.zip",
},
],
},
{
name: "name",
tag_name: "v1.0.0",
body: "notes",
assets: [
{
name: "mac.zip",
browser_download_url: "mac.zip",
},
{
name: "arm64-mac.zip",
browser_download_url: "arm64-mac.zip",
},
],
},
])
Expand Down Expand Up @@ -321,6 +362,11 @@ test("Updates", async (t) => {
body = await res.json();
t.equal(body.url, "mac.zip");

res = await fetch(`${address}/owner/repo/darwin-universal/0.0.0`);
t.equal(res.status, 200);
body = await res.json();
t.equal(body.url, "universal-mac.zip");

res = await fetch(`${address}/owner/repo-darwin/darwin-x64/0.0.0`);
t.equal(res.status, 200);
body = await res.json();
Expand All @@ -335,6 +381,13 @@ test("Updates", async (t) => {
t.equal(res.status, 200);
body = await res.json();
t.match(body.url, "darwin.zip");

res = await fetch(
`${address}/owner/repo-darwin/darwin-universal/0.0.0`
);
t.equal(res.status, 200);
body = await res.json();
t.match(body.url, "universal-mac.zip");
}
});

Expand All @@ -346,15 +399,41 @@ test("Updates", async (t) => {
let body = await res.text();
t.equal(
body,
"No updates found (needs asset matching *(mac|darwin|osx).*(-arm).*.zip in public repository)"
"No updates found (needs asset matching *(mac|darwin|osx).*.zip in public repository)"
);

res = await fetch(`${address}/owner/repo-win32-zip/darwin/0.0.0`);
t.equal(res.status, 404);
body = await res.text();
t.equal(
body,
"No updates found (needs asset matching *(mac|darwin|osx).*(-arm).*.zip in public repository)"
"No updates found (needs asset matching *(mac|darwin|osx).*.zip in public repository)"
);
});

await t.test("darwin universal assets", async (t) => {
await t.test(
"use universal asset if platform-specific asset not available",
async (t) => {
let res = await fetch(
`${address}/owner/repo-universal/darwin-x64/0.0.0`
);
t.equal(res.status, 200);
let body = await res.json();
t.match(body.url, "universal-mac.zip");
}
);

await t.test(
"skip universal asset if platform-specific asset available",
async (t) => {
let res = await fetch(
`${address}/owner/repo-universal/darwin-arm64/0.0.0`
);
t.equal(res.status, 200);
let body = await res.json();
t.match(body.url, "arm64-mac.zip");
}
);
});
});
Expand Down Expand Up @@ -491,7 +570,7 @@ test("Updates", async (t) => {
const body = await res.text();
t.equal(
body,
'Unsupported platform: "linux". Supported: darwin-x64, darwin-arm64, win32-x64, win32-ia32, win32-arm64.'
'Unsupported platform: "linux". Supported: darwin-x64, darwin-arm64, darwin-universal, win32-x64, win32-ia32, win32-arm64.'
);
});
});
Expand All @@ -503,7 +582,7 @@ test("Updates", async (t) => {
const body = await res.text();
t.equal(
body,
'Unsupported platform: "os". Supported: darwin-x64, darwin-arm64, win32-x64, win32-ia32, win32-arm64.'
'Unsupported platform: "os". Supported: darwin-x64, darwin-arm64, darwin-universal, win32-x64, win32-ia32, win32-arm64.'
);
});
});
Expand Down