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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use pyo3::prelude::*;
use std::path::Path;
use std::process::Command;

pub trait DownloaderImpl: Sized {
    /// Convert from a Python `Downloader` instance to a Rust [`Downloader`] instance.
    /// If this is not possible (due to an invalid value, for example), [`None`] is returned.
    ///
    /// # Note
    /// `object` must be a valid `Downloader` instance in Python.
    fn from_py(object: &Bound<PyAny>) -> Option<Self>;

    /// Download the source code into the specified `path`.
    ///
    /// If the action is performed successfully, the path specified by [`path`] will contain the
    /// source code (or binaries, depending on the [`Downloader`] implementation) of the requested
    /// program.
    ///
    /// # Errors
    /// The function will return [`Err::<String>`], where the [`String`] contains an appropriate
    /// error message.
    fn download<P: AsRef<Path>>(&self, path: &P) -> Result<(), String>;
}

#[derive(Debug, Clone)]
pub struct GitClone {
    url: String,
    branch: Option<String>,
    commit: Option<String>,
    submodules: bool,
}

impl GitClone {
    #[must_use]
    pub fn new(url: &str) -> Self {
        Self {
            url: url.to_string(),
            branch: None,
            commit: None,
            submodules: false,
        }
    }
}

impl DownloaderImpl for GitClone {
    fn from_py(object: &Bound<PyAny>) -> Option<Self> {
        let url: String = object
            .getattr("url")
            .expect("Failed to find attribute .url")
            .extract()
            .expect("Failed to extract url");

        let branch: Option<String> = object.getattr("branch").ok()?.extract().ok();
        let commit: Option<String> = object.getattr("commit").ok()?.extract().ok();
        let submodules: bool = object.getattr("submodules").ok()?.extract().ok()?;

        Some(Self {
            url,
            branch,
            commit,
            submodules,
        })
    }

    fn download<P: AsRef<Path>>(&self, path: &P) -> Result<(), String> {
        // Check if the directory already exists

        let skip_clone = std::fs::try_exists(path).map_err(|err| err.to_string())?;

        if skip_clone {
            crate::log::warn("Module download directory already exists. Pulling latest changes");
        } else {
            let mut command = Command::new("git");
            command.arg("clone");
            command.arg(&self.url);

            if let Some(branch) = &self.branch {
                command.arg("-b");
                command.arg(branch);
            }

            if self.submodules {
                command.arg("--recurse");
            }

            command.arg(path.as_ref());
            command.stdout(std::process::Stdio::piped());
            command.stderr(std::process::Stdio::piped());

            let spawn = command.spawn().map_err(|e| e.to_string())?;
            let (result, stdout, stderr) = crate::cli::child_logger(spawn);

            if result.is_err() {
                return Err("Failed to run git command".to_string());
            }
            let result = result.unwrap();

            if !result.success() {
                return Err(format!(
                    "Failed to clone repository: \n{}\n{}",
                    stdout.join("\n"),
                    stderr.join("\n")
                ));
            }
        }

        // Checkout or pull, depending on the commit specified
        let mut command = Command::new("git");
        command.current_dir(path);

        let msg = match &self.commit {
            Some(commit) => {
                command.arg("checkout");
                command.arg(commit);
                format!("Failed to checkout commit '{commit}'")
            }
            None => {
                command.arg("pull");
                "Failed to pull changes".to_string()
            }
        };

        command.stdout(std::process::Stdio::piped());
        command.stderr(std::process::Stdio::piped());

        let spawn = command.spawn().map_err(|e| e.to_string())?;
        let (result, stdout, stderr) = crate::cli::child_logger(spawn);

        if result.is_err() || !result.unwrap().success() {
            return Err(format!(
                "{msg}: \n{}\n{}",
                stdout.join("\n"),
                stderr.join("\n")
            ));
        }

        // if let Some(commit) = &self.commit {
        //     let mut command = Command::new("git");
        //     command.current_dir(path);
        //     command.arg("checkout");
        //     command.arg(commit);
        //     command.stdout(std::process::Stdio::piped());
        //     command.stderr(std::process::Stdio::piped());
        //
        //     let spawn = command.spawn().map_err(|e| e.to_string())?;
        //     let (result, stdout, stderr) = crate::cli::child_logger(spawn);
        //
        //     if result.is_err() || !result.unwrap().success() {
        //         return Err(format!(
        //             "Failed to checkout commit {commit:?}: \n{}\n{}",
        //             stdout.join("\n"),
        //             stderr.join("\n")
        //         ));
        //     }
        // } else {
        //     // No commit specified, so pull latest changes
        //
        //     let mut command = Command::new("git");
        //     command.current_dir(path);
        //     command.arg("pull");
        //     command.stdout(std::process::Stdio::piped());
        //     command.stderr(std::process::Stdio::piped());
        //
        //     let spawn = command.spawn().map_err(|e| e.to_string())?;
        //     let (result, stdout, stderr) = crate::cli::child_logger(spawn);
        //
        //     if result.is_err() || !result.unwrap().success() {
        //         return Err(format!(
        //             "Failed to pull: \n{}\n{}",
        //             stdout.join("\n"),
        //             stderr.join("\n")
        //         ));
        //     }
        // }

        Ok(())
    }
}

#[derive(Debug)]
pub enum Downloader {
    GitClone(GitClone),
    Curl,
}

impl DownloaderImpl for Downloader {
    fn from_py(object: &Bound<PyAny>) -> Option<Self> {
        let name = object.get_type().name().unwrap().to_string();

        match name.as_str() {
            "GitClone" => Some(Self::GitClone(GitClone::from_py(object)?)),
            _ => None,
        }
    }

    fn download<P: AsRef<Path>>(&self, path: &P) -> Result<(), String> {
        match self {
            Self::GitClone(clone) => clone.download(path),
            Self::Curl => Err("Not implemented yet".to_string()),
        }
    }
}