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
#![allow(unused_assignments)]
use clap::{ArgAction, Parser};
use crossterm::{
cursor,
style::Stylize,
terminal::{Clear, ClearType},
ExecutableCommand,
};
use scoop_rs::{operation, Event, Session, SyncOption};
use std::io::Write;
use crate::{cui, flavor, util, Result};
/// Install package(s)
#[derive(Debug, Parser)]
#[clap(arg_required_else_help = true)]
pub struct Args {
/// The package(s) to install (name, manifest URL, or local manifest file)
#[arg(required = true, action = ArgAction::Append)]
package: Vec<String>,
/// Download package(s) without performing installation
#[arg(short = 'd', long, action = ArgAction::SetTrue)]
download_only: bool,
/// Ignore failures to ensure a complete transaction
#[arg(short = 'f', long, action = ArgAction::SetTrue)]
ignore_failure: bool,
/// Leverage cache and suppress network access
#[arg(short = 'o', long, action = ArgAction::SetTrue)]
offline: bool,
/// Assume yes to all prompts and run non-interactively
#[arg(short = 'y', long, action = ArgAction::SetTrue)]
assume_yes: bool,
/// Ignore cache and force download
#[arg(short = 'D', long, action = ArgAction::SetTrue)]
ignore_cache: bool,
/// Do not install dependencies (may break packages)
#[arg(short = 'I', long, action = ArgAction::SetTrue)]
independent: bool,
/// Do not replace package(s)
#[arg(short = 'R', long, action = ArgAction::SetTrue)]
no_replace: bool,
/// Escape hold to allow changes on held package(s)
#[arg(short = 'S', long, action = ArgAction::SetTrue)]
escape_hold: bool,
/// Do not upgrade package(s)
#[arg(short = 'U', long, action = ArgAction::SetTrue)]
no_upgrade: bool,
/// Skip package integrity check
#[arg(long, action = ArgAction::SetTrue)]
no_hash_check: bool,
/// Install globally for all users (requires admin rights)
#[arg(short = 'g', long, action = ArgAction::SetTrue)]
global: bool,
/// Target architecture (32bit, 64bit or arm64; default: host arch)
#[arg(long)]
arch: Option<String>,
}
impl Args {
/// Build args for programmatic installs (e.g. `import`).
pub(crate) fn from_packages(
package: Vec<String>,
assume_yes: bool,
arch: Option<String>,
) -> Self {
Self {
package,
download_only: false,
ignore_failure: false,
offline: false,
assume_yes,
ignore_cache: false,
independent: false,
no_replace: false,
escape_hold: false,
no_upgrade: false,
no_hash_check: false,
global: false,
arch,
}
}
}
pub fn execute(args: Args, session: &Session) -> Result<()> {
crate::util::apply_global_flag(args.global, session)?;
crate::util::apply_arch_flag(args.arch.as_deref(), session)?;
let mut options = vec![];
if args.assume_yes {
options.push(SyncOption::AssumeYes);
}
if args.download_only {
options.push(SyncOption::DownloadOnly);
}
if args.escape_hold {
options.push(SyncOption::EscapeHold);
}
if args.ignore_failure {
options.push(SyncOption::IgnoreFailure);
}
if args.ignore_cache {
options.push(SyncOption::IgnoreCache);
}
if args.no_upgrade {
options.push(SyncOption::NoUpgrade);
}
if args.no_replace {
options.push(SyncOption::NoReplace);
}
if args.offline {
options.push(SyncOption::Offline);
}
if args.independent {
options.push(SyncOption::NoDependencies);
}
if args.no_hash_check {
options.push(SyncOption::NoHashCheck);
}
let rx = session.event_bus().receiver();
let tx = session.event_bus().sender();
let show_manifest = session.config().show_manifest();
let mut stdout = std::io::stdout();
let _ = stdout.execute(cursor::Hide);
let mut dlprogress = cui::MultiProgressUI::new();
let handle = std::thread::spawn(move || {
while let Ok(event) = rx.recv() {
match event {
Event::PackageResolveStart => println!("{}", flavor::progress("resolve")),
Event::PackageRunningSkipped { name, processes } => {
println!("Skipping '{name}' (running: {processes}). Quit it and retry to include it.");
}
Event::PackageAlreadyInstalled { name, version } => {
println!(
"{}",
format!("'{name}' ({version}) is already installed. Skipping.").yellow()
);
}
Event::PackageDownloadSizingStart => println!("{}", flavor::progress("sizing")),
Event::PackageDownloadStart => println!("{}", flavor::progress("download")),
Event::PackageDownloadProgress(ctx) => {
let ident = ctx.ident.to_owned();
let url = ctx.url.to_owned();
let filename = ctx.filename.to_owned();
let dltotal = ctx.dltotal;
let dlnow = ctx.dlnow;
dlprogress.update(ident, url, filename, dltotal, dlnow);
}
Event::PackageDownloadDone => {}
Event::PackageIntegrityCheckStart => {
println!("{}", flavor::progress("integrity"))
}
Event::PackageIntegrityCheckProgress(ctx) => {
let mut stdout = std::io::stdout();
stdout
.execute(cursor::MoveToPreviousLine(1))
.unwrap()
.execute(Clear(ClearType::CurrentLine))
.unwrap();
println!("{}{}", flavor::progress("integrity"), ctx.dark_grey());
}
Event::PackageIntegrityCheckDone => {
let mut stdout = std::io::stdout();
stdout
.execute(cursor::MoveToPreviousLine(1))
.unwrap()
.execute(Clear(ClearType::CurrentLine))
.unwrap();
println!("{}{}", flavor::progress("integrity"), "Ok".green());
}
Event::PromptPackageCandidate(pkgs) => {
let name = pkgs[0].split_once('/').unwrap().1;
println!("Found multiple candidates for package '{}':\n", name);
for (i, pkg) in pkgs.iter().enumerate() {
println!(" {}: {}", i, pkg);
}
let mut index = 0;
let mut stdout = std::io::stdout();
let _ = stdout.execute(cursor::Show);
loop {
print!("\nPlease select one, enter the number to continue: ");
std::io::stdout().flush().unwrap();
let mut input = String::new();
std::io::stdin().read_line(&mut input).unwrap();
let parsed = input.trim().parse::<usize>();
if let Ok(num) = parsed {
index = num;
// bounds check
if num < pkgs.len() {
break;
}
}
}
let _ = stdout.execute(cursor::Hide);
let _ = tx.send(Event::PromptPackageCandidateResult(index));
}
Event::PromptTransactionNeedConfirm(transaction) => {
if show_manifest {
cui::show_manifests(&transaction);
}
if let Some(install) = transaction.install_view() {
println!("The following packages will be INSTALLED:");
let output = install
.iter()
.map(|p| {
format!(
"{}{}{}",
p.ident(),
"-".dark_grey(),
p.version().dark_grey(),
)
})
.collect::<Vec<_>>()
.join(" ");
println!(" {}", output);
}
if let Some(upgrade) = transaction.upgrade_view() {
if transaction.install_view().is_some() {
println!();
}
println!("The following packages will be UPGRADED:");
let output = upgrade
.iter()
.map(|p| {
format!(
"{}{}{}",
p.ident(),
"-".dark_grey(),
// Force reinstalls target the current
// version, which has no "upgradable" ref.
p.upgradable_version()
.unwrap_or_else(|| p.version())
.dark_grey(),
)
})
.collect::<Vec<_>>()
.join(" ");
println!(" {}", output);
}
if let Some(replace) = transaction.replace_view() {
if transaction.install_view().is_some()
|| transaction.upgrade_view().is_some()
{
println!();
}
println!("The following packages will be REPLACED:");
let output = replace
.iter()
.map(|p| {
format!(
"{}{}/{}",
p.installed_bucket().unwrap().dark_grey().crossed_out(),
p.bucket(),
p.name(),
)
})
.collect::<Vec<_>>()
.join(" ");
println!(" {}", output);
}
if let Some(download_size) = transaction.download_size() {
let out = util::humansize(download_size.total, true);
if download_size.total > 0 {
if download_size.estimated {
println!(
"\nTotal download size: {} {}",
out,
"(estimated)".dark_grey()
);
} else {
println!("\nTotal download size: {}", out);
}
} else {
println!("\n{}", flavor::progress("cached"));
}
}
let mut stdout = std::io::stdout();
let _ = stdout.execute(cursor::Show);
let answer = cui::prompt_yes_no();
let _ = tx.send(Event::PromptTransactionNeedConfirmResult(answer));
let _ = stdout.execute(cursor::Hide);
}
Event::PackageInstalledNotes {
ident,
notes,
suggest,
} => {
if !notes.is_empty() {
println!("\nNotes for {}:", ident);
for note in ¬es {
println!(" {}", note);
}
}
if !suggest.is_empty() {
println!("Suggestions for {}:", ident);
for entry in &suggest {
println!(" {}", entry);
}
}
}
Event::PackageSyncDone => break,
_ => {}
}
}
});
let queries = args.package.iter().map(|s| s.as_str()).collect::<Vec<_>>();
operation::package_sync(session, queries, options)?;
handle.join().unwrap();
let mut stdout = std::io::stdout();
let _ = stdout.execute(cursor::Show);
Ok(())
}