summaryrefslogtreecommitdiffstats
path: root/src/repository/repository.rs
blob: 170c2b0a6b3d3b3d3887678961154834ac646067 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//
// Copyright (c) 2020-2021 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 {
    pub fn load(path: &Path, progress: &indicatif::ProgressBar) -> Result<Self> {
        fn all_subdirs(p: &Path) -> Result<Vec<PathBuf>> {
            let mut v = Vec::new();
            for de in p.read_dir()? {
                let de = de?;
                let is_dir = de.file_type()?.is_dir();
                let is_hidden = de
                    .path()
                    .file_name()
                    .and_then(|s| s.to_str())
                    .map(|s| s.starts_with('.'))
                    .unwrap_or(false);

                if is_dir && !is_hidden {
                    v.push(de.path());
                }
            }

            Ok(v)
        }

        fn load_recursive(
            root: &Path,
            path: &Path,
            mut config: config::Config,
            progress: &indicatif::ProgressBar,
        ) -> Result<Vec<Result<Package>>> {
            let pkg_file = path.join("pkg.toml");

            if pkg_file.is_file() {
                let buf = std::fs::read_to_string(&pkg_file)
                    .with_context(|| format!("Reading {}", pkg_file.display()))?;

                // This function has an issue: It loads packages recursively, but if there are
                // patches set for a package, these patches are set _relative_ to the current
                // pkg.toml file.
                //
                // E.G.:
                // (1) /pkg.toml
                // (2) /a/pkg.toml
                // (3) /a/1.0/pkg.toml
                // (4) /a/2.0/pkg.toml
                //
                // If (2) defines a patches = ["./1.patch"], the patch exists at /a/1.patch.
                // We can fix that by modifying the Config object after loading (2) and fixing the
                // path of the patch to be relative to the repostory root.
                //
                // But if we continue loading the /a/ subdirectory recursively, this value gets
                // overwritten by Config::refresh(), which is called by Config::merge, for example.
                //
                // The trick is, to get the list of patches _before_ the merge, and later
                // re-setting them after the merge, if there were no new patches set (which itself
                // is tricky to find out, because the `Config` object _looks like_ there is a new
                // array set).
                //
                // If (3), for example, does set a new patches=[] array, the old array is
                // invalidated and no longer relevant for that package!
                // Thus, we can savely throw it away and continue with the new array, fixing the
                // pathes to be relative to repo root again.
                //
                // If (4) does _not_ set any patches, we must ensure that the patches from the
                // loading of (2) are used and not overwritten by the Config::refresh() call
                // happening during Config::merge().
                //

                // first of all, we get the patches array.
                // This is either the patches array from the last recursion or the newly set one,
                // that doesn't matter here.
                let patches_before_merge = match config.get_array("patches") {
                    Ok(v)  => {
                        v.into_iter()
                            .map(|p| {
                                p.into_str()
                                    .map(PathBuf::from)
                                    .with_context(|| anyhow!("patches must be strings"))
                                    .map_err(Error::from)
                            })
                            .collect::<Result<Vec<_>>>()?
                    },
                    Err(config::ConfigError::NotFound(_)) => vec![],
                    Err(e)                                => return Err(e).map_err(Error::from),
                };
                trace!("Patches before merging: {:?}", patches_before_merge);

                // Merge the new pkg.toml file over the already loaded configuration
                config
                    .merge(config::File::from_str(&buf, config::FileFormat::Toml))
                    .with_context(|| format!("Loading contents of {}", pkg_file.display()))?;

                let path_relative_to_root = path.strip_prefix(root)?;

                // get the patches that are in the `config` object after the merge
                let patches = match config.get_array("patches") {
                    Ok(v) => {
                        trace!("Patches after merging: {:?}", v);
                        v
                    },

                    // if there was none, we simply use an empty array
                    // This is cheap because Vec::with_capacity(0) does not allocate
                    Err(config::ConfigError::NotFound(_)) => Vec::with_capacity(0),
                    Err(e)                                => return Err(e).map_err(Error::from),
                }
                .into_iter()

                // Map all `Value`s to String and then join them on the path that is relative to
                // the root directory of the repository.
                .map(|patch| patch.into_str().map_err(Error::from))
                .map_ok(|patch| path_relative_to_root.join(patch))
                .inspect(|patch| trace!("Patch relative to root: {:?}", patch.as_ref().map(|p| p.display())))

                // 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 current recursion.
                    // 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.iter().all(|p| p.exists()) {
                    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))?;
            }

            let subdirs = all_subdirs(path)
                .with_context(|| format!("Finding subdirs for {}", pkg_file.display()))?;

            if subdirs.is_empty() {
                progress.tick();
                if pkg_file.is_file() {
                    let package = config.try_into()
                        .with_context(|| format!("Failed to parse {} into package", path.display()))
                        .and_then(|package: Package| {
                            if package.name().is_empty() {
                                Err(anyhow!("Package name cannot be empty: {}", pkg_file.display()))
                            } else if package.version().is_empty() {
                                Err(anyhow!("Package version cannot be empty: {}", pkg_file.display()))
                            } else {
                                Ok(package)
                            }
                        });

                    Ok(vec![package])
                } else {
                    Ok(vec![])
                }
            } else {
                subdirs.into_iter().fold(Ok(Vec::new()), |vec, dir| {
                    vec.and_then(|mut v| {
                        trace!("Recursing into {}", dir.display());
                        let mut loaded = load_recursive(root, &dir, config.clone(), progress)
                            .with_context(|| format!("Reading package from {}", pkg_file.display()))?;

                        v.append(&mut loaded);
                        Ok(v)
                    })
                })
            }
        }

        let inner = load_recursive(path, path, config::Config::default(), progress)
            .with_context(|| format!("Recursing for {}", path.display()))?
            .into_iter()
            .inspect(|p| trace!("Loading into repository: {:?}", p))
            .map_ok(|p| ((p.name().clone