skeleton continues

This commit is contained in:
Qyriad 2026-01-27 17:20:04 +01:00
parent bcd11513ef
commit e5d0bdf0c0
5 changed files with 161 additions and 101 deletions

View file

@ -1,3 +1,5 @@
use std::{io::BufWriter, sync::Arc};
pub(crate) mod prelude {
#![allow(unused_imports)]
@ -27,22 +29,20 @@ pub(crate) mod prelude {
use prelude::*;
use std::{
io::{BufRead, BufReader},
sync::Arc,
};
pub mod args;
pub use args::Parser;
mod color;
pub use color::{CLI_ENABLE_COLOR, SHOULD_COLOR};
pub mod line;
mod nixcmd;
pub use line::Line;
pub mod source;
pub use source::SourceLine;
use serde::{Deserialize, Serialize};
use crate::source::SourceFile;
#[derive(Debug, Clone, PartialEq, Hash, Serialize, Deserialize)]
pub struct DefinitionWithLocation {
pub file: Box<Path>,
@ -59,57 +59,29 @@ pub fn get_where(option_name: &str, configuration_nix: &Path) -> Result<Box<Path
.into_iter()
.map(ToOwned::to_owned)
.collect();
let result = Command::new("nix-instantiate")
.arg("--eval")
.arg("--json")
.arg("--strict")
.arg("--expr")
.arg(expr)
.arg("-A")
.arg(format!("options.{}.definitionsWithLocations", option_name))
let attrpath = format!("options.{}.definitionsWithLocations", option_name);
let output = nixcmd::NixEvalExpr { expr, attrpath }
.into_command()
.output_checked_utf8()?;
let stdout = result.stdout();
let stdout = output.stdout();
let definitions: Box<[DefinitionWithLocation]> = serde_json::from_str(&stdout)?;
let last_location = definitions.last().unwrap();
let file = &*last_location.file;
dbg!(&file);
let last_location = definitions.into_iter().last().unwrap();
Ok(Box::from(file))
Ok(Box::from(last_location.file))
}
pub fn get_highest_prio(
option_name: &str,
file_with_definition: &Path,
mut source: SourceFile,
) -> Result<SourceLine, BoxDynError> {
let mut file = File::options()
.read(true)
.write(true)
.create(false)
.custom_flags(libc::O_CLOEXEC)
.open(file_with_definition)?;
// TODO: seek and read backwards.
let mut lines = BufReader::new(&mut file)
.lines()
.enumerate()
.map(|(index, line_res)| {
line_res.map(|line| SourceLine {
line: Line::from_index(index as u64),
path: Arc::from(file_with_definition),
text: Arc::from(line),
})
})
.collect::<Result<Vec<SourceLine>, IoError>>()?;
lines.reverse();
let lines_reversed = lines;
// Get the current highest priority.
let expr: OsString = [
OsStr::new("import <nixpkgs/nixos> { configuration = "),
file_with_definition.as_os_str(),
source.path().as_os_str(),
OsStr::new("; }"),
]
.into_iter()
@ -117,41 +89,40 @@ pub fn get_highest_prio(
.collect();
// Get the highest priority, and the file its defined in.
let result = Command::new("nix-instantiate")
.arg("--eval")
.arg("--json")
.arg("--strict")
.arg("--expr")
.arg(expr)
.arg("-A")
.arg(format!("options.{}.highestPrio", option_name))
let attrpath = format!("options.{}.highestPrio", option_name);
let output = nixcmd::NixEvalExpr { expr, attrpath }
.into_command()
.output_checked_utf8()?;
let stdout = result.stdout();
let stdout = output.stdout();
let highest_prio = stdout.trim();
let needle = format!("lib.mkOverride ({})", highest_prio);
eprintln!("looking for {needle:?}");
let line_with_current_highest = lines_reversed
let path = source.path();
let lines = source.lines()?;
let line = lines
.iter()
.position(|line| {
eprintln!("looking for {needle} in {line}");
line.text.contains(&needle)
})
// We're more likely to find it at the end, so let's start there.
.rev()
.find(|&line| line.text.contains(&needle))
.unwrap_or_else(|| {
panic!(
"couldn't find override number {highest_prio} in {}",
file_with_definition.display()
path.display(),
)
});
let line = lines_reversed
.into_iter()
.nth(line_with_current_highest)
.unwrap();
eprintln!("found, on line index {}", line);
Ok(line)
Ok(line.clone())
}
pub fn write_next_prio(
mut source: SourceFile,
last_line_def: SourceLine,
new_prio: u64,
) -> Result<(), BoxDynError> {
let lines = source.lines()?;
for line in lines {}
Ok(())
}