Skip to content

Commit

Permalink
Merge pull request #16 from cdbrkfxrpt/main
Browse files Browse the repository at this point in the history
Add ability to specify a workspace and enable features
  • Loading branch information
tfpk authored Feb 11, 2024
2 parents 5dd3f56 + 5e87b7d commit c3a0a2c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ and `src/main.rs` (or `src/lib.rs`). Place all required dependencies in the `Car

Then, point `manifest_dir` to that directory, as shown above.

## Working with `cargo` workspaces and build features

If you are working with a `cargo` workspace, you'll need to have the `--workspace` argument
passed to the invocation of `cargo build`. Likewise, if you need to build your crate with
specific features enabled, you'll need to pass them via `--features first,second` - as a
comma separated list. In the `mdbook-keeper` config, you can do so via:

```toml
is_workspace = true
build_features = ["first", "second"]
```

Both of these are optional: if they are not present in the config, the manifest dir is not
considered a workspace and no features get enabled.

## Other Configuration Options

All of these options can be placed in the `book.toml` file, after `[preprocessor.keeper]`.
Expand Down
47 changes: 39 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ mod skeptic;
#[cfg(test)]
mod tests;

use std::fs::File;
use std::io::prelude::*;
use std::{fs::File, io::prelude::*};

use atty::Stream;
use colored::{control::set_override, Colorize};
use glob::glob;
use mdbook::book::{Book, BookItem};
use mdbook::errors::Error;
use mdbook::preprocess::{Preprocessor, PreprocessorContext};
use mdbook::{
book::{Book, BookItem},
errors::Error,
preprocess::{Preprocessor, PreprocessorContext},
};
use serde::{Deserialize, Serialize};
use slug::slugify;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{
collections::HashMap,
path::{Path, PathBuf},
process::Command,
};
use toml::value::Table;

use run_tests::{handle_test, CompileType, TestResult};
Expand Down Expand Up @@ -79,6 +82,20 @@ struct KeeperConfigParser {
#[serde(default)]
manifest_dir: Option<String>,

/// This allows you to specify if the manifest dir is
/// of a cargo workspace. If set to true, `--workspace`
/// will be passed to the invocation of `cargo build`.
#[serde(default)]
is_workspace: Option<bool>,

/// This allows you to specify the features you want to
/// invoke `cargo build` with. If you set this to
/// `["first", "second"], it causes `--features
/// first,second` to be added to the invocation of
/// `cargo build`.
#[serde(default)]
build_features: Vec<String>,

/// Whether to show terminal colours.
#[serde(default)]
terminal_colors: Option<bool>,
Expand All @@ -89,6 +106,8 @@ struct KeeperConfig {
test_dir: PathBuf,
target_dir: PathBuf,
manifest_dir: Option<PathBuf>,
is_workspace: bool,
build_features: Vec<String>,
terminal_colors: bool,
externs: Vec<String>,
}
Expand Down Expand Up @@ -123,6 +142,7 @@ impl KeeperConfig {
});

let manifest_dir = keeper_config.manifest_dir.map(PathBuf::from);
let is_workspace = keeper_config.is_workspace.unwrap_or(false);

let terminal_colors = keeper_config
.terminal_colors
Expand All @@ -134,6 +154,8 @@ impl KeeperConfig {
test_dir,
target_dir,
manifest_dir,
is_workspace,
build_features: keeper_config.build_features,
terminal_colors,
externs: keeper_config.externs,
}
Expand All @@ -146,13 +168,22 @@ impl KeeperConfig {

if let Some(manifest_dir) = &self.manifest_dir {
let cargo = std::env::var("CARGO").unwrap_or_else(|_| String::from("cargo"));

let mut command = Command::new(cargo);
command
.arg("build")
.current_dir(manifest_dir)
.env("CARGO_TARGET_DIR", &self.target_dir)
.env("CARGO_MANIFEST_DIR", manifest_dir);

if self.is_workspace {
command.arg("--workspace");
}

if !self.build_features.is_empty() {
command.args(["--features", &self.build_features.join(",")]);
}

let mut join_handle = command.spawn().expect("failed to execute process");

let build_was_ok = join_handle.wait().expect("Could not join on thread");
Expand Down

0 comments on commit c3a0a2c

Please sign in to comment.