-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathlib.rs
438 lines (379 loc) · 14.2 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! # Fornjot Model Host
//!
//! This library is part of the [Fornjot] ecosystem. Fornjot is an open-source,
//! code-first CAD application; and collection of libraries that make up the CAD
//! application, but can be used independently.
//!
//! This library is an internal component of Fornjot. It is not relevant to end
//! users that just want to create CAD models.
//!
//! The purpose of this library is to load Fornjot models and watch them for
//! changes. Fornjot models are basically plugins that can be loaded into a CAD
//! application. This library is the host for these model plugins.
//!
//! [Fornjot]: https://www.fornjot.app/
#![warn(missing_docs)]
mod platform;
use fj_interop::status_report::StatusReport;
use std::{
collections::{HashMap, HashSet},
ffi::OsStr,
io,
ops::{Deref, DerefMut},
path::{Path, PathBuf},
process::Command,
sync::mpsc,
thread,
};
use fj::abi;
use notify::Watcher as _;
use thiserror::Error;
use self::platform::HostPlatform;
/// Represents a Fornjot model
pub struct Model {
src_path: PathBuf,
lib_path: PathBuf,
manifest_path: PathBuf,
}
impl Model {
/// Initialize the model using the path to its crate (i.e. the folder
/// containing `Cargo.toml`).
///
/// Optionally, the target directory where plugin files are compiled to can
/// be provided. If it is not provided, the target directory is assumed to
/// be located within the model path.
pub fn from_path(path: PathBuf) -> Result<Self, Error> {
let crate_dir = path.canonicalize()?;
let metadata = cargo_metadata::MetadataCommand::new()
.current_dir(&crate_dir)
.exec()?;
let pkg = package_associated_with_directory(&metadata, &crate_dir)?;
let src_path = crate_dir.join("src");
let lib_path = {
let name = pkg.name.replace('-', "_");
let file = HostPlatform::lib_file_name(&name);
let target_dir =
metadata.target_directory.clone().into_std_path_buf();
target_dir.join("debug").join(file)
};
Ok(Self {
src_path,
lib_path,
manifest_path: pkg.manifest_path.as_std_path().to_path_buf(),
})
}
/// Load the model once
///
/// The passed arguments are provided to the model. Returns the shape that
/// the model returns.
///
/// Please refer to [`Model::load_and_watch`], if you want to watch the
/// model for changes, reloading it continually.
pub fn load_once(
&self,
arguments: &Parameters,
status: &mut StatusReport,
) -> Result<fj::Shape, Error> {
let manifest_path = self.manifest_path.display().to_string();
let mut command_root = Command::new("cargo");
let command = command_root
.arg("build")
.arg("-q")
.args(["--manifest-path", &manifest_path]);
let exit_status = command.status()?;
if exit_status.success() {
status.update_status("Model compiled successfully!");
} else {
let output = match command.output() {
Ok(output) => {
String::from_utf8(output.stderr).unwrap_or_else(|_| {
String::from("Failed to fetch command output")
})
}
Err(_) => String::from("Failed to fetch command output"),
};
status.update_status(&format!(
"Failed to compile model:\n{}",
output
));
return Err(Error::Compile);
}
// So, strictly speaking this is all unsound:
// - `Library::new` requires us to abide by the arbitrary requirements
// of any library initialization or termination routines.
// - `Library::get` requires us to specify the correct type for the
// model function.
// - The model function itself is `unsafe`, because it is a function
// from across an FFI interface.
//
// Typical models won't have initialization or termination routines (I
// think), should abide by the `ModelFn` signature, and might not do
// anything unsafe. But we have no way to know that the library the user
// told us to load actually does (I think).
//
// I don't know of a way to fix this. We should take this as motivation
// to switch to a better technique:
// https://github.com/hannobraun/Fornjot/issues/71
let shape = unsafe {
let lib = libloading::Library::new(&self.lib_path)?;
let init: libloading::Symbol<abi::InitFunction> =
lib.get(abi::INIT_FUNCTION_NAME.as_bytes())?;
let mut host = Host {
args: arguments,
model: None,
};
match init(&mut abi::Host::from(&mut host)) {
abi::ffi_safe::Result::Ok(_metadata) => {}
abi::ffi_safe::Result::Err(e) => {
return Err(Error::InitializeModel(e.into()));
}
}
let model = host.model.take().ok_or(Error::NoModelRegistered)?;
model.shape(&host).map_err(Error::Shape)?
};
Ok(shape)
}
/// Load the model, then watch it for changes
///
/// Whenever a change is detected, the model is being reloaded.
///
/// Consumes this instance of `Model` and returns a [`Watcher`], which can
/// be queried for changes to the model.
pub fn load_and_watch(
self,
parameters: Parameters,
) -> Result<Watcher, Error> {
let (tx, rx) = mpsc::sync_channel(0);
let tx2 = tx.clone();
let watch_path = self.src_path.clone();
let mut watcher = notify::recommended_watcher(
move |event: notify::Result<notify::Event>| {
// Unfortunately the `notify` documentation doesn't say when
// this might happen, so no idea if it needs to be handled.
let event = event.expect("Error handling watch event");
// Various acceptable ModifyKind kinds. Varies across platforms
// (e.g. MacOs vs. Windows10)
if let notify::EventKind::Modify(
notify::event::ModifyKind::Any,
)
| notify::EventKind::Modify(
notify::event::ModifyKind::Data(
notify::event::DataChange::Any,
),
)
| notify::EventKind::Modify(
notify::event::ModifyKind::Data(
notify::event::DataChange::Content,
),
) = event.kind
{
let file_ext = event
.paths
.get(0)
.expect("File path missing in watch event")
.extension();
let black_list = HashSet::from([
OsStr::new("swp"),
OsStr::new("tmp"),
OsStr::new("swx"),
]);
if let Some(ext) = file_ext {
if black_list.contains(ext) {
return;
}
}
// This will panic, if the other end is disconnected, which
// is probably the result of a panic on that thread, or the
// application is being shut down.
//
// Either way, not much we can do about it here.
tx.send(()).expect("Channel is disconnected");
}
},
)?;
watcher.watch(&watch_path, notify::RecursiveMode::Recursive)?;
// To prevent a race condition between the initial load and the start of
// watching, we'll trigger the initial load here, after having started
// watching.
//
// Will panic, if the receiving end has panicked. Not much we can do
// about that, if it happened.
thread::spawn(move || tx2.send(()).expect("Channel is disconnected"));
Ok(Watcher {
_watcher: Box::new(watcher),
channel: rx,
model: self,
parameters,
})
}
}
fn package_associated_with_directory<'m>(
metadata: &'m cargo_metadata::Metadata,
dir: &Path,
) -> Result<&'m cargo_metadata::Package, Error> {
for pkg in metadata.workspace_packages() {
let crate_dir = pkg
.manifest_path
.parent()
.and_then(|p| p.canonicalize().ok());
if crate_dir.as_deref() == Some(dir) {
return Ok(pkg);
}
}
Err(ambiguous_path_error(metadata, dir))
}
fn ambiguous_path_error(
metadata: &cargo_metadata::Metadata,
dir: &Path,
) -> Error {
let mut possible_paths = Vec::new();
for id in &metadata.workspace_members {
let cargo_toml = &metadata[id].manifest_path;
let crate_dir = cargo_toml
.parent()
.expect("A Cargo.toml always has a parent");
// Try to make the path relative to the workspace root so error messages
// aren't super long.
let simplified_path = crate_dir
.strip_prefix(&metadata.workspace_root)
.unwrap_or(crate_dir);
possible_paths.push(simplified_path.into());
}
Error::AmbiguousPath {
dir: dir.to_path_buf(),
possible_paths,
}
}
/// Watches a model for changes, reloading it continually
pub struct Watcher {
_watcher: Box<dyn notify::Watcher>,
channel: mpsc::Receiver<()>,
model: Model,
parameters: Parameters,
}
impl Watcher {
/// Receive an updated shape that the reloaded model created
///
/// Returns `None`, if the model has not changed since the last time this
/// method was called.
pub fn receive(&self, status: &mut StatusReport) -> Option<fj::Shape> {
match self.channel.try_recv() {
Ok(()) => {
let shape = match self.model.load_once(&self.parameters, status)
{
Ok(shape) => shape,
Err(Error::Compile) => {
// It would be better to display an error in the UI,
// where the user can actually see it. Issue:
// https://github.com/hannobraun/fornjot/issues/30
return None;
}
Err(err) => {
panic!("Error reloading model: {:?}", err);
}
};
Some(shape)
}
Err(mpsc::TryRecvError::Empty) => {
// Nothing to receive from the channel.
None
}
Err(mpsc::TryRecvError::Disconnected) => {
// The other end has disconnected. This is probably the result
// of a panic on the other thread, or a program shutdown in
// progress. In any case, not much we can do here.
panic!();
}
}
}
}
/// Parameters that are passed to a model.
#[derive(Debug, Clone, PartialEq)]
pub struct Parameters(pub HashMap<String, String>);
impl Parameters {
/// Construct an empty instance of `Parameters`
pub fn empty() -> Self {
Self(HashMap::new())
}
/// Insert a value into the [`Parameters`] dictionary, implicitly converting
/// the arguments to strings and returning `&mut self` to enable chaining.
pub fn insert(
&mut self,
key: impl Into<String>,
value: impl ToString,
) -> &mut Self {
self.0.insert(key.into(), value.to_string());
self
}
}
impl Deref for Parameters {
type Target = HashMap<String, String>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Parameters {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
/// An error that can occur when loading or reloading a model
#[derive(Debug, Error)]
pub enum Error {
/// Model failed to compile
#[error("Error compiling model")]
Compile,
/// I/O error while loading the model
#[error("I/O error while loading model")]
Io(#[from] io::Error),
/// Failed to load the model's dynamic library
#[error("Error loading model from dynamic library")]
LibLoading(#[from] libloading::Error),
/// Initializing a model failed.
#[error("Unable to initialize the model")]
InitializeModel(#[source] fj::models::Error),
/// The user forgot to register a model when calling
/// [`fj::register_model!()`].
#[error("No model was registered")]
NoModelRegistered,
/// An error was returned from [`fj::models::Model::shape()`].
#[error("Unable to determine the model's geometry")]
Shape(#[source] fj::models::Error),
/// Error while watching the model code for changes
#[error("Error watching model for changes")]
Notify(#[from] notify::Error),
/// An error occurred while trying to use evaluate
/// [`cargo_metadata::MetadataCommand`].
#[error("Unable to determine the crate's metadata")]
CargoMetadata(#[from] cargo_metadata::Error),
/// The user pointed us to a directory, but it doesn't look like that was
/// a crate root (i.e. the folder containing `Cargo.toml`).
#[error(
"It doesn't look like \"{}\" is a crate directory. Did you mean one of {}?",
dir.display(),
possible_paths.iter().map(|p| p.display().to_string())
.collect::<Vec<_>>()
.join(", ")
)]
AmbiguousPath {
/// The model directory supplied by the user.
dir: PathBuf,
/// The directories for each crate in the workspace, relative to the
/// workspace root.
possible_paths: Vec<PathBuf>,
},
}
struct Host<'a> {
args: &'a Parameters,
model: Option<Box<dyn fj::models::Model>>,
}
impl<'a> fj::models::Host for Host<'a> {
fn register_boxed_model(&mut self, model: Box<dyn fj::models::Model>) {
self.model = Some(model);
}
}
impl<'a> fj::models::Context for Host<'a> {
fn get_argument(&self, name: &str) -> Option<&str> {
self.args.get(name).map(|s| s.as_str())
}
}