Skip to main content

code_moniker_workspace/registry/
local.rs

1// code-moniker: ignore-file[smell-clone-reflex]
2// Local registry wiring clones cache/options into independently owned workspace ports.
3use std::path::PathBuf;
4
5use crate::changes::LocalChangeOverlay;
6use crate::code::{LocalCodeIndex, LocalCodeIndexOptions};
7use crate::linkage::LocalLinkage;
8use crate::source::{
9	LocalIdentityResolver, LocalResourceCache, LocalSourceCatalog, LocalSourceCatalogOptions,
10};
11
12use super::{WorkspacePorts, WorkspaceRegistry};
13
14pub type LocalWorkspaceRegistry = WorkspaceRegistry;
15
16#[derive(Clone, Debug, Eq, PartialEq)]
17pub struct LocalWorkspaceOptions {
18	pub paths: Vec<PathBuf>,
19	pub project: Option<String>,
20	pub cache_dir: Option<PathBuf>,
21	pub files: Option<Vec<PathBuf>>,
22	pub identity: LocalIdentityResolver,
23}
24
25impl LocalWorkspaceOptions {
26	pub fn new(paths: Vec<PathBuf>, project: Option<String>) -> Self {
27		Self {
28			paths,
29			project,
30			cache_dir: None,
31			files: None,
32			identity: LocalIdentityResolver::default(),
33		}
34	}
35
36	pub fn with_cache_dir(mut self, cache_dir: Option<PathBuf>) -> Self {
37		self.cache_dir = cache_dir;
38		self
39	}
40
41	pub fn with_files(mut self, files: Vec<PathBuf>) -> Self {
42		self.files = Some(files);
43		self
44	}
45
46	pub fn with_identity(mut self, identity: LocalIdentityResolver) -> Self {
47		self.identity = identity;
48		self
49	}
50}
51
52impl LocalWorkspaceRegistry {
53	pub fn local(options: LocalWorkspaceOptions) -> Self {
54		Self::local_with_cache(options, LocalResourceCache::default())
55	}
56
57	pub fn local_with_cache(options: LocalWorkspaceOptions, cache: LocalResourceCache) -> Self {
58		Self::new(local_workspace_ports(options, cache))
59	}
60}
61
62pub(crate) fn local_workspace_ports(
63	options: LocalWorkspaceOptions,
64	cache: LocalResourceCache,
65) -> WorkspacePorts {
66	let watch_paths = options.paths.clone();
67	let watch_cache_dir = options.cache_dir.clone();
68	let mut source_options = LocalSourceCatalogOptions::new(options.paths, options.project)
69		.with_identity(options.identity);
70	if let Some(files) = options.files {
71		source_options = source_options.with_files(files);
72	}
73	WorkspacePorts::new(
74		LocalSourceCatalog::new(source_options, cache.clone()),
75		LocalCodeIndex::new(LocalCodeIndexOptions::new(options.cache_dir), cache.clone()),
76		LocalLinkage::new(cache.clone()),
77		LocalChangeOverlay::new(cache),
78	)
79	.with_live_watch_roots(move |_| {
80		crate::live::watch_roots_for_paths(&watch_paths, watch_cache_dir.as_deref())
81	})
82}