/
aur.rs
56 lines (52 loc) · 2.15 KB
/
aur.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
use directories::ProjectDirs;
use regex::Regex;
use std::env;
use std::fs;
use std::io;
use std::path::Path;
use std::process::Command;
use std::process::Output;
use util;
fn assert_command_success(command: &Output) {
assert!(command.status.success(),
"Command failed with exit code {:?}\nStderr: {}\nStdout: {}",
command.status.code(),
String::from_utf8_lossy(&command.stderr),
String::from_utf8_lossy(&command.stdout),
);
}
pub fn fresh_download(name: &str, dirs: &ProjectDirs) {
let valid_name_regexp = Regex::new(r"[a-zA-Z][a-zA-Z._-]*").unwrap();
assert!(valid_name_regexp.is_match(name), "unexpected package name {}", name);
fs::remove_dir_all(dirs.cache_dir().join(name)).expect(&format!("Failed to clean cache dir for {}", name));
fs::create_dir_all(dirs.cache_dir().join(name)).expect(&format!("Failed to create cache dir for {}", name));
env::set_current_dir(dirs.cache_dir().join(name)).expect(&format!("Faild to cd into build dir for {}", name));
let dir = "aur.tmp";
let git_http_ref = format!("https://aur.archlinux.org/{}.git", name);
let command = Command::new("git").args(&["clone", &git_http_ref, dir])
.output().expect(&format!("Failed to git-clone repository {}", name));
assert_command_success(&command);
env::set_current_dir(&dir).unwrap();
assert!(Path::new("PKGBUILD").exists(), "PKGBUILD not found for package {}. \
Does this package really exist in AUR?", name);
loop {
eprint!("Downloaded {}. V=view PKGBUILD, E=edit PKGBUILD, \
I=run shell to inspect, O=ok, use the file: ", name);
let mut string = String::new();
io::stdin().read_line(&mut string).expect("RUA requires console to ask confirmation.");
let string = string.trim().to_lowercase();
if string == "v" {
util::run_env_command("PAGER", "less", &["PKGBUILD"]);
} else if string == "e" {
util::run_env_command("EDITOR", "nano", &["PKGBUILD"]);
} else if string == "i" {
eprintln!("Exit the shell with `logout` or Ctrl-D...");
util::run_env_command("SHELL", "bash", &[]);
} else if string == "o" {
break;
}
}
env::set_current_dir("..").unwrap();
fs::rename(dir, "build")
.expect(&format!("Failed to move temporary directory '{}' to 'build'", dir));
}