Base implementation of verbose logging

main
Michal 2 years ago
parent 703eb74e25
commit efa953c43a
No known key found for this signature in database
GPG Key ID: A6A1A4DCB22279B9

@ -1,5 +1,6 @@
use colored::*;
use std::process::exit;
use std::time::UNIX_EPOCH;
use crate::internal::AppExitCode;
@ -13,6 +14,13 @@ macro_rules! info {
}
}
#[macro_export]
macro_rules! log {
($($arg:tt)+) => {
$crate::internal::strings::log_fn(format!($($arg)+))
}
}
#[macro_export]
macro_rules! crash {
($exit_code:expr, $($arg:tt)+) => {
@ -20,11 +28,17 @@ macro_rules! crash {
}
}
pub fn info_fn<S: ToString>(msg: S) {
let msg = msg.to_string();
println!("{} {}", LOGO_SYMBOL.black(), msg.bold())
}
pub fn log_fn<S: ToString>(msg: S) {
let msg = msg.to_string();
eprintln!("{} {}", std::time::SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(), msg)
}
pub fn crash_fn<S: ToString>(msg: S, exit_code: AppExitCode) {
let msg = msg.to_string();
println!("{} {}", ERR_SYMBOL.red(), msg.bold());

@ -1,23 +1,31 @@
use crate::internal::structs::{ErroredPackage, Repo};
use crate::internal::AppExitCode;
use crate::{crash, info, repository, workspace};
use crate::{crash, info, log, repository, workspace};
pub fn build(packages: Vec<String>, exclude: Vec<String>, no_regen: bool) {
// Read config struct from mlc.toml
let config = workspace::read_cfg();
log!("Config: {:?}", config);
let all = packages.is_empty();
log!("All: {:?}", all);
log!("Signing: {:?}", config.sign);
// Get list of repos and subtract exclude
let mut repos: Vec<Repo> = config.repo;
log!("{} Repos: {:?}", repos.len(), repos );
if !exclude.is_empty() {
log!("Exclude not empty: {:?}", exclude);
for ex in exclude {
repos.retain(|x| *x.name != ex);
}
}
log!("Exclusions parsed. Now {} Repos: {:?}", repos.len(), repos);
// If packages is not empty and all isn't specified, build specified packages
let mut errored: Vec<ErroredPackage> = vec![];
if !packages.is_empty() && !all {
log!("Packages not empty: {:?}", packages);
for pkg in &packages {
// If repo is not in config, crash
if !repos.iter().map(|x| x.name.clone()).any(|x| x == *pkg) {
@ -28,7 +36,9 @@ pub fn build(packages: Vec<String>, exclude: Vec<String>, no_regen: bool) {
);
} else {
// Otherwise, build
log!("Building {}", pkg);
let code = repository::build(pkg, config.sign);
log!("Package {} finished with exit code: {:?}", pkg, code);
if code != 0 {
let error = ErroredPackage {
name: pkg.to_string(),
@ -42,10 +52,16 @@ pub fn build(packages: Vec<String>, exclude: Vec<String>, no_regen: bool) {
// If all is specified, attempt to build a package from all repos
if all {
log!("Proceeding to build all");
// Sort by package priority
log!("Sorting by priority: {:?}", repos);
repos.sort_by(|a, b| b.priority.cmp(&a.priority));
log!("Sorted: {:?}", repos);
for pkg in repos {
log!("Building {}", pkg.name);
let code = repository::build(&pkg.name, config.sign);
log!("Package {} finished with exit code: {:?}", pkg.name, code);
if code != 0 {
let error = ErroredPackage {
name: pkg.name,
@ -58,11 +74,13 @@ pub fn build(packages: Vec<String>, exclude: Vec<String>, no_regen: bool) {
// If all is not specified, but packages is empty, crash
if !all && packages.is_empty() {
log!("Packages empty. Crashing");
crash!(AppExitCode::NoPkgs, "No packages specified");
}
// If no_regen is passed, do not generate a repository
if !no_regen {
log!("Generating repository");
repository::generate();
}
@ -74,6 +92,7 @@ pub fn build(packages: Vec<String>, exclude: Vec<String>, no_regen: bool) {
// If errored is not empty, let the user know which packages failed
if !errored.is_empty() {
log!("Errored packages: \n{:?}", error_strings);
info!(
"The following packages build jobs returned a non-zero exit code: {}",
error_strings.join("\n")

@ -1,15 +1,17 @@
use crate::info;
use crate::{info, log};
pub fn clean() {
info!("Resetting mlc repo, deleting all directories");
// Get a vec of all files/dirs in the current directory
let dir_paths = std::fs::read_dir("./").unwrap();
log!("Paths: {:?}", dir_paths);
let mut dirs = dir_paths
.map(|x| x.unwrap().path().display().to_string())
.collect::<Vec<String>>();
// Remove all files/dirs in the current directory, excluding ./mlc.toml
dirs.retain(|x| *x != "./mlc.toml");
log!("Paths with mlc.toml excluded: {:?}", dirs);
for dir in dirs {
std::fs::remove_dir_all(dir).unwrap();
}

@ -1,11 +1,13 @@
use std::process::Command;
use crate::{info, workspace};
use crate::{info, workspace, log};
pub fn clone() {
// Read config struct from mlc.toml
let config = workspace::read_cfg();
log!("Config: {:?}", config);
let repos = &config.repo;
log!("Repos: {:?}", repos);
// Get a vector of all files/dirs in the current directory, excluding config file
let dir_paths = std::fs::read_dir("./").unwrap();
@ -13,6 +15,7 @@ pub fn clone() {
.map(|x| x.unwrap().path().display().to_string())
.collect::<Vec<String>>();
dirs.retain(|x| *x != "./mlc.toml");
log!("Paths with mlc.toml excluded: {:?}", dirs);
// Creates a vector of the difference between cloned repos and repos defined in config
let mut repo_diff = vec![];
@ -26,8 +29,10 @@ pub fn clone() {
// Diff logic
if repo_diff.is_empty() {
// No diff, do nothing
log!("No diff");
info!("All repos are already cloned");
} else {
log!("Diff: {:?}", repo_diff);
// This is just for pretty display purposes
let display = repo_diff
.iter()
@ -38,6 +43,7 @@ pub fn clone() {
// Clone all diff repos
for r in repo_diff {
log!("Cloning {}", r.name);
info!("Cloning ({} mode): {}", config.mode, r.name);
Command::new("git")
.args(&["clone", &r.url, &r.name])

@ -2,16 +2,18 @@ use std::env;
use std::path::Path;
use std::process::Command;
use crate::create_config;
use crate::{create_config, log};
pub fn config() {
// Generate new config file if not already present
if !Path::exists("mlc.toml".as_ref()) {
log!("Creating mlc.toml");
create_config();
}
// Open config file in user's editor of choice
let editor = env::var("EDITOR").unwrap_or_else(|_| "nano".to_string());
log!("Opening mlc.toml in {}", editor);
Command::new(editor)
.arg("mlc.toml")
.spawn()

@ -2,14 +2,17 @@ use std::env;
use std::process::Command;
use crate::info;
use crate::{crash, internal::AppExitCode};
use crate::{crash, internal::AppExitCode, log};
fn do_the_pulling(repos: Vec<String>) {
for repo in repos {
// Set root dir to return after each git pull
let root_dir = env::current_dir().unwrap();
log!("Root dir: {:?}", root_dir);
info!("Entering working directory: {}", &repo);
env::set_current_dir(repo).unwrap();
log!("Current dir: {:?}", env::current_dir().unwrap());
log!("Pulling");
Command::new("git")
.arg("pull")
.spawn()
@ -19,12 +22,14 @@ fn do_the_pulling(repos: Vec<String>) {
// Return to root dir
env::set_current_dir(root_dir).unwrap();
log!("Returned to root dir: {:?}", env::current_dir().unwrap());
}
}
pub fn pull(packages: Vec<String>, exclude: Vec<String>) {
// If no packages are specified, imply all
let all = packages.is_empty();
log!("All: {}", all);
// Read repos from config file
let repos = crate::workspace::read_cfg()
@ -32,16 +37,20 @@ pub fn pull(packages: Vec<String>, exclude: Vec<String>) {
.iter()
.map(|x| x.name.clone())
.collect::<Vec<String>>();
log!("Repos: {:?}", repos);
// Set repos_applicable for next function
let mut repos_applicable = if all { repos } else { packages };
log!("Repos applicable: {:?}", repos_applicable);
// Subtract exclude from repos_applicable
if !exclude.is_empty() {
for ex in exclude {
repos_applicable.retain(|x| *x != ex);
for ex in &exclude {
repos_applicable.retain(|x| *x != *ex);
}
}
log!("Exclude: {:?}", exclude);
log!("Repos applicable excluded: {:?}", repos_applicable);
// If all is not specified and packages is empty, crash
if repos_applicable.is_empty() {
@ -49,5 +58,6 @@ pub fn pull(packages: Vec<String>, exclude: Vec<String>) {
}
// Pull!
log!("Pulling {:?}", repos_applicable);
do_the_pulling(repos_applicable);
}

@ -3,7 +3,7 @@ use std::fs::File;
use std::io::Write;
use std::path::Path;
use crate::crash;
use crate::{crash, log};
use crate::internal::AppExitCode;
const DEFAULT_CONFIG: &str = r#"
@ -43,10 +43,12 @@ pub fn create_config() {
"Directory is not empty, please only create a repository in an empty directory"
);
}
log!("Creating config file");
// If config file exists, create it
if !Path::exists("mlc.toml".as_ref()) {
let mut file = File::create("mlc.toml").unwrap();
file.write_all(DEFAULT_CONFIG.as_ref()).unwrap();
}
log!("Config file created");
}

@ -2,20 +2,23 @@ use std::path::Path;
use std::process::Command;
use std::{env, fs};
use crate::crash;
use crate::{crash, log};
use crate::internal::AppExitCode;
pub fn build(pkg: &str, sign: bool) -> i32 {
log!("Building {}", pkg);
log!("Signing: {}", sign);
// Set root dir to return after build
let dir = env::current_dir().unwrap();
log!("Root dir: {:?}", dir);
// Create out dir if not already present
if !Path::exists("out".as_ref()) {
log!("Creating out dir");
fs::create_dir_all("out").unwrap();
}
// If directory is not found, crash
// If package directory is not found, crash
if !Path::exists(pkg.as_ref()) {
crash!(
AppExitCode::RepoNotFound,
@ -26,6 +29,7 @@ pub fn build(pkg: &str, sign: bool) -> i32 {
// Enter build directory
env::set_current_dir(pkg).unwrap();
log!("Current dir: {:?}", env::current_dir().unwrap());
// Build each package
let a = Command::new("makepkg")
@ -39,6 +43,7 @@ pub fn build(pkg: &str, sign: bool) -> i32 {
.unwrap()
.wait()
.unwrap();
log!("{} Build job returned: {:?}", pkg, a);
// Copy built package to out dir
Command::new("bash")
@ -47,9 +52,11 @@ pub fn build(pkg: &str, sign: bool) -> i32 {
.unwrap()
.wait()
.unwrap();
log!("Copied built package to out dir");
// Return to root dir
env::set_current_dir(dir).unwrap();
log!("Returned to root dir: {:?}", env::current_dir().unwrap());
// Return exit code
a.code().unwrap()

@ -2,22 +2,26 @@ use std::path::Path;
use std::process::Command;
use std::{env, fs};
use crate::workspace::read_cfg;
use crate::{workspace::read_cfg, log};
pub fn generate() {
// Read config struct from mlc.toml
let config = read_cfg();
log!("Config: {:?}", config);
// Get repository name from config
let name = config.name.unwrap();
log!("Name: {}", name);
// If repository exists, delete it
if Path::exists(name.as_ref()) {
log!("Deleting {}", name);
fs::remove_dir_all(&name).unwrap();
}
// Create or recreate repository directory
fs::create_dir_all(&name).unwrap();
log!("Created {}", name);
// Copy out packages to repository directory
Command::new("bash")
@ -26,9 +30,11 @@ pub fn generate() {
.unwrap()
.wait()
.unwrap();
log!("Copied out packages to {}", name);
// Enter repository directory
env::set_current_dir(&name).unwrap();
log!("Current dir: {:?}", env::current_dir().unwrap());
let db = format!("{}.db", &name);
let files = format!("{}.files", &name);
@ -43,6 +49,7 @@ pub fn generate() {
.unwrap()
.wait()
.unwrap();
log!("Created {} and {}", db, files);
// Replace repo.{db,files}.tar.gz with just repo.{db,files}
Command::new("bash")
@ -54,4 +61,5 @@ pub fn generate() {
.unwrap()
.wait()
.unwrap();
log!("Renamed {}.tar.gz to {} and {}.tar.gz to {}", db, db, files, files);
}

Loading…
Cancel
Save