-
Notifications
You must be signed in to change notification settings - Fork 13
/
artifact.rs
248 lines (216 loc) · 7.33 KB
/
artifact.rs
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
use std::{fmt, path::Path, str::FromStr};
use tracing::instrument;
use url::Url;
use crate::{
descriptor::{Descriptor, OS},
result::RokitResult,
tool::ToolSpec,
};
use super::{
decompression::decompress_gzip,
extraction::{extract_tar_file, extract_zip_file},
github::models::Asset,
ExtractError,
};
/**
An artifact provider supported by Rokit.
The default provider is [`ArtifactProvider::GitHub`].
*/
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ArtifactProvider {
#[default]
GitHub,
}
impl ArtifactProvider {
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::GitHub => "github",
}
}
#[must_use]
pub fn display_name(self) -> &'static str {
match self {
Self::GitHub => "GitHub",
}
}
}
impl FromStr for ArtifactProvider {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let l = s.trim().to_lowercase();
match l.as_str() {
"github" => Ok(Self::GitHub),
_ => Err(format!("unknown artifact provider '{l}'")),
}
}
}
impl fmt::Display for ArtifactProvider {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_str().fmt(f)
}
}
/**
An artifact format supported by Rokit.
*/
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ArtifactFormat {
Zip,
Tar,
TarGz,
}
impl ArtifactFormat {
#[must_use]
pub fn as_str(&self) -> &'static str {
match self {
Self::Zip => "zip",
Self::Tar => "tar",
Self::TarGz => "tar.gz",
}
}
pub fn from_path_or_url(path_or_url: impl AsRef<str>) -> Option<Self> {
let lowercased = path_or_url.as_ref().trim().to_lowercase();
let extension = Path::new(&lowercased).extension()?.to_str()?;
match extension {
ext if ext.eq_ignore_ascii_case("zip") => Some(Self::Zip),
ext if ext.eq_ignore_ascii_case("tar") => Some(Self::Tar),
ext if ext.eq_ignore_ascii_case("tgz") => Some(Self::TarGz),
ext if ext.eq_ignore_ascii_case("gz") => {
let stem = Path::new(&lowercased).file_stem()?;
let ext2 = Path::new(stem).extension()?;
if ext2.eq_ignore_ascii_case("tar") {
Some(Self::TarGz)
} else {
None
}
}
_ => None,
}
}
}
impl FromStr for ArtifactFormat {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let l = s.trim().to_lowercase();
match l.as_str() {
"zip" => Ok(Self::Zip),
"tar" => Ok(Self::Tar),
"tar.gz" => Ok(Self::TarGz),
_ => Err(format!("unknown artifact format '{l}'")),
}
}
}
impl fmt::Display for ArtifactFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_str().fmt(f)
}
}
/**
An artifact found by Rokit, to be downloaded and installed.
*/
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Artifact {
pub provider: ArtifactProvider,
pub format: Option<ArtifactFormat>,
pub id: Option<String>,
pub url: Option<Url>,
pub name: Option<String>,
pub tool_spec: ToolSpec,
}
impl Artifact {
pub(crate) fn from_github_release_asset(asset: &Asset, spec: &ToolSpec) -> Self {
let format = ArtifactFormat::from_path_or_url(&asset.name);
Self {
provider: ArtifactProvider::GitHub,
format,
id: Some(asset.id.to_string()),
url: Some(asset.url.clone()),
name: Some(asset.name.clone()),
tool_spec: spec.clone(),
}
}
/**
Extract the contents of the artifact.
The given contents must be the raw bytes of the artifact,
in the expected format, as downloaded from the download URL.
This generally means that, as long as the same artifact provider
is used to both create and download the artifact, the format
should be known and the contents should be in the correct format.
*/
#[instrument(skip(self, contents), level = "debug")]
pub async fn extract_contents(&self, contents: Vec<u8>) -> RokitResult<Vec<u8>> {
let format = self.format.ok_or(ExtractError::UnknownFormat)?;
let file_name = self.tool_spec.name().to_string();
let file_res = match format {
ArtifactFormat::Zip => extract_zip_file(&contents, &file_name).await,
ArtifactFormat::Tar => extract_tar_file(&contents, &file_name).await,
ArtifactFormat::TarGz => {
let tar = decompress_gzip(&contents).await?;
extract_tar_file(&tar, &file_name).await
}
};
// Make sure we got back the file we need ...
let file_opt = file_res.map_err(|err| ExtractError::Generic {
source: err.into(),
body: {
if contents.len() > 128 + 6 {
let bytes = contents.iter().copied().take(128).collect::<Vec<_>>();
format!("{} <...>", String::from_utf8_lossy(bytes.as_slice()).trim())
} else {
String::from_utf8_lossy(&contents).to_string()
}
},
})?;
let file_bytes = file_opt.ok_or_else(|| ExtractError::FileMissing {
format,
file_name: self.tool_spec.name().to_string(),
archive_name: self.name.clone().unwrap_or_default(),
})?;
// ... and parse the OS from the executable binary, or error,
// to ensure that the user will actually be able to run it
let os_current = OS::current_system();
let os_file = OS::detect_from_executable(&file_bytes);
if os_file.is_some_and(|os| os != os_current) {
Err(ExtractError::OSMismatch {
current_os: os_current,
file_os: os_file.unwrap(),
file_name: self.tool_spec.name().to_string(),
archive_name: self.name.clone().unwrap_or_default(),
})?;
}
Ok(file_bytes)
}
/**
Sorts the given artifacts by system compatibility.
See also:
- [`Descriptor::current_system`]
- [`Descriptor::is_compatible_with`]
- [`Descriptor::sort_by_preferred_compat`]
*/
pub fn sort_by_system_compatibility(artifacts: impl AsRef<[Self]>) -> Vec<Self> {
let current_desc = Descriptor::current_system();
let mut compatible_artifacts = artifacts
.as_ref()
.iter()
.filter_map(|artifact| {
let name = artifact.name.as_deref()?;
if let Some(asset_desc) = Descriptor::detect(name) {
if current_desc.is_compatible_with(&asset_desc) {
Some((asset_desc, artifact))
} else {
None
}
} else {
None
}
})
.collect::<Vec<_>>();
compatible_artifacts.sort_by(|(desc_a, _), (desc_b, _)| {
current_desc.sort_by_preferred_compat(desc_a, desc_b)
});
compatible_artifacts
.into_iter()
.map(|(_, artifact)| artifact.clone())
.collect()
}
}