141 lines
5.2 KiB
Rust
141 lines
5.2 KiB
Rust
use std::{
|
|
collections::{BTreeMap, BTreeSet},
|
|
fs,
|
|
io::{self, Read},
|
|
path::{Path, PathBuf},
|
|
sync::Arc,
|
|
time::SystemTime,
|
|
};
|
|
use texrra_engine::native::CompileFailure;
|
|
use texrra_tex_core::resources::{FileModificationTime, FileWorld, ProjectSnapshot, ResourceKind, SearchPath};
|
|
|
|
// Use the opened file for both observations so a renamed path cannot supply
|
|
// a date from a different file.
|
|
pub(super) fn read_with_date(path: &Path) -> io::Result<(Vec<u8>, Option<SystemTime>)> {
|
|
let mut file = fs::File::open(path)?;
|
|
let modified = file.metadata()?.modified().ok();
|
|
let mut bytes = Vec::new();
|
|
file.read_to_end(&mut bytes)?;
|
|
if file.metadata()?.modified().ok() != modified {
|
|
return Err(io::Error::other(format!("Input changed during capture: {}", path.display())));
|
|
}
|
|
Ok((bytes, modified))
|
|
}
|
|
|
|
#[derive(Default)]
|
|
pub(super) struct PackageDiscovery {
|
|
expansions: u8,
|
|
prepare_ctex: bool,
|
|
}
|
|
|
|
impl PackageDiscovery {
|
|
pub(super) fn needs_preparation(&self) -> bool {
|
|
self.prepare_ctex || self.expansions >= 2
|
|
}
|
|
}
|
|
|
|
#[derive(Default)]
|
|
pub(super) struct ResourceCapture {
|
|
resources: BTreeMap<PathBuf, Arc<[u8]>>,
|
|
dates: BTreeMap<PathBuf, FileModificationTime>,
|
|
host: Option<FileWorld>,
|
|
}
|
|
|
|
impl ResourceCapture {
|
|
pub(super) fn apply(
|
|
&self,
|
|
project: &Arc<ProjectSnapshot>,
|
|
) -> Result<Arc<ProjectSnapshot>, String> {
|
|
project.with_external_resources_and_dates(&self.resources, &self.dates)
|
|
}
|
|
|
|
pub(super) fn expand(
|
|
&mut self,
|
|
project: &Arc<ProjectSnapshot>,
|
|
failure: &CompileFailure,
|
|
package_discovery: Option<&mut PackageDiscovery>,
|
|
) -> Option<Arc<ProjectSnapshot>> {
|
|
let trace = failure.trace.as_ref()?;
|
|
let config = project.configuration();
|
|
let search_roots = config
|
|
.texmf_roots
|
|
.iter()
|
|
.cloned()
|
|
.chain(
|
|
config
|
|
.paths
|
|
.values()
|
|
.flatten()
|
|
.filter_map(|path| match path {
|
|
SearchPath::Directory { path, .. } => Some(config.cwd.join(path)),
|
|
SearchPath::Defaults => None,
|
|
}),
|
|
)
|
|
.collect::<Vec<_>>();
|
|
let host = match &mut self.host {
|
|
Some(host) => host,
|
|
slot => slot.insert(FileWorld::new(config.clone()).ok()?),
|
|
};
|
|
let mut queried = BTreeSet::new();
|
|
let mut added = BTreeMap::new();
|
|
let mut dates = BTreeMap::new();
|
|
let mut added_package_file = false;
|
|
let mut added_ctex = false;
|
|
for lookup in trace
|
|
.lookups
|
|
.iter()
|
|
.filter(|lookup| lookup.resolved.is_none())
|
|
{
|
|
if !queried.insert((lookup.kind, &lookup.name, &lookup.from)) {
|
|
continue;
|
|
}
|
|
let resolved = if lookup.kind == ResourceKind::Tex {
|
|
host.resolve_tex_input(&lookup.name, &lookup.from)
|
|
} else {
|
|
host.resolve(lookup.kind, &lookup.name, &lookup.from)
|
|
};
|
|
if let Ok(Some(path)) = resolved
|
|
&& !path.starts_with(&config.cwd)
|
|
&& search_roots.iter().any(|root| path.starts_with(root))
|
|
&& project.file(&path).is_none()
|
|
&& fs::metadata(&path).is_ok_and(|metadata| metadata.is_file())
|
|
&& let Ok((bytes, modified)) = read_with_date(&path)
|
|
{
|
|
if let Some(date) = modified.and_then(|time| FileModificationTime::from_system_time(time).ok()) {
|
|
dates.insert(path.clone(), date);
|
|
}
|
|
added_package_file |= lookup.kind == ResourceKind::Tex
|
|
&& path.extension().and_then(|value| value.to_str()).is_some_and(|extension| {
|
|
["sty", "cls", "clo", "def", "cfg", "fd", "ldf"]
|
|
.iter().any(|known| extension.eq_ignore_ascii_case(known))
|
|
});
|
|
added_ctex |= lookup.kind == ResourceKind::Tex
|
|
&& lookup.error.is_none()
|
|
&& Path::new(&lookup.name).file_name().and_then(|name| name.to_str())
|
|
.is_some_and(|name| name.eq_ignore_ascii_case("ctex.sty"))
|
|
&& path.file_name().and_then(|name| name.to_str())
|
|
.is_some_and(|name| name.eq_ignore_ascii_case("ctex.sty"));
|
|
added.insert(path, Arc::from(bytes));
|
|
}
|
|
}
|
|
if added.is_empty() {
|
|
return None;
|
|
}
|
|
let expanded = project.with_external_resources_and_dates(&added, &dates).ok()?;
|
|
if let Some(discovery) = package_discovery
|
|
&& added_package_file
|
|
{
|
|
// CTeX brings a package tree. Other packages can first retry
|
|
// with a single capture before preparing a new tree.
|
|
discovery.prepare_ctex |= added_ctex;
|
|
discovery.expansions += 1;
|
|
if discovery.needs_preparation() {
|
|
return None;
|
|
}
|
|
}
|
|
self.resources.extend(added);
|
|
self.dates.extend(dates);
|
|
Some(expanded)
|
|
}
|
|
}
|