简体   繁体   English

如何告诉 Cargo 使用 git 标签来确定 crate 版本?

[英]How to tell Cargo to use git tags to determine the crate version?

I'm chewing through the Rust book, and learning about Cargo.我正在阅读 Rust 的书,并了解 Cargo。 In the description of the Cargo.toml format, it seems to require that you hard-code the version you're currently working on or have most recently released into that file, which is checked into revision control.Cargo.toml格式的描述中,似乎要求您对当前正在使用或最近发布到该文件中的版本进行硬编码,该文件被签入修订控制。 Since anyone sane tags their releases, this means that the version information is duplicated, and we all know what a bad idea it is to have the same information in two places.因为任何理智的人都会标记他们的版本,这意味着版本信息是重复的,我们都知道在两个地方拥有相同的信息是一个多么糟糕的主意。

Given that Cargo seems admirably opinionated on the subject of revision control (creating a git repo on cargo new ), I'm a bit surprised that I can't find a way to tell Cargo, "grab version information from the annotated tags in the repo".鉴于 Cargo 似乎对修订控制主题(在cargo new上创建一个 git repo)的观点令人钦佩,我有点惊讶我找不到一种方法告诉 Cargo,“从注释中的标签中获取版本信息回购”。 Am I missing something, or is this a feature that is just out-and-out missing from Cargo entirely?我是不是遗漏了什么,还是 Cargo 完全没有这个功能?

crates.io stores full snapshots of crates' sources without any VCS meta-information . crates.io 存储任何来源的完整快照,没有任何VCS元信息 So this information about the crate must be encoded in Cargo.toml which is a part of a snapshot. 因此,有关包装箱的信息必须在Cargo.toml中编码,该文件是快照的一部分。

There's also an old issue about the idea of a reversed approach: make cargo subcommands create git tags when publishing a new version on crates.io. 关于逆向方法的想法还有一个老问题 :在crates.io上发布新版本时,使cargo子命令创建git标签。

To close the loop on this, I've started just doing things the brutal way and setting a "fake" version in Cargo.toml , and then during release builds (done via GitHub Actions) doing a bit of light sed to set the real version number, like this:为了结束这个循环,我开始只是以残酷的方式做事并在Cargo.toml设置一个“假”版本,然后在发布版本期间(通过 GitHub Actions 完成)做一些简单的sed来设置真实的版本号,像这样:

  - name: Set Cargo.toml version
    shell: bash
    env:
      RELEASE_TAG: ${{ github.ref }}
    run: |
      mv Cargo.toml Cargo.toml.orig
      sed "s/0\\.0\\.0-git/${RELEASE_TAG##*\/v}/" Cargo.toml.orig >Cargo.toml
      mv Cargo.lock Cargo.lock.orig
      sed "s/0\\.0\\.0-git/${RELEASE_TAG##*\/v}/" Cargo.lock.orig >Cargo.lock

Then leave Cargo.toml like this:然后像这样离开Cargo.toml

[package]
version = "0.0.0-git"

It's ugly, but it works.这很丑陋,但它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM