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
#![deny(warnings, clippy::all, missing_docs)]
use std::path::PathBuf;
use std::process::Command;
use anyhow::{anyhow, Context, Error, Result};
use colored::Colorize;
use fehler::throws;
use versions::Versioning;
pub use dirs::*;
pub use manifest::{Manifest, ManifestRepo, ManifestStore};
pub use repos::HomebinRepos;
use crate::operations::{ApplyOperation, Operation};
use crate::tools::{manpath, path_contains};
mod checksum;
mod dirs;
mod process;
mod repos;
mod tools;
pub mod manifest;
pub mod operations;
#[throws]
pub fn check_environment(install_dirs: &InstallDirs) -> () {
match std::env::var_os("PATH") {
None => eprintln!("{}", "WARNING: $PATH not set!".yellow().bold()),
Some(path) => {
if !path_contains(&path, install_dirs.bin_dir()) {
eprintln!(
"{}\nAdd {} to $PATH in your shell profile.",
format!(
"WARNING: $PATH does not contain bin dir at {}",
install_dirs.bin_dir().display()
)
.yellow()
.bold(),
install_dirs.bin_dir().display()
)
}
}
};
if !path_contains(&manpath()?, install_dirs.man_dir()) {
eprintln!(
"{}\nAdd {} to $MANPATH in your shell profile; see man 1 manpath for more information",
format!(
"WARNING: manpath does not contain man dir at {}",
install_dirs.man_dir().display()
)
.yellow()
.bold(),
install_dirs.man_dir().display()
);
}
}
#[throws]
fn apply_operations(
dirs: &HomebinProjectDirs,
install_dirs: &mut InstallDirs,
manifest: &Manifest,
operations: &[Operation<'_>],
) -> () {
let op_dirs = ManifestOperationDirs::for_manifest(dirs, install_dirs, manifest)?;
op_dirs.ensure()?;
for operation in operations {
operation.apply_operation(&op_dirs)?;
}
}
pub fn install_manifest(
dirs: &HomebinProjectDirs,
install_dirs: &mut InstallDirs,
manifest: &Manifest,
) -> Result<()> {
apply_operations(
dirs,
install_dirs,
manifest,
&operations::install_manifest(manifest),
)
}
pub fn update_manifest(
dirs: &HomebinProjectDirs,
install_dirs: &mut InstallDirs,
manifest: &Manifest,
) -> Result<()> {
apply_operations(
dirs,
install_dirs,
manifest,
&operations::update_manifest(manifest),
)
}
pub fn remove_manifest(
dirs: &HomebinProjectDirs,
install_dirs: &mut InstallDirs,
manifest: &Manifest,
) -> Result<()> {
apply_operations(
dirs,
install_dirs,
manifest,
&operations::remove_manifest(manifest),
)
}
#[throws]
pub fn installed_manifest_version(dirs: &InstallDirs, manifest: &Manifest) -> Option<Versioning> {
let args = &manifest.discover.version_check.args;
let binary = dirs.bin_dir().join(&manifest.discover.binary);
if binary.is_file() {
let output = Command::new(&binary).args(args).output().with_context(|| {
format!(
"Failed to run {} with {:?}",
binary.display(),
&manifest.discover.version_check.args
)
})?;
let pattern = manifest.discover.version_check.regex().with_context(|| {
format!(
"Version check for {} failed: Invalid regex {}",
manifest.info.name, manifest.discover.version_check.pattern
)
})?;
let output = std::str::from_utf8(&output.stdout).with_context(|| {
format!(
"Output of command {} with {:?} returned non-utf8 stdout: {:?}",
binary.display(),
args,
output.stdout
)
})?;
let version = pattern
.captures(output)
.and_then(|c| c.get(1))
.map(|m| m.as_str());
version
.map(|s| {
Versioning::new(s).ok_or_else(|| {
anyhow!(
"Output of command {} with {:?} returned invalid version {:?}",
binary.display(),
args,
version
)
})
})
.transpose()?
} else {
None
}
}
#[throws]
pub fn outdated_manifest_version(dirs: &InstallDirs, manifest: &Manifest) -> Option<Versioning> {
installed_manifest_version(dirs, manifest)?
.filter(|installed| installed < &manifest.info.version)
}
pub fn installed_files(dirs: &InstallDirs, manifest: &Manifest) -> Vec<PathBuf> {
operations::operation_destinations(operations::install_manifest(manifest).iter())
.map(|destination| dirs.path(destination.directory()).join(destination.name()))
.collect()
}
pub fn files_to_remove(dirs: &InstallDirs, manifest: &Manifest) -> Vec<PathBuf> {
operations::operation_destinations(operations::remove_manifest(manifest).iter())
.map(|destination| dirs.path(destination.directory()).join(destination.name()))
.collect()
}