-
-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use the "cargo metadata" command instead of manipulating paths directly #828
Merged
hannobraun
merged 2 commits into
hannobraun:main
from
Michael-F-Bryan:hotfix/target-dir
Jul 18, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ use std::{ | |
collections::{HashMap, HashSet}, | ||
ffi::OsStr, | ||
io, | ||
path::PathBuf, | ||
path::{Path, PathBuf}, | ||
process::Command, | ||
sync::mpsc, | ||
thread, | ||
|
@@ -40,7 +40,8 @@ pub struct Model { | |
} | ||
|
||
impl Model { | ||
/// Initialize the model from a path | ||
/// 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 | ||
|
@@ -49,31 +50,28 @@ impl Model { | |
path: PathBuf, | ||
target_dir: Option<PathBuf>, | ||
) -> io::Result<Self> { | ||
let name = { | ||
// Can't panic. It only would, if the path ends with "..", and we | ||
// are canonicalizing it here to prevent that. | ||
let canonical = path.canonicalize()?; | ||
let file_name = canonical | ||
.file_name() | ||
.expect("Expected path to be canonical"); | ||
|
||
file_name.to_string_lossy().replace('-', "_") | ||
}; | ||
let crate_dir = path.canonicalize()?; | ||
|
||
let metadata = cargo_metadata::MetadataCommand::new() | ||
.current_dir(&crate_dir) | ||
.exec() | ||
.map_err(metadata_error_to_io)?; | ||
|
||
let src_path = path.join("src"); | ||
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 = target_dir.unwrap_or_else(|| path.join("target")); | ||
let target_dir = target_dir | ||
.unwrap_or_else(|| metadata.target_directory.clone().into()); | ||
target_dir.join("debug").join(file) | ||
}; | ||
|
||
let manifest_path = path.join("Cargo.toml"); | ||
|
||
Ok(Self { | ||
src_path, | ||
lib_path, | ||
manifest_path, | ||
manifest_path: pkg.manifest_path.as_std_path().to_path_buf(), | ||
}) | ||
} | ||
|
||
|
@@ -208,6 +206,33 @@ impl Model { | |
} | ||
} | ||
|
||
fn metadata_error_to_io(e: cargo_metadata::Error) -> std::io::Error { | ||
match e { | ||
cargo_metadata::Error::Io(io) => io, | ||
_ => std::io::Error::new(io::ErrorKind::Other, e), | ||
} | ||
} | ||
|
||
fn package_associated_with_directory<'m>( | ||
metadata: &'m cargo_metadata::Metadata, | ||
dir: &Path, | ||
) -> Result<&'m cargo_metadata::Package, io::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); | ||
} | ||
} | ||
|
||
let msg = format!("\"{}\" doesn't point to a crate", dir.display()); | ||
|
||
Err(io::Error::new(io::ErrorKind::Other, msg)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use a more appropriate custom error type here. We can probably reuse whatever |
||
} | ||
|
||
/// Watches a model for changes, reloading it continually | ||
pub struct Watcher { | ||
_watcher: Box<dyn notify::Watcher>, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove this conversion. The function doesn't need to return
io::Error
. It can return a custom error type that covers all its failure modes. (The crate already depends onthiserror
, so we don't even need an additional dependency.)