build.rs/src/task.rs

83 lines
2.4 KiB
Rust

use std::path::{PathBuf, Path};
use std::ffi::OsString;
use std::collections::HashMap;
//use crate::log;
use crate::dvcs;
use crate::db;
use crate::db::{Entry,is_executable};
#[derive(Debug)]
pub struct Task {
pub name: OsString,
pub bin: PathBuf,
pub source: Option<String>,
pub dvcs: dvcs::Backend,
pub config: HashMap<String, String>,
pub branch: Option<String>,
pub host: Option<String>,
pub cloned: bool,
}
impl Task {
pub fn from_entry(entry: &Entry) -> Task {
let source = entry.read_setting("source");
let cloned = source.clone().map_or(false, |_| {
let mut path = entry.base_dir.clone();
path.push(format!(".{}", entry.name.to_str().expect("WTF")));
path.is_dir()
});
Task {
name: entry.name.clone(),
bin: entry.path.clone(),
source,
dvcs: dvcs::from_setting(entry.read_setting("dvcs")),
config: HashMap::new(),
branch: entry.read_setting("branch"),
host: entry.read_setting("host"),
cloned,
}
}
}
// Takes an already instanced database (ie Vec<Entry>)
// to turn into a dictionary of Tasks
pub fn from_entries(db: Vec<Entry>) -> HashMap<String, Task> {
let mut res: HashMap<String, Task> = HashMap::new();
for entry in db {
let task = Task::from_entry(&entry);
res.insert(
task.name.clone().into_string().expect("Failed to convert"),
task
);
}
return res;
}
/// Returns a hashmap of tasks, or std::io::Error
/// Reads all entries in a directory
pub fn from_dir(base_dir: &Path) -> Result<HashMap<String, Task>, std::io::Error> {
Ok(from_entries(
db::from(base_dir, is_executable)?
))
}
/// Returns a hashmap of tasks, or std::io::Error
/// Reads entries in a given list from a directory, fails if a requested entry doesn't exist
/// (does not load the whole folder)
pub fn from_dir_and_list(basedir: &Path, list: Vec<String>) -> Result<HashMap<String, Task>, db::Error> {
let mut entries: HashMap<String, Task> = HashMap::new();
for item in list {
if let Some(entry) = db::entry(&basedir, is_executable, &item) {
entries.insert(item.clone(), Task::from_entry(&entry));
} else {
return Err(db::Error::EntryNotFound(item.clone()))
}
}
Ok(
entries
)
}