问题1:
我使用Postman来验证我的自动更新服务器URL是否返回我期望的响应。当我知道这些URL提供了预期的结果时,我知道我可以在我的应用程序的Electron's Auto Updater中使用这些URL。
使用Postman测试Mac端点的示例:
Request:
https://my-server.com/api/macupdates/checkforupdate.php?appversion=1.0.5&cpuarchitecture=x64
当有更新可用时的JSON响应:
{
"url": "https:/my-server.com/updates/darwin/x64/my-electron=app-x64-1.1.0.zip",
"name": "1.1.0",
"pub_date": "2021-07-03T15:17:12+00:00"
}
问题2:
是的,在Mac上使用自动更新功能,你的Electron应用程序必须进行代码签名。至于Windows,我不确定,因为我的Windows Electron应用程序已经进行了代码签名,并且我没有尝试过没有代码签名的情况。尽管如此,建议您即使自动更新可以在没有代码签名的情况下正常工作(不仅是出于安全原因,而主要是因为否则当用户第一次安装应用程序时,他们会从Windows收到可怕的危险警告,他们可能会立即将其删除),也应该签署应用程序。
问题3:
为了获得良好的文档,您应该从官方Electron自动更新程序文档开始,截至2021年7月7日,它非常好。
难点在于如何让Mac正常工作。对于Windows来说,只需要几分钟就可以完成。实际上...
对于Windows自动更新,设置很容易 - 您只需将RELEASES和nupkg文件放在服务器上,然后将该URL用作Electron App的autoUpdater中的FeedURL。因此,如果您的应用程序的更新文件位于https://my-server.com/updates/win32/x64/ - 您只需将Electron Auto Updater指向该URL即可。
针对 Mac 自动更新,您需要手动指定最新的 Electron App .zip 文件的绝对 URL 给 Electron autoUpdater。因此,为了使 Mac autoUpdater 正常工作,您需要找到一种方法来获取 一个特定格式的 JSON 响应。不幸的是,您不能只是将 Electron App 的文件放在服务器上,然后期望它能像其他平台一样与 Mac 兼容。相反,autoUpdater 需要一个 URL 来返回上述 JSON 响应。因此,您需要将 Electron 的 Auto Updater feedURL
传递给能够返回这种预期 JSON 响应的 URL。
您可以使用任何方式来实现这一点,但我使用 PHP,因为那是我已经支付的服务器。
因此,总之,在 Mac 上,即使您的文件位于 https://my-server.com/updates/darwin/x64/ - 您也不会将该 URL 提供给 Electron 的 Auto Updater FeedURL。而是提供另一个 URL,该 URL 返回所需的 JSON 响应。
这是我应用程序的 Electron 主进程的 main.js 文件的示例:
function registerAutoUpdater() {
const appVersion = app.getVersion();
const os = require('os');
const cpuArchitecture = os.arch();
const domain = 'https://my-server.com';
const windowsURL = `${domain}/updates/win32/x64`;
const macURL = `${domain}/api/macupdates/checkforupdate.php?appversion=${appVersion}&cpuarchitecture=${cpuArchitecture}`;
const autoUpdateURL = `${isMac ? macURL : windowsURL}`;
autoUpdater.setFeedURL({url: autoUpdateURL});
log.info('Registered autoUpdateURL = ' + (isMac ? 'macURL' : 'windowsURL'));
autoUpdater.checkForUpdates();
setInterval(() => {
autoUpdater.checkForUpdates();
}, 7200000);
}
这里是一个示例checkforupdate.php文件,它将预期的JSON响应返回给Electron自动更新程序:
<?php
$clientAppVersion = $_GET["appversion"] ?? null;
if (!isValidVersionString($clientAppVersion)) {
http_response_code(204);
exit();
}
$clientCpuArchitecture = $_GET["cpuarchitecture"] ?? null;
$latestVersionInfo = getLatestVersionInfo($clientAppVersion, $clientCpuArchitecture);
if (!isset($latestVersionInfo["versionNumber"])) {
http_response_code(204);
exit();
}
$isUpdateVailable = isUpdateAvailable($clientAppVersion, $latestVersionInfo["versionNumber"]);
if ($isUpdateVailable) {
http_response_code(200);
header('Content-Type: application/json;charset=utf-8');
$jsonResponse = array(
"url" => $latestVersionInfo["directZipFileURL"],
"name" => $latestVersionInfo["versionNumber"],
"pub_date" => date('c', $latestVersionInfo["createdAtUnixTimeStamp"]),
);
echo json_encode($jsonResponse);
} else {
http_response_code(204);
}
exit();
function getLatestVersionInfo($clientAppVersion, $clientCpuArchitecture): array {
if ($clientCpuArchitecture === 'arm64') {
$directory = "../../updates/darwin/arm64/";
$baseUrl = "https://my-server.com/updates/darwin/arm64/";
} else if (!$clientCpuArchitecture || $clientCpuArchitecture === 'x64') {
$directory = "../../updates/darwin/";
$baseUrl = "https://my-server.com/updates/darwin/";
}
$latestVersionFileName = "Finance D - Tenue de livres-darwin-x64-0.0.0.zip";
$arrayOfFiles = scandir($directory);
foreach ($arrayOfFiles as $file) {
if (is_file($directory . $file)) {
$serverFileVersion = getVersionNumberFromFileName($file);
if (isVersionNumberGreater($serverFileVersion, $clientAppVersion)) {
$latestVersionFileName = $file;
}
}
}
return array(
"versionNumber" => getVersionNumberFromFileName($latestVersionFileName),
"directZipFileURL" => $baseUrl . rawurlencode($latestVersionFileName),
"createdAtUnixTimeStamp" => filemtime(realpath($directory . $latestVersionFileName))
);
}
function isUpdateAvailable($clientVersion, $serverVersion): bool {
return
isValidVersionString($clientVersion) &&
isValidVersionString($serverVersion) &&
isVersionNumberGreater($serverVersion, $clientVersion);
}
function getVersionNumberFromFileName($fileName) {
return preg_replace("/Finance D - Tenue de livres-darwin-(x64|arm64)-|\.zip/", "", $fileName);
}
function removeAllNonDigits($semanticVersionString) {
return preg_replace("/\D+/", "", $semanticVersionString);
}
function isVersionNumberGreater($serverFileVersion, $clientFileVersion): bool {
return removeAllNonDigits($serverFileVersion) > removeAllNonDigits($clientFileVersion);
}
function isValidVersionString($versionString) {
return preg_match("/\d\.\d\.\d/", $versionString);
}