Skip to content

Commit

Permalink
Add --auto-build/-b argument to automate build promps (issue vn971#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
lilly-lizard committed May 16, 2022
1 parent d397ee2 commit d5dd1f5
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 37 deletions.
12 changes: 9 additions & 3 deletions src/action_builddir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ use std::path::Path;
use std::path::PathBuf;

/// Build and install a package, see `crate::cli_args::Action::Builddir` for details
pub fn action_builddir(dir: &Option<PathBuf>, rua_paths: &RuaPaths, offline: bool, force: bool) {
pub fn action_builddir(
dir: &Option<PathBuf>,
rua_paths: &RuaPaths,
offline: bool,
force: bool,
autobuild: bool,
) {
// Set `.` as default dir in case no build directory is provided.
let dir = match dir {
Some(path) => path,
Expand Down Expand Up @@ -39,9 +45,9 @@ pub fn action_builddir(dir: &Option<PathBuf>, rua_paths: &RuaPaths, offline: boo

for (_, file) in &packages {
let file_str = file.to_str().expect("Builddir target has unvalid UTF-8");
tar_check::tar_check(file, file_str).ok();
tar_check::tar_check(file, file_str, false).ok();
}
eprintln!("Package built and checked.");

pacman::ensure_aur_packages_installed(packages, false);
pacman::ensure_aur_packages_installed(packages, false, autobuild);
}
43 changes: 33 additions & 10 deletions src/action_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::rua_paths::RuaPaths;
use crate::tar_check;
use crate::terminal_util;
use crate::wrapped;
use colored::Colorize;
use fs_extra::dir::CopyOptions;
use indexmap::IndexMap;
use indexmap::IndexSet;
Expand All @@ -17,7 +18,13 @@ use std::fs;
use std::fs::ReadDir;
use std::path::PathBuf;

pub fn install(targets: &[String], rua_paths: &RuaPaths, is_offline: bool, asdeps: bool) {
pub fn install(
targets: &[String],
rua_paths: &RuaPaths,
is_offline: bool,
asdeps: bool,
autobuild: bool,
) {
let alpm = new_alpm_wrapper();
let (split_to_raur, pacman_deps, split_to_depth) =
aur_rpc_utils::recursive_info(targets, &*alpm).unwrap_or_else(|err| {
Expand All @@ -39,21 +46,22 @@ pub fn install(targets: &[String], rua_paths: &RuaPaths, is_offline: bool, asdep
std::process::exit(1)
}

show_install_summary(&pacman_deps, &split_to_depth);
show_install_summary(&pacman_deps, &split_to_depth, autobuild);
for pkgbase in split_to_pkgbase.values().collect::<HashSet<_>>() {
let dir = rua_paths.review_dir(pkgbase);
fs::create_dir_all(&dir).unwrap_or_else(|err| {
panic!("Failed to create repository dir for {}, {}", pkgbase, err)
});
reviewing::review_repo(&dir, pkgbase, rua_paths);
reviewing::review_repo(&dir, pkgbase, rua_paths, autobuild);
}
pacman::ensure_pacman_packages_installed(pacman_deps);
pacman::ensure_pacman_packages_installed(pacman_deps, autobuild);
install_all(
rua_paths,
split_to_depth,
split_to_pkgbase,
is_offline,
asdeps,
autobuild,
);
for target in targets {
// Delete temp directories after successful build+install
Expand All @@ -68,7 +76,11 @@ pub fn install(targets: &[String], rua_paths: &RuaPaths, is_offline: bool, asdep
}
}

fn show_install_summary(pacman_deps: &IndexSet<String>, aur_packages: &IndexMap<String, i32>) {
fn show_install_summary(
pacman_deps: &IndexSet<String>,
aur_packages: &IndexMap<String, i32>,
autobuild: bool,
) {
if pacman_deps.len() + aur_packages.len() == 1 {
return;
}
Expand All @@ -91,7 +103,12 @@ fn show_install_summary(pacman_deps: &IndexSet<String>, aur_packages: &IndexMap<
);
loop {
eprint!("Proceed? [O]=ok, Ctrl-C=abort. ");
let string = terminal_util::read_line_lowercase();
let string = if autobuild {
eprintln!("\n{} {}", "Autobuild:".italic(), "[O]".italic());
"o".to_string()
} else {
terminal_util::read_line_lowercase()
};
if &string == "o" {
break;
}
Expand All @@ -104,6 +121,7 @@ fn install_all(
split_to_pkgbase: IndexMap<String, String>,
offline: bool,
asdeps: bool,
autobuild: bool,
) {
let archive_whitelist = split_to_depth
.iter()
Expand Down Expand Up @@ -163,7 +181,7 @@ fn install_all(
);
}
for (pkgbase, _depth, _split) in &packages {
check_tars_and_move(pkgbase, rua_paths, &archive_whitelist);
check_tars_and_move(pkgbase, rua_paths, &archive_whitelist, autobuild);
}
// This relation between split_name and the archive file is not actually correct here.
// Instead, all archive files of some group will be bound to one split name only here.
Expand All @@ -186,11 +204,16 @@ fn install_all(
));
}
}
pacman::ensure_aur_packages_installed(files_to_install, asdeps || depth > 0);
pacman::ensure_aur_packages_installed(files_to_install, asdeps || depth > 0, autobuild);
}
}

pub fn check_tars_and_move(name: &str, rua_paths: &RuaPaths, archive_whitelist: &IndexSet<&str>) {
pub fn check_tars_and_move(
name: &str,
rua_paths: &RuaPaths,
archive_whitelist: &IndexSet<&str>,
autobuild: bool,
) {
debug!("checking tars and moving for package {}", name);
let build_dir = rua_paths.build_dir(name);
let dir_items: ReadDir = build_dir.read_dir().unwrap_or_else(|err| {
Expand Down Expand Up @@ -219,7 +242,7 @@ pub fn check_tars_and_move(name: &str, rua_paths: &RuaPaths, archive_whitelist:
.retain(|(_, name)| archive_whitelist.contains(&name[..name.len() - common_suffix_length]));
trace!("Files filtered for tar checking: {:?}", &dir_items);
for (file, file_name) in dir_items.iter() {
tar_check::tar_check_unwrap(&file.path(), file_name);
tar_check::tar_check_unwrap(&file.path(), file_name, autobuild);
}
debug!("all package (tar) files checked, moving them");
let checked_tars_dir = rua_paths.checked_tars_dir(name);
Expand Down
11 changes: 8 additions & 3 deletions src/action_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn upgrade_printonly(devel: bool, ignored: &HashSet<&str>) {
}
}

pub fn upgrade_real(devel: bool, rua_paths: &RuaPaths, ignored: &HashSet<&str>) {
pub fn upgrade_real(devel: bool, rua_paths: &RuaPaths, ignored: &HashSet<&str>, autobuild: bool) {
let alpm = new_alpm_wrapper();
let (outdated, nonexistent) =
calculate_upgrade(&*alpm, devel, ignored).expect("calculating upgrade failed");
Expand All @@ -67,10 +67,15 @@ pub fn upgrade_real(devel: bool, rua_paths: &RuaPaths, ignored: &HashSet<&str>)
eprintln!();
loop {
eprint!("Do you wish to upgrade them? [O]=ok, [X]=exit. ");
let user_input = terminal_util::read_line_lowercase();
let user_input = if autobuild {
eprintln!("\n{} {}", "Autobuild:".italic(), "[O]".italic());
"o".to_string()
} else {
terminal_util::read_line_lowercase()
};
if &user_input == "o" {
let outdated: Vec<String> = outdated.iter().map(|o| o.0.to_string()).collect();
action_install::install(&outdated, rua_paths, false, true);
action_install::install(&outdated, rua_paths, false, true, autobuild);
break;
} else if &user_input == "x" {
break;
Expand Down
14 changes: 14 additions & 0 deletions src/cli_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Sources are downloaded using .SRCINFO only"
help = "Target directory. Defaults to current directory '.' if not specified."
)]
target: Option<PathBuf>,
#[structopt(short = "b", long = "auto-build", help = "Automate build promps")]
autobuild: bool,
},
#[structopt(about = "Show package information")]
Info {
Expand All @@ -69,6 +71,12 @@ Sources are downloaded using .SRCINFO only"
offline: bool,
#[structopt(help = "Target package", multiple = true, required = true)]
target: Vec<String>,
#[structopt(
short = "b",
long = "auto-build",
help = "Automate build promps (doesn't apply --noconfirm to pacman commands)"
)]
autobuild: bool,
},
#[structopt(
about = "Search for packages by name or description. If multiple keywords are used, all of them must match."
Expand Down Expand Up @@ -114,6 +122,12 @@ Supports: git, hg, bzr, svn, cvs, darcs. Currently by suffix only."
help = "Don't upgrade the specified package(s). Accepts multiple arguments separated by `,`."
)]
ignored: Option<String>,
#[structopt(
short = "b",
long = "auto-build",
help = "Automate build promps (doesn't apply --noconfirm to pacman commands)"
)]
autobuild: bool,
},
}

Expand Down
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,19 @@ fn main() {
asdeps,
offline,
target,
autobuild,
} => {
let paths = rua_paths::RuaPaths::initialize_paths();
action_install::install(target, &paths, *offline, *asdeps);
action_install::install(target, &paths, *offline, *asdeps, *autobuild);
}
Action::Builddir {
offline,
force,
target,
autobuild,
} => {
let paths = rua_paths::RuaPaths::initialize_paths();
action_builddir::action_builddir(target, &paths, *offline, *force);
action_builddir::action_builddir(target, &paths, *offline, *force, *autobuild);
}
Action::Search { target } => action_search::action_search(target),
Action::Shellcheck { target } => {
Expand All @@ -67,13 +69,15 @@ fn main() {
tar_check::tar_check_unwrap(
target,
target.to_str().expect("target is not valid UTF-8"),
false,
);
eprintln!("Finished checking package: {:?}", target);
}
Action::Upgrade {
devel,
printonly,
ignored,
autobuild,
} => {
let ignored_set = ignored
.iter()
Expand All @@ -83,7 +87,7 @@ fn main() {
action_upgrade::upgrade_printonly(*devel, &ignored_set);
} else {
let paths = rua_paths::RuaPaths::initialize_paths();
action_upgrade::upgrade_real(*devel, &paths, &ignored_set);
action_upgrade::upgrade_real(*devel, &paths, &ignored_set, *autobuild);
}
}
};
Expand Down
38 changes: 31 additions & 7 deletions src/pacman.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::alpm_wrapper::new_alpm_wrapper;
use crate::rua_environment;
use crate::terminal_util;
use colored::Colorize;
use indexmap::IndexSet;
use itertools::Itertools;
use lazy_static::lazy_static;
Expand All @@ -20,7 +21,11 @@ pub fn get_ignored_packages() -> Result<HashSet<String>, String> {
Ok(output.lines().map(ToOwned::to_owned).collect())
}

fn ensure_packages_installed(mut packages: Vec<(String, PathBuf)>, base_args: &[&str]) {
fn ensure_packages_installed(
mut packages: Vec<(String, PathBuf)>,
base_args: &[&str],
autobuild: bool,
) {
let mut attempt = 0;
while !packages.is_empty() {
{
Expand Down Expand Up @@ -55,7 +60,22 @@ fn ensure_packages_installed(mut packages: Vec<(String, PathBuf)>, base_args: &[
eprint!("or install manually and enter M when done. ");
}
attempt += 1;
let string = terminal_util::read_line_lowercase();

let string = if autobuild {
if attempt == 1 {
eprintln!("\n{} {}", "Autobuild:".italic(), "[S]".italic());
"s".to_string()
} else {
eprintln!(
"\n{} {}",
"Autobuild: install attempt failed, skipping installation...".italic(),
"[X]".italic()
);
"x".to_string()
}
} else {
terminal_util::read_line_lowercase()
};
if string == "s" {
let exit_status = Command::new(rua_environment::sudo_command())
.arg("pacman")
Expand All @@ -82,21 +102,25 @@ fn ensure_packages_installed(mut packages: Vec<(String, PathBuf)>, base_args: &[
}
}

pub fn ensure_aur_packages_installed(packages: Vec<(String, PathBuf)>, is_dependency: bool) {
pub fn ensure_aur_packages_installed(
packages: Vec<(String, PathBuf)>,
is_dependency: bool,
autobuild: bool,
) {
if is_dependency {
ensure_packages_installed(packages, &["-U", "--asdeps"]);
ensure_packages_installed(packages, &["-U", "--asdeps"], autobuild);
} else {
ensure_packages_installed(packages, &["-U"]);
ensure_packages_installed(packages, &["-U"], autobuild);
}
}

pub fn ensure_pacman_packages_installed(packages: IndexSet<String>) {
pub fn ensure_pacman_packages_installed(packages: IndexSet<String>, autobuild: bool) {
let mut map: Vec<(String, PathBuf)> = Vec::new();
for package in packages {
let path = Path::new(&package).to_path_buf();
map.push((package, path));
}
ensure_packages_installed(map, &["-S", "--asdeps", "--needed"]);
ensure_packages_installed(map, &["-S", "--asdeps", "--needed"], autobuild);
}

// Architecture as defined in the local pacman configuration
Expand Down
14 changes: 12 additions & 2 deletions src/reviewing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use colored::Colorize;
use log::debug;
use std::path::Path;

pub fn review_repo(dir: &Path, pkgbase: &str, rua_paths: &RuaPaths) {
pub fn review_repo(dir: &Path, pkgbase: &str, rua_paths: &RuaPaths, autobuild: bool) {
let mut dir_contents = dir.read_dir().unwrap_or_else(|err| {
panic!(
"{}:{} Failed to read directory for reviewing, {}",
Expand Down Expand Up @@ -83,8 +83,18 @@ pub fn review_repo(dir: &Path, pkgbase: &str, rua_paths: &RuaPaths) {
"[O]=(cannot use the package until you merge) ".dimmed()
);
}
let user_input = terminal_util::read_line_lowercase();

let user_input = if autobuild {
if is_upstream_merged {
eprintln!("\n{} {}", "Autobuild:".italic(), "[O]".italic().red());
"o".to_string()
} else {
eprintln!("\n{} {}", "Autobuild:".italic(), "[M]".italic().yellow());
"m".to_string()
}
} else {
terminal_util::read_line_lowercase()
};
if &user_input == "t" {
eprintln!("Changes that you make will be merged with upstream updates in future.");
eprintln!("Exit the shell with `logout` or Ctrl-D...");
Expand Down
Loading

0 comments on commit d5dd1f5

Please sign in to comment.