Skip to content

Commit 2e0c0a9

Browse files
committed
CLI args defaults from cargo metadata
First attempt at providing defaults for CLI arguments in cargo manifest metadata.
1 parent 9cb2aba commit 2e0c0a9

File tree

6 files changed

+76
-3
lines changed

6 files changed

+76
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# cargo-docset changelog
22

3+
## Unreleased - v0.4.0
4+
* Feature: add the ability to specify defaults for a subset of the CLI arguments in the cargo metadata for a crate.
5+
36
## 9/26/2022 - v0.3.1
47

58
* Bugfix: update the crate version in Cargo.lock (thanks @antifuchs)

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ clap-cargo = { version = "0.9", features = ["cargo_metadata"] }
2626
clap = { version = "3.2", features = ["std", "suggestions", "derive"], default_features = false }
2727
derive_more = "0.99"
2828
rusqlite = "0.28"
29+
serde = { version = "1.0", features = ["derive"] }
30+
serde_json = "1.0"
2931
snafu = "0.7"
3032
termcolor = { version = "1.1", optional = true }

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ Just run `cargo docset` in your crate's directory to generate the docset. It wil
2121
directory. cargo-docset generally supports the same options as `cargo doc`, with a few additional ones. For more
2222
information, run `cargo docset --help` or look below in this README.
2323

24+
Most arguments accepted by the CLI can also be given default values for your crate by adding them as cargo
25+
[workspace metadata](https://doc.rust-lang.org/cargo/reference/workspaces.html#the-workspacemetadata-table);
26+
Arguments provided in a CLI invocation will take precedence over such defaults. For CLI flags that don't take a value,
27+
use the `true` value in the metadata to signify the presence of the flag. The following keys are supported (please
28+
refer to the CLI documentation for what each option does): `features`, `no-deps`, `document-private-items`, `target`,
29+
`lib`, `bin`, `bins`, `docset-name`, `docset-index`, `platform-family`.
30+
2431
To install your shiny new docset, copy it to your Zeal/Dash docset directory (available in the preferences, on Zeal at
2532
least) and restart Zeal/Dash.
2633

src/commands/generate.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Implementation of the `docset` subcommand.
22
3-
use crate::{error::*, io::*, DocsetParams};
3+
use crate::{error::*, io::*, DocsetParams, WorkspaceMetadata};
44

55
use cargo_metadata::Metadata;
66
use derive_more::Constructor;
@@ -383,7 +383,7 @@ fn get_docset_platform_family(cfg: &DocsetParams, metadata: &Metadata) -> Option
383383
}
384384
}
385385

386-
pub fn generate_docset(cfg: DocsetParams) -> Result<()> {
386+
pub fn generate_docset(mut cfg: DocsetParams) -> Result<()> {
387387
// Step 1: generate rustdoc
388388
// Figure out for which crate to build the doc and invoke cargo doc.
389389
// If no crate is specified, run cargo doc for the current crate/workspace.
@@ -396,7 +396,50 @@ pub fn generate_docset(cfg: DocsetParams) -> Result<()> {
396396
);
397397
}
398398

399+
// Merge the options specified in cargo metadata. Options specified on the CLI take precedence.
399400
let cargo_metadata = cfg.manifest.metadata().exec().context(CargoMetadataSnafu)?;
401+
let workspace_metadata_res = serde_json::from_value::<WorkspaceMetadata>(cargo_metadata.workspace_metadata.clone());
402+
if let Ok(workspace_metadata) = workspace_metadata_res {
403+
if !cfg.features.all_features && cfg.features.features.is_empty() && workspace_metadata.features.is_some() {
404+
cfg.features.features = workspace_metadata.features.unwrap();
405+
}
406+
407+
if !cfg.no_dependencies && workspace_metadata.no_deps.unwrap_or(false) {
408+
cfg.no_dependencies = true;
409+
}
410+
411+
if !cfg.doc_private_items && workspace_metadata.document_private_items.unwrap_or(false) {
412+
cfg.doc_private_items = true;
413+
}
414+
415+
if cfg.target.is_none() && workspace_metadata.target.is_some() {
416+
cfg.target = workspace_metadata.target;
417+
}
418+
419+
if !cfg.lib && workspace_metadata.lib.unwrap_or(false) {
420+
cfg.lib = true;
421+
}
422+
423+
if cfg.bin.is_empty() && workspace_metadata.bin.is_some() {
424+
cfg.bin = workspace_metadata.bin.unwrap();
425+
}
426+
427+
if !cfg.bins && workspace_metadata.bins.unwrap_or(false) {
428+
cfg.bins = true;
429+
}
430+
431+
if cfg.docset_name.is_none() && workspace_metadata.docset_name.is_some() {
432+
cfg.docset_name = workspace_metadata.docset_name;
433+
}
434+
435+
if cfg.docset_index.is_none() && workspace_metadata.docset_index.is_some() {
436+
cfg.docset_index = workspace_metadata.docset_index;
437+
}
438+
439+
if cfg.platform_family.is_none() && workspace_metadata.platform_family.is_some() {
440+
cfg.platform_family = workspace_metadata.platform_family;
441+
}
442+
}
400443

401444
// Clean the documentation directory if the user didn't explicitly ask not to clean it.
402445
if !cfg.no_clean {

src/main.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::path::PathBuf;
22

33
use clap::{Parser, Subcommand, Args};
4+
use serde::Deserialize;
45

56
mod commands;
67
mod error;
@@ -15,6 +16,21 @@ struct Cli {
1516
command: Commands
1617
}
1718

19+
#[derive(Debug, Deserialize)]
20+
#[serde(rename_all = "kebab-case")]
21+
pub struct WorkspaceMetadata {
22+
pub features: Option<Vec<String>>,
23+
pub no_deps: Option<bool>,
24+
pub document_private_items: Option<bool>,
25+
pub target: Option<String>,
26+
pub lib: Option<bool>,
27+
pub bin: Option<Vec<String>>,
28+
pub bins: Option<bool>,
29+
pub docset_name: Option<String>,
30+
pub docset_index: Option<String>,
31+
pub platform_family: Option<String>
32+
}
33+
1834
#[derive(Args, Default, Debug, Clone)]
1935
/// Generate a docset. This is currently the only available command, and should remain the
2036
/// default one in the future if new ones are added.
@@ -24,7 +40,7 @@ pub struct DocsetParams {
2440
#[clap(flatten)]
2541
pub workspace: clap_cargo::Workspace,
2642
#[clap(flatten)]
27-
features: clap_cargo::Features,
43+
pub features: clap_cargo::Features,
2844
#[clap(long("no-deps"))]
2945
/// Do not document dependencies.
3046
pub no_dependencies: bool,

0 commit comments

Comments
 (0)