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
use std::{
collections::HashMap,
ffi::OsStr,
path::{Path, PathBuf},
};
use serde::{Deserialize, Serialize};
use walkdir::WalkDir;
use crate::{Bin, Directories, Manifest};
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct RawBuildManifest {
#[serde(default)]
pub name: Option<String>,
#[serde(default)]
pub bin: Option<Bin>,
#[serde(default)]
pub directories: Option<Directories>,
#[serde(default)]
pub scripts: HashMap<String, String>,
}
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BuildManifest {
#[serde(default)]
pub bin: HashMap<String, PathBuf>,
#[serde(default)]
pub scripts: HashMap<String, String>,
}
impl BuildManifest {
pub fn from_path(path: impl AsRef<Path>) -> std::io::Result<Self> {
let path = path.as_ref();
let pkg_str = std::fs::read_to_string(path)?;
let raw: RawBuildManifest = serde_json::from_str(&pkg_str)?;
Self::normalize(raw)
}
pub fn from_manifest(manifest: &Manifest) -> std::io::Result<Self> {
let raw = RawBuildManifest {
name: manifest.name.clone(),
bin: manifest.bin.clone(),
directories: manifest.directories.clone(),
scripts: manifest.scripts.clone(),
};
Self::normalize(raw)
}
fn normalize(raw: RawBuildManifest) -> std::io::Result<Self> {
let mut bin_map = HashMap::new();
if let Some(Bin::Hash(bins)) = raw.bin {
for (name, bin) in &bins {
let base = Path::new(name).file_name();
if base.is_none() || base == Some(OsStr::new("")) {
continue;
}
let base = Path::new("/")
.join(Path::new(
&base
.unwrap()
.to_string_lossy()
.to_string()
.replace(['\\', ':'], "/"),
))
.strip_prefix(
#[cfg(windows)]
"\\",
#[cfg(not(windows))]
"/",
)
.expect("We added this ourselves")
.to_path_buf();
if base == Path::new("") {
continue;
}
let bin_target = Path::new("/")
.join(bin.to_string_lossy().to_string())
.strip_prefix(
#[cfg(windows)]
"\\",
#[cfg(not(windows))]
"/",
)
.expect("We added this ourselves")
.to_path_buf();
if bin_target == Path::new("") {
continue;
}
bin_map.insert(base.to_string_lossy().to_string(), bin_target);
}
} else if let Some(Bin::Str(bin)) = raw.bin {
if let Some(name) = raw.name {
bin_map.insert(name, PathBuf::from(bin));
}
} else if let Some(Bin::Array(bins)) = raw.bin {
for bin in bins {
let name = bin
.as_path()
.file_name()
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("invalid bin name: {}", bin.to_string_lossy()),
)
})?
.to_string_lossy()
.to_string();
bin_map.insert(name, bin);
}
} else if let Some(Directories {
bin: Some(bin_dir), ..
}) = raw.directories
{
for entry in WalkDir::new(bin_dir) {
let entry = entry?;
let path = entry.path();
if path.starts_with(".") {
continue;
}
if let Some(file_name) = path.file_name() {
bin_map.insert(file_name.to_string_lossy().to_string(), path.into());
}
}
};
Ok(Self {
bin: bin_map,
scripts: raw.scripts,
})
}
}