gst-plugin-version-helper-0.8.1/.cargo_vcs_info.json0000644000000001540000000000100160730ustar { "git": { "sha1": "16e001e3f243cf86036a59ac19452dd6e0097864" }, "path_in_vcs": "version-helper" }gst-plugin-version-helper-0.8.1/Cargo.toml0000644000000022720000000000100140740ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" rust-version = "1.69" name = "gst-plugin-version-helper" version = "0.8.1" authors = [ "Sajeer Ahamed ", "Sebastian Dröge ", ] description = "build.rs helper function for GStreamer plugin metadata" homepage = "https://gstreamer.freedesktop.org" readme = "README.md" keywords = [ "gstreamer", "multimedia", "cargo", ] categories = ["development-tools"] license = "MIT" repository = "https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs" [dependencies.chrono] version = "0.4.23" features = [ "std", "clock", ] default-features = false [dependencies.toml_edit] version = "0.22" features = ["parse"] default-features = false gst-plugin-version-helper-0.8.1/Cargo.toml.orig000064400000000000000000000012251046102023000175520ustar 00000000000000[package] name = "gst-plugin-version-helper" version = "0.8.1" authors = ["Sajeer Ahamed ", "Sebastian Dröge "] categories = ["development-tools"] description = "build.rs helper function for GStreamer plugin metadata" repository.workspace = true license = "MIT" homepage = "https://gstreamer.freedesktop.org" keywords = ["gstreamer", "multimedia", "cargo"] edition.workspace = true rust-version = "1.69" [dependencies] chrono = { version = "0.4.23", default-features = false, features = ["std", "clock"] } toml_edit = { version = "0.22", default-features = false, features = ["parse"] } gst-plugin-version-helper-0.8.1/LICENSE000064400000000000000000000017771046102023000157040ustar 00000000000000Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. gst-plugin-version-helper-0.8.1/README.md000064400000000000000000000026461046102023000161520ustar 00000000000000# gst-plugin-version-helper [![crates.io](https://img.shields.io/crates/v/gst-plugin-version-helper.svg)](https://crates.io/crates/gst-plugin-version-helper) [![docs.rs](https://docs.rs/gst-plugin-version-helper/badge.svg)](https://docs.rs/gst-plugin-version-helper) Extracts release for [GStreamer](https://gstreamer.freedesktop.org) plugin metadata See the [documentation](https://docs.rs/gst-plugin-version-helper) for details. This function is supposed to be used as follows in the `build.rs` of a crate that implements a plugin: ```rust,ignore gst_plugin_version_helper::info(); ``` Inside `lib.rs` of the plugin, the information provided by `info` are usable as follows: ```rust,ignore gst::plugin_define!( 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") ); ``` ## LICENSE `gst-plugin-version-helper` is licensed under the MIT license ([LICENSE](LICENSE) or http://opensource.org/licenses/MIT). ## Contribution Any kinds of contributions are welcome as a pull request. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in `gst-plugin-version-helper` by you shall be licensed under the MIT license as above, without any additional terms or conditions. gst-plugin-version-helper-0.8.1/src/git.rs000064400000000000000000000025401046102023000166040ustar 00000000000000// Copyright (C) 2020 Guillaume Desmottes // // Licensed under the MIT license, see the LICENSE file or // // SPDX-License-Identifier: MIT use std::path::Path; use std::process::Command; pub fn repo_hash>(path: P) -> Option<(String, String)> { let git_path = path.as_ref().to_str(); let mut args = match git_path { Some(path) => vec!["-C", path], None => vec![], }; args.extend(["log", "-1", "--format=%h_%cd", "--date=short"]); let output = Command::new("git").args(&args).output().ok()?; if !output.status.success() { return None; } let output = String::from_utf8(output.stdout).ok()?; let output = output.trim_end_matches('\n'); let mut s = output.split('_'); let hash = s.next()?; let date = s.next()?; let hash = if dirty(path) { format!("{hash}+") } else { hash.into() }; Some((hash, date.into())) } fn dirty>(path: P) -> bool { let path = path.as_ref().to_str(); let mut args = match path { Some(path) => vec!["-C", path], None => vec![], }; args.extend(["ls-files", "-m"]); match Command::new("git").args(&args).output() { Ok(modified_files) => !modified_files.stdout.is_empty(), Err(_) => false, } } gst-plugin-version-helper-0.8.1/src/lib.rs000064400000000000000000000116421046102023000165720ustar 00000000000000// Copyright (C) 2019 Sajeer Ahamed // Copyright (C) 2019 Sebastian Dröge // // Licensed under the MIT license, see the LICENSE file or // // SPDX-License-Identifier: MIT //! Extracts release for [GStreamer](https://gstreamer.freedesktop.org) plugin metadata //! //! See [`info`](fn.info.html) for details. //! //! This function is supposed to be used as follows in the `build.rs` of a crate that implements a //! plugin: //! //! ```rust,ignore //! gst_plugin_version_helper::info(); //! ``` //! //! Inside `lib.rs` of the plugin, the information provided by `info` are usable as follows: //! //! ```rust,ignore //! gst::plugin_define!( //! 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") //! ); //! ``` mod git; use chrono::{Datelike, 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, 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 not, `COMMIT_ID` will be set to the string `RELEASE` and the /// `BUILD_REL_DATE` variable will be set to the `package.metadata.gstreamer.release_date` key of /// `Cargo.toml`, if it exists. /// /// - 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`. Note that the crates created by `cargo package` and /// `cargo publish` have bogus mtimes for all files and won't be used. /// /// - If neither is possible, `COMMIT_ID` is set to the string `UNKNOWN` and `BUILD_REL_DATE` to the /// current date. /// pub fn info() { let crate_dir = path::PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set")); let mut repo_dir = crate_dir.clone(); // 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 let git_info = git::repo_hash(&repo_dir).or_else(move || { repo_dir.pop(); git::repo_hash(&repo_dir) }); // If there is a git repository, extract the version information from there. // 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_metadata_release_date(&crate_dir) .or_else(|| cargo_mtime_date(&crate_dir)) .unwrap_or_else(chrono::Utc::now); ("RELEASE".into(), date.format("%Y-%m-%d").to_string()) }); println!("cargo:rustc-env=COMMIT_ID={commit_id}"); println!("cargo:rustc-env=BUILD_REL_DATE={commit_date}"); } fn cargo_metadata_release_date(crate_dir: &path::Path) -> Option> { use std::io::prelude::*; let mut cargo_toml = path::PathBuf::from(crate_dir); cargo_toml.push("Cargo.toml"); let mut file = fs::File::open(&cargo_toml).ok()?; let mut contents = String::new(); file.read_to_string(&mut contents).ok()?; let doc = contents.parse::().ok()?; let release_date = doc .get("package") .and_then(|package| package.as_table_like()) .and_then(|package| package.get("metadata")) .and_then(|metadata| metadata.as_table_like()) .and_then(|metadata| metadata.get("gstreamer")) .and_then(|gstreamer| gstreamer.as_table_like()) .and_then(|gstreamer| gstreamer.get("release_date")) .and_then(|release_date| release_date.as_str())?; let release_date = release_date.parse::().ok()?; Some(chrono::DateTime::from_naive_utc_and_offset( release_date.and_hms_opt(0, 0, 0)?, chrono::Utc, )) } fn cargo_mtime_date(crate_dir: &path::Path) -> Option> { let mut cargo_toml = path::PathBuf::from(crate_dir); cargo_toml.push("Cargo.toml"); 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_opt(unix_time.as_secs().try_into().ok()?, 0) .latest()?; // FIXME: Work around https://github.com/rust-lang/cargo/issues/10285 if dt.date_naive().year() < 2015 { return None; } Some(dt) }