Skip to content

Commit 86b6390

Browse files
committed
Define cargo features
fix #19
1 parent c5a2f9e commit 86b6390

File tree

12 files changed

+160
-46
lines changed

12 files changed

+160
-46
lines changed

.github/workflows/deploy.yaml

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,47 @@ jobs:
2929
override: 'true'
3030
default: 'true'
3131

32-
- name: Build (dev)
33-
run: cargo build --locked
34-
35-
- name: Test (dev)
36-
run: cargo test
37-
38-
- name: Build (release)
39-
run: cargo build --locked --release
40-
41-
- name: Test (release)
42-
run: cargo test --release
43-
44-
- name: Check formatting
45-
if: runner.os == 'Linux'
46-
run: cargo fmt -- --check
47-
48-
- name: Clippy check (dev)
32+
- name: Build and Test (dev)
33+
env:
34+
FMT: 'false'
35+
LINT: 'false'
36+
BUILD: 'true'
37+
TEST: 'true'
38+
BUILD_FLAGS: '--locked'
39+
TEST_FLAGS: ''
40+
run: ./test.sh
41+
42+
- name: Build and Test (release)
43+
env:
44+
FMT: 'false'
45+
LINT: 'false'
46+
BUILD: 'true'
47+
TEST: 'true'
48+
BUILD_FLAGS: '--locked'
49+
TEST_FLAGS: ''
50+
run: ./test.sh --release
51+
52+
- name: Fmt and Clippy (dev)
4953
if: runner.os == 'Linux'
50-
run: cargo clippy -- -D warnings
51-
52-
- name: Clippy check (release)
54+
env:
55+
FMT: 'true'
56+
LINT: 'true'
57+
BUILD: 'false'
58+
TEST: 'false'
59+
BUILD_FLAGS: ''
60+
TEST_FLAGS: ''
61+
run: ./test.sh
62+
63+
- name: Clippy (release)
5364
if: runner.os == 'Linux'
54-
run: cargo clippy --release -- -D warnings
65+
env:
66+
FMT: 'false'
67+
LINT: 'true'
68+
BUILD: 'false'
69+
TEST: 'false'
70+
BUILD_FLAGS: ''
71+
TEST_FLAGS: ''
72+
run: ./test.sh --release
5573

5674
build_linux:
5775
name: Build
@@ -76,7 +94,7 @@ jobs:
7694
default: 'true'
7795

7896
- name: Build
79-
run: cargo build --target ${{ matrix.target }} --release
97+
run: cargo build --target ${{ matrix.target }} --release --all-features
8098

8199
- name: Strip all debug symbols
82100
run: strip --strip-all target/${{ matrix.target }}/release/pdu
@@ -109,7 +127,7 @@ jobs:
109127
default: 'true'
110128

111129
- name: Build
112-
run: cargo build --target ${{ matrix.target }} --release
130+
run: cargo build --target ${{ matrix.target }} --release --all-features
113131

114132
- name: Strip all debug symbols
115133
run: strip target/${{ matrix.target }}/release/pdu
@@ -143,7 +161,7 @@ jobs:
143161
default: 'true'
144162

145163
- name: Build
146-
run: cargo build --target ${{ matrix.target }} --release
164+
run: cargo build --target ${{ matrix.target }} --release --all-features
147165

148166
- name: Upload build artifact
149167
uses: actions/upload-artifact@v2

.github/workflows/test.yaml

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,22 @@ jobs:
4242
override: 'true'
4343
default: 'true'
4444

45-
- name: Build (dev)
46-
run: cargo build --locked
47-
4845
- name: Test (dev)
49-
run: cargo test --no-fail-fast
50-
51-
- name: Build (release)
52-
run: cargo build --locked --release
46+
env:
47+
FMT: 'false'
48+
LINT: 'true'
49+
BUILD: 'true'
50+
TEST: 'true'
51+
BUILD_FLAGS: '--locked'
52+
TEST_FLAGS: '--no-fail-fast'
53+
run: ./test.sh
5354

5455
- name: Test (release)
55-
run: cargo test --no-fail-fast --release
56+
env:
57+
FMT: 'false'
58+
LINT: 'true'
59+
BUILD: 'true'
60+
TEST: 'true'
61+
BUILD_FLAGS: '--locked'
62+
TEST_FLAGS: '--no-fail-fast'
63+
run: ./test.sh --release

Cargo.toml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,20 @@ path = "src/lib.rs"
3434
[[bin]]
3535
name = "pdu"
3636
path = "cli/main.rs"
37+
required-features = ["cli"]
3738

3839
[[bin]]
3940
name = "pdu-completions"
4041
path = "cli/completions.rs"
42+
required-features = ["cli-completions"]
43+
44+
[features]
45+
default = ["cli"]
46+
cli = ["structopt", "structopt-utilities"]
47+
cli-completions = ["cli"]
4148

4249
[dependencies]
4350
pipe-trait = "^0.3.2"
44-
structopt = "^0.3.21"
45-
structopt-utilities = "^0.0.8"
4651
smart-default = "^0.6.0"
4752
derive_more = "^0.99.14"
4853
rayon = "^1.5.1"
@@ -54,6 +59,14 @@ assert-cmp = "^0.2.0"
5459
zero-copy-pads = "^0.2.0"
5560
terminal_size = "^0.1.17"
5661

62+
[dependencies.structopt]
63+
version = "^0.3.21"
64+
optional = true
65+
66+
[dependencies.structopt-utilities]
67+
version = "^0.0.8"
68+
optional = true
69+
5770
[dependencies.strum]
5871
version = "^0.20.0"
5972
features = ["derive"]

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,24 @@ The benchmark was generated by [a GitHub Workflow](https://github.com/KSXGitHub/
4646
### Test
4747

4848
```sh
49-
cargo clippy -- -D warnings && cargo fmt --check && cargo test
49+
./test.sh && ./test.sh --release
5050
```
5151

52+
<details><summary>
53+
Environment Variables
54+
</summary>
55+
56+
| name | type | default value | description |
57+
|---------------|-------------------|---------------|-------------------------------------------------|
58+
| `FMT` | `true` or `false` | `true` | Whether to run `cargo fmt` |
59+
| `LINT` | `true` or `false` | `true` | Whether to run `cargo clippy` |
60+
| `BUILD` | `true` or `false` | `true` | Whether to run `cargo build` |
61+
| `TEST` | `true` or `false` | `true` | Whether to run `cargo test` |
62+
| `BUILD_FLAGS` | string | _(empty)_ | Space-separated list of flags for `cargo build` |
63+
| `TEST_FLAGS` | string | _(empty)_ | Space-separated list of flags for `cargo test` |
64+
65+
</details>
66+
5267
### Run
5368

5469
```sh

run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#! /bin/bash
22
set -o errexit -o pipefail -o nounset
3-
exec cargo run --bin="$1" -- "${@:2}"
3+
exec cargo run --bin="$1" --features cli-completions -- "${@:2}"

src/bytes_format.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub enum BytesFormat {
2525
}
2626

2727
impl BytesFormat {
28+
#[cfg(feature = "cli")]
2829
pub(crate) fn default_value() -> &'static str {
2930
BytesFormat::MetricUnits.as_ref()
3031
}

src/data_tree/retain.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ where
2222
}
2323

2424
/// Recursively cull all descendants whose data are too small relative to root.
25+
#[cfg(feature = "cli")]
2526
pub(crate) fn par_cull_insignificant_data(&mut self, min_ratio: f32)
2627
where
2728
Data: Into<u64>,
@@ -32,6 +33,7 @@ where
3233

3334
/// Process the tree via [`par_cull_insignificant_data`](Self::par_cull_insignificant_data) method.
3435
#[cfg(test)]
36+
#[cfg(feature = "cli")]
3537
fn into_insignificant_data_par_culled(mut self, min_ratio: f32) -> Self
3638
where
3739
Data: Into<u64>,
@@ -42,4 +44,5 @@ where
4244
}
4345

4446
#[cfg(test)]
47+
#[cfg(feature = "cli")]
4548
mod test;

src/lib.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,26 @@
22

33
mod utils;
44

5+
#[cfg(feature = "cli")]
56
pub mod app;
7+
#[cfg(feature = "cli")]
68
pub mod args;
9+
10+
/// The main program.
11+
#[cfg(feature = "cli")]
12+
pub fn main() {
13+
if let Err(error) = app::App::from_env().run() {
14+
eprintln!("[error] {}", error);
15+
}
16+
}
17+
18+
#[cfg(feature = "cli")]
19+
pub use structopt;
20+
#[cfg(feature = "cli")]
21+
pub use structopt::clap;
22+
#[cfg(feature = "cli")]
23+
pub use structopt_utilities;
24+
725
pub mod bytes_format;
826
pub mod data_tree;
927
pub mod fs_tree_builder;
@@ -16,15 +34,5 @@ pub mod status_board;
1634
pub mod tree_builder;
1735
pub mod visualizer;
1836

19-
/// The main program.
20-
pub fn main() {
21-
if let Err(error) = app::App::from_env().run() {
22-
eprintln!("[error] {}", error);
23-
}
24-
}
25-
26-
pub use structopt;
27-
pub use structopt::clap;
28-
pub use structopt_utilities;
2937
pub use strum;
3038
pub use zero_copy_pads;

src/visualizer/direction.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub enum Direction {
88
}
99

1010
impl Direction {
11+
#[cfg(feature = "cli")]
1112
pub(crate) const fn from_top_down(top_down: bool) -> Self {
1213
if top_down {
1314
Direction::TopDown

test.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#! /bin/bash
2+
set -o errexit -o pipefail -o nounset
3+
4+
run() {
5+
echo >&2
6+
echo "exec> $*" >&2
7+
"$@"
8+
}
9+
10+
skip() {
11+
echo >&2
12+
echo "skip> $*" >&2
13+
}
14+
15+
run_if() {
16+
condition="$1"
17+
shift
18+
case "$condition" in
19+
true) run "$@" ;;
20+
false) skip "$@" ;;
21+
*)
22+
echo "error: Invalid condition: $condition" >&2
23+
exit 1
24+
;;
25+
esac
26+
}
27+
28+
flags=("$@")
29+
build_flags=()
30+
test_flags=()
31+
eval "build_flags+=(${BUILD_FLAGS:-})"
32+
eval "test_flags+=(${TEST_FLAGS:-})"
33+
unit() {
34+
arguments=("$@" "${flags[@]}")
35+
run_if "${LINT:-true}" cargo clippy "${arguments[@]}" -- -D warnings
36+
run_if "${BUILD:-true}" cargo build "${build_flags[@]}" "${arguments[@]}"
37+
run_if "${TEST:-true}" cargo test "${test_flags[@]}" "${arguments[@]}"
38+
}
39+
40+
run_if "${FMT:-true}" cargo fmt
41+
unit
42+
unit --no-default-features
43+
unit --all-features
44+
unit --features cli
45+
unit --features cli-completions

0 commit comments

Comments
 (0)