2019-07-07 10:01:50 +00:00
|
|
|
// Copyright (C) 2019 Sajeer Ahamed <ahamedsajeer.15.15@cse.mrt.ac.lk>
|
|
|
|
// Copyright (C) 2019 Sebastian Dröge <sebastian@centricular.com>
|
|
|
|
//
|
|
|
|
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>
|
2022-01-15 19:18:47 +00:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
2019-07-07 10:01:50 +00:00
|
|
|
|
2019-07-07 10:19:34 +00:00
|
|
|
//! Extracts release for [GStreamer](https://gstreamer.freedesktop.org) plugin metadata
|
|
|
|
//!
|
2022-10-19 10:34:21 +00:00
|
|
|
//! See [`info`](fn.info.html) for details.
|
2019-07-07 10:19:34 +00:00
|
|
|
//!
|
|
|
|
//! This function is supposed to be used as follows in the `build.rs` of a crate that implements a
|
|
|
|
//! plugin:
|
|
|
|
//!
|
|
|
|
//! ```rust,ignore
|
2021-07-09 10:37:00 +00:00
|
|
|
//! gst_plugin_version_helper::info();
|
2019-07-07 10:19:34 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
2022-10-19 10:34:21 +00:00
|
|
|
//! Inside `lib.rs` of the plugin, the information provided by `info` are usable as follows:
|
2019-07-07 10:19:34 +00:00
|
|
|
//!
|
|
|
|
//! ```rust,ignore
|
2021-06-28 21:23:09 +00:00
|
|
|
//! gst::plugin_define!(
|
2019-07-07 10:19:34 +00:00
|
|
|
//! the_plugin_name,
|
|
|
|
//! env!("CARGO_PKG_DESCRIPTION"),
|
|
|
|
//! plugin_init,
|
|
|
|
//! concat!(env!("CARGO_PKG_VERSION"), "-", env!("COMMIT_ID")),
|
|
|
|
//! "The Plugin's License",
|
|
|
|
//! env!("CARGO_PKG_NAME"),
|
|
|
|
//! env!("CARGO_PKG_NAME"),
|
|
|
|
//! env!("CARGO_PKG_REPOSITORY"),
|
|
|
|
//! env!("BUILD_REL_DATE")
|
|
|
|
//! );
|
|
|
|
//! ```
|
|
|
|
|
2020-04-03 12:02:25 +00:00
|
|
|
mod git;
|
|
|
|
|
2022-01-12 10:17:22 +00:00
|
|
|
use chrono::{Datelike, TimeZone};
|
|
|
|
use std::convert::TryInto;
|
2020-04-03 14:13:25 +00:00
|
|
|
use std::time::SystemTime;
|
2019-07-07 10:27:37 +00:00
|
|
|
use std::{env, fs, path};
|
2019-06-03 10:53:58 +00:00
|
|
|
|
2019-07-07 10:19:34 +00:00
|
|
|
/// 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
|
2020-04-03 14:13:25 +00:00
|
|
|
/// git repository is found, we assume this is a release.
|
2019-07-07 10:19:34 +00:00
|
|
|
///
|
|
|
|
/// - 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.
|
|
|
|
///
|
2020-04-03 14:13:25 +00:00
|
|
|
/// - 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`.
|
2019-07-07 10:19:34 +00:00
|
|
|
///
|
|
|
|
/// - If neither is possible, `COMMIT_ID` is set to the string `UNKNOWN` and `BUILD_REL_DATE` to the
|
|
|
|
/// current date.
|
|
|
|
///
|
2021-04-20 12:57:40 +00:00
|
|
|
pub fn info() {
|
2019-07-07 10:27:37 +00:00
|
|
|
let crate_dir =
|
|
|
|
path::PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set"));
|
2019-07-07 09:52:15 +00:00
|
|
|
let mut repo_dir = crate_dir.clone();
|
2019-07-07 09:56:26 +00:00
|
|
|
|
|
|
|
// First check for a git repository in the manifest directory and if there
|
|
|
|
// is none try one directory up in case we're in a Cargo workspace
|
2020-04-03 12:02:25 +00:00
|
|
|
let git_info = git::repo_hash(&repo_dir).or_else(move || {
|
2019-07-07 09:56:26 +00:00
|
|
|
repo_dir.pop();
|
2020-04-03 12:02:25 +00:00
|
|
|
git::repo_hash(&repo_dir)
|
2019-07-07 09:56:26 +00:00
|
|
|
});
|
2019-06-03 10:53:58 +00:00
|
|
|
|
2019-07-07 09:52:15 +00:00
|
|
|
// If there is a git repository, extract the version information from there.
|
2020-04-03 14:13:25 +00:00
|
|
|
// Otherwise assume this is a release and use Cargo.toml mtime as date.
|
|
|
|
let (commit_id, commit_date) = git_info.unwrap_or_else(|| {
|
2022-01-12 10:17:22 +00:00
|
|
|
let date = cargo_mtime_date(crate_dir).unwrap_or_else(chrono::Utc::now);
|
|
|
|
("RELEASE".into(), date.format("%Y-%m-%d").to_string())
|
2020-04-03 14:13:25 +00:00
|
|
|
});
|
2019-06-03 10:53:58 +00:00
|
|
|
|
|
|
|
println!("cargo:rustc-env=COMMIT_ID={}", commit_id);
|
|
|
|
println!("cargo:rustc-env=BUILD_REL_DATE={}", commit_date);
|
|
|
|
}
|
|
|
|
|
2022-01-12 10:17:22 +00:00
|
|
|
fn cargo_mtime_date(crate_dir: path::PathBuf) -> Option<chrono::DateTime<chrono::Utc>> {
|
2020-04-03 14:13:25 +00:00
|
|
|
let mut cargo_toml = crate_dir;
|
|
|
|
cargo_toml.push("Cargo.toml");
|
2019-07-07 09:52:15 +00:00
|
|
|
|
2020-04-03 14:13:25 +00:00
|
|
|
let metadata = fs::metadata(&cargo_toml).ok()?;
|
|
|
|
let mtime = metadata.modified().ok()?;
|
|
|
|
let unix_time = mtime.duration_since(SystemTime::UNIX_EPOCH).ok()?;
|
2022-11-12 16:24:02 +00:00
|
|
|
let dt = chrono::Utc
|
|
|
|
.timestamp_opt(unix_time.as_secs().try_into().ok()?, 0)
|
|
|
|
.latest()?;
|
2019-07-07 09:52:15 +00:00
|
|
|
|
2022-01-12 10:17:22 +00:00
|
|
|
// FIXME: Work around https://github.com/rust-lang/cargo/issues/10285
|
2022-11-12 16:24:02 +00:00
|
|
|
if dt.date_naive().year() < 2015 {
|
2022-01-12 10:17:22 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(dt)
|
2019-07-07 09:52:15 +00:00
|
|
|
}
|