use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use crate::plugin::{PackTypeRegistry, Registry};
use crate::scheduler::Scheduler;
use crate::vars::VarEnv;
pub type MetaVisitedSet = Arc<Mutex<HashSet<PathBuf>>>;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Platform {
Linux,
MacOs,
Windows,
Other(&'static str),
}
impl Platform {
#[must_use]
pub fn current() -> Self {
#[cfg(target_os = "linux")]
{
Self::Linux
}
#[cfg(target_os = "macos")]
{
Self::MacOs
}
#[cfg(target_os = "windows")]
{
Self::Windows
}
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
{
Self::Other(std::env::consts::OS)
}
}
#[must_use]
pub fn matches_os_token(&self, token: &str) -> bool {
matches!(
(self, token),
(Self::Windows, "windows")
| (Self::Linux, "linux")
| (Self::MacOs, "macos")
| (Self::Linux | Self::MacOs, "unix")
)
}
}
#[non_exhaustive]
#[derive(Debug)]
pub struct ExecCtx<'a> {
pub vars: &'a VarEnv,
pub pack_root: &'a Path,
pub workspace: &'a Path,
pub platform: Platform,
pub registry: Option<&'a Arc<Registry>>,
pub pack_type_registry: Option<&'a Arc<PackTypeRegistry>>,
pub visited_meta: Option<&'a MetaVisitedSet>,
pub scheduler: Option<&'a Arc<Scheduler>>,
}
impl<'a> ExecCtx<'a> {
#[must_use]
pub fn new(vars: &'a VarEnv, pack_root: &'a Path, workspace: &'a Path) -> Self {
Self {
vars,
pack_root,
workspace,
platform: Platform::current(),
registry: None,
pack_type_registry: None,
visited_meta: None,
scheduler: None,
}
}
#[must_use]
pub fn with_platform(mut self, p: Platform) -> Self {
self.platform = p;
self
}
#[must_use]
pub fn with_registry(mut self, reg: &'a Arc<Registry>) -> Self {
self.registry = Some(reg);
self
}
#[must_use]
pub fn with_pack_type_registry(mut self, reg: &'a Arc<PackTypeRegistry>) -> Self {
self.pack_type_registry = Some(reg);
self
}
#[must_use]
pub fn with_visited_meta(mut self, visited: &'a MetaVisitedSet) -> Self {
self.visited_meta = Some(visited);
self
}
#[must_use]
pub fn with_scheduler(mut self, scheduler: &'a Arc<Scheduler>) -> Self {
self.scheduler = Some(scheduler);
self
}
}