summaryrefslogtreecommitdiffstats
path: root/src/repository/repository.rs
blob: f90c31f6eeb2e18733e749ad4215774520d0bc38 (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
//
// Copyright (c) 2020-2022 science+computing ag and other contributors
//
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//

use std::collections::BTreeMap;
use std::path::Path;
use std::path::PathBuf;

use anyhow::anyhow;
use anyhow::Context;
use anyhow::Error;
use anyhow::Result;
use log::trace;
use resiter::AndThen;
use resiter::FilterMap;
use resiter::Map;

use crate::package::Package;
use crate::package::PackageName;
use crate::package::PackageVersion;
use crate::package::PackageVersionConstraint;

/// A repository represents a collection of packages
pub struct Repository {
    inner: BTreeMap<(PackageName, PackageVersion), Package>,
}

#[cfg(test)]
impl From<BTreeMap<(PackageName, PackageVersion), Package>> for Repository {
    fn from(inner: BTreeMap<(PackageName, PackageVersion), Package>) -> Self {
        Repository { inner }
    }
}

impl Repository {
    fn new(inner: BTreeMap<(PackageName, PackageVersion), Package>) -> Self {
        Repository { inner }
    }

    pub fn load(path: &Path, progress: &indicatif::ProgressBar) -> Result<Self> {
        use crate::repository::fs::FileSystemRepresentation;
        use config::Config;
        use rayon::iter::IntoParallelRefIterator;
        use rayon::iter::ParallelIterator;

        trace!("Loading files from filesystem");
        let fsr = FileSystemRepresentation::load(path.to_path_buf())?;

        fn get_patches(config: &Config) -> Result<Vec<PathBuf>> {
            match config.get_array("patches") {
                Ok(v)  => v.into_iter()
                    .map(config::Value::into_str)
                    .map_err(Error::from)
                    .map_err(|e| e.context("patches must be strings"))
                    .map_err(Error::from)
                    .map_ok(PathBuf::from)
                    .collect(),
                Err(config::ConfigError::NotFound(_)) => Ok(Vec::with_capacity(0)),
                Err(e) => Err(e).map_err(Error::from),
            }
        }

        fsr.files()
            .par_iter()
            .inspect(|path| trace!("Checking for leaf file: {}", path.display()))
            .filter_map(|path| {
                match fsr.is_leaf_file(path) {
                    Ok(true) => Some(Ok(path)),
                    Ok(false) => None,
                    Err(e) => Some(Err(e)),
                }
            })
            .inspect(|r| trace!("Loading files for {:?}", r))
            .map(|path| {
                progress.tick();
                let path = path?;
                fsr.get_files_for(path)?
                    .iter()
                    .inspect(|(path, _)| trace!("Loading layer at {}", path.display()))
                    .fold(Ok(Config::default()) as Result<_>, |config, (path, content)| {
                        let mut config = config?;
                        let patches_before_merge = get_patches(&config)?;

                        config.merge(config::File::from_str(content, config::FileFormat::Toml))
                            .with_context(|| anyhow!("Loading contents of {}", path.display()))?;

                        // get the patches that are in the `config` object after the merge
                        let patches = get_patches(&config)?
                            .into_iter()
                            .map(|p| if let Some(current_dir) = path.parent() {
                                Ok(current_dir.join(p))
                            } else {
                                Err(anyhow!("Path should point to path with parent, but doesn't: {}", path.display()))
                            })
                            .inspect(|patch| trace!("Patch: {:?}", patch))

                            // if the patch file exists, use it (as config::Value).
                            //
                            // Otherwise we have an error here, because we're refering to a non-existing file.
                            .and_then_ok(|patch| if patch.exists() {
                                trace!("Path to patch exists: {}", patch.display());
                                Ok(Some(patch))
                            } else if patches_before_merge.iter().any(|pb| pb.file_name() == patch.file_name()) {
                                // We have a patch already in the array that is named equal to the patch
                                // we have in the fold iteration.
                                // It seems like this patch was already in the list and we re-found it
                                // because we loaded a "deeper" pkg.toml file.
                                Ok(None)
                            } else {
                                trace!("Path to patch does not exist: {}", patch.display());
                                Err(anyhow!("Patch does not exist: {}", patch.display()))
                            })
                            .filter_map_ok(|o| o)
                            .collect::<Result<Vec<_>>>()?;

                        // If we found any patches, use them. Otherwise use the array from before the merge
                        // (which already has the correct pathes from the previous recursion).
                        let patches = if !patches.is_empty() {
                            patches
                        } else {
                            patches_before_merge
                        };

                        trace!("Patches after postprocessing merge: {:?}", patches);
                        let patches = patches
                            .into_iter()
                            .map(|p| p.display().to_string())
                            .map(config::Value::from)
                            .collect::<Vec<_>>();
                        config.set_once("patches", config::Value::from(patches))?;
                        Ok(config)
                    })
                    .and_then(|c| c.try_into::<Package>().map_err(Error::from))
                    .map(|pkg| ((pkg.name().clone(), pkg.version().clone()), pkg))
            })
            .collect::<Result<BTreeMap<_, _>>>()
            .map(Repository::new)
    }

    pub fn find_by_name<'a>(&'a self, name: &PackageName) -> Vec<&'a Package> {
        trace!("Searching for '{}' in repository", name);
        self.inner
            .iter()
            .filter(|((n, _), _)| {
                trace!("{} == {} -> {}", name, n, name == n);
                name == n
            })
            .map(|(_, pack)| pack)
            .collect()
    }

    pub fn find<'a>(&'a self, name: &PackageName, version: &PackageVersion) -> Vec<&'a Package> {
        self.inner
            .iter()
            .filter(|((n, v), _)| n == name && v == version)
            .map(|(_, p)| p)
            .collect()
    }

    pub fn find_with_version<'a>(
        &'a self,
        name: &PackageName,
        vc: &PackageVersionConstraint,
    ) -> Vec<&'a Package> {
        self.inner
            .iter()
            .filter(|((n, v), _)| n == name && vc.matches(v))
            .map(|(_, p)| p)
            .collect()
    }

    pub fn packages(&self) -> impl Iterator<Item = &Package> {
        self.inner.values()
    }
}

#[cfg(test)]
pub mod tests {
    use super::*;
    use crate::package::tests::package;
    use crate::package::tests::pname;
    use crate::package::tests::pversion;

    #[test]
    fn test_finding_by_name() {
        let mut btree = BTreeMap::new();

        {
            let name = "a";
            let vers = "1";
            let pack = package(name, vers, "https://rust-lang.org", "123");
            btree.insert((pname(name), pversion(vers)), pack);
        }

        let repo = Repository::from(btree);

        let ps = repo.find_by_name(&pname("a"));
        assert_eq!(ps.len(), 1);

        let p = ps.get(0).unwrap();
        assert_eq!(*p.name(), pname("a"));
        assert_eq!(*p.version(), pversion("1"));
        assert!(!p.version_is_semver());
    }

    #[test]
    fn test_find() {
        let mut btree = BTreeMap::new();

        {
            let name = "a";
            let vers = "1";
            let pack = package(name, vers, "https://rust-lang.org", "123");
            btree.insert((pname(name), pversion(vers)), pack);
        }
        {
            let