mirror of
https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs.git
synced 2024-12-24 11:00:29 +00:00
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:
parent
6607377496
commit
2341f73106
1 changed files with 19 additions and 75 deletions
|
@ -34,47 +34,28 @@
|
|||
|
||||
mod git;
|
||||
|
||||
use chrono::TimeZone;
|
||||
use std::convert::TryInto;
|
||||
use std::time::SystemTime;
|
||||
use std::{env, fs, path};
|
||||
|
||||
/// Extracts release for GStreamer plugin metadata
|
||||
///
|
||||
/// 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
|
||||
/// git repository is found, the information is extract from a `release.txt` in the same
|
||||
/// directory as the `Cargo.toml`.
|
||||
/// git repository is found, we assume this is a release.
|
||||
///
|
||||
/// - 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.
|
||||
///
|
||||
/// - If extracted from a `release.txt`, `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`.
|
||||
/// - If not, `COMMIT_ID` will be set to the string `RELEASE` and the
|
||||
/// `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
|
||||
/// 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() {
|
||||
let mut commit_id = "UNKNOWN".into();
|
||||
let mut commit_date = chrono::Utc::now().format("%Y-%m-%d").to_string();
|
||||
|
||||
let crate_dir =
|
||||
path::PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set"));
|
||||
let mut repo_dir = crate_dir.clone();
|
||||
|
@ -86,62 +67,25 @@ pub fn get_info() {
|
|||
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.
|
||||
// Otherwise use a 'release.txt' file as fallback, or report the current
|
||||
// date and an unknown revision.
|
||||
if let Some((id, date)) = git_info {
|
||||
commit_id = id;
|
||||
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();
|
||||
}
|
||||
}
|
||||
// Otherwise assume this is a release and use Cargo.toml mtime as date.
|
||||
let (commit_id, commit_date) = git_info.unwrap_or_else(|| {
|
||||
let date = cargo_mtime_date(crate_dir).expect("Failed to get Cargo.toml mtime");
|
||||
("RELEASE".into(), date)
|
||||
});
|
||||
|
||||
println!("cargo:rustc-env=COMMIT_ID={}", commit_id);
|
||||
println!("cargo:rustc-env=BUILD_REL_DATE={}", commit_date);
|
||||
}
|
||||
|
||||
fn read_release_date(mut cargo_toml: fs::File, mut release_file: fs::File) -> String {
|
||||
use std::io::Read;
|
||||
fn cargo_mtime_date(crate_dir: path::PathBuf) -> Option<String> {
|
||||
let mut cargo_toml = crate_dir;
|
||||
cargo_toml.push("Cargo.toml");
|
||||
|
||||
let mut content = String::new();
|
||||
release_file
|
||||
.read_to_string(&mut content)
|
||||
.expect("Failed to read version file");
|
||||
let metadata = fs::metadata(&cargo_toml).ok()?;
|
||||
let mtime = metadata.modified().ok()?;
|
||||
let unix_time = mtime.duration_since(SystemTime::UNIX_EPOCH).ok()?;
|
||||
let dt = chrono::Utc.timestamp(unix_time.as_secs().try_into().ok()?, 0);
|
||||
|
||||
let mut lines = content.lines();
|
||||
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()
|
||||
Some(dt.format("%Y-%m-%d").to_string())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue