File: build.rs

package info (click to toggle)
rust-sequoia-sop 0.37.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 412 kB
  • sloc: makefile: 22
file content (54 lines) | stat: -rw-r--r-- 1,493 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use std::{
    env,
    error::Error,
    fs,
    path::PathBuf,
};

#[cfg(any(feature = "cli", feature = "cliv"))]
use sop::cli::Variant;

#[path = "src/version.rs"]
mod version;

fn main() {
    #[cfg(feature = "cli")]
    cli("sqop", Variant::Full).unwrap();

    #[cfg(feature = "cliv")]
    cli("sqopv", Variant::Verification).unwrap();
}

/// Writes shell completions and man pages.
#[cfg(any(feature = "cli", feature = "cliv"))]
fn cli(name: &'static str, variant: Variant) -> Result<(), Box<dyn Error>> {
    let version = version::Version::with_name(name);

    sop::cli::write_shell_completions2(
        variant, name, asset_out_dir("shell-completions")?)?;
    sop::cli::write_man_pages(
        variant, &version, "Sequoia PGP",
        asset_out_dir("man-pages")?)?;

    Ok(())
}

/// Variable name to control the asset out directory with.
const ASSET_OUT_DIR: &str = "ASSET_OUT_DIR";

/// Returns the directory to write the given assets to.
#[allow(dead_code)]
fn asset_out_dir(asset: &str) -> Result<PathBuf, Box<dyn Error>> {
    println!("cargo:rerun-if-env-changed={}", ASSET_OUT_DIR);
    let outdir: PathBuf =
        env::var_os(ASSET_OUT_DIR).unwrap_or_else(
            || env::var_os("OUT_DIR").expect("OUT_DIR not set")).into();
    if outdir.exists() && ! outdir.is_dir() {
        return Err(
            format!("{}={:?} is not a directory", ASSET_OUT_DIR, outdir).into());
    }

    let path = outdir.join(asset);
    fs::create_dir_all(&path)?;
    Ok(path)
}