54 个版本 (破坏性更新)

0.41.0 2024年7月3日
0.39.0 2023年10月23日
0.37.0 2023年6月3日
0.36.0 2023年2月20日
0.2.5 2017年7月28日

#16 in 开发工具

Download history 15677/week @ 2024-05-03 18066/week @ 2024-05-10 16631/week @ 2024-05-17 13815/week @ 2024-05-24 19088/week @ 2024-05-31 16312/week @ 2024-06-07 15848/week @ 2024-06-14 21448/week @ 2024-06-21 19129/week @ 2024-06-28 15722/week @ 2024-07-05 15942/week @ 2024-07-12 17498/week @ 2024-07-19 17176/week @ 2024-07-26 13935/week @ 2024-08-02 14000/week @ 2024-08-09 15443/week @ 2024-08-16

64,063 每月下载量
用于 78 个 Crates (73 个直接使用)

MIT 许可证

160KB
3.5K SLoC

self_update

Build status Build Status crates.io:clin docs

self_update 提供了用于从各种发布分布后端就地更新 Rust 可执行文件的更新器。

用法

更新(替换)当前可执行文件,使用从 https://api.github.com/repos/jaemk/self_update/releases/latest 下载的最新版本。注意,trust 项目提供了一个通过 CI(travis/appveyor)生成发布构建的设置。

特性

以下 Cargo 特性 可用(但默认情况下是 禁用 的)

  • archive-tar:支持 tar 存档格式;
  • archive-zip:支持 zip 存档格式;
  • compression-flate2:支持 gzip 压缩;
  • compression-zip-deflate:支持 zipdeflate 压缩格式;
  • compression-zip-bzip2:支持 zipbzip2 压缩格式;
  • rustls:使用 纯 Rust TLS 实现 进行网络请求。该特性不支持 32 位 macOS;
  • signatures:使用 zipsign 验证 .zip.tar.gz 文件。假定文件已使用 zipsign 签名。

请激活您发布文件所需的特性。

示例

运行以下示例以查看 self_update 的实际应用

cargorun --examplegithub --features "archive-tar archive-zip compression-flate2 compression-zip-deflate".

还有一个针对 gitlab 的等效示例

cargorun --examplegitlab --features "archive-tar archive-zip compression-flate2 compression-zip-deflate".

运行着大致相当于

use self_update::cargo_crate_version;

fn update() -> Result<(), Box<::std::error::Error>> {
    let status = self_update::backends::github::Update::configure()
        .repo_owner("jaemk")
        .repo_name("self_update")
        .bin_name("github")
        .show_download_progress(true)
        .current_version(cargo_crate_version!())
        .build()?
        .update()?;
    println!("Update status: `{}`!", status.version());
    Ok(())
}

Amazon S3、Google GCS 和 DigitalOcean Spaces 也通过 S3 后端支持来检查新版本。提供一个 bucket_nameasset_prefix 字符串,self_update 将使用以下格式作为文件名的约定来查找所有匹配的文件:[目录/]<资产名称>-<semver>-<平台/目标>.<扩展名>。将删除文件名中的前导目录,允许在 S3 桶中使用子目录,任何不符合格式或不匹配提供的前缀字符串的文件将被忽略。

use self_update::cargo_crate_version;

fn update() -> Result<(), Box<::std::error::Error>> {
    let status = self_update::backends::s3::Update::configure()
        .bucket_name("self_update_releases")
        .asset_prefix("something/self_update")
        .region("eu-west-2")
        .bin_name("self_update_example")
        .show_download_progress(true)
        .current_version(cargo_crate_version!())
        .build()?
        .update()?;
    println!("S3 Update status: `{}`!", status.version());
    Ok(())
}

还公开了单独的实用工具(注意:以下示例 需要 archive-tar 功能,请参阅上面的 功能 部分)。self_replace 包被重新导出以方便使用

fn update() -> Result<(), Box<::std::error::Error>> {
    let releases = self_update::backends::github::ReleaseList::configure()
        .repo_owner("jaemk")
        .repo_name("self_update")
        .build()?
        .fetch()?;
    println!("found releases:");
    println!("{:#?}\n", releases);

    // get the first available release
    let asset = releases[0]
        .asset_for(&self_update::get_target()).unwrap();

    let tmp_dir = tempfile::Builder::new()
            .prefix("self_update")
            .tempdir_in(::std::env::current_dir()?)?;
    let tmp_tarball_path = tmp_dir.path().join(&asset.name);
    let tmp_tarball = ::std::fs::File::open(&tmp_tarball_path)?;

    self_update::Download::from_url(&asset.download_url)
        .set_header(reqwest::header::ACCEPT, "application/octet-stream".parse()?)
        .download_to(&tmp_tarball)?;

    let bin_name = std::path::PathBuf::from("self_update_bin");
    self_update::Extract::from_source(&tmp_tarball_path)
        .archive(self_update::ArchiveKind::Tar(Some(self_update::Compression::Gz)))
        .extract_file(&tmp_dir.path(), &bin_name)?;

    let new_exe = tmp_dir.path().join(bin_name);
    self_replace::self_replace(new_exe)?;

    Ok(())
}

许可证:MIT

依赖关系

~12–28MB
~444K SLoC