2024-08-11 03:52:49 +01:00
|
|
|
use crate::running_command::Command;
|
2024-08-13 22:55:59 +01:00
|
|
|
use ego_tree::{NodeMut, Tree};
|
2024-08-11 03:52:49 +01:00
|
|
|
use serde::Deserialize;
|
2024-08-13 22:55:59 +01:00
|
|
|
use std::path::{Path, PathBuf};
|
2024-08-09 10:22:19 +01:00
|
|
|
|
2024-08-11 03:52:49 +01:00
|
|
|
#[derive(Deserialize)]
|
2024-08-15 07:09:31 +01:00
|
|
|
struct TabList {
|
|
|
|
directories: Vec<PathBuf>,
|
2024-08-13 22:55:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
2024-08-15 07:09:31 +01:00
|
|
|
struct TabEntry {
|
|
|
|
name: String,
|
|
|
|
data: Vec<Entry>,
|
2024-08-13 22:55:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
2024-08-15 07:09:31 +01:00
|
|
|
struct Entry {
|
2024-08-13 22:55:59 +01:00
|
|
|
name: String,
|
2024-08-11 04:05:19 +01:00
|
|
|
#[allow(dead_code)]
|
2024-08-11 03:52:49 +01:00
|
|
|
#[serde(default)]
|
|
|
|
description: String,
|
|
|
|
#[serde(default)]
|
|
|
|
preconditions: Option<Vec<Precondition>>,
|
2024-08-15 07:09:31 +01:00
|
|
|
#[serde(default)]
|
|
|
|
entries: Option<Vec<Entry>>,
|
|
|
|
#[serde(default)]
|
|
|
|
command: Option<String>,
|
|
|
|
#[serde(default)]
|
|
|
|
script: Option<PathBuf>,
|
2024-08-11 03:52:49 +01:00
|
|
|
}
|
2024-08-09 10:22:19 +01:00
|
|
|
|
2024-08-15 07:09:31 +01:00
|
|
|
impl Entry {
|
2024-08-11 03:52:49 +01:00
|
|
|
fn is_supported(&self) -> bool {
|
|
|
|
self.preconditions.as_deref().map_or(true, |preconditions| {
|
|
|
|
preconditions.iter().all(
|
|
|
|
|Precondition {
|
|
|
|
matches,
|
|
|
|
data,
|
|
|
|
values,
|
|
|
|
}| {
|
|
|
|
match data {
|
|
|
|
SystemDataType::Environment(var_name) => std::env::var(var_name)
|
|
|
|
.map_or(false, |var| values.contains(&var) == *matches),
|
|
|
|
SystemDataType::File(path) => {
|
|
|
|
std::fs::read_to_string(path).map_or(false, |data| {
|
|
|
|
values
|
|
|
|
.iter()
|
|
|
|
.any(|matching_value| data.contains(matching_value))
|
|
|
|
== *matches
|
|
|
|
})
|
|
|
|
}
|
2024-08-16 16:48:09 +01:00
|
|
|
SystemDataType::CommandExists => values
|
|
|
|
.iter()
|
|
|
|
.all(|command| which::which(command).is_ok() == *matches),
|
2024-08-11 03:52:49 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Precondition {
|
|
|
|
// If true, the data must be contained within the list of values.
|
|
|
|
// Otherwise, the data must not be contained within the list of values
|
|
|
|
matches: bool,
|
|
|
|
data: SystemDataType,
|
|
|
|
values: Vec<String>,
|
|
|
|
}
|
2024-08-09 10:22:19 +01:00
|
|
|
|
2024-08-11 03:52:49 +01:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
enum SystemDataType {
|
|
|
|
#[serde(rename = "environment")]
|
|
|
|
Environment(String),
|
|
|
|
#[serde(rename = "file")]
|
|
|
|
File(PathBuf),
|
2024-08-16 16:48:09 +01:00
|
|
|
#[serde(rename = "command_exists")]
|
|
|
|
CommandExists,
|
2024-08-11 03:52:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Hash, Eq, PartialEq)]
|
2024-08-09 10:22:19 +01:00
|
|
|
pub struct Tab {
|
2024-08-11 03:52:49 +01:00
|
|
|
pub name: String,
|
2024-08-09 10:22:19 +01:00
|
|
|
pub tree: Tree<ListNode>,
|
|
|
|
}
|
|
|
|
|
2024-08-11 03:52:49 +01:00
|
|
|
#[derive(Clone, Hash, Eq, PartialEq)]
|
2024-08-09 10:22:19 +01:00
|
|
|
pub struct ListNode {
|
2024-08-11 03:52:49 +01:00
|
|
|
pub name: String,
|
2024-08-09 10:22:19 +01:00
|
|
|
pub command: Command,
|
|
|
|
}
|
|
|
|
|
2024-08-11 03:52:49 +01:00
|
|
|
pub fn get_tabs(command_dir: &Path, validate: bool) -> Vec<Tab> {
|
2024-08-15 07:09:31 +01:00
|
|
|
let tab_files = TabList::get_tabs(command_dir);
|
2024-08-13 22:55:59 +01:00
|
|
|
let tabs = tab_files.into_iter().map(|path| {
|
2024-08-15 07:09:31 +01:00
|
|
|
let directory = path.parent().unwrap().to_owned();
|
|
|
|
let data = std::fs::read_to_string(path).expect("Failed to read tab data");
|
|
|
|
let mut tab_data: TabEntry = toml::from_str(&data).expect("Failed to parse tab data");
|
2024-08-11 03:52:49 +01:00
|
|
|
|
2024-08-13 22:55:59 +01:00
|
|
|
if validate {
|
|
|
|
filter_entries(&mut tab_data.data);
|
2024-08-11 03:52:49 +01:00
|
|
|
}
|
2024-08-13 22:55:59 +01:00
|
|
|
(tab_data, directory)
|
|
|
|
});
|
|
|
|
|
|
|
|
let tabs: Vec<Tab> = tabs
|
|
|
|
.map(|(TabEntry { name, data }, directory)| {
|
|
|
|
let mut tree = Tree::new(ListNode {
|
|
|
|
name: "root".to_string(),
|
|
|
|
command: Command::None,
|
|
|
|
});
|
|
|
|
let mut root = tree.root_mut();
|
|
|
|
create_directory(data, &mut root, &directory);
|
|
|
|
Tab { name, tree }
|
|
|
|
})
|
|
|
|
.collect();
|
2024-08-11 03:52:49 +01:00
|
|
|
|
|
|
|
if tabs.is_empty() {
|
2024-08-13 22:55:59 +01:00
|
|
|
panic!("No tabs found");
|
2024-08-11 03:52:49 +01:00
|
|
|
}
|
|
|
|
tabs
|
|
|
|
}
|
|
|
|
|
2024-08-13 22:55:59 +01:00
|
|
|
fn filter_entries(entries: &mut Vec<Entry>) {
|
2024-08-15 07:09:31 +01:00
|
|
|
entries.retain_mut(|entry| {
|
|
|
|
if !entry.is_supported() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if let Some(entries) = &mut entry.entries {
|
|
|
|
filter_entries(entries);
|
|
|
|
!entries.is_empty()
|
|
|
|
} else {
|
|
|
|
true
|
2024-08-13 22:55:59 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2024-08-11 03:52:49 +01:00
|
|
|
|
2024-08-13 22:55:59 +01:00
|
|
|
fn create_directory(data: Vec<Entry>, node: &mut NodeMut<ListNode>, command_dir: &Path) {
|
|
|
|
for entry in data {
|
2024-08-15 07:09:31 +01:00
|
|
|
if [
|
|
|
|
entry.entries.is_some(),
|
|
|
|
entry.command.is_some(),
|
|
|
|
entry.script.is_some(),
|
|
|
|
]
|
|
|
|
.iter()
|
|
|
|
.filter(|&&x| x)
|
|
|
|
.count()
|
|
|
|
> 1
|
|
|
|
{
|
|
|
|
panic!("Entry must have only one data type");
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(entries) = entry.entries {
|
|
|
|
let mut node = node.append(ListNode {
|
|
|
|
name: entry.name,
|
|
|
|
command: Command::None,
|
|
|
|
});
|
|
|
|
create_directory(entries, &mut node, command_dir);
|
|
|
|
} else if let Some(command) = entry.command {
|
|
|
|
node.append(ListNode {
|
|
|
|
name: entry.name,
|
|
|
|
command: Command::Raw(command),
|
|
|
|
});
|
|
|
|
} else if let Some(script) = entry.script {
|
|
|
|
let dir = command_dir.join(script);
|
|
|
|
if !dir.exists() {
|
|
|
|
panic!("Script {} does not exist", dir.display());
|
2024-08-13 22:55:59 +01:00
|
|
|
}
|
2024-08-15 07:09:31 +01:00
|
|
|
node.append(ListNode {
|
|
|
|
name: entry.name,
|
|
|
|
command: Command::LocalFile(dir),
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
panic!("Entry must have data");
|
2024-08-13 22:55:59 +01:00
|
|
|
}
|
|
|
|
}
|
2024-08-11 03:52:49 +01:00
|
|
|
}
|
2024-08-15 07:09:31 +01:00
|
|
|
impl TabList {
|
|
|
|
fn get_tabs(command_dir: &Path) -> Vec<PathBuf> {
|
|
|
|
let tab_files = std::fs::read_to_string(command_dir.join("tabs.toml"))
|
|
|
|
.expect("Failed to read tabs.toml");
|
|
|
|
let data: Self = toml::from_str(&tab_files).expect("Failed to parse tabs.toml");
|
|
|
|
|
|
|
|
data.directories
|
|
|
|
.into_iter()
|
|
|
|
.map(|path| command_dir.join(path).join("tab_data.toml"))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|