aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/repository/repository_manager.rs
blob: 247236c303f3142203c5a686b658f04041969891 (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
//! ref: composer/src/Composer/Repository/RepositoryManager.php

use indexmap::IndexMap;
use shirabe_php_shim::{InvalidArgumentException, PhpMixed, json_encode};
use shirabe_semver::constraint::AnyConstraint;

use crate::config::Config;
use crate::event_dispatcher::EventDispatcher;
use crate::io::IOInterface;
use crate::io::IOInterfaceImmutable;
use crate::package::PackageInterfaceHandle;
use crate::repository::FilterRepository;
use crate::repository::RepositoryInterfaceHandle;
use crate::util::HttpDownloader;
use crate::util::ProcessExecutor;

#[derive(Debug)]
pub struct RepositoryManager {
    local_repository: Option<RepositoryInterfaceHandle>,
    repositories: Vec<RepositoryInterfaceHandle>,
    repository_classes: IndexMap<String, String>,
    io: std::rc::Rc<std::cell::RefCell<dyn IOInterface>>,
    config: std::rc::Rc<std::cell::RefCell<Config>>,
    http_downloader: std::rc::Rc<std::cell::RefCell<HttpDownloader>>,
    event_dispatcher: Option<std::rc::Rc<std::cell::RefCell<EventDispatcher>>>,
    process: std::rc::Rc<std::cell::RefCell<ProcessExecutor>>,
}

impl RepositoryManager {
    pub fn new(
        io: std::rc::Rc<std::cell::RefCell<dyn IOInterface>>,
        config: std::rc::Rc<std::cell::RefCell<Config>>,
        http_downloader: std::rc::Rc<std::cell::RefCell<HttpDownloader>>,
        event_dispatcher: Option<std::rc::Rc<std::cell::RefCell<EventDispatcher>>>,
        process: Option<std::rc::Rc<std::cell::RefCell<ProcessExecutor>>>,
    ) -> Self {
        let process = process.unwrap_or_else(|| {
            std::rc::Rc::new(std::cell::RefCell::new(ProcessExecutor::new(Some(
                io.clone(),
            ))))
        });
        Self {
            local_repository: None,
            repositories: vec![],
            repository_classes: IndexMap::new(),
            io,
            config,
            http_downloader,
            event_dispatcher,
            process,
        }
    }

    pub fn find_package(
        &self,
        name: &str,
        constraint: &AnyConstraint,
    ) -> anyhow::Result<Option<PackageInterfaceHandle>> {
        for repository in &self.repositories {
            if let Some(package) = repository.find_package(
                name,
                crate::repository::FindPackageConstraint::Constraint(constraint.clone()),
            )? {
                return Ok(Some(package));
            }
        }
        Ok(None)
    }

    pub fn find_packages(
        &self,
        name: &str,
        constraint: &AnyConstraint,
    ) -> anyhow::Result<Vec<PackageInterfaceHandle>> {
        let mut packages: Vec<PackageInterfaceHandle> = vec![];
        for repository in self.get_repositories() {
            for p in repository.find_packages(
                name,
                Some(crate::repository::FindPackageConstraint::Constraint(
                    constraint.clone(),
                )),
            )? {
                packages.push(p);
            }
        }
        Ok(packages)
    }

    pub fn add_repository(&mut self, repository: RepositoryInterfaceHandle) {
        self.repositories.push(repository);
    }

    pub fn prepend_repository(&mut self, repository: RepositoryInterfaceHandle) {
        self.repositories.insert(0, repository);
    }

    pub fn create_repository(
        &self,
        r#type: &str,
        config: IndexMap<String, PhpMixed>,
        name: Option<&str>,
    ) -> anyhow::Result<RepositoryInterfaceHandle> {
        if !self.repository_classes.contains_key(r#type) {
            return Err(InvalidArgumentException {
                message: format!("Repository type is not registered: {}", r#type),
                code: 0,
            }
            .into());
        }

        if config.get("packagist").and_then(|v| v.as_bool()) == Some(false) {
            let config_json = json_encode(&PhpMixed::Array(
                config.iter().map(|(k, v)| (k.clone(), v.clone())).collect(),
            ))
            .unwrap_or_default();
            self.io.write_error(&format!("<warning>Repository \"{}\" ({}) has a packagist key which should be in its own repository definition</warning>", name.unwrap_or(""), config_json));
        }

        let class = self.repository_classes[r#type].clone();

        let has_filter = config.contains_key("only")
            || config.contains_key("exclude")
            || config.contains_key("canonical");
        let filter_config = if has_filter {
            Some(config.clone())
        } else {
            None
        };

        let mut cleaned_config = config;
        cleaned_config.shift_remove("only");
        cleaned_config.shift_remove("exclude");
        cleaned_config.shift_remove("canonical");

        // Phase B: implement dynamic class instantiation by class name
        let repository = self.create_repository_by_class(&class, cleaned_config)?;

        if let Some(filter_config) = filter_config {
            return Ok(RepositoryInterfaceHandle::new(FilterRepository::new(
                repository,
                filter_config,
            )?));
        }

        Ok(repository)
    }

    fn create_repository_by_class(
        &self,
        class: &str,
        config: IndexMap<String, PhpMixed>,
    ) -> anyhow::Result<RepositoryInterfaceHandle> {
        // PHP: `new $class($config, $this->io, $this->config, $this->httpDownloader,
        // $this->eventDispatcher, $this->process)`. Rust cannot instantiate by string class name, so
        // dispatch over the classes registered in `createDefaultRepositoryManager`.
        match class {
            "Composer\\Repository\\ComposerRepository" => Ok(RepositoryInterfaceHandle::new(
                crate::repository::ComposerRepository::new(
                    config,
                    self.io.clone(),
                    &self.config.borrow(),
                    self.http_downloader.clone(),
                    self.event_dispatcher.clone(),
                )?,
            )),
            "Composer\\Repository\\PackageRepository" => Ok(RepositoryInterfaceHandle::new(
                crate::repository::PackageRepository::new(config),
            )),
            other => todo!(
                "Phase B: dynamic class instantiation by class name: {}",
                other
            ),
        }
    }

    pub fn set_repository_class(&mut self, r#type: &str, class: &str) {
        self.repository_classes
            .insert(r#type.to_string(), class.to_string());
    }

    pub fn get_repositories(&self) -> &Vec<RepositoryInterfaceHandle> {
        &self.repositories
    }

    /// For testing only: exposes the private `repository_classes` map so tests
    /// can assert on its registered type keys (PHP reads it via ReflectionProperty).
    pub fn __repository_classes(&self) -> &IndexMap<String, String> {
        &self.repository_classes
    }

    pub fn set_local_repository(&mut self, repository: RepositoryInterfaceHandle) {
        self.local_repository = Some(repository);
    }

    pub fn get_local_repository(&self) -> RepositoryInterfaceHandle {
        self.local_repository.clone().unwrap()
    }
}