use crate::cli_kind::CliKind;
use semver::Version;
use sha2::Digest;
use std::path::{Path, PathBuf};
#[derive(Debug, Clone)]
pub struct PavexLocator {
pavex_dir: PathBuf,
}
impl PavexLocator {
pub fn new(system_home_dir: &Path) -> Self {
PavexLocator {
pavex_dir: system_home_dir.join(".pavex"),
}
}
pub fn toolchains(&self) -> ToolchainsLocator {
ToolchainsLocator {
toolchain_dir: self.pavex_dir.join("toolchains"),
}
}
pub fn auth(&self) -> AuthLocator {
AuthLocator {
dir: self.pavex_dir.join("auth"),
}
}
pub fn root_dir(&self) -> &Path {
&self.pavex_dir
}
}
pub struct ToolchainsLocator {
toolchain_dir: PathBuf,
}
impl ToolchainsLocator {
pub fn root_dir(&self) -> &Path {
&self.toolchain_dir
}
pub fn git(&self) -> GitToolchainsLocator {
GitToolchainsLocator {
git_toolchain_dir: self.toolchain_dir.join("git"),
}
}
pub fn registry(&self) -> RegistryToolchainsLocator {
RegistryToolchainsLocator {
registry_toolchain_dir: self.toolchain_dir.join("registry"),
}
}
}
pub struct GitToolchainsLocator {
git_toolchain_dir: PathBuf,
}
impl GitToolchainsLocator {
pub fn root_dir(&self) -> &Path {
&self.git_toolchain_dir
}
pub fn toolchain_dir(&self, repository: &str, revision_sha: &str) -> ToolchainLocator {
let repository_hash = sha2::Sha256::digest(repository.as_bytes());
let repository_hash = format!("{repository_hash:x}")
.chars()
.take(7)
.collect::<String>();
let revision_sha = revision_sha.chars().take(7).collect::<String>();
let toolchain_dir = self
.git_toolchain_dir
.join(repository_hash)
.join(revision_sha);
ToolchainLocator { toolchain_dir }
}
}
pub struct RegistryToolchainsLocator {
registry_toolchain_dir: PathBuf,
}
impl RegistryToolchainsLocator {
pub fn root_dir(&self) -> &Path {
&self.registry_toolchain_dir
}
pub fn toolchain_dir(&self, registry: &str, version: &Version) -> ToolchainLocator {
let registry_hash = sha2::Sha256::digest(registry.as_bytes());
let registry_hash = format!("{registry_hash:x}")
.chars()
.take(7)
.collect::<String>();
let version = version.to_string().chars().take(7).collect::<String>();
let toolchain_dir = self
.registry_toolchain_dir
.join(registry_hash)
.join(version);
ToolchainLocator { toolchain_dir }
}
}
pub struct ToolchainLocator {
toolchain_dir: PathBuf,
}
impl ToolchainLocator {
pub fn root_dir(&self) -> &Path {
&self.toolchain_dir
}
pub fn pavexc(&self) -> PathBuf {
self.toolchain_dir.join(CliKind::Pavexc.binary_filename())
}
}
pub struct AuthLocator {
dir: PathBuf,
}
impl AuthLocator {
pub fn root_dir(&self) -> &Path {
&self.dir
}
pub fn token_cache(&self) -> PathBuf {
self.dir.join("cli_token.toml")
}
pub fn tmp(&self) -> PathBuf {
self.dir.join(".tmp")
}
}