aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/repository/vcs/perforce_driver.rs
blob: 0bd6d126016f17de2601b38317786074ee4cb4f6 (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
//! ref: composer/src/Composer/Repository/Vcs/PerforceDriver.php

use indexmap::IndexMap;
use shirabe_external_packages::composer::pcre::Preg;
use shirabe_php_shim::{BadMethodCallException, PhpMixed, RuntimeException};

use crate::cache::Cache;
use crate::config::Config;
use crate::io::IOInterface;
use crate::repository::vcs::VcsDriverBase;
use crate::util::Perforce;
use crate::util::ProcessExecutor;
use crate::util::http::Response;

#[derive(Debug)]
pub struct PerforceDriver {
    inner: VcsDriverBase,
    pub(crate) depot: String,
    pub(crate) branch: String,
    pub(crate) perforce: Option<Perforce>,
}

impl PerforceDriver {
    pub fn initialize(&mut self) -> anyhow::Result<()> {
        self.depot = self
            .inner
            .repo_config
            .get("depot")
            .and_then(|v| v.as_string())
            .unwrap_or("")
            .to_string();
        self.branch = String::new();
        if let Some(branch) = self
            .inner
            .repo_config
            .get("branch")
            .and_then(|v| v.as_string())
        {
            if !branch.is_empty() {
                self.branch = branch.to_string();
            }
        }

        let repo_config = self.inner.repo_config.clone();
        self.init_perforce(&repo_config)?;
        self.perforce.as_mut().unwrap().p4_login()?;
        self.perforce.as_mut().unwrap().check_stream();
        self.perforce.as_mut().unwrap().write_p4_client_spec()?;
        self.perforce.as_mut().unwrap().connect_client();

        Ok(())
    }

    fn init_perforce(&mut self, repo_config: &IndexMap<String, PhpMixed>) -> anyhow::Result<()> {
        if self.perforce.is_some() {
            return Ok(());
        }

        let cache_vcs_dir = self
            .inner
            .config
            .borrow_mut()
            .get("cache-vcs-dir")
            .as_string()
            .unwrap_or("")
            .to_string();
        if !Cache::is_usable(&cache_vcs_dir) {
            return Err(RuntimeException {
                message: "PerforceDriver requires a usable cache directory, and it looks like you set it to be disabled".to_string(),
                code: 0,
            }.into());
        }

        let repo_dir = format!("{}/{}", cache_vcs_dir, self.depot);
        self.perforce = Some(Perforce::create(
            repo_config.clone(),
            self.inner.url.clone(),
            repo_dir,
            std::rc::Rc::clone(&self.inner.process),
            self.inner.io.clone_box(),
        ));

        Ok(())
    }

    pub fn get_file_content(
        &mut self,
        file: &str,
        identifier: &str,
    ) -> anyhow::Result<Option<String>> {
        Ok(self
            .perforce
            .as_mut()
            .unwrap()
            .get_file_content(file, identifier))
    }

    pub fn get_change_date(
        &self,
        _identifier: &str,
    ) -> anyhow::Result<Option<chrono::DateTime<chrono::Utc>>> {
        Ok(None)
    }

    pub fn get_root_identifier(&self) -> &str {
        &self.branch
    }

    pub fn get_branches(&mut self) -> anyhow::Result<IndexMap<String, String>> {
        Ok(self.perforce.as_mut().unwrap().get_branches())
    }

    pub fn get_tags(&mut self) -> anyhow::Result<IndexMap<String, String>> {
        Ok(self.perforce.as_mut().unwrap().get_tags())
    }

    pub fn get_dist(&self, _identifier: &str) -> Option<IndexMap<String, PhpMixed>> {
        None
    }

    pub fn get_source(&self, identifier: &str) -> IndexMap<String, PhpMixed> {
        let mut source = IndexMap::new();
        source.insert("type".to_string(), PhpMixed::String("perforce".to_string()));
        source.insert(
            "url".to_string(),
            self.inner
                .repo_config
                .get("url")
                .cloned()
                .unwrap_or(PhpMixed::Null),
        );
        source.insert(
            "reference".to_string(),
            PhpMixed::String(identifier.to_string()),
        );
        source.insert(
            "p4user".to_string(),
            PhpMixed::String(
                self.perforce
                    .as_ref()
                    .unwrap()
                    .get_user()
                    .unwrap_or_default(),
            ),
        );
        source
    }

    pub fn get_url(&self) -> &str {
        &self.inner.url
    }

    pub fn has_composer_file(&mut self, identifier: &str) -> bool {
        let path = format!("//{}/{}", self.depot, identifier);
        self.perforce
            .as_mut()
            .unwrap()
            .get_composer_information(&path)
            .map_or(false, |info| info.map_or(false, |i| !i.is_empty()))
    }

    pub fn get_contents(&self, _url: &str) -> anyhow::Result<Response> {
        Err(BadMethodCallException {
            message: "Not implemented/used in PerforceDriver".to_string(),
            code: 0,
        }
        .into())
    }

    pub fn supports(io: &dyn IOInterface, _config: &Config, url: &str, deep: bool) -> bool {
        if deep || Preg::is_match(r"#\b(perforce|p4)\b#i", url).unwrap_or(false) {
            return Perforce::check_server_exists(url, &mut ProcessExecutor::new(io));
        }
        false
    }

    pub fn cleanup(&mut self) -> anyhow::Result<()> {
        self.perforce.as_mut().unwrap().cleanup_client_spec();
        self.perforce = None;
        Ok(())
    }

    pub fn get_depot(&self) -> &str {
        &self.depot
    }

    pub fn get_branch(&self) -> &str {
        &self.branch
    }
}