pesde/src/lib.rs

202 lines
5.9 KiB
Rust
Raw Normal View History

2024-08-03 21:18:38 +01:00
#![deny(missing_docs)]
//! pesde is a package manager for Luau, designed to be feature-rich and easy to use.
//! pesde has its own registry, however it can also use Wally, and GitHub as package sources.
//! It has been designed with multiple targets in mind, namely Roblox, Lune, and Luau.
2024-07-12 23:09:37 +01:00
#[cfg(not(any(feature = "roblox", feature = "lune", feature = "luau")))]
compile_error!("at least one of the features `roblox`, `lune`, or `luau` must be enabled");
use crate::lockfile::Lockfile;
2024-07-12 23:09:37 +01:00
use std::path::{Path, PathBuf};
2024-08-03 21:18:38 +01:00
/// Downloading packages
2024-07-17 18:38:01 +01:00
pub mod download;
2024-08-03 21:18:38 +01:00
/// Linking packages
2024-07-17 18:38:01 +01:00
pub mod linking;
2024-08-03 21:18:38 +01:00
/// Lockfile
2024-07-12 23:09:37 +01:00
pub mod lockfile;
2024-08-03 21:18:38 +01:00
/// Manifest
2024-03-04 20:18:49 +00:00
pub mod manifest;
2024-08-03 21:18:38 +01:00
/// Package names
2024-07-12 23:09:37 +01:00
pub mod names;
2024-08-03 21:18:38 +01:00
/// Patching packages
2024-07-23 23:53:34 +01:00
#[cfg(feature = "patches")]
2024-07-23 15:37:47 +01:00
pub mod patches;
2024-08-03 21:18:38 +01:00
/// Resolving packages
pub mod resolver;
2024-08-03 21:18:38 +01:00
/// Running scripts
pub mod scripts;
2024-08-03 21:18:38 +01:00
/// Package sources
2024-07-12 23:09:37 +01:00
pub mod source;
2024-07-22 21:00:09 +01:00
pub(crate) mod util;
2024-07-12 23:09:37 +01:00
2024-08-03 21:18:38 +01:00
/// The name of the manifest file
pub const MANIFEST_FILE_NAME: &str = "pesde.toml";
2024-08-03 21:18:38 +01:00
/// The name of the lockfile
2024-07-12 23:09:37 +01:00
pub const LOCKFILE_FILE_NAME: &str = "pesde.lock";
2024-08-03 21:18:38 +01:00
/// The name of the default index
pub const DEFAULT_INDEX_NAME: &str = "default";
2024-08-03 21:18:38 +01:00
/// The name of the packages container
2024-07-17 18:38:01 +01:00
pub const PACKAGES_CONTAINER_NAME: &str = ".pesde";
2024-08-03 21:18:38 +01:00
/// Maximum size of a package's archive
2024-07-22 15:41:45 +01:00
pub const MAX_ARCHIVE_SIZE: usize = 4 * 1024 * 1024;
2024-07-12 23:09:37 +01:00
2024-08-03 21:18:38 +01:00
/// Struct containing the authentication configuration
2024-07-12 23:09:37 +01:00
#[derive(Debug, Default, Clone)]
pub struct AuthConfig {
2024-07-22 15:41:45 +01:00
pesde_token: Option<String>,
git_credentials: Option<gix::sec::identity::Account>,
2024-07-12 23:09:37 +01:00
}
impl AuthConfig {
2024-08-03 21:18:38 +01:00
/// Create a new `AuthConfig`
2024-07-12 23:09:37 +01:00
pub fn new() -> Self {
AuthConfig::default()
}
2024-08-03 21:18:38 +01:00
/// Access the pesde token
2024-07-22 15:41:45 +01:00
pub fn pesde_token(&self) -> Option<&str> {
self.pesde_token.as_deref()
2024-07-12 23:09:37 +01:00
}
2024-08-03 21:18:38 +01:00
/// Access the git credentials
2024-07-22 15:41:45 +01:00
pub fn git_credentials(&self) -> Option<&gix::sec::identity::Account> {
self.git_credentials.as_ref()
}
2024-08-03 21:18:38 +01:00
/// Set the pesde token
2024-07-22 15:41:45 +01:00
pub fn with_pesde_token<S: AsRef<str>>(mut self, token: Option<S>) -> Self {
self.pesde_token = token.map(|s| s.as_ref().to_string());
2024-07-12 23:09:37 +01:00
self
}
2024-08-03 21:18:38 +01:00
/// Set the git credentials
2024-07-22 15:41:45 +01:00
pub fn with_git_credentials(
mut self,
git_credentials: Option<gix::sec::identity::Account>,
) -> Self {
self.git_credentials = git_credentials;
self
2024-07-12 23:09:37 +01:00
}
}
2024-08-03 21:18:38 +01:00
/// The main struct of the pesde library, representing a project
#[derive(Debug, Clone)]
2024-07-12 23:09:37 +01:00
pub struct Project {
path: PathBuf,
data_dir: PathBuf,
auth_config: AuthConfig,
2024-07-28 17:19:54 +01:00
cas_dir: PathBuf,
2024-07-12 23:09:37 +01:00
}
impl Project {
2024-08-03 21:18:38 +01:00
/// Create a new `Project`
2024-07-28 17:19:54 +01:00
pub fn new<P: AsRef<Path>, Q: AsRef<Path>, R: AsRef<Path>>(
2024-07-12 23:09:37 +01:00
path: P,
data_dir: Q,
2024-07-28 17:19:54 +01:00
cas_dir: R,
2024-07-12 23:09:37 +01:00
auth_config: AuthConfig,
) -> Self {
Project {
path: path.as_ref().to_path_buf(),
data_dir: data_dir.as_ref().to_path_buf(),
auth_config,
2024-07-28 17:19:54 +01:00
cas_dir: cas_dir.as_ref().to_path_buf(),
2024-07-12 23:09:37 +01:00
}
}
2024-08-03 21:18:38 +01:00
/// Access the path
2024-07-12 23:09:37 +01:00
pub fn path(&self) -> &Path {
&self.path
}
2024-08-03 21:18:38 +01:00
/// Access the data directory
2024-07-12 23:09:37 +01:00
pub fn data_dir(&self) -> &Path {
&self.data_dir
}
2024-08-03 21:18:38 +01:00
/// Access the authentication configuration
2024-07-22 15:41:45 +01:00
pub fn auth_config(&self) -> &AuthConfig {
&self.auth_config
}
2024-08-03 21:18:38 +01:00
/// Access the CAS (content-addressable storage) directory
2024-07-28 17:19:54 +01:00
pub fn cas_dir(&self) -> &Path {
&self.cas_dir
}
2024-08-03 21:18:38 +01:00
/// Read the manifest file
2024-07-23 15:37:47 +01:00
pub fn read_manifest(&self) -> Result<String, errors::ManifestReadError> {
let string = std::fs::read_to_string(self.path.join(MANIFEST_FILE_NAME))?;
Ok(string)
2024-07-12 23:09:37 +01:00
}
2024-08-03 21:18:38 +01:00
/// Deserialize the manifest file
2024-07-12 23:09:37 +01:00
pub fn deser_manifest(&self) -> Result<manifest::Manifest, errors::ManifestReadError> {
let string = std::fs::read_to_string(self.path.join(MANIFEST_FILE_NAME))?;
Ok(toml::from_str(&string)?)
2024-07-12 23:09:37 +01:00
}
2024-08-03 21:18:38 +01:00
/// Write the manifest file
2024-07-12 23:09:37 +01:00
pub fn write_manifest<S: AsRef<[u8]>>(&self, manifest: S) -> Result<(), std::io::Error> {
std::fs::write(self.path.join(MANIFEST_FILE_NAME), manifest.as_ref())
}
2024-08-03 21:18:38 +01:00
/// Deserialize the lockfile
pub fn deser_lockfile(&self) -> Result<Lockfile, errors::LockfileReadError> {
let string = std::fs::read_to_string(self.path.join(LOCKFILE_FILE_NAME))?;
Ok(toml::from_str(&string)?)
}
2024-08-03 21:18:38 +01:00
/// Write the lockfile
pub fn write_lockfile(&self, lockfile: Lockfile) -> Result<(), errors::LockfileWriteError> {
let string = toml::to_string(&lockfile)?;
std::fs::write(self.path.join(LOCKFILE_FILE_NAME), string)?;
Ok(())
}
2024-07-12 23:09:37 +01:00
}
2024-08-03 21:18:38 +01:00
/// Errors that can occur when reading or writing files
2024-07-12 23:09:37 +01:00
pub mod errors {
use thiserror::Error;
2024-08-03 21:18:38 +01:00
/// Errors that can occur when reading the manifest file
2024-07-12 23:09:37 +01:00
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ManifestReadError {
2024-08-03 21:18:38 +01:00
/// An IO error occurred
2024-07-12 23:09:37 +01:00
#[error("io error reading manifest file")]
Io(#[from] std::io::Error),
2024-08-03 21:18:38 +01:00
/// An error occurred while deserializing the manifest file
2024-07-12 23:09:37 +01:00
#[error("error deserializing manifest file")]
Serde(#[from] toml::de::Error),
2024-07-12 23:09:37 +01:00
}
2024-08-03 21:18:38 +01:00
/// Errors that can occur when reading the lockfile
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum LockfileReadError {
2024-08-03 21:18:38 +01:00
/// An IO error occurred
#[error("io error reading lockfile")]
Io(#[from] std::io::Error),
2024-08-03 21:18:38 +01:00
/// An error occurred while deserializing the lockfile
#[error("error deserializing lockfile")]
Serde(#[from] toml::de::Error),
}
2024-08-03 21:18:38 +01:00
/// Errors that can occur when writing the lockfile
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum LockfileWriteError {
2024-08-03 21:18:38 +01:00
/// An IO error occurred
#[error("io error writing lockfile")]
Io(#[from] std::io::Error),
2024-08-03 21:18:38 +01:00
/// An error occurred while serializing the lockfile
#[error("error serializing lockfile")]
Serde(#[from] toml::ser::Error),
}
}