Skip to content

Commit

Permalink
CLI args defaults from cargo metadata
Browse files Browse the repository at this point in the history
First attempt at providing defaults for CLI arguments in cargo manifest
metadata.
  • Loading branch information
Robzz committed Sep 22, 2022
1 parent 870ad70 commit 1ddef2d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# cargo-docset changelog

## Unreleased - v0.4.0
* Feature: add the ability to specify defaults for a subset of the CLI arguments in the cargo metadata for a crate.

## Unreleased - v0.3.1

* Bugfix: update the crate version in Cargo.lock (thanks @antifuchs)
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ clap-cargo = { version = "0.9", features = ["cargo_metadata"] }
clap = { version = "3.2", features = ["std", "suggestions", "derive"], default_features = false }
derive_more = "0.99"
rusqlite = "0.28"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
snafu = "0.7"
termcolor = { version = "1.1", optional = true }
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ Just run `cargo docset` in your crate's directory to generate the docset. It wil
directory. cargo-docset generally supports the same options as `cargo doc`, with a few additional ones. For more
information, run `cargo docset --help` or look below in this README.

Most arguments accepted by the CLI can also be given default values for your crate by adding them as cargo
[workspace metadata](https://doc.rust-lang.org/cargo/reference/workspaces.html#the-workspacemetadata-table);
Arguments provided in a CLI invocation will take precedence over such defaults. For CLI flags that don't take a value,
use the `true` value in the metadata to signify the presence of the flag. The following keys are supported (please
refer to the CLI documentation for what each option does): `features`, `no-deps`, `document-private-items`, `target`,
`lib`, `bin`, `bins`, `docset-name`, `docset-index`, `platform-family`.

To install your shiny new docset, copy it to your Zeal/Dash docset directory (available in the preferences, on Zeal at
least) and restart Zeal/Dash.

Expand Down
47 changes: 45 additions & 2 deletions src/commands/generate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Implementation of the `docset` subcommand.

use crate::{error::*, io::*, DocsetParams};
use crate::{error::*, io::*, DocsetParams, WorkspaceMetadata};

use cargo_metadata::Metadata;
use derive_more::Constructor;
Expand Down Expand Up @@ -383,7 +383,7 @@ fn get_docset_platform_family(cfg: &DocsetParams, metadata: &Metadata) -> Option
}
}

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

// Merge the options specified in cargo metadata. Options specified on the CLI take precedence.
let cargo_metadata = cfg.manifest.metadata().exec().context(CargoMetadataSnafu)?;
let workspace_metadata_res = serde_json::from_value::<WorkspaceMetadata>(cargo_metadata.workspace_metadata.clone());
if let Ok(workspace_metadata) = workspace_metadata_res {
if !cfg.features.all_features && cfg.features.features.is_empty() && workspace_metadata.features.is_some() {
cfg.features.features = workspace_metadata.features.unwrap();
}

if !cfg.no_dependencies && workspace_metadata.no_deps.unwrap_or(false) {
cfg.no_dependencies = true;
}

if !cfg.doc_private_items && workspace_metadata.document_private_items.unwrap_or(false) {
cfg.doc_private_items = true;
}

if cfg.target.is_none() && workspace_metadata.target.is_some() {
cfg.target = workspace_metadata.target;
}

if !cfg.lib && workspace_metadata.lib.unwrap_or(false) {
cfg.lib = true;
}

if cfg.bin.is_empty() && workspace_metadata.bin.is_some() {
cfg.bin = workspace_metadata.bin.unwrap();
}

if !cfg.bins && workspace_metadata.bins.unwrap_or(false) {
cfg.bins = true;
}

if cfg.docset_name.is_none() && workspace_metadata.docset_name.is_some() {
cfg.docset_name = workspace_metadata.docset_name;
}

if cfg.docset_index.is_none() && workspace_metadata.docset_index.is_some() {
cfg.docset_index = workspace_metadata.docset_index;
}

if cfg.platform_family.is_none() && workspace_metadata.platform_family.is_some() {
cfg.platform_family = workspace_metadata.platform_family;
}
}

// Clean the documentation directory if the user didn't explicitly ask not to clean it.
if !cfg.no_clean {
Expand Down
18 changes: 17 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::PathBuf;

use clap::{Parser, Subcommand, Args};
use serde::Deserialize;

mod commands;
mod error;
Expand All @@ -15,6 +16,21 @@ struct Cli {
command: Commands
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct WorkspaceMetadata {
pub features: Option<Vec<String>>,
pub no_deps: Option<bool>,
pub document_private_items: Option<bool>,
pub target: Option<String>,
pub lib: Option<bool>,
pub bin: Option<Vec<String>>,
pub bins: Option<bool>,
pub docset_name: Option<String>,
pub docset_index: Option<String>,
pub platform_family: Option<String>
}

#[derive(Args, Default, Debug, Clone)]
/// Generate a docset. This is currently the only available command, and should remain the
/// default one in the future if new ones are added.
Expand All @@ -24,7 +40,7 @@ pub struct DocsetParams {
#[clap(flatten)]
pub workspace: clap_cargo::Workspace,
#[clap(flatten)]
features: clap_cargo::Features,
pub features: clap_cargo::Features,
#[clap(long("no-deps"))]
/// Do not document dependencies.
pub no_dependencies: bool,
Expand Down

0 comments on commit 1ddef2d

Please sign in to comment.