mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
More consistent self-update
assets determination (#5333)
This commit is contained in:
parent
dcb130bd71
commit
f0d7f54cbf
@ -17,6 +17,28 @@ use crate::download::{self, PrintDownload};
|
|||||||
const TYPST_GITHUB_ORG: &str = "typst";
|
const TYPST_GITHUB_ORG: &str = "typst";
|
||||||
const TYPST_REPO: &str = "typst";
|
const TYPST_REPO: &str = "typst";
|
||||||
|
|
||||||
|
/// Determine the asset to download based on the target platform.
|
||||||
|
///
|
||||||
|
/// See `.github/workflows/release.yml` for the list of prebuilt assets.
|
||||||
|
macro_rules! determine_asset {
|
||||||
|
() => {
|
||||||
|
// For some platforms, only some targets are prebuilt in the release.
|
||||||
|
determine_asset!(__impl: {
|
||||||
|
"x86_64-unknown-linux-gnu" => "x86_64-unknown-linux-musl",
|
||||||
|
"aarch64-unknown-linux-gnu" => "aarch64-unknown-linux-musl",
|
||||||
|
"armv7-unknown-linux-gnueabi" => "armv7-unknown-linux-musleabi",
|
||||||
|
"riscv64gc-unknown-linux-musl" => "riscv64gc-unknown-linux-gnu",
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
(__impl: { $($origin:literal => $target:literal),* $(,)? }) => {
|
||||||
|
match env!("TARGET") {
|
||||||
|
$($origin => concat!("typst-", $target),)*
|
||||||
|
_ => concat!("typst-", env!("TARGET")),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/// Self update the Typst CLI binary.
|
/// Self update the Typst CLI binary.
|
||||||
///
|
///
|
||||||
/// Fetches a target release or the latest release (if no version was specified)
|
/// Fetches a target release or the latest release (if no version was specified)
|
||||||
@ -77,7 +99,7 @@ pub fn update(command: &UpdateCommand) -> StrResult<()> {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let binary_data = release.download_binary(needed_asset()?, &downloader)?;
|
let binary_data = release.download_binary(determine_asset!(), &downloader)?;
|
||||||
let mut temp_exe = NamedTempFile::new()
|
let mut temp_exe = NamedTempFile::new()
|
||||||
.map_err(|err| eco_format!("failed to create temporary file ({err})"))?;
|
.map_err(|err| eco_format!("failed to create temporary file ({err})"))?;
|
||||||
temp_exe
|
temp_exe
|
||||||
@ -141,11 +163,12 @@ impl Release {
|
|||||||
asset_name: &str,
|
asset_name: &str,
|
||||||
downloader: &Downloader,
|
downloader: &Downloader,
|
||||||
) -> StrResult<Vec<u8>> {
|
) -> StrResult<Vec<u8>> {
|
||||||
let asset = self
|
let asset = self.assets.iter().find(|a| a.name.starts_with(asset_name)).ok_or(
|
||||||
.assets
|
eco_format!(
|
||||||
.iter()
|
"could not find prebuilt binary `{}` for your platform",
|
||||||
.find(|a| a.name.starts_with(asset_name))
|
asset_name
|
||||||
.ok_or("could not find release for your target platform")?;
|
),
|
||||||
|
)?;
|
||||||
|
|
||||||
let data = match downloader.download_with_progress(
|
let data = match downloader.download_with_progress(
|
||||||
&asset.browser_download_url,
|
&asset.browser_download_url,
|
||||||
@ -203,22 +226,6 @@ fn extract_binary_from_tar_xz(data: &[u8]) -> StrResult<Vec<u8>> {
|
|||||||
Ok(buffer)
|
Ok(buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Determine what asset to download according to the target platform the CLI
|
|
||||||
/// is running on.
|
|
||||||
fn needed_asset() -> StrResult<&'static str> {
|
|
||||||
Ok(match env!("TARGET") {
|
|
||||||
"x86_64-unknown-linux-gnu" => "typst-x86_64-unknown-linux-musl",
|
|
||||||
"x86_64-unknown-linux-musl" => "typst-x86_64-unknown-linux-musl",
|
|
||||||
"aarch64-unknown-linux-musl" => "typst-aarch64-unknown-linux-musl",
|
|
||||||
"aarch64-unknown-linux-gnu" => "typst-aarch64-unknown-linux-musl",
|
|
||||||
"armv7-unknown-linux-musleabi" => "typst-armv7-unknown-linux-musleabi",
|
|
||||||
"x86_64-apple-darwin" => "typst-x86_64-apple-darwin",
|
|
||||||
"aarch64-apple-darwin" => "typst-aarch64-apple-darwin",
|
|
||||||
"x86_64-pc-windows-msvc" => "typst-x86_64-pc-windows-msvc",
|
|
||||||
target => bail!("unsupported target: {target}"),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Compare the release version to the CLI version to see if an update is needed.
|
/// Compare the release version to the CLI version to see if an update is needed.
|
||||||
fn update_needed(release: &Release) -> StrResult<bool> {
|
fn update_needed(release: &Release) -> StrResult<bool> {
|
||||||
let current_tag: Version = env!("CARGO_PKG_VERSION").parse().unwrap();
|
let current_tag: Version = env!("CARGO_PKG_VERSION").parse().unwrap();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user