Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

Commit

Permalink
Also move non-referenced files
Browse files Browse the repository at this point in the history
When the containing directory is being moved, but a bit more complicated
  • Loading branch information
infinisil committed Jan 20, 2023
1 parent 2ea11e8 commit e292b27
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
9 changes: 4 additions & 5 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ impl GlobalIndex {
path_index.referenced_by.push(pointer.clone());

let mut focused_dir = subpath.parent().unwrap().to_path_buf();
// The directory of the file is referenced by the file
path_indices.get_mut(&focused_dir).unwrap().referenced_by.push(pointer.clone());
for component in rel_to_source.components() {
match component {
Component::CurDir => {}
Expand Down Expand Up @@ -197,13 +199,10 @@ pub fn resolve_reference(source: &PathBuf, line: usize, reference: &PathBuf, kno
}
}

if rel_to_root.is_dir() {
// This should only be done for the top-level
if rel_to_root.is_dir() && known_files.contains_key(&rel_to_root.join("default.nix")) {
rel_to_root = rel_to_root.join("default.nix");
rel_to_source = rel_to_source.join("default.nix");
if ! known_files.contains_key(&rel_to_root) {
eprintln!("Warning: File {:?} on line {:?} refers to a directory that doesn't contain a default.nix file, ignoring it: {:?}", source, line, reference);
return None
}
}
Some((rel_to_source, movable_ancestor, rel_to_root))
}
Expand Down
28 changes: 24 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use ignore::Walk;
use crate::unit::attr_shard_dir;
use std::path::PathBuf;
use std::collections::HashSet;
Expand Down Expand Up @@ -42,12 +43,12 @@ fn main() {
let mut stack = vec![value.path.clone()];
let mut seen : HashSet<PathBuf> = HashSet::new();
seen.insert(value.path.clone());
let old_dir = value.path.parent().unwrap();
let old_dir = value.path.parent().unwrap().to_path_buf();

while let Some(next) = stack.pop() {
for reference in reference_index.path_indices.get(&next).unwrap().references.to_owned() {
// println!("Reference: {:#?}", reference);
if ! reference.movable_ancestor.starts_with(old_dir) {
if ! reference.movable_ancestor.starts_with(&old_dir) {
eprintln!("Cannot move attribute {:?} pointing to file {:?}, because it transitively references file {:?} which in line {:?} contains a path reference {:?} which would break", key, value.path, next, reference.line, reference.text);
continue 'attrs;
}
Expand Down Expand Up @@ -78,8 +79,27 @@ fn main() {
std::fs::create_dir_all(&unit_dir).unwrap();
// println!("Moving attribute {:?} pointing to file {:?} to unit directory {:?}", key, value.path, unit_dir);

for old in seen {
let base = old.strip_prefix(old_dir).unwrap();
std::env::set_current_dir(&cli.path).unwrap();
for result in Walk::new(&old_dir) {
let old = result.unwrap().into_path();
let old_dir_ref_bys = &reference_index.path_indices.get(&old_dir).unwrap().referenced_by;
if old.is_dir() {
continue
}
if seen.contains(&old) {
// println!("Moving {:?} to {:?} because it's being transitively referenced", old, new);
} else if
// There can only be one reference from all-packages.nix, a bit hacky
old_dir_ref_bys.iter().filter(|(path, _)| path == &PathBuf::from("./pkgs/top-level/all-packages.nix")).count() == 1 &&
// And all the other references must come from the file itself
old_dir_ref_bys.iter().all(|(path, _)| path == &PathBuf::from("./pkgs/top-level/all-packages.nix") || path == &value.path) &&
// And the file to be moved must not be referenced from anywhere else
reference_index.path_indices.get(&old).unwrap().referenced_by.is_empty() {
eprintln!("For attribute {:?}, only all-packages.nix and its file {:?} may reference the containing directory {:?}, which also contains the file {:?} which is not referenced from anywhere else. Also moving that to the unit directory", key, value.path, old_dir, old);
} else {
continue
}
let base = old.strip_prefix(&old_dir).unwrap();
let mut new = unit_dir.join(base);
if old == value.path {
new.pop();
Expand Down

0 comments on commit e292b27

Please sign in to comment.