|
| 1 | +use relative_path::RelativePath; |
| 2 | +use relative_path::RelativePathBuf; |
| 3 | +use std::collections::BTreeMap; |
| 4 | +use std::path::Path; |
| 5 | + |
| 6 | +use crate::nix_file::NixFileStore; |
| 7 | +use crate::validation::ResultIteratorExt; |
| 8 | +use crate::validation::Validation::Success; |
| 9 | +use crate::{nix_file, ratchet, structure, validation}; |
| 10 | + |
| 11 | +/// Runs check on all Nix files, returning a ratchet result for each |
| 12 | +pub fn check_files( |
| 13 | + nixpkgs_path: &Path, |
| 14 | + nix_file_store: &mut NixFileStore, |
| 15 | +) -> validation::Result<BTreeMap<RelativePathBuf, ratchet::File>> { |
| 16 | + process_nix_files(nixpkgs_path, nix_file_store, |_nix_file| { |
| 17 | + // Noop for now, only boilerplate to make it easier to add future file-based checks |
| 18 | + Ok(Success(ratchet::File {})) |
| 19 | + }) |
| 20 | +} |
| 21 | + |
| 22 | +/// Processes all Nix files in a Nixpkgs directory according to a given function `f`, collecting the |
| 23 | +/// results into a mapping from each file to a ratchet value. |
| 24 | +fn process_nix_files( |
| 25 | + nixpkgs_path: &Path, |
| 26 | + nix_file_store: &mut NixFileStore, |
| 27 | + f: impl Fn(&nix_file::NixFile) -> validation::Result<ratchet::File>, |
| 28 | +) -> validation::Result<BTreeMap<RelativePathBuf, ratchet::File>> { |
| 29 | + // Get all Nix files |
| 30 | + let files = { |
| 31 | + let mut files = vec![]; |
| 32 | + collect_nix_files(nixpkgs_path, &RelativePathBuf::new(), &mut files)?; |
| 33 | + files |
| 34 | + }; |
| 35 | + |
| 36 | + let results = files |
| 37 | + .into_iter() |
| 38 | + .map(|path| { |
| 39 | + // Get the (optionally-cached) parsed Nix file |
| 40 | + let nix_file = nix_file_store.get(&path.to_path(nixpkgs_path))?; |
| 41 | + let result = f(nix_file)?; |
| 42 | + let val = result.map(|ratchet| (path, ratchet)); |
| 43 | + Ok::<_, anyhow::Error>(val) |
| 44 | + }) |
| 45 | + .collect_vec()?; |
| 46 | + |
| 47 | + Ok(validation::sequence(results).map(|entries| { |
| 48 | + // Convert the Vec to a BTreeMap |
| 49 | + entries.into_iter().collect() |
| 50 | + })) |
| 51 | +} |
| 52 | + |
| 53 | +/// Recursively collects all Nix files in the relative `dir` within `base` |
| 54 | +/// into the `files` `Vec`. |
| 55 | +fn collect_nix_files( |
| 56 | + base: &Path, |
| 57 | + dir: &RelativePath, |
| 58 | + files: &mut Vec<RelativePathBuf>, |
| 59 | +) -> anyhow::Result<()> { |
| 60 | + for entry in structure::read_dir_sorted(&dir.to_path(base))? { |
| 61 | + let mut relative_path = dir.to_relative_path_buf(); |
| 62 | + relative_path.push(entry.file_name().to_string_lossy().into_owned()); |
| 63 | + |
| 64 | + let absolute_path = entry.path(); |
| 65 | + |
| 66 | + // We'll get to every file based on directory recursion, no need to follow symlinks. |
| 67 | + if absolute_path.is_symlink() { |
| 68 | + continue; |
| 69 | + } |
| 70 | + if absolute_path.is_dir() { |
| 71 | + collect_nix_files(base, &relative_path, files)? |
| 72 | + } else if absolute_path.extension().is_some_and(|x| x == "nix") { |
| 73 | + files.push(relative_path) |
| 74 | + } |
| 75 | + } |
| 76 | + Ok(()) |
| 77 | +} |
0 commit comments