Capture editor file dates and make desktop ZIP import observable
This commit is contained in:
1 parent
0ea71d57a9
commit
fbc339f5c4
11 files changed
+231
-72
No files matched your search
@@ -4,13 +4,13 @@ use serde_json::{Value, json};
|
||||
use std::{
|
||||
collections::{BTreeMap, BTreeSet},
|
||||
fs,
|
||||
io::Read,
|
||||
io::{Read, Write},
|
||||
path::{Component, Path, PathBuf},
|
||||
sync::{
|
||||
Arc, Condvar, Mutex, mpsc,
|
||||
atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering},
|
||||
},
|
||||
time::Instant,
|
||||
time::{Instant, SystemTime},
|
||||
};
|
||||
use texrra_engine::native::{
|
||||
CheckpointTimings, CompileError, CompileJob, CompileObserver, CompileRequest, CompileStage,
|
||||
@@ -20,7 +20,7 @@ use texrra_engine::native::{
|
||||
};
|
||||
use texrra_tex_core::{
|
||||
execution::InputProfile,
|
||||
resources::{InputPolicy, ResourceKind, SearchPath, WorldConfig},
|
||||
resources::{FileModificationTime, InputPolicy, ResourceKind, SearchPath, WorldConfig},
|
||||
};
|
||||
use thread_usage::ThreadUsage;
|
||||
use tiny_http::{Header, Method, Request, Response, Server};
|
||||
@@ -761,6 +761,21 @@ struct EditorProject {
|
||||
options: CompileRequest,
|
||||
}
|
||||
|
||||
fn copy_with_date(
|
||||
path: &Path,
|
||||
bytes: &[u8],
|
||||
modified: Option<SystemTime>,
|
||||
dates: &mut BTreeMap<PathBuf, FileModificationTime>,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut copy = fs::File::create(path)?;
|
||||
copy.write_all(bytes)?;
|
||||
if let Some(modified) = modified {
|
||||
copy.set_times(fs::FileTimes::new().set_modified(modified))?;
|
||||
dates.insert(path.to_owned(), FileModificationTime::from_system_time(modified)?);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn prepare(
|
||||
source_root: &Path,
|
||||
source: &Path,
|
||||
@@ -788,9 +803,10 @@ fn prepare(
|
||||
.or_insert_with(|| vec![SearchPath::Defaults])
|
||||
.insert(0, SearchPath::directory(entry_dir, false));
|
||||
}
|
||||
let original = fs::read(source)?;
|
||||
let mut dates = BTreeMap::new();
|
||||
let (original, modified) = resource_capture::read_with_date(source)?;
|
||||
std::str::from_utf8(&original)?;
|
||||
fs::write(&entry, &original)?;
|
||||
copy_with_date(&entry, &original, modified, &mut dates)?;
|
||||
let mut editable = BTreeMap::from([(name.to_owned(), entry.clone())]);
|
||||
let mut files = BTreeMap::<PathBuf, Arc<[u8]>>::from([(entry.clone(), Arc::from(original))]);
|
||||
let mut declared = BTreeSet::from([entry.clone()]);
|
||||
@@ -812,8 +828,8 @@ fn prepare(
|
||||
return Err(format!("Project file is listed twice: {}", relative.display()).into());
|
||||
}
|
||||
let physical = source_root.join(relative);
|
||||
let data = match physical.canonicalize() {
|
||||
Ok(path) if path.starts_with(source_root) => Some(Arc::<[u8]>::from(fs::read(path)?)),
|
||||
let captured = match physical.canonicalize() {
|
||||
Ok(path) if path.starts_with(source_root) => Some(resource_capture::read_with_date(&path)?),
|
||||
Ok(_) => {
|
||||
return Err(format!(
|
||||
"Project file is outside the entry directory: {}",
|
||||
@@ -842,15 +858,15 @@ fn prepare(
|
||||
.map(Path::to_owned),
|
||||
);
|
||||
if let Some(name) = relative.to_str()
|
||||
&& data
|
||||
.as_deref()
|
||||
.is_none_or(|bytes| std::str::from_utf8(bytes).is_ok())
|
||||
&& captured
|
||||
.as_ref()
|
||||
.is_none_or(|(bytes, _)| std::str::from_utf8(bytes).is_ok())
|
||||
{
|
||||
editable.insert(name.into(), logical.clone());
|
||||
}
|
||||
if let Some(data) = data {
|
||||
fs::write(&logical, &data)?;
|
||||
files.insert(logical, data);
|
||||
if let Some((data, modified)) = captured {
|
||||
copy_with_date(&logical, &data, modified, &mut dates)?;
|
||||
files.insert(logical, Arc::from(data));
|
||||
}
|
||||
}
|
||||
let mut output = entry.with_extension("pdf");
|
||||
@@ -897,6 +913,24 @@ fn prepare(
|
||||
files.insert(path.clone(), Arc::from(fs::read(path)?));
|
||||
}
|
||||
}
|
||||
// Keep timestamps already observed by the seed and provide dates for
|
||||
// external files which may first be queried after an edit.
|
||||
if let Some(trace) = trace {
|
||||
for item in &trace.file_modifications {
|
||||
if let Ok(Some(date)) = &item.result
|
||||
&& files.contains_key(&item.path)
|
||||
{
|
||||
dates.entry(item.path.clone()).or_insert(date.time);
|
||||
}
|
||||
}
|
||||
}
|
||||
for path in files.keys().filter(|path| !path.starts_with(run)) {
|
||||
if !dates.contains_key(path)
|
||||
&& let Ok(modified) = fs::metadata(path).and_then(|m| m.modified())
|
||||
{
|
||||
dates.insert(path.clone(), FileModificationTime::from_system_time(modified)?);
|
||||
}
|
||||
}
|
||||
let mut coverage = BTreeSet::from_iter(world.texmf_roots.iter().cloned());
|
||||
coverage.insert(run.to_owned());
|
||||
// A successful lookup can follow a miss in an explicit search directory.
|
||||
@@ -919,7 +953,7 @@ fn prepare(
|
||||
.flat_map(|artifacts| &artifacts.writes)
|
||||
.map(|write| write.path.clone())
|
||||
.collect();
|
||||
let project = ProjectSnapshot::new(ProjectDefinition {
|
||||
let project = ProjectSnapshot::with_file_modifications(ProjectDefinition {
|
||||
configuration: world,
|
||||
revision: 1,
|
||||
roots: coverage,
|
||||
@@ -928,7 +962,7 @@ fn prepare(
|
||||
aliases: BTreeMap::new(),
|
||||
generated_roots: BTreeSet::from([run.to_owned()]),
|
||||
generated,
|
||||
})?;
|
||||
}, dates)?;
|
||||
Ok(EditorProject {
|
||||
project,
|
||||
entry,
|
||||
|
||||
Reference in new issue
Block a user