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
55
56
57
58
59
60
61
pub use crate::jormungandr::{Block0ConfigurationBuilder, SecretModelFactory};
use std::{env, path::PathBuf};

/// Get jormungandr executable from current environment
pub fn get_jormungandr_app() -> PathBuf {
    const JORMUNGANDR_NAME: &str = env!("JORMUNGANDR_NAME");
    get_app_from_current_dir(JORMUNGANDR_NAME)
}

/// Get jcli executable from current environment
pub fn get_jcli_app() -> PathBuf {
    const JOR_CLI_NAME: &str = env!("JOR_CLI_NAME");
    get_app_from_current_dir(JOR_CLI_NAME)
}

/// Get explorer executable from current environment
pub fn get_explorer_app() -> PathBuf {
    const JOR_EXPLORER_NAME: &str = env!("JOR_EXPLORER_NAME");
    get_app_from_current_dir(JOR_EXPLORER_NAME)
}

/// Get executable from current environment
pub fn get_app_from_current_dir(app_name: &str) -> PathBuf {
    let mut path = get_working_directory();
    path.push(app_name);
    if cfg!(windows) {
        path.set_extension("exe");
    }
    assert!(
        path.is_file(),
        "File does not exist: {:?}, pwd: {:?}",
        path,
        env::current_dir()
    );
    path
}

/// Gets working directory
/// Uses std::env::current_exe() for this purpose.
/// Current exe directory is ./target/{profile}/deps/{app_name}.exe
/// Function returns ./target/{profile}
fn get_working_directory() -> PathBuf {
    let mut output_directory: PathBuf = std::env::current_exe().unwrap();

    output_directory.pop();

    if output_directory.ends_with("deps") {
        output_directory.pop();
    }
    output_directory
}

pub fn get_openapi_path() -> PathBuf {
    let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    path.pop();
    path.pop();
    path.push("doc");
    path.push("api");
    path.push("v0.yaml");
    path
}