artifactize_core/config/
site.rs1use super::*;
3
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct Site {
6 pub path: Target,
7 pub definition: PathBuf,
8}
9impl Site {
10 pub fn folder(&self) -> &Path {
12 folder_of(self.path.kind(), &self.path)
13 }
14 pub fn declaration_path(&self) -> PathBuf {
15 self.definition.clone()
16 }
17}
18pub(super) fn folder_of(kind: ArtifactKind, path: &Target) -> &Path {
19 match kind {
20 ArtifactKind::Folder | ArtifactKind::Aspect | ArtifactKind::Graph => {
21 path.local().expect("filesystem kind")
22 }
23 ArtifactKind::File => path
24 .local()
25 .expect("filesystem kind")
26 .parent()
27 .unwrap_or(Path::new("")),
28 ArtifactKind::Live => Path::new(""),
29 }
30}
31pub(super) fn declaration_of(kind: ArtifactKind, target: &Target) -> PathBuf {
33 let path = target.local().unwrap_or(Path::new(""));
34 match kind {
35 ArtifactKind::Live => PathBuf::new(),
36 ArtifactKind::Graph => PathBuf::from(".artf"),
37 ArtifactKind::Folder => logical_join(path, std::ffi::OsStr::new(CONFIG_FILE)),
38 ArtifactKind::File | ArtifactKind::Aspect => {
39 let mut path = path.as_os_str().to_owned();
40 path.push(".artf");
41 PathBuf::from(path)
42 }
43 }
44}
45pub fn read_sites(repo: &Path) -> Result<BTreeMap<ArtifactName, Site>, ConfigError> {
46 let root =
47 crate::platform::canonicalize(repo).map_err(|error| ConfigError::new(repo, error))?;
48 discovery::sources(&root)?
49 .into_iter()
50 .map(|(file, source)| {
51 let name = discovery::definition_name(
52 file.file_stem()
53 .and_then(crate::platform::os_text)
54 .unwrap_or_default(),
55 )
56 .map_err(|error| ConfigError::new(&file, error))?;
57 let parsed =
58 discovery::parse_definition(&name, &source, &mut schemas::Schemas::default())
59 .map_err(|error| ConfigError::new(&file, error))?;
60 let path = parsed
61 .path
62 .with_kind(parsed.kind, parsed.declaration.select.unwrap_or_default())
63 .map_err(|error| ConfigError::new(&file, error))?;
64 Ok((
65 name,
66 Site {
67 path,
68 definition: file.strip_prefix(&root).unwrap().to_owned(),
69 },
70 ))
71 })
72 .collect()
73}
74#[derive(Debug, Error)]
75pub enum ScopeReadError {
76 #[error("{name} is no longer declared at {}.", crate::platform::path_text(.path))]
77 Moved { name: ArtifactName, path: PathBuf },
78 #[error(transparent)]
79 Config(#[from] ConfigError),
80}
81fn read_declared(
82 repo: &Path,
83 name: &ArtifactName,
84 site: &Site,
85) -> Result<RepoConfig, ScopeReadError> {
86 let root =
87 crate::platform::canonicalize(repo).map_err(|error| ConfigError::new(repo, error))?;
88 let all = discovery::files(&root)?;
89 let by_name = all
90 .iter()
91 .map(|(file, _)| file)
92 .filter_map(|file| {
93 file.file_stem()
94 .and_then(crate::platform::os_text)
95 .map(|name| (name.to_owned(), file))
96 })
97 .collect::<BTreeMap<_, _>>();
98 let expected = root.join(site.declaration_path());
99 if by_name
100 .get(name.as_str())
101 .is_none_or(|file| **file != expected)
102 {
103 return Err(ScopeReadError::Moved {
104 name: name.clone(),
105 path: site.declaration_path(),
106 });
107 }
108 let mut selected = BTreeMap::new();
109 let mut pending = vec![name.clone()];
110 while let Some(next) = pending.pop() {
111 let file = if next == *name {
112 root.join(site.declaration_path())
113 } else {
114 by_name
115 .get(next.as_str())
116 .map(|file| (*file).clone())
117 .ok_or_else(|| ConfigError::new(&root, format!("Unknown Artifact {next}.")))?
118 };
119 if selected.contains_key(&file) {
120 continue;
121 }
122 let source = read_regular_text(&file).map_err(|error| {
123 if error.kind() == std::io::ErrorKind::NotFound {
124 ScopeReadError::Moved {
125 name: next.clone(),
126 path: site.declaration_path(),
127 }
128 } else {
129 ScopeReadError::Config(ConfigError::new(&file, error))
130 }
131 })?;
132 let parsed = discovery::parse_definition(&next, &source, &mut schemas::Schemas::default())
133 .map_err(|error| ConfigError::new(&file, error))?;
134 pending.extend(parsed.declaration.parts.into_iter().flatten());
135 selected.insert(file, source);
136 }
137 let mut config = discovery::read_sources_unresolved(root, &selected)?;
138 discovery::inspect_targets(&mut config);
139 if !config
140 .artifacts
141 .get(name)
142 .is_some_and(|artifact| artifact.site() == *site)
143 {
144 return Err(ScopeReadError::Moved {
145 name: name.clone(),
146 path: site.declaration_path(),
147 });
148 }
149 Ok(config)
150}
151pub(crate) fn read_artifact_scope(
152 repo: &Path,
153 name: &ArtifactName,
154 site: &Site,
155) -> Result<RepoConfig, ScopeReadError> {
156 let mut config = read_declared(repo, name, site)?;
157 config.evals.clear();
158 Ok(config)
159}
160pub(crate) fn read_eval_scope(
161 repo: &Path,
162 id: &EvalId,
163 sites: &BTreeMap<ArtifactName, Site>,
164) -> Result<Option<RepoConfig>, ScopeReadError> {
165 let mut combined: Option<RepoConfig> = None;
166 for (name, site) in sites {
167 let mut read = read_declared(repo, name, site)?;
168 read.evals.retain(|eval| &eval.id == id);
169 if let Some(config) = &mut combined {
170 config.artifacts.extend(read.artifacts);
171 if config.evals.is_empty() {
172 config.evals = read.evals;
173 }
174 } else {
175 combined = Some(read);
176 }
177 }
178 let Some(mut config) = combined else {
179 return Ok(None);
180 };
181 if crate::scope::resolve_config(&mut config)
182 .and_then(|()| crate::scope::validate_eval_tools(&config))
183 .is_err()
184 {
185 return Ok(None);
186 }
187 Ok(Some(config))
188}