summaryrefslogtreecommitdiff
path: root/crates/typst-cli
diff options
context:
space:
mode:
authorYip Coekjan <69834864+Coekjan@users.noreply.github.com>2024-10-31 19:55:37 +0800
committerGitHub <noreply@github.com>2024-10-31 11:55:37 +0000
commitf0d7f54cbf374a85770bc8ec1075912ab8d4ecbe (patch)
treec2b009e025d4a93955e87573ca55cfa3142b4748 /crates/typst-cli
parentdcb130bd710a775846b94ba340c7be833a4fad2b (diff)
More consistent `self-update` assets determination (#5333)
Diffstat (limited to 'crates/typst-cli')
-rw-r--r--crates/typst-cli/src/update.rs51
1 files changed, 29 insertions, 22 deletions
diff --git a/crates/typst-cli/src/update.rs b/crates/typst-cli/src/update.rs
index b2b3932a..ec8ca71e 100644
--- a/crates/typst-cli/src/update.rs
+++ b/crates/typst-cli/src/update.rs
@@ -17,6 +17,28 @@ use crate::download::{self, PrintDownload};
const TYPST_GITHUB_ORG: &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.
///
/// 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(());
}
- let binary_data = release.download_binary(needed_asset()?, &downloader)?;
+ let binary_data = release.download_binary(determine_asset!(), &downloader)?;
let mut temp_exe = NamedTempFile::new()
.map_err(|err| eco_format!("failed to create temporary file ({err})"))?;
temp_exe
@@ -141,11 +163,12 @@ impl Release {
asset_name: &str,
downloader: &Downloader,
) -> StrResult<Vec<u8>> {
- let asset = self
- .assets
- .iter()
- .find(|a| a.name.starts_with(asset_name))
- .ok_or("could not find release for your target platform")?;
+ let asset = self.assets.iter().find(|a| a.name.starts_with(asset_name)).ok_or(
+ eco_format!(
+ "could not find prebuilt binary `{}` for your platform",
+ asset_name
+ ),
+ )?;
let data = match downloader.download_with_progress(
&asset.browser_download_url,
@@ -203,22 +226,6 @@ fn extract_binary_from_tar_xz(data: &[u8]) -> StrResult<Vec<u8>> {
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.
fn update_needed(release: &Release) -> StrResult<bool> {
let current_tag: Version = env!("CARGO_PKG_VERSION").parse().unwrap();