aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/downloader/hg_downloader.rs
blob: 16cf73cd371eeb0ce7b87156ac8145b368e07b80 (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//! ref: composer/src/Composer/Downloader/HgDownloader.php

use crate::config::Config;
use crate::downloader::DownloaderInterface;
use crate::downloader::VcsDownloaderBase;
use crate::io::IOInterface;
use crate::package::PackageInterface;
use crate::util::Filesystem;
use crate::util::Hg as HgUtils;
use crate::util::ProcessExecutor;
use anyhow::Result;
use shirabe_php_shim::{PhpMixed, RuntimeException};

#[derive(Debug)]
pub struct HgDownloader {
    inner: VcsDownloaderBase,
}

impl HgDownloader {
    pub fn new(
        io: Box<dyn IOInterface>,
        config: std::rc::Rc<std::cell::RefCell<Config>>,
        process: std::rc::Rc<std::cell::RefCell<ProcessExecutor>>,
        fs: std::rc::Rc<std::cell::RefCell<Filesystem>>,
    ) -> Self {
        Self {
            inner: VcsDownloaderBase::new(io, config, Some(process), Some(fs)),
        }
    }

    pub(crate) async fn do_download(
        &self,
        package: &dyn PackageInterface,
        path: String,
        url: String,
        prev_package: Option<&dyn PackageInterface>,
    ) -> Result<Option<PhpMixed>> {
        if HgUtils::get_version(&self.inner.process).is_none() {
            return Err(RuntimeException {
                message: "hg was not found in your PATH, skipping source download".to_string(),
                code: 0,
            }
            .into());
        }

        Ok(shirabe_external_packages::react::promise::resolve(None))
    }

    pub(crate) async fn do_install(
        &self,
        package: &dyn PackageInterface,
        path: String,
        url: String,
    ) -> Result<Option<PhpMixed>> {
        let hg_utils = HgUtils::new(
            &*self.inner.io,
            &*self.inner.config.borrow(),
            &self.inner.process,
        );

        let path_clone = path.clone();
        let clone_command = move |url: String| -> Vec<String> {
            vec![
                "hg".to_string(),
                "clone".to_string(),
                "--".to_string(),
                url,
                path_clone.clone(),
            ]
        };
        hg_utils.run_command(clone_command, url, Some(path.clone()));

        let command = vec![
            "hg".to_string(),
            "up".to_string(),
            "--".to_string(),
            package
                .get_source_reference()
                .unwrap_or_default()
                .to_string(),
        ];
        let mut ignored_output = String::new();
        if self.inner.process.borrow_mut().execute_args(
            &command,
            &mut ignored_output,
            shirabe_php_shim::realpath(&path),
        ) != 0
        {
            return Err(RuntimeException {
                message: format!(
                    "Failed to execute {}\n\n{}",
                    command.join(" "),
                    self.inner.process.borrow().get_error_output()
                ),
                code: 0,
            }
            .into());
        }

        Ok(shirabe_external_packages::react::promise::resolve(None))
    }

    pub(crate) async fn do_update(
        &self,
        initial: &dyn PackageInterface,
        target: &dyn PackageInterface,
        path: String,
        url: String,
    ) -> Result<Option<PhpMixed>> {
        let hg_utils = HgUtils::new(
            &*self.inner.io,
            &*self.inner.config.borrow(),
            &self.inner.process,
        );

        let ref_ = target
            .get_source_reference()
            .unwrap_or_default()
            .to_string();
        self.inner.io.write_error(&format!(
            " Updating to {}",
            target.get_source_reference().unwrap_or_default()
        ));

        if !self.has_metadata_repository(path.clone()) {
            return Err(RuntimeException {
                message: format!(
                    "The .hg directory is missing from {}, see https://getcomposer.org/commit-deps for more information",
                    path
                ),
                code: 0,
            }.into());
        }

        let pull_command = |url: String| -> Vec<String> {
            vec!["hg".to_string(), "pull".to_string(), "--".to_string(), url]
        };
        hg_utils.run_command(pull_command, url.clone(), Some(path.clone()));

        let ref_clone = ref_.clone();
        let up_command = move |_url: String| -> Vec<String> {
            vec![
                "hg".to_string(),
                "up".to_string(),
                "--".to_string(),
                ref_clone.clone(),
            ]
        };
        hg_utils.run_command(up_command, url, Some(path));

        Ok(shirabe_external_packages::react::promise::resolve(None))
    }

    pub fn get_local_changes(
        &self,
        package: &dyn PackageInterface,
        path: String,
    ) -> Option<String> {
        if !std::path::Path::new(&format!("{}/.hg", path)).is_dir() {
            return None;
        }

        let mut output = String::new();
        self.inner.process.borrow_mut().execute_args(
            &["hg".to_string(), "st".to_string()],
            &mut output,
            shirabe_php_shim::realpath(&path),
        );

        let output = output.trim().to_string();

        if !output.is_empty() {
            Some(output)
        } else {
            None
        }
    }

    pub(crate) fn get_commit_logs(
        &self,
        from_reference: String,
        to_reference: String,
        path: String,
    ) -> Result<String> {
        let command = vec![
            "hg".to_string(),
            "log".to_string(),
            "-r".to_string(),
            format!("{}:{}", from_reference, to_reference),
            "--style".to_string(),
            "compact".to_string(),
        ];

        let mut output = String::new();
        if self.inner.process.borrow_mut().execute_args(
            &command,
            &mut output,
            shirabe_php_shim::realpath(&path),
        ) != 0
        {
            return Err(RuntimeException {
                message: format!(
                    "Failed to execute {}\n\n{}",
                    command.join(" "),
                    self.inner.process.borrow().get_error_output()
                ),
                code: 0,
            }
            .into());
        }

        Ok(output)
    }

    pub(crate) fn has_metadata_repository(&self, path: String) -> bool {
        std::path::Path::new(&format!("{}/.hg", path)).is_dir()
    }
}

// TODO(phase-b): wire up VcsDownloader trait properly. HgDownloader extends VcsDownloader which
// implements DownloaderInterface in PHP. Delegating each trait method to todo!() until the inner
// VcsDownloaderBase exposes the matching impl surface.
impl DownloaderInterface for HgDownloader {
    fn get_installation_source(&self) -> String {
        todo!()
    }

    async fn download(
        &self,
        _package: &dyn PackageInterface,
        _path: &str,
        _prev_package: Option<&dyn PackageInterface>,
        _output: bool,
    ) -> Result<Option<PhpMixed>> {
        todo!()
    }

    async fn prepare(
        &self,
        _type: &str,
        _package: &dyn PackageInterface,
        _path: &str,
        _prev_package: Option<&dyn PackageInterface>,
    ) -> Result<Option<PhpMixed>> {
        todo!()
    }

    async fn install(
        &self,
        _package: &dyn PackageInterface,
        _path: &str,
        _output: bool,
    ) -> Result<Option<PhpMixed>> {
        todo!()
    }

    async fn update(
        &self,
        _initial: &dyn PackageInterface,
        _target: &dyn PackageInterface,
        _path: &str,
    ) -> Result<Option<PhpMixed>> {
        todo!()
    }

    async fn remove(
        &self,
        _package: &dyn PackageInterface,
        _path: &str,
        _output: bool,
    ) -> Result<Option<PhpMixed>> {
        todo!()
    }

    async fn cleanup(
        &self,
        _type: &str,
        _package: &dyn PackageInterface,
        _path: &str,
        _prev_package: Option<&dyn PackageInterface>,
    ) -> Result<Option<PhpMixed>> {
        todo!()
    }
}