Skip to content

Commit b8b5731

Browse files
committed
Added --manifest-path command line option.
Closes #261.
1 parent bb0fa3f commit b8b5731

11 files changed

Lines changed: 101 additions & 11 deletions

File tree

src/lib.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,23 @@ pub struct Project {
5454
impl Project {
5555
/// Creates a [`Project`] the current directory. It will search ancestor paths until it finds
5656
/// the root of the project.
57-
pub fn from_current_dir() -> Result<Project, ProjectError> {
58-
let metadata = Project::get_cargo_metadata()?;
57+
pub fn from_current_dir(manifest_path: Option<&Path>) -> Result<Project, ProjectError> {
58+
let metadata = Project::get_cargo_metadata(manifest_path)?;
5959
let package = metadata.root_package().ok_or(ProjectError::ProjectHasNoRootPackage)?;
6060

6161
Ok(Project::from_package(package))
6262
}
6363

64-
fn get_cargo_metadata() -> Result<cargo_metadata::Metadata, ProjectError> {
65-
Ok(cargo_metadata::MetadataCommand::new().exec()?)
64+
fn get_cargo_metadata(
65+
manifest_path: Option<&Path>,
66+
) -> Result<cargo_metadata::Metadata, ProjectError> {
67+
let mut command = cargo_metadata::MetadataCommand::new();
68+
69+
if let Some(manifest_path) = manifest_path {
70+
command.manifest_path(manifest_path);
71+
}
72+
73+
Ok(command.exec()?)
6674
}
6775

6876
fn select_package<'a>(
@@ -74,8 +82,11 @@ impl Project {
7482
})
7583
}
7684

77-
pub fn from_current_dir_workspace_project(project_name: &str) -> Result<Project, ProjectError> {
78-
let metadata = Project::get_cargo_metadata()?;
85+
pub fn from_current_dir_workspace_project(
86+
manifest_path: Option<&Path>,
87+
project_name: &str,
88+
) -> Result<Project, ProjectError> {
89+
let metadata = Project::get_cargo_metadata(manifest_path)?;
7990

8091
let package = Project::select_package(&metadata, project_name)
8192
.ok_or_else(|| ProjectError::ProjectHasNoPackage(project_name.to_owned()))?;

src/main.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,10 @@ fn update_readme(
436436

437437
fn run(options: options::Options) -> Result<(), RunError> {
438438
let project: Project = match options.workspace_project {
439-
None => Project::from_current_dir()?,
440-
Some(ref project) => Project::from_current_dir_workspace_project(project)?,
439+
None => Project::from_current_dir(options.manifest_path.as_deref())?,
440+
Some(ref project) => {
441+
Project::from_current_dir_workspace_project(options.manifest_path.as_deref(), project)?
442+
}
441443
};
442444
let entryfile: &Path =
443445
entrypoint(&project, &options.entrypoint).ok_or(RunError::NoEntrySourceFile)?;
@@ -495,8 +497,13 @@ fn run(options: options::Options) -> Result<(), RunError> {
495497
fn main() {
496498
let cmd_options = options::cmd_options();
497499

498-
let exit_code: ExitCode = match std::env::current_dir() {
499-
Ok(current_dir) => match options::config_file_options(current_dir) {
500+
let directory = cmd_options
501+
.manifest_path()
502+
.and_then(|path| path.parent())
503+
.map_or_else(std::env::current_dir, |p| Ok(p.to_owned()));
504+
505+
let exit_code: ExitCode = match directory {
506+
Ok(dir) => match options::config_file_options(dir) {
500507
Ok(config_file_options) => {
501508
let options = options::merge_options(cmd_options, config_file_options);
502509

src/options.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,16 @@ pub struct CmdOptions {
8181
intralinks_strip_links: bool,
8282
force: bool,
8383
readme_path: Option<PathBuf>,
84+
manifest_path: Option<PathBuf>,
8485
heading_base_level: Option<u8>,
8586
}
8687

88+
impl CmdOptions {
89+
pub fn manifest_path(&self) -> Option<&Path> {
90+
self.manifest_path.as_deref()
91+
}
92+
}
93+
8794
fn get_cmd_args() -> Vec<OsString> {
8895
let mut args: Vec<OsString> = std::env::args_os().collect();
8996
let subcommand: &str = {
@@ -127,7 +134,13 @@ pub fn cmd_options() -> CmdOptions {
127134
Arg::new("readme-path")
128135
.long("readme-path")
129136
.short('r')
130-
.help("README file path to use (overrides of what is specified in the project `Cargo.toml`)")
137+
.help("README file path to use (overrides what is specified in the project `Cargo.toml`)")
138+
.value_parser(value_parser!(PathBuf)),
139+
)
140+
.arg(
141+
Arg::new("manifest-path")
142+
.long("manifest-path")
143+
.help("path to `Cargo.toml`")
131144
.value_parser(value_parser!(PathBuf)),
132145
)
133146
.arg(
@@ -178,6 +191,7 @@ pub fn cmd_options() -> CmdOptions {
178191
let entrypoint = cmd_opts.get_one::<EntrypointOpt>("entrypoint").cloned();
179192

180193
let readme_path = cmd_opts.get_one::<PathBuf>("readme-path").cloned();
194+
let manifest_path = cmd_opts.get_one::<PathBuf>("manifest-path").cloned();
181195

182196
let heading_base_level = cmd_opts.get_one::<u8>("heading-base-level").copied();
183197

@@ -190,6 +204,7 @@ pub fn cmd_options() -> CmdOptions {
190204
intralinks_strip_links: cmd_opts.get_flag("intralinks-strip-links"),
191205
force: cmd_opts.get_flag("force"),
192206
readme_path,
207+
manifest_path,
193208
heading_base_level,
194209
}
195210
}
@@ -314,6 +329,7 @@ pub struct Options {
314329
pub no_fail_on_warnings: bool,
315330
pub force: bool,
316331
pub readme_path: Option<PathBuf>,
332+
pub manifest_path: Option<PathBuf>,
317333
pub intralinks: Option<IntralinksConfig>,
318334
pub heading_base_level: Option<u8>,
319335
}
@@ -343,6 +359,7 @@ pub fn merge_options(
343359
readme_path: cmd_options
344360
.readme_path
345361
.or_else(|| config_file_options.as_mut().and_then(|c| c.readme_path.take())),
362+
manifest_path: cmd_options.manifest_path,
346363
intralinks: Some(IntralinksConfig {
347364
docs_rs: IntralinksDocsRsConfig {
348365
docs_rs_base_url: config_file_options
@@ -425,6 +442,7 @@ mod tests {
425442
intralinks_strip_links: true,
426443
force: true,
427444
readme_path: Some(PathBuf::from("rEaDmE.md")),
445+
manifest_path: Some(PathBuf::from("foo/Cargo.toml")),
428446
heading_base_level: Some(4),
429447
};
430448
let config_file_options = ConfigFileOptions {
@@ -452,6 +470,7 @@ mod tests {
452470
no_fail_on_warnings: true,
453471
force: true,
454472
readme_path: Some(PathBuf::from("rEaDmE.md")),
473+
manifest_path: Some(PathBuf::from("foo/Cargo.toml")),
455474
intralinks: Some(IntralinksConfig {
456475
docs_rs: IntralinksDocsRsConfig {
457476
docs_rs_base_url: Some("https://internaldocs.rs".to_owned()),
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[intralinks]
2+
docs-rs-base-url = "https://this-should-not-be-used.rs"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
header
2+
3+
<!-- cargo-rdme start -->
4+
5+
This [beautiful crate](https://mydocs.rs/integration_test/latest/integration_test/) is cool because it contains [modules](https://mydocs.rs/integration_test/latest/integration_test/amodule/) and may use
6+
[copy](https://doc.rust-lang.org/stable/std/fs/fn.copy.html).
7+
8+
<!-- cargo-rdme end -->
9+
10+
footer
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
header
2+
3+
<!-- cargo-rdme start -->
4+
5+
Some old text here.
6+
7+
<!-- cargo-rdme end -->
8+
9+
footer
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[intralinks]
2+
docs-rs-base-url = "https://mydocs.rs"

tests/option_cmd_manifest_path/foo/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "integration_test"
3+
version = "0.1.0"
4+
edition = "2021"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//! This [beautiful crate](crate) is cool because it contains [modules](crate::amodule) and may use
2+
//! [copy](::std::fs::copy).
3+
4+
mod amodule {}
5+
6+
fn main() {}

0 commit comments

Comments
 (0)