Skip to main content

artifactize_core/config/
site.rs

1//! Where each Artifact is declared, and one Artifact's declarations read alone: what an
2//! Artifact's Detail needs without reading the whole workspace.
3
4use super::discovery::{FileTargets, Found, Reach, declaration, declare, discovery_ignore, walk};
5use super::*;
6
7/// Where an Artifact is declared: its kind and logical path, as a full read places it.
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct Site {
10    pub kind: ArtifactKind,
11    pub path: PathBuf,
12}
13
14impl Site {
15    /// The folder the Artifact's declaration is in.
16    pub fn folder(&self) -> &Path {
17        folder_of(self.kind, &self.path)
18    }
19
20    /// The declaration file's logical path.
21    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
45/// Every Artifact's site in the workspace at `repo`, from its declarations' names alone:
46/// nothing else in them is parsed or validated, so this is no check of the workspace.
47pub 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
77/// The `name` of a declaration, read without deserializing or validating the rest.
78fn 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/// Why [`read_artifact_scope`] read no declarations.
88#[derive(Debug, Error)]
89pub enum ScopeReadError {
90    /// The site no longer declares the Artifact: where it is now needs [`read_sites`] again.
91    #[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
97/// The declarations of Artifact `name`, declared at `site`, and of the Artifacts it composes,
98/// each parsed and validated as a full read does; no other declaration is read. Checks across
99/// the workspace (unique names, references, the graph) stay with the full read, so the config
100/// holds only these Artifacts, their children and no evals.
101pub(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
111/// The declarations eval `id` reviews: those of the Artifacts in its scope, each declared at
112/// its site in `sites`, with the Artifacts they compose, and the eval resolved against them.
113/// No other declaration is read, and the checks across the workspace stay with the full read.
114/// The config holds no eval when the target no longer declares it, and is none when these
115/// declarations name an Artifact outside them, which only a full read places.
116pub(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    // Explicit parts may be declared outside the folders this read visited. A full read
145    // must place those before fingerprint preparation can use the target's composition.
146    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
158/// The Artifacts an Artifact's fingerprint script and Human tools reference by name.
159fn 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        // `{artifactPath}` stands for the tool's own Artifact.
180        .filter(|name| name.as_str() != "artifactPath")
181}
182
183/// [`read_artifact_scope`] with the evals these declarations declare.
184fn 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;