-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Infer multi-file binaries like src/bin/server/main.rs
by convention
#4214
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
10661f3
Infer multi-file binaries like `src/bin/server/main.rs` by convention
5d12c5b
Infer paths as well as names for binaries
msehnout 1a08d2c
"main" can be valid name for binary again, improve test
8833f3d
Prevent unnecessary heap allocations and simplify condition
f08a9d3
Add documentation for multi-file binary convention
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
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 |
---|---|---|
|
@@ -52,6 +52,7 @@ impl Layout { | |
|
||
try_add_file(&mut bins, root_path.join("src").join("main.rs")); | ||
try_add_files(&mut bins, root_path.join("src").join("bin")); | ||
try_add_mains_from_dirs(&mut bins, root_path.join("src").join("bin")); | ||
|
||
try_add_files(&mut examples, root_path.join("examples")); | ||
|
||
|
@@ -74,6 +75,25 @@ fn try_add_file(files: &mut Vec<PathBuf>, file: PathBuf) { | |
files.push(file); | ||
} | ||
} | ||
|
||
// Add directories form src/bin which contain main.rs file | ||
fn try_add_mains_from_dirs(files: &mut Vec<PathBuf>, root: PathBuf) { | ||
if let Ok(new) = fs::read_dir(&root) { | ||
let new: Vec<PathBuf> = new.filter_map(|i| i.ok()) | ||
// Filter only directories | ||
.filter(|i| { | ||
i.file_type().map(|f| f.is_dir()).unwrap_or(false) | ||
// Convert DirEntry into PathBuf and append "main.rs" | ||
}).map(|i| { | ||
i.path().join("main.rs") | ||
// Filter only directories where main.rs is present | ||
}).filter(|f| { | ||
f.as_path().exists() | ||
}).collect(); | ||
files.extend(new); | ||
} | ||
} | ||
|
||
fn try_add_files(files: &mut Vec<PathBuf>, root: PathBuf) { | ||
if let Ok(new) = fs::read_dir(&root) { | ||
files.extend(new.filter_map(|dir| { | ||
|
@@ -505,7 +525,24 @@ fn inferred_bin_targets(name: &str, layout: &Layout) -> Vec<TomlTarget> { | |
*bin == layout.root.join("src").join("main.rs") { | ||
Some(name.to_string()) | ||
} else { | ||
bin.file_stem().and_then(|s| s.to_str()).map(|f| f.to_string()) | ||
// bin is either a source file or a directory with main.rs inside. | ||
if bin.ends_with("main.rs") { | ||
if let Some(parent) = bin.parent() { | ||
// if the path ends with main.rs it is probably a directory, but it can also be | ||
// a file directly inside src/bin | ||
if parent.ends_with("src/bin") { | ||
// This will always return name "main" | ||
bin.file_stem().and_then(|s| s.to_str()).map(|f| f.to_string()) | ||
} else { | ||
parent.file_stem().and_then(|s| s.to_str()).map(|f| f.to_string()) | ||
} | ||
} else { | ||
None | ||
} | ||
} else { | ||
// regular case, just a file in the bin directory | ||
bin.file_stem().and_then(|s| s.to_str()).map(|f| f.to_string()) | ||
} | ||
}; | ||
|
||
name.map(|name| { | ||
|
@@ -1444,7 +1481,7 @@ fn inferred_bin_path(bin: &TomlBinTarget, | |
package_root: &Path, | ||
bin_len: usize) -> PathBuf { | ||
// here we have a single bin, so it may be located in src/main.rs, src/foo.rs, | ||
// srb/bin/foo.rs or src/bin/main.rs | ||
// src/bin/foo.rs, src/bin/foo/main.rs or src/bin/main.rs | ||
if bin_len == 1 { | ||
let path = Path::new("src").join(&format!("main.rs")); | ||
if package_root.join(&path).exists() { | ||
|
@@ -1463,6 +1500,12 @@ fn inferred_bin_path(bin: &TomlBinTarget, | |
return path.to_path_buf() | ||
} | ||
|
||
// check for the case where src/bin/foo/main.rs is present | ||
let path = Path::new("src").join("bin").join(bin.name()).join(&format!("main.rs")); | ||
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. The last bit could be |
||
if package_root.join(&path).exists() { | ||
return path.to_path_buf() | ||
} | ||
|
||
return Path::new("src").join("bin").join(&format!("main.rs")).to_path_buf() | ||
} | ||
|
||
|
@@ -1472,6 +1515,12 @@ fn inferred_bin_path(bin: &TomlBinTarget, | |
return path.to_path_buf() | ||
} | ||
|
||
// we can also have src/bin/foo/main.rs, but the former one is preferred | ||
let path = Path::new("src").join("bin").join(bin.name()).join(&format!("main.rs")); | ||
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. ditto |
||
if package_root.join(&path).exists() { | ||
return path.to_path_buf() | ||
} | ||
|
||
if !has_lib { | ||
let path = Path::new("src").join(&format!("{}.rs", bin.name())); | ||
if package_root.join(&path).exists() { | ||
|
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
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 change this to
bin.ends_with("main.rs") && !bin.ends_with("src/bin/main.rs")
so that we oneif
inside.