1use super::discovery::{FileTargets, Found, Reach, declaration, declare, discovery_ignore, walk};
5use super::*;
6
7#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct Site {
10 pub kind: ArtifactKind,
11 pub path: PathBuf,
12}
13
14impl Site {
15 pub fn folder(&self) -> &Path {
17 folder_of(self.kind, &self.path)
18 }
19
20 pub fn declaration_path(&self) -> PathBuf {
22 declaration_of(self.kind, &self.path)
23 }
24}
25
26pub(super) fn folder_of(kind: ArtifactKind, path: &Path) -> &Path {
27 match kind {
28 ArtifactKind::Folder | ArtifactKind::Graph => path,
29 ArtifactKind::File | ArtifactKind::Aspect => path.parent().unwrap_or(Path::new("")),
30 }
31}
32
33pub(super) fn declaration_of(kind: ArtifactKind, path: &Path) -> PathBuf {
34 match kind {
35 ArtifactKind::Graph => PathBuf::from(".artf"),
36 ArtifactKind::Folder => logical_join(path, std::ffi::OsStr::new(CONFIG_FILE)),
37 ArtifactKind::File | ArtifactKind::Aspect => {
38 let mut path = path.as_os_str().to_owned();
39 path.push(".artf");
40 PathBuf::from(path)
41 }
42 }
43}
44
45pub fn read_sites(repo: &Path) -> Result<BTreeMap<ArtifactName, Site>, ConfigError> {
48 let root =
49 crate::platform::canonicalize(repo).map_err(|error| ConfigError::new(repo, error))?;
50 let ignored = discovery_ignore(&root)?;
51 let mut sites = BTreeMap::new();
52 walk(
53 &root,
54 &ignored,
55 Reach::Workspace,
56 FileTargets::DeclarationsOnly,
57 &mut |found: Found<'_>| {
58 let name = declared_name(&found.source)
59 .map_err(|error| ConfigError::new(found.file, error))?;
60 let site = Site {
61 kind: found.kind,
62 path: found.path,
63 };
64 if sites.insert(name.clone(), site).is_some() {
65 return Err(ConfigError::declaration(
66 found.file,
67 &["name"],
68 format!("Duplicate Artifact name: {name}."),
69 ));
70 }
71 Ok(name)
72 },
73 )?;
74 Ok(sites)
75}
76
77fn declared_name(source: &str) -> Result<ArtifactName, String> {
79 let table = toml::de::DeTable::parse(source).map_err(|error| error.to_string())?;
80 match table.get_ref().get("name").map(|name| name.get_ref()) {
81 Some(toml::de::DeValue::String(name)) => name.parse(),
82 Some(_) => Err("name must be a string.".into()),
83 None => Err("missing field `name`".into()),
84 }
85}
86
87#[derive(Debug, Error)]
89pub enum ScopeReadError {
90 #[error("{name} is no longer declared at {}.", crate::platform::path_text(.path))]
92 Moved { name: ArtifactName, path: PathBuf },
93 #[error(transparent)]
94 Config(#[from] ConfigError),
95}
96
97pub(crate) fn read_artifact_scope(
102 repo: &Path,
103 name: &ArtifactName,
104 site: &Site,
105) -> Result<RepoConfig, ScopeReadError> {
106 let mut config = read_declared(repo, name, site)?;
107 config.evals.clear();
108 Ok(config)
109}
110
111pub(crate) fn read_eval_scope(
117 repo: &Path,
118 id: &crate::types::EvalId,
119 sites: &BTreeMap<ArtifactName, Site>,
120) -> Result<Option<RepoConfig>, ScopeReadError> {
121 let mut scoped: Option<RepoConfig> = None;
122 for (name, site) in sites {
123 let mut read = read_declared(repo, name, site)?;
124 read.evals.retain(|eval| &eval.id == id);
125 match &mut scoped {
126 None => scoped = Some(read),
127 Some(config) => {
128 for (name, artifact) in read.artifacts {
129 config.artifacts.entry(name).or_insert(artifact);
130 }
131 if config.evals.is_empty() {
132 config.evals = read.evals;
133 }
134 }
135 }
136 }
137 let Some(mut config) = scoped else {
138 return Ok(None);
139 };
140 let outside = sites
141 .keys()
142 .flat_map(|owner| named(&config.artifacts[owner]))
143 .any(|name| !config.artifacts.contains_key(name.as_str()));
144 let missing_parts = config.artifacts.values().any(|artifact| {
147 artifact
148 .parts
149 .iter()
150 .any(|part| !config.artifacts.contains_key(part))
151 });
152 if outside || missing_parts || crate::scope::resolve_evals(&mut config).is_err() {
153 return Ok(None);
154 }
155 Ok(Some(config))
156}
157
158fn named(artifact: &Artifact) -> impl Iterator<Item = &ReferenceName> {
160 let script = match &artifact.fingerprint {
161 Some(Fingerprint::Script { args, .. }) => args.as_slice(),
162 _ => &[],
163 };
164 let tools = artifact
165 .views
166 .human_tools
167 .values()
168 .flat_map(|tool| match tool {
169 HumanTool::Command(tool) => tool.args.as_slice(),
170 HumanTool::Builtin(tool) => tool.args.as_slice(),
171 });
172 script
173 .iter()
174 .chain(tools)
175 .filter_map(|argument| match argument {
176 crate::scope::Argument::Reference { name, .. } => Some(name),
177 crate::scope::Argument::Literal(_) => None,
178 })
179 .filter(|name| name.as_str() != "artifactPath")
181}
182
183fn read_declared(
185 repo: &Path,
186 name: &ArtifactName,
187 site: &Site,
188) -> Result<RepoConfig, ScopeReadError> {
189 let root =
190 crate::platform::canonicalize(repo).map_err(|error| ConfigError::new(repo, error))?;
191 let moved = || ScopeReadError::Moved {
192 name: name.clone(),
193 path: site.declaration_path(),
194 };
195 let ignored = discovery_ignore(&root)?;
196 let folder = site.folder();
197 if !crate::platform::path_kind(&root.join(site.declaration_path()))
198 .is_ok_and(|kind| kind == crate::platform::FileKind::File)
199 || !folder.as_os_str().is_empty()
200 && ignored
201 .matched_path_or_any_parents(root.join(folder), true)
202 .is_ignore()
203 {
204 return Err(moved());
205 }
206 let reach = match site.kind {
207 ArtifactKind::Folder => Reach::Folder(&site.path),
208 ArtifactKind::File | ArtifactKind::Aspect | ArtifactKind::Graph => Reach::File(
209 folder,
210 site.path
211 .file_name()
212 .and_then(|target| target.to_str())
213 .unwrap_or_default(),
214 ),
215 };
216 let mut config = RepoConfig {
217 root: root.clone(),
218 artifacts: BTreeMap::new(),
219 evals: Vec::new(),
220 relations: Vec::new(),
221 };
222 let mut schemas = super::schemas::Schemas::default();
223 walk(
224 &root,
225 &ignored,
226 reach,
227 FileTargets::Required,
228 &mut |found: Found<'_>| {
229 let own = config.artifacts.is_empty();
230 if own && (found.kind != site.kind || found.path != site.path) {
231 return Err(moved());
232 }
233 let declaration = declaration(&found, &mut schemas)?;
234 if own && &declaration.name != name {
235 return Err(moved());
236 }
237 Ok(declare(&mut config, found, declaration)?)
238 },
239 )?;
240 if !config.artifacts.contains_key(name) {
241 return Err(moved());
242 }
243 Ok(config)
244}
245
246#[cfg(test)]
247mod tests;