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
|
use std::collections::HashMap;
use std::path::PathBuf;
use anyhow::Result;
use getset::Getters;
use serde::Deserialize;
use serde::Serialize;
use crate::package::ParseDependency;
use crate::package::dependency::*;
use crate::package::name::*;
use crate::package::source::*;
use crate::package::version::*;
use crate::phase::{PhaseName, Phase};
use crate::util::docker::ImageName;
#[derive(Clone, Serialize, Deserialize, Getters)]
pub struct Package {
#[getset(get = "pub")]
name: PackageName,
#[getset(get = "pub")]
version: PackageVersion,
#[getset(get = "pub")]
version_is_semver: bool,
#[getset(get = "pub")]
sources: Vec<Source>,
#[getset(get = "pub")]
dependencies: Dependencies,
#[getset(get = "pub")]
patches: Vec<PathBuf>,
#[getset(get = "pub")]
#[serde(skip_serializing_if = "Option::is_none")]
environment: Option<HashMap<String, String>>,
#[getset(get = "pub")]
#[serde(skip_serializing_if = "Option::is_none")]
flags: Option<PackageFlags>,
#[getset(get = "pub")]
#[serde(skip_serializing_if = "Option::is_none")]
deny_on_images: Option<Vec<ImageName>>,
#[getset(get = "pub")]
phases: HashMap<PhaseName, Phase>,
}
impl Package {
#[cfg(test)]
pub fn new(name: PackageName, version: PackageVersion, version_is_semver: bool, sources: Vec<Source>, dependencies: Dependencies) -> Self {
Package {
name,
version,
version_is_semver,
sources,
dependencies,
patches: vec![],
environment: None,
flags: None,
deny_on_images: None,
phases: HashMap::new(),
}
}
#[cfg(test)]
pub fn set_dependencies(&mut self, dependencies: Dependencies) {
self.dependencies = dependencies;
}
/// Get all dependencies of the package
///
/// Either return the list of dependencies or, if available, run the dependencies_script to
/// read the dependencies from there.
pub fn get_all_dependencies<'a>(&'a self) -> impl Iterator<Item = Result<(PackageName, PackageVersionConstraint)>> + 'a {
self.get_system_dependencies().chain(self.get_self_packaged_dependencies())
}
pub fn get_system_dependencies<'a>(&'a self) -> impl Iterator<Item = Result<(PackageName, PackageVersionConstraint)>> + 'a {
let system_iter = self.dependencies()
.system()
.iter()
.cloned()
.map(|d| d.parse_as_name_and_version());
let system_runtime_iter = self.dependencies()
.system_runtime()
.iter()
.cloned()
.map(|d| d.parse_as_name_and_version());
system_iter.chain(system_runtime_iter)
}
pub fn get_self_packaged_dependencies<'a>(&'a self) -> impl Iterator<Item = Result<(PackageName, PackageVersionConstraint)>> + 'a {
let build_iter = self.dependencies()
.build()
.iter()
.cloned()
.map(|d| d.parse_as_name_and_version());
let runtime_iter = self.dependencies()
.runtime()
.iter()
.cloned()
.map(|d| d.parse_as_name_and_version());
build_iter.chain(runtime_iter)
}
}
impl std::fmt::Debug for Package {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
if self.patches().is_empty() {
write!(f, "Package({:?}, {:?})", self.name(), self.version())
} else {
write!(f, "Package({:?}, {:?} + patches)", self.name(), self.version())
}
}
}
impl PartialEq for Package {
fn eq(&self, other: &Package) -> bool {
(self.name(), self.version()).eq(&(other.name(), other.version()))
}
}
impl PartialOrd for Package {
fn partial_cmp(&self, other: &Package) -> Option<std::cmp::Ordering> {
(self.name(), self.version()).partial_cmp(&(other.name(), other.version()))
}
}
impl Ord for Package {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
(self.name(), self.version()).cmp(&(other.name(), other.version()))
}
}
impl Eq for Package {
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PackageFlags {
build_parallel: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize, Getters)]
pub struct Dependencies {
#[getset(get = "pub")]
system: Vec<SystemBuildDependency>,
#[getset(get = "pub")]
system_runtime: Vec<SystemDependency>,
#[getset(get = "pub")]
build: Vec<BuildDependency>,
#[getset(get = "pub")]
runtime: Vec<Dependency>,
}
#[cfg(test)]
impl Dependencies {
pub fn empty() -> Self {
Dependencies {
system: vec![],
system_runtime: vec![],
build: vec![],
runtime: vec![],
}
}
pub fn with_runtime_dependency(runtime_dependency: Dependency) -> Self {
Dependencies::with_runtime_dependencies(vec![runtime_dependency])
}
pub fn with_runtime_dependencies(runtime_dependencies: Vec<Dependency>) -> Self {
Dependencies {
system: vec![],
system_runtime: vec![],
build: vec![],
runtime: runtime_dependencies,
}
}
}
#[cfg(test)]
pub mod tests {
use super::*;
use url::Url;
use crate::package::Source;
use crate::package::SourceHash;
use crate::package::HashType;
use crate::package::HashValue;
use crate::package::Dependencies;
/// helper function for quick object construction
pub fn pname(name: &str) -> PackageName {
PackageName::from(String::from(name))
}
/// helper function for quick object construction
pub fn pversion(version: &str) -> PackageVersion {
PackageVersion::from(String::from(version))
}
/// helper function for quick object construction
pub fn package(name: &str, vers: &str, srcurl: &str, hash: &str) -> Package {
let name = pname(name);
let version = pversion(vers);
let version_is_semver = false;
let sources = {
let url = Url::parse(srcurl).unwrap();
let hashvalue = HashValue::from(String::from(hash));
vec![Source::new(url, SourceHash::new(HashType::Sha1, hashvalue))]
};
let dependencies = Dependencies::empty();
Package::new(name, version, version_is_semver, sources, dependencies)
}
}
|