Skip to content

Commit

Permalink
make use of pimalaya/tui build module
Browse files Browse the repository at this point in the history
  • Loading branch information
soywod committed Nov 29, 2024
1 parent 1a193f3 commit 85a12a5
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 202 deletions.
77 changes: 38 additions & 39 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ pgp-gpg = ["email-lib/pgp-gpg", "mml-lib/pgp-gpg", "pimalaya-tui/pgp-gpg"]
pgp-native = ["email-lib/pgp-native", "mml-lib/pgp-native", "pimalaya-tui/pgp-native"]

[build-dependencies]
git2 = { version = "0.19", default-features = false }
serde = { version = "1", features = ["derive"] }
toml = "0.8"
pimalaya-tui = { version = "=0.1", default-features = false, features = ["build-envs"] }

[dependencies]
ariadne = "0.2"
Expand All @@ -72,6 +70,7 @@ uuid = { version = "0.8", features = ["v4"] }

[patch.crates-io]
email-lib = { git = "https://github.com/pimalaya/core" }
keyring-lib = { git = "https://github.com/pimalaya/core" }
mml-lib = { git = "https://github.com/pimalaya/core" }
oauth-lib = { git = "https://github.com/pimalaya/core" }
pimalaya-tui = { git = "https://github.com/pimalaya/tui" }
Expand Down
126 changes: 2 additions & 124 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,129 +1,7 @@
use std::{
collections::HashMap,
env::{self, VarError},
};

use git2::{DescribeOptions, Repository};
use serde::Deserialize;
use pimalaya_tui::build::{features_env, git_envs, target_envs};

fn main() {
features_env();
features_env(include_str!("./Cargo.toml"));
target_envs();
git_envs();
}

/// Builds the `CARGO_FEATURES` environment variable.
///
/// This function turns enabled cargo features into a simple string
/// `+feature1 +feature2 +featureN`, which then exposes it via the
/// `CARGO_FEATURES` environment variable.
///
/// It first reads and parses the Cargo.toml in order to extract all
/// available features (omitting "default"). It then checks for
/// enabled features via `CARGO_FEATURE_<name>` to finally collect
/// them into a string.
fn features_env() {
#[derive(Deserialize)]
struct Config {
features: HashMap<String, Vec<String>>,
}

impl Config {
fn enabled_features(self) -> impl Iterator<Item = String> {
self.features
.into_keys()
.filter(|feature| feature != "default")
.filter(|feature| {
let feature = feature.replace('-', "_").to_uppercase();
env::var(format!("CARGO_FEATURE_{feature}")).is_ok()
})
}
}

let config: Config =
toml::from_str(include_str!("./Cargo.toml")).expect("should parse Cargo.toml");

let mut features = String::new();

for feature in config.enabled_features() {
if !features.is_empty() {
features.push(' ');
}
features.push_str(&format!("+{feature}"));
}

println!("cargo::rustc-env=CARGO_FEATURES={features}");
}

/// Builds environment variables related to the target platform.
///
/// This function basically forwards existing cargo environments
/// related to the target platform.
fn target_envs() {
forward_env("CARGO_CFG_TARGET_OS");
forward_env("CARGO_CFG_TARGET_ENV");
forward_env("CARGO_CFG_TARGET_ARCH");
}

/// Builds environment variables related to git.
///
/// This function basically tries to forward existing git environment
/// variables. In case of failure, it tries to build them using
/// [`git2`].
fn git_envs() {
let git = Repository::open(".").ok();

if try_forward_env("GIT_DESCRIBE").is_err() {
let description = match &git {
None => String::from("unknown"),
Some(git) => {
let mut opts = DescribeOptions::new();
opts.describe_all();
opts.show_commit_oid_as_fallback(true);

git.describe(&opts)
.expect("should describe git object")
.format(None)
.expect("should format git object description")
}
};

println!("cargo::rustc-env=GIT_DESCRIBE={description}");
};

if try_forward_env("GIT_REV").is_err() {
let rev = match &git {
None => String::from("unknown"),
Some(git) => {
let head = git.head().expect("should get git HEAD");
let commit = head.peel_to_commit().expect("should get git HEAD commit");
commit.id().to_string()
}
};

println!("cargo::rustc-env=GIT_REV={rev}");
};
}

/// Tries to forward the given environment variable.
///
/// For a more strict version, see [`forward_env`].
fn try_forward_env(key: &str) -> Result<String, VarError> {
let env = env::var(key);

if let Ok(val) = &env {
println!("cargo::rustc-env={key}={val}");
}

env
}

/// Forwards the given environment variable.
///
/// This function panics in case the forward fails (when the
/// environment variable does not exist for example).
///
/// For a less strict version, see [`try_forward_env`].
fn forward_env(key: &str) {
try_forward_env(key).expect(&format!("should get env {key}"));
}
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 85a12a5

Please sign in to comment.