Edit multiple project buffers in the native preview
This commit is contained in:
1 parent
b5e87a8706
commit
f6c493e462
4 files changed
+150
-22
No files matched your search
@@ -5,7 +5,7 @@ use std::{
|
||||
collections::{BTreeMap, BTreeSet},
|
||||
fs,
|
||||
io::Read,
|
||||
path::{Path, PathBuf},
|
||||
path::{Component, Path, PathBuf},
|
||||
sync::{
|
||||
Arc, Condvar, Mutex,
|
||||
atomic::{AtomicU64, Ordering},
|
||||
@@ -28,7 +28,9 @@ const SCRIPT: &str = include_str!("app.js");
|
||||
struct Edit {
|
||||
sequence: u64,
|
||||
visible_revision: Option<u64>,
|
||||
source: String,
|
||||
source: Option<String>,
|
||||
#[serde(default)]
|
||||
files: BTreeMap<String, String>,
|
||||
#[serde(default)]
|
||||
preview_draft: bool,
|
||||
}
|
||||
@@ -168,7 +170,7 @@ struct Queue {
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut args = std::env::args_os().skip(1).map(PathBuf::from);
|
||||
let usage = "native_editor SOURCE FORMAT FORMAT_RESOURCES NEW_RUN_DIR [PORT]";
|
||||
let usage = "native_editor SOURCE FORMAT FORMAT_RESOURCES NEW_RUN_DIR [PORT] [FILE ...]";
|
||||
let source = args.next().ok_or(usage)?.canonicalize()?;
|
||||
let format = args.next().ok_or(usage)?.canonicalize()?;
|
||||
let manifest = args.next().ok_or(usage)?;
|
||||
@@ -178,12 +180,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.map(|s| s.to_string_lossy().parse())
|
||||
.transpose()?
|
||||
.unwrap_or(0u16);
|
||||
if args.next().is_some() {
|
||||
return Err(usage.into());
|
||||
}
|
||||
let additional = args.collect::<Vec<_>>();
|
||||
fs::create_dir(&run)?;
|
||||
let run = run.canonicalize()?;
|
||||
let (mut project, entry, options) = prepare(&source, &format, &manifest, &run)?;
|
||||
let EditorProject {
|
||||
mut project,
|
||||
entry,
|
||||
editable,
|
||||
options,
|
||||
} = prepare(&source, &format, &manifest, &run, &additional)?;
|
||||
let compiler = NativeCompiler::default().with_automatic_entry_checkpoints();
|
||||
let server = Server::http(("127.0.0.1", port)).map_err(|e| e.to_string())?;
|
||||
let queue = Arc::new(Queue {
|
||||
@@ -207,12 +212,22 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
),
|
||||
(&Method::Get, "/project") => {
|
||||
let file = project.file(&entry).expect("the editor owns its entry");
|
||||
let files = editable
|
||||
.iter()
|
||||
.map(|(name, path)| {
|
||||
let file = project
|
||||
.file(path)
|
||||
.expect("editable project files are immutable");
|
||||
Ok((name, std::str::from_utf8(&file.data)?))
|
||||
})
|
||||
.collect::<Result<BTreeMap<_, _>, std::str::Utf8Error>>()?;
|
||||
reply(
|
||||
request,
|
||||
200,
|
||||
json!({
|
||||
"name": entry.file_name().unwrap().to_string_lossy(),
|
||||
"source": std::str::from_utf8(&file.data)?,
|
||||
"files": files,
|
||||
"sequence": queue.current.load(Ordering::Acquire),
|
||||
}),
|
||||
);
|
||||
@@ -235,10 +250,26 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
);
|
||||
continue;
|
||||
}
|
||||
let next = project.with_file_edits(
|
||||
edit.sequence,
|
||||
BTreeMap::from([(entry.clone(), Arc::from(edit.source.into_bytes()))]),
|
||||
);
|
||||
let updates = edit
|
||||
.files
|
||||
.into_iter()
|
||||
.map(|(name, value)| {
|
||||
let path = editable
|
||||
.get(&name)
|
||||
.ok_or_else(|| format!("File is not editable: {name}"))?;
|
||||
Ok((path.clone(), Arc::from(value.into_bytes())))
|
||||
})
|
||||
.collect::<Result<BTreeMap<_, _>, String>>();
|
||||
let next = updates.and_then(|mut updates| {
|
||||
if let Some(source) = edit.source
|
||||
&& updates
|
||||
.insert(entry.clone(), Arc::from(source.into_bytes()))
|
||||
.is_some()
|
||||
{
|
||||
return Err("The entry file was edited twice".into());
|
||||
}
|
||||
project.with_file_edits(edit.sequence, updates)
|
||||
});
|
||||
project = match next {
|
||||
Ok(project) => project,
|
||||
Err(error) => {
|
||||
@@ -339,20 +370,66 @@ fn read_edit(request: &mut Request) -> Result<Edit, String> {
|
||||
serde_json::from_slice(&body).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
struct EditorProject {
|
||||
project: Arc<ProjectSnapshot>,
|
||||
entry: PathBuf,
|
||||
editable: BTreeMap<String, PathBuf>,
|
||||
options: CompileRequest,
|
||||
}
|
||||
|
||||
fn prepare(
|
||||
source: &Path,
|
||||
format: &Path,
|
||||
manifest: &Path,
|
||||
run: &Path,
|
||||
) -> Result<(Arc<ProjectSnapshot>, PathBuf, CompileRequest), Box<dyn std::error::Error>> {
|
||||
additional: &[PathBuf],
|
||||
) -> Result<EditorProject, Box<dyn std::error::Error>> {
|
||||
let format_resources: Value = serde_json::from_slice(&fs::read(manifest)?)?;
|
||||
let roots: Vec<PathBuf> = serde_json::from_value(format_resources["texmf_roots"].clone())?;
|
||||
let mut world = WorldConfig::from_environment(run, roots.clone())?;
|
||||
world.input_policy = InputPolicy::Tex;
|
||||
let entry = run.join(source.file_name().ok_or("Source needs a filename")?);
|
||||
let name = source
|
||||
.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.ok_or("Source needs a UTF-8 filename")?;
|
||||
let entry = run.join(name);
|
||||
let original = fs::read(source)?;
|
||||
std::str::from_utf8(&original)?;
|
||||
fs::write(&entry, &original)?;
|
||||
let mut editable = BTreeMap::from([(name.to_owned(), entry.clone())]);
|
||||
let mut files = BTreeMap::<PathBuf, Arc<[u8]>>::from([(entry.clone(), Arc::from(original))]);
|
||||
let source_root = source.parent().expect("the entry has a parent");
|
||||
for relative in additional {
|
||||
if relative.as_os_str().is_empty()
|
||||
|| relative
|
||||
.components()
|
||||
.any(|item| !matches!(item, Component::Normal(_)))
|
||||
{
|
||||
return Err(format!(
|
||||
"Project file must be relative to the entry directory: {}",
|
||||
relative.display()
|
||||
)
|
||||
.into());
|
||||
}
|
||||
let logical = run.join(relative);
|
||||
let physical = source_root.join(relative).canonicalize()?;
|
||||
if !physical.starts_with(source_root) || files.contains_key(&logical) {
|
||||
return Err(format!(
|
||||
"Project file is outside the entry directory or is listed twice: {}",
|
||||
relative.display()
|
||||
)
|
||||
.into());
|
||||
}
|
||||
let data: Arc<[u8]> = Arc::from(fs::read(physical)?);
|
||||
fs::create_dir_all(logical.parent().expect("project file has a parent"))?;
|
||||
fs::write(&logical, &data)?;
|
||||
if let Some(name) = relative.to_str()
|
||||
&& std::str::from_utf8(&data).is_ok()
|
||||
{
|
||||
editable.insert(name.into(), logical.clone());
|
||||
}
|
||||
files.insert(logical, data);
|
||||
}
|
||||
let mut request = CompileRequest::new(
|
||||
world.clone(),
|
||||
&entry,
|
||||
@@ -366,7 +443,6 @@ fn prepare(
|
||||
// Provision the finite, immutable resource bundle used by this trial.
|
||||
// The seed uses the same native compiler and actual package bytes.
|
||||
let seed = NativeCompiler::default().compile(request.clone())?;
|
||||
let mut files = BTreeMap::from([(entry.clone(), Arc::from(original))]);
|
||||
for (path, captured) in seed.inputs.files() {
|
||||
if !path.starts_with(run) {
|
||||
files.insert(path.to_owned(), captured?.data.clone());
|
||||
@@ -404,7 +480,12 @@ fn prepare(
|
||||
generated_roots: BTreeSet::from([run.to_owned()]),
|
||||
generated,
|
||||
})?;
|
||||
Ok((project, entry, request))
|
||||
Ok(EditorProject {
|
||||
project,
|
||||
entry,
|
||||
editable,
|
||||
options: request,
|
||||
})
|
||||
}
|
||||
|
||||
fn worker(queue: &Arc<Queue>) {
|
||||
|
||||
Reference in new issue
Block a user