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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
mod detect;
mod download;
mod execute;
mod install;
mod resolve;
mod schema;
mod shim;
mod verify;

use once_cell::sync::OnceCell;
use proto_core::{impl_tool, Describable, Manifest, Proto, ProtoError, Resolvable, Tool};
pub use schema::*;
use std::{
    any::Any,
    env::consts,
    path::{Path, PathBuf},
};

#[derive(Debug)]
pub struct SchemaPlugin {
    pub schema: Schema,
    pub id: String,
    pub base_dir: PathBuf,
    pub bin_path: Option<PathBuf>,
    pub shim_path: Option<PathBuf>,
    pub temp_dir: PathBuf,
    pub version: Option<String>,

    manifest: OnceCell<Manifest>,
}

impl SchemaPlugin {
    pub fn new<P: AsRef<Proto>>(proto: P, id: String, schema: Schema) -> Self {
        let proto = proto.as_ref();

        SchemaPlugin {
            base_dir: proto.tools_dir.join(&id),
            bin_path: None,
            manifest: OnceCell::new(),
            shim_path: None,
            temp_dir: proto.temp_dir.join(&id),
            version: None,
            id,
            schema,
        }
    }

    pub fn get_platform(&self) -> Result<&PlatformMapper, ProtoError> {
        let mut platform = self.schema.platform.get(consts::OS);

        // Fallback to linux for other OSes
        if platform.is_none() && consts::OS.ends_with("bsd") {
            platform = self.schema.platform.get("linux");
        }

        platform
            .ok_or_else(|| ProtoError::UnsupportedPlatform(self.get_name(), consts::OS.to_owned()))
    }

    pub fn get_checksum_file(&self) -> Result<String, ProtoError> {
        Ok(if let Some(file) = &self.get_platform()?.checksum_file {
            self.interpolate_tokens(file)
        } else {
            format!("v{}-SHASUMS256.txt", self.get_resolved_version())
        })
    }

    pub fn get_download_file(&self) -> Result<String, ProtoError> {
        Ok(self.interpolate_tokens(&self.get_platform()?.download_file))
    }

    pub fn interpolate_tokens(&self, value: &str) -> String {
        let mut value = value
            .replace("{version}", self.get_resolved_version())
            .replace("{arch}", self.schema.get_arch());

        // Avoid detecting musl unless requested
        if value.contains("{libc}") {
            value = value.replace("{libc}", self.schema.get_libc());
        }

        value
    }
}

impl Describable<'_> for SchemaPlugin {
    fn get_id(&self) -> &str {
        &self.id
    }

    fn get_name(&self) -> String {
        self.schema.name.clone()
    }
}

impl_tool!(SchemaPlugin);