use std::path::Path;
use std::sync::Arc;
use crate::cache::Cache;
use crate::lockfile::LockFile;
use crate::manifest::Manifest;
pub struct InstallContext<'a> {
pub project_dir: &'a Path,
pub cache: &'a Cache,
pub force_refresh: bool,
pub verbose: bool,
pub manifest: Option<&'a Manifest>,
pub lockfile: Option<&'a Arc<LockFile>>,
pub old_lockfile: Option<&'a LockFile>,
pub project_patches: Option<&'a crate::manifest::ManifestPatches>,
pub private_patches: Option<&'a crate::manifest::ManifestPatches>,
pub max_content_file_size: Option<u64>,
pub template_context_builder: Arc<crate::templating::TemplateContextBuilder>,
pub trust_lockfile_checksums: bool,
pub token_warning_threshold: Option<u64>,
}
pub struct InstallContextBuilder<'a> {
project_dir: &'a Path,
cache: &'a Cache,
force_refresh: bool,
verbose: bool,
trust_lockfile_checksums: bool,
manifest: Option<&'a Manifest>,
lockfile: Option<&'a Arc<LockFile>>,
old_lockfile: Option<&'a LockFile>,
project_patches: Option<&'a crate::manifest::ManifestPatches>,
private_patches: Option<&'a crate::manifest::ManifestPatches>,
max_content_file_size: Option<u64>,
token_warning_threshold: Option<u64>,
}
impl<'a> InstallContextBuilder<'a> {
pub fn new(project_dir: &'a Path, cache: &'a Cache) -> Self {
Self {
project_dir,
cache,
force_refresh: false,
verbose: false,
trust_lockfile_checksums: false,
manifest: None,
lockfile: None,
old_lockfile: None,
project_patches: None,
private_patches: None,
max_content_file_size: None,
token_warning_threshold: None,
}
}
pub fn force_refresh(mut self, value: bool) -> Self {
self.force_refresh = value;
self
}
pub fn verbose(mut self, value: bool) -> Self {
self.verbose = value;
self
}
pub fn trust_lockfile_checksums(mut self, value: bool) -> Self {
self.trust_lockfile_checksums = value;
self
}
pub fn manifest(mut self, manifest: &'a Manifest) -> Self {
self.manifest = Some(manifest);
self
}
pub fn lockfile(mut self, lockfile: &'a Arc<LockFile>) -> Self {
self.lockfile = Some(lockfile);
self
}
pub fn old_lockfile(mut self, old_lockfile: &'a LockFile) -> Self {
self.old_lockfile = Some(old_lockfile);
self
}
pub fn project_patches(mut self, patches: &'a crate::manifest::ManifestPatches) -> Self {
self.project_patches = Some(patches);
self
}
pub fn private_patches(mut self, patches: &'a crate::manifest::ManifestPatches) -> Self {
self.private_patches = Some(patches);
self
}
pub fn max_content_file_size(mut self, size: u64) -> Self {
self.max_content_file_size = Some(size);
self
}
pub fn token_warning_threshold(mut self, threshold: u64) -> Self {
self.token_warning_threshold = Some(threshold);
self
}
pub fn with_common_options(
mut self,
force_refresh: bool,
verbose: bool,
manifest: Option<&'a Manifest>,
lockfile: Option<&'a Arc<LockFile>>,
) -> Self {
self.force_refresh = force_refresh;
self.verbose = verbose;
self.manifest = manifest;
self.lockfile = lockfile;
self
}
#[must_use] pub fn build(self) -> InstallContext<'a> {
let (lockfile_for_builder, project_config) = if let Some(lf) = self.lockfile {
(lf.clone(), self.manifest.and_then(|m| m.project.clone()))
} else {
(Arc::new(LockFile::default()), None)
};
let template_context_builder = Arc::new(crate::templating::TemplateContextBuilder::new(
lockfile_for_builder,
project_config,
Arc::new(self.cache.clone()),
self.project_dir.to_path_buf(),
));
InstallContext {
project_dir: self.project_dir,
cache: self.cache,
force_refresh: self.force_refresh,
verbose: self.verbose,
manifest: self.manifest,
lockfile: self.lockfile,
old_lockfile: self.old_lockfile,
project_patches: self.project_patches,
private_patches: self.private_patches,
max_content_file_size: self.max_content_file_size,
template_context_builder,
trust_lockfile_checksums: self.trust_lockfile_checksums,
token_warning_threshold: self.token_warning_threshold,
}
}
}
impl<'a> InstallContext<'a> {
pub fn builder(project_dir: &'a Path, cache: &'a Cache) -> InstallContextBuilder<'a> {
InstallContextBuilder::new(project_dir, cache)
}
pub fn with_common_options(
project_dir: &'a Path,
cache: &'a Cache,
manifest: Option<&'a Manifest>,
lockfile: Option<&'a Arc<LockFile>>,
force_refresh: bool,
verbose: bool,
old_lockfile: Option<&'a LockFile>,
) -> Self {
Self::with_common_options_and_trust(
project_dir,
cache,
manifest,
lockfile,
force_refresh,
verbose,
old_lockfile,
false, None, )
}
#[allow(clippy::too_many_arguments)]
pub fn with_common_options_and_trust(
project_dir: &'a Path,
cache: &'a Cache,
manifest: Option<&'a Manifest>,
lockfile: Option<&'a Arc<LockFile>>,
force_refresh: bool,
verbose: bool,
old_lockfile: Option<&'a LockFile>,
trust_lockfile_checksums: bool,
token_warning_threshold: Option<u64>,
) -> Self {
let mut builder = Self::builder(project_dir, cache)
.force_refresh(force_refresh)
.verbose(verbose)
.trust_lockfile_checksums(trust_lockfile_checksums);
if let Some(m) = manifest {
builder = builder.manifest(m);
if !m.project_patches.is_empty() {
builder = builder.project_patches(&m.project_patches);
}
if !m.private_patches.is_empty() {
builder = builder.private_patches(&m.private_patches);
}
}
if let Some(lf) = lockfile {
builder = builder.lockfile(lf);
}
if let Some(old_lf) = old_lockfile {
builder = builder.old_lockfile(old_lf);
}
if let Some(threshold) = token_warning_threshold {
builder = builder.token_warning_threshold(threshold);
}
builder.build()
}
}