2024-02-19 17:09:47 +00:00
|
|
|
//! CI script used for Bevy.
|
|
|
|
|
2022-05-30 16:21:03 +00:00
|
|
|
use xshell::{cmd, Shell};
|
2021-02-22 08:42:19 +00:00
|
|
|
|
2022-05-02 19:13:34 +00:00
|
|
|
use bitflags::bitflags;
|
|
|
|
|
|
|
|
bitflags! {
|
2023-06-01 08:41:42 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
2022-05-02 19:13:34 +00:00
|
|
|
struct Check: u32 {
|
|
|
|
const FORMAT = 0b00000001;
|
|
|
|
const CLIPPY = 0b00000010;
|
|
|
|
const COMPILE_FAIL = 0b00000100;
|
|
|
|
const TEST = 0b00001000;
|
|
|
|
const DOC_TEST = 0b00010000;
|
|
|
|
const DOC_CHECK = 0b00100000;
|
|
|
|
const BENCH_CHECK = 0b01000000;
|
|
|
|
const EXAMPLE_CHECK = 0b10000000;
|
2022-05-06 19:29:44 +00:00
|
|
|
const COMPILE_CHECK = 0b100000000;
|
2024-02-25 15:19:27 +00:00
|
|
|
const CFG_CHECK = 0b1000000000;
|
2022-05-02 19:13:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 08:42:19 +00:00
|
|
|
fn main() {
|
2021-03-11 00:27:30 +00:00
|
|
|
// When run locally, results may differ from actual CI runs triggered by
|
|
|
|
// .github/workflows/ci.yml
|
2021-02-22 08:42:19 +00:00
|
|
|
// - Official CI runs latest stable
|
|
|
|
// - Local runs use whatever the default Rust is locally
|
|
|
|
|
Warn when passing invalid argument to CI (#5858)
Example:
```sh
cargo run -p ci -- lint
Invalid argument: "lint".
Enter one of: lints, test, doc, compile, format, clippy, compile-fail, bench-check, example-check, doc-check, doc-test.
```
Co-authored-by: devil-ira <justthecooldude@gmail.com>
2022-09-02 12:18:45 +00:00
|
|
|
let arguments = [
|
|
|
|
("lints", Check::FORMAT | Check::CLIPPY),
|
|
|
|
("test", Check::TEST),
|
|
|
|
("doc", Check::DOC_TEST | Check::DOC_CHECK),
|
|
|
|
(
|
|
|
|
"compile",
|
|
|
|
Check::COMPILE_FAIL | Check::BENCH_CHECK | Check::EXAMPLE_CHECK | Check::COMPILE_CHECK,
|
|
|
|
),
|
|
|
|
("format", Check::FORMAT),
|
|
|
|
("clippy", Check::CLIPPY),
|
|
|
|
("compile-fail", Check::COMPILE_FAIL),
|
|
|
|
("bench-check", Check::BENCH_CHECK),
|
|
|
|
("example-check", Check::EXAMPLE_CHECK),
|
2024-02-25 15:19:27 +00:00
|
|
|
("cfg-check", Check::CFG_CHECK),
|
Warn when passing invalid argument to CI (#5858)
Example:
```sh
cargo run -p ci -- lint
Invalid argument: "lint".
Enter one of: lints, test, doc, compile, format, clippy, compile-fail, bench-check, example-check, doc-check, doc-test.
```
Co-authored-by: devil-ira <justthecooldude@gmail.com>
2022-09-02 12:18:45 +00:00
|
|
|
("doc-check", Check::DOC_CHECK),
|
|
|
|
("doc-test", Check::DOC_TEST),
|
|
|
|
];
|
|
|
|
|
|
|
|
let what_to_run = if let Some(arg) = std::env::args().nth(1).as_deref() {
|
|
|
|
if let Some((_, check)) = arguments.iter().find(|(str, _)| *str == arg) {
|
|
|
|
*check
|
|
|
|
} else {
|
|
|
|
println!(
|
|
|
|
"Invalid argument: {arg:?}.\nEnter one of: {}.",
|
|
|
|
arguments[1..]
|
|
|
|
.iter()
|
|
|
|
.map(|(s, _)| s)
|
|
|
|
.fold(arguments[0].0.to_owned(), |c, v| c + ", " + v)
|
|
|
|
);
|
|
|
|
return;
|
2022-05-06 19:29:44 +00:00
|
|
|
}
|
Warn when passing invalid argument to CI (#5858)
Example:
```sh
cargo run -p ci -- lint
Invalid argument: "lint".
Enter one of: lints, test, doc, compile, format, clippy, compile-fail, bench-check, example-check, doc-check, doc-test.
```
Co-authored-by: devil-ira <justthecooldude@gmail.com>
2022-09-02 12:18:45 +00:00
|
|
|
} else {
|
|
|
|
Check::all()
|
2022-05-02 19:13:34 +00:00
|
|
|
};
|
2021-02-22 08:42:19 +00:00
|
|
|
|
2022-05-30 16:21:03 +00:00
|
|
|
let sh = Shell::new().unwrap();
|
|
|
|
|
2022-05-02 19:13:34 +00:00
|
|
|
if what_to_run.contains(Check::FORMAT) {
|
|
|
|
// See if any code needs to be formatted
|
2022-05-30 16:21:03 +00:00
|
|
|
cmd!(sh, "cargo fmt --all -- --check")
|
2022-05-02 19:13:34 +00:00
|
|
|
.run()
|
|
|
|
.expect("Please run 'cargo fmt --all' to format your code.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if what_to_run.contains(Check::CLIPPY) {
|
|
|
|
// See if clippy has any complaints.
|
|
|
|
// - Type complexity must be ignored because we use huge templates for queries
|
2022-05-31 01:38:07 +00:00
|
|
|
cmd!(
|
|
|
|
sh,
|
2023-11-28 04:12:48 +00:00
|
|
|
"cargo clippy --workspace --all-targets --all-features -- -Dwarnings"
|
2022-05-31 01:38:07 +00:00
|
|
|
)
|
2021-11-13 22:43:19 +00:00
|
|
|
.run()
|
|
|
|
.expect("Please fix clippy errors in output above.");
|
2022-05-02 19:13:34 +00:00
|
|
|
}
|
2021-11-13 22:43:19 +00:00
|
|
|
|
2022-05-02 19:13:34 +00:00
|
|
|
if what_to_run.contains(Check::COMPILE_FAIL) {
|
2023-01-02 21:07:33 +00:00
|
|
|
{
|
|
|
|
// ECS Compile Fail Tests
|
|
|
|
// Run UI tests (they do not get executed with the workspace tests)
|
|
|
|
// - See crates/bevy_ecs_compile_fail_tests/README.md
|
|
|
|
let _subdir = sh.push_dir("crates/bevy_ecs_compile_fail_tests");
|
|
|
|
cmd!(sh, "cargo test --target-dir ../../target")
|
|
|
|
.run()
|
|
|
|
.expect("Compiler errors of the ECS compile fail tests seem to be different than expected! Check locally and compare rust versions.");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// Reflect Compile Fail Tests
|
|
|
|
// Run tests (they do not get executed with the workspace tests)
|
|
|
|
// - See crates/bevy_reflect_compile_fail_tests/README.md
|
|
|
|
let _subdir = sh.push_dir("crates/bevy_reflect_compile_fail_tests");
|
|
|
|
cmd!(sh, "cargo test --target-dir ../../target")
|
|
|
|
.run()
|
|
|
|
.expect("Compiler errors of the Reflect compile fail tests seem to be different than expected! Check locally and compare rust versions.");
|
|
|
|
}
|
bevy_derive: Add `#[deref]` attribute (#8552)
# Objective
Bevy code tends to make heavy use of the [newtype](
https://doc.rust-lang.org/rust-by-example/generics/new_types.html)
pattern, which is why we have a dedicated derive for
[`Deref`](https://doc.rust-lang.org/std/ops/trait.Deref.html) and
[`DerefMut`](https://doc.rust-lang.org/std/ops/trait.DerefMut.html).
This derive works for any struct with a single field:
```rust
#[derive(Component, Deref, DerefMut)]
struct MyNewtype(usize);
```
One reason for the single-field limitation is to prevent confusion and
footguns related that would arise from allowing multi-field structs:
<table align="center">
<tr>
<th colspan="2">
Similar structs, different derefs
</th>
</tr>
<tr>
<td>
```rust
#[derive(Deref, DerefMut)]
struct MyStruct {
foo: usize, // <- Derefs usize
bar: String,
}
```
</td>
<td>
```rust
#[derive(Deref, DerefMut)]
struct MyStruct {
bar: String, // <- Derefs String
foo: usize,
}
```
</td>
</tr>
<tr>
<th colspan="2">
Why `.1`?
</th>
</tr>
<tr>
<td colspan="2">
```rust
#[derive(Deref, DerefMut)]
struct MyStruct(Vec<usize>, Vec<f32>);
let mut foo = MyStruct(vec![123], vec![1.23]);
// Why can we skip the `.0` here?
foo.push(456);
// But not here?
foo.1.push(4.56);
```
</td>
</tr>
</table>
However, there are certainly cases where it's useful to allow for
structs with multiple fields. Such as for structs with one "real" field
and one `PhantomData` to allow for generics:
```rust
#[derive(Deref, DerefMut)]
struct MyStruct<T>(
// We want use this field for the `Deref`/`DerefMut` impls
String,
// But we need this field so that we can make this struct generic
PhantomData<T>
);
// ERROR: Deref can only be derived for structs with a single field
// ERROR: DerefMut can only be derived for structs with a single field
```
Additionally, the possible confusion and footguns are mainly an issue
for newer Rust/Bevy users. Those familiar with `Deref` and `DerefMut`
understand what adding the derive really means and can anticipate its
behavior.
## Solution
Allow users to opt into multi-field `Deref`/`DerefMut` derives using a
`#[deref]` attribute:
```rust
#[derive(Deref, DerefMut)]
struct MyStruct<T>(
// Use this field for the `Deref`/`DerefMut` impls
#[deref] String,
// We can freely include any other field without a compile error
PhantomData<T>
);
```
This prevents the footgun pointed out in the first issue described in
the previous section, but it still leaves the possible confusion
surrounding `.0`-vs-`.#`. However, the idea is that by making this
behavior explicit with an attribute, users will be more aware of it and
can adapt appropriately.
---
## Changelog
- Added `#[deref]` attribute to `Deref` and `DerefMut` derives
2023-05-16 18:29:09 +00:00
|
|
|
{
|
|
|
|
// Macro Compile Fail Tests
|
|
|
|
// Run tests (they do not get executed with the workspace tests)
|
|
|
|
// - See crates/bevy_macros_compile_fail_tests/README.md
|
|
|
|
let _subdir = sh.push_dir("crates/bevy_macros_compile_fail_tests");
|
|
|
|
cmd!(sh, "cargo test --target-dir ../../target")
|
|
|
|
.run()
|
|
|
|
.expect("Compiler errors of the macros compile fail tests seem to be different than expected! Check locally and compare rust versions.");
|
|
|
|
}
|
2021-11-13 22:43:19 +00:00
|
|
|
}
|
2022-02-03 04:25:45 +00:00
|
|
|
|
2022-05-02 19:13:34 +00:00
|
|
|
if what_to_run.contains(Check::TEST) {
|
|
|
|
// Run tests (except doc tests and without building examples)
|
2022-05-30 16:21:03 +00:00
|
|
|
cmd!(sh, "cargo test --workspace --lib --bins --tests --benches")
|
2022-02-03 04:25:45 +00:00
|
|
|
.run()
|
|
|
|
.expect("Please fix failing tests in output above.");
|
2022-05-02 19:13:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if what_to_run.contains(Check::DOC_TEST) {
|
|
|
|
// Run doc tests
|
2022-05-30 16:21:03 +00:00
|
|
|
cmd!(sh, "cargo test --workspace --doc")
|
2022-05-02 19:13:34 +00:00
|
|
|
.run()
|
|
|
|
.expect("Please fix failing doc-tests in output above.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if what_to_run.contains(Check::DOC_CHECK) {
|
|
|
|
// Check that building docs work and does not emit warnings
|
|
|
|
std::env::set_var("RUSTDOCFLAGS", "-D warnings");
|
2022-05-30 16:21:03 +00:00
|
|
|
cmd!(
|
|
|
|
sh,
|
|
|
|
"cargo doc --workspace --all-features --no-deps --document-private-items"
|
|
|
|
)
|
|
|
|
.run()
|
|
|
|
.expect("Please fix doc warnings in output above.");
|
2022-05-02 19:13:34 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 22:18:21 +00:00
|
|
|
if what_to_run.contains(Check::BENCH_CHECK) {
|
2022-05-30 16:21:03 +00:00
|
|
|
let _subdir = sh.push_dir("benches");
|
2022-05-02 19:13:34 +00:00
|
|
|
// Check that benches are building
|
2022-05-30 16:21:03 +00:00
|
|
|
cmd!(sh, "cargo check --benches --target-dir ../target")
|
2022-05-02 19:13:34 +00:00
|
|
|
.run()
|
|
|
|
.expect("Failed to check the benches.");
|
|
|
|
}
|
2022-02-03 04:25:45 +00:00
|
|
|
|
2022-05-02 19:13:34 +00:00
|
|
|
if what_to_run.contains(Check::EXAMPLE_CHECK) {
|
|
|
|
// Build examples and check they compile
|
2022-05-30 16:21:03 +00:00
|
|
|
cmd!(sh, "cargo check --workspace --examples")
|
2022-02-03 04:25:45 +00:00
|
|
|
.run()
|
2023-01-25 03:20:58 +00:00
|
|
|
.expect("Please fix compiler errors for examples in output above.");
|
2022-02-03 04:25:45 +00:00
|
|
|
}
|
2022-05-06 19:29:44 +00:00
|
|
|
|
|
|
|
if what_to_run.contains(Check::COMPILE_CHECK) {
|
2023-01-25 03:20:58 +00:00
|
|
|
// Build bevy and check that it compiles
|
2022-05-30 16:21:03 +00:00
|
|
|
cmd!(sh, "cargo check --workspace")
|
2022-05-06 19:29:44 +00:00
|
|
|
.run()
|
2023-01-25 03:20:58 +00:00
|
|
|
.expect("Please fix compiler errors in output above.");
|
2022-05-06 19:29:44 +00:00
|
|
|
}
|
2024-02-25 15:19:27 +00:00
|
|
|
|
|
|
|
if what_to_run.contains(Check::CFG_CHECK) {
|
|
|
|
// Check cfg and imports
|
|
|
|
std::env::set_var("RUSTFLAGS", "-D warnings");
|
2024-03-01 18:25:19 +00:00
|
|
|
cmd!(sh, "cargo +nightly check -Zcheck-cfg --workspace")
|
2024-02-25 15:19:27 +00:00
|
|
|
.run()
|
|
|
|
.expect("Please fix failing cfg checks in output above.");
|
|
|
|
}
|
2021-02-22 08:42:19 +00:00
|
|
|
}
|