version-helper: always assume we are in a release if not in git

No longer need to have a 'release.txt' file in tarballs.
This commit is contained in:
Guillaume Desmottes 2020-04-03 16:13:25 +02:00 committed by Sebastian Dröge
parent 6607377496
commit 2341f73106

View file

@ -34,47 +34,28 @@
mod git; mod git;
use chrono::TimeZone;
use std::convert::TryInto;
use std::time::SystemTime;
use std::{env, fs, path}; use std::{env, fs, path};
/// Extracts release for GStreamer plugin metadata /// Extracts release for GStreamer plugin metadata
/// ///
/// Release information is first tried to be extracted from a git repository at the same /// Release information is first tried to be extracted from a git repository at the same
/// place as the `Cargo.toml`, or one directory up to allow for Cargo workspaces. If no /// place as the `Cargo.toml`, or one directory up to allow for Cargo workspaces. If no
/// git repository is found, the information is extract from a `release.txt` in the same /// git repository is found, we assume this is a release.
/// directory as the `Cargo.toml`.
/// ///
/// - If extracted from a git repository, sets the `COMMIT_ID` environment variable to the short /// - If extracted from a git repository, sets the `COMMIT_ID` environment variable to the short
/// commit id of the latest commit and the `BUILD_REL_DATE` environment variable to the date of the /// commit id of the latest commit and the `BUILD_REL_DATE` environment variable to the date of the
/// commit. /// commit.
/// ///
/// - If extracted from a `release.txt`, `COMMIT_ID` will be set to the string `RELEASE` and the /// - If not, `COMMIT_ID` will be set to the string `RELEASE` and the
/// `BUILD_REL_DATE` variable will be set to the release date extracted from `release.txt`. /// `BUILD_REL_DATE` variable will be set to the mtime of `Cargo.toml`.
/// ///
/// - If neither is possible, `COMMIT_ID` is set to the string `UNKNOWN` and `BUILD_REL_DATE` to the /// - If neither is possible, `COMMIT_ID` is set to the string `UNKNOWN` and `BUILD_REL_DATE` to the
/// current date. /// current date.
/// ///
/// ## `release.txt`
///
/// `release.txt` is only parsed if no git repository was found and is assumed to be in the format
///
/// ```txt
/// version_number
/// release_date
/// ```
///
/// for example
///
/// ```txt
/// 1.16.0
/// 2019-04-19
/// ```
///
/// If the version number from `Cargo.toml` is not equivalent to the one in `release.txt`, this
/// function will panic.
pub fn get_info() { pub fn get_info() {
let mut commit_id = "UNKNOWN".into();
let mut commit_date = chrono::Utc::now().format("%Y-%m-%d").to_string();
let crate_dir = let crate_dir =
path::PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set")); path::PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set"));
let mut repo_dir = crate_dir.clone(); let mut repo_dir = crate_dir.clone();
@ -86,62 +67,25 @@ pub fn get_info() {
git::repo_hash(&repo_dir) git::repo_hash(&repo_dir)
}); });
let mut release_file = crate_dir.clone();
release_file.push("release.txt");
// If there is a git repository, extract the version information from there. // If there is a git repository, extract the version information from there.
// Otherwise use a 'release.txt' file as fallback, or report the current // Otherwise assume this is a release and use Cargo.toml mtime as date.
// date and an unknown revision. let (commit_id, commit_date) = git_info.unwrap_or_else(|| {
if let Some((id, date)) = git_info { let date = cargo_mtime_date(crate_dir).expect("Failed to get Cargo.toml mtime");
commit_id = id; ("RELEASE".into(), date)
commit_date = date; });
} else if let Ok(release_file) = fs::File::open(release_file) {
let mut cargo_toml = crate_dir;
cargo_toml.push("Cargo.toml");
if let Ok(cargo_toml) = fs::File::open(cargo_toml) {
commit_date = read_release_date(cargo_toml, release_file);
commit_id = "RELEASE".into();
}
}
println!("cargo:rustc-env=COMMIT_ID={}", commit_id); println!("cargo:rustc-env=COMMIT_ID={}", commit_id);
println!("cargo:rustc-env=BUILD_REL_DATE={}", commit_date); println!("cargo:rustc-env=BUILD_REL_DATE={}", commit_date);
} }
fn read_release_date(mut cargo_toml: fs::File, mut release_file: fs::File) -> String { fn cargo_mtime_date(crate_dir: path::PathBuf) -> Option<String> {
use std::io::Read; let mut cargo_toml = crate_dir;
cargo_toml.push("Cargo.toml");
let mut content = String::new(); let metadata = fs::metadata(&cargo_toml).ok()?;
release_file let mtime = metadata.modified().ok()?;
.read_to_string(&mut content) let unix_time = mtime.duration_since(SystemTime::UNIX_EPOCH).ok()?;
.expect("Failed to read version file"); let dt = chrono::Utc.timestamp(unix_time.as_secs().try_into().ok()?, 0);
let mut lines = content.lines(); Some(dt.format("%Y-%m-%d").to_string())
let version = lines.next().expect("Failed to read version number");
let date = lines.next().expect("Failed to read release date");
let mut cargo_toml_content = String::new();
cargo_toml
.read_to_string(&mut cargo_toml_content)
.expect("Failed to read `Cargo.toml`");
let cargo_toml = cargo_toml_content
.parse::<toml::Value>()
.expect("Failed to parse `Cargo.toml`");
let package = cargo_toml["package"]
.as_table()
.expect("Failed to find 'package' section in `Cargo.toml`");
let cargo_version = package["version"]
.as_str()
.expect("Failed to read version from `Cargo.toml`");
if cargo_version != version {
panic!(
"Version from 'version.txt' `{}` different from 'Cargo.toml' `{}`",
version, cargo_version
);
}
date.into()
} }