Skip to content

Commit

Permalink
Merge branch 'main' into dynamic-queries-example
Browse files Browse the repository at this point in the history
  • Loading branch information
makspll authored Sep 7, 2024
2 parents 26fbae9 + c2c3bda commit 5d775dc
Show file tree
Hide file tree
Showing 58 changed files with 14,252 additions and 13,202 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/bevy_api_gen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly-2024-01-24
toolchain: nightly-2024-05-20
override: true
- name: Rust Cache
uses: Swatinem/[email protected]
Expand All @@ -54,7 +54,7 @@ jobs:
with:
profile: minimal
components: rustfmt
toolchain: nightly-2024-01-24
toolchain: nightly-2024-05-20
override: true
- name: Rust Cache
uses: Swatinem/[email protected]
Expand All @@ -77,7 +77,7 @@ jobs:
rm -rf crates
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly-2024-01-24
toolchain: nightly-2024-05-20
components: clippy
override: true
- name: Rust Cache
Expand All @@ -100,7 +100,7 @@ jobs:
rm -rf crates
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly-2024-01-24
toolchain: nightly-2024-05-20
override: true
- name: Rust Cache
uses: Swatinem/[email protected]
Expand All @@ -122,7 +122,7 @@ jobs:
rm -rf crates
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly-2024-01-24
toolchain: nightly-2024-05-20
override: true
- name: Rust Cache
uses: Swatinem/[email protected]
Expand Down
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ bevy_script_api = { path = "crates/bevy_script_api", version = "0.6.0", optional


[workspace.dependencies]
bevy = { version = "=0.13.1", default-features = false }
bevy = { version = "0.14.2", default-features = false }
bevy_mod_scripting_core = { path = "crates/bevy_mod_scripting_core", version = "0.6.0" }
bevy_mod_scripting_common = { path = "crates/bevy_mod_scripting_common", version = "0.6.0" }

[dev-dependencies]
bevy = { workspace = true, default-features = true }
clap = { version = "4.1", features = ["derive"] }
rand = "0.8.5"
bevy_console = "0.11.1"
bevy_console = "0.12"
rhai-rand = "0.1"

[workspace]
Expand Down Expand Up @@ -118,7 +118,7 @@ required-features = [
"lua54",
"lua_script_api",
"bevy/file_watcher",
"bevy/multi-threaded",
"bevy/multi_threaded",
]

[[example]]
Expand All @@ -128,7 +128,7 @@ required-features = [
"rhai",
"rhai_script_api",
"bevy/file_watcher",
"bevy/multi-threaded",
"bevy/multi_threaded",
]

[[example]]
Expand All @@ -153,7 +153,7 @@ required-features = [
"lua54",
"lua_script_api",
"bevy/file_watcher",
"bevy/multi-threaded",
"bevy/multi_threaded",
]

[[example]]
Expand All @@ -163,7 +163,7 @@ required-features = [
"rhai",
"rhai_script_api",
"bevy/file_watcher",
"bevy/multi-threaded",
"bevy/multi_threaded",
]

[[example]]
Expand Down
6 changes: 3 additions & 3 deletions check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CURRENT_DIR=$(basename "$PWD")


if [[ "$CURRENT_DIR" == "bevy_api_gen" ]]; then
cargo clippy --all-targets --message-format=json
cargo +nightly-2024-05-20 clippy --all-targets --message-format=json
else
cargo clippy --workspace --all-targets --message-format=json --features="lua54 lua_script_api rhai rhai_script_api teal rune bevy/file_watcher bevy/multi-threaded"
fi
cargo clippy --workspace --all-targets --message-format=json --features="lua54 lua_script_api rhai rhai_script_api teal rune bevy/file_watcher bevy/multi_threaded"
fi
8 changes: 7 additions & 1 deletion crates/bevy_api_gen/Cargo.bootstrap.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ edition = "2021"

[dependencies]
mlua = { version = "0.9.2", features = ["lua54", "vendored", "send", "macros"] }
bevy_reflect = { version = "0.13.1", features = ["bevy", "bevy_math"] }
bevy_reflect = { version = "0.14", features = [
"bevy",
"glam",
"petgraph",
"smallvec",
"uuid",
] }

[workspace]
4 changes: 2 additions & 2 deletions crates/bevy_api_gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ source = "discover"
rustc_private = true

[rust-analyzer.check]
overrideCommand = ["cargo", "+nightly-2024-01-24", "a", "--message-format=json"]
overrideCommand = ["cargo", "+nightly-2024-05-20", "a", "--message-format=json"]


[dependencies]
log = "0.4"
env_logger = "0.11"
rustc_plugin = "=0.9.0-nightly-2024-01-24"
rustc_plugin = "=0.10.0-nightly-2024-05-20"
indexmap = "2"
tempdir = "0.3"
cargo_metadata = "0.18"
Expand Down
14 changes: 7 additions & 7 deletions crates/bevy_api_gen/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ bevy_api_gen is a Cargo plugin that generates reflection-powered wrappers for Be
To install bevy_api_gen, use the following command:

```bash
cargo +nightly-2024-01-24 install bevy_api_gen
cargo +nightly-2024-05-20 install bevy_api_gen
```

# Usage
Expand All @@ -18,39 +18,39 @@ cargo +nightly-2024-01-24 install bevy_api_gen
To run the main codegen process, use the following command:

```bash
cargo +nightly-2024-01-24 bevy-api-gen generate
cargo +nightly-2024-05-20 bevy-api-gen generate
```

This will perform all parts of the process and generate meta as well as .rs files for each crate in your workspace in your `/target/plugin-nightly-2024-01-24/bevy_api_gen` directory
This will perform all parts of the process and generate meta as well as .rs files for each crate in your workspace in your `/target/plugin-nightly-2024-05-20/bevy_api_gen` directory

## Collect

After generating all the files, you can 'collect' them in a mod.rs file like so:

```bash
cargo +nightly-2024-01-24 bevy-api-gen collect
cargo +nightly-2024-05-20 bevy-api-gen collect
```

## List Types

To see a list of all `Reflect` implementing types in your workspace run:

```bash
cargo +nightly-2024-01-24 bevy-api-gen list-types > all_types.txt
cargo +nightly-2024-05-20 bevy-api-gen list-types > all_types.txt
```

## List Templates

To see the list of all templates which you can override use:

```bash
cargo +nightly-2024-01-24 bevy-api-gen list-templates
cargo +nightly-2024-05-20 bevy-api-gen list-templates
```

## Print Template

You can also print any of the templates to stdout:

```bash
cargo +nightly-2024-01-24 bevy-api-gen print item.tera
cargo +nightly-2024-05-20 bevy-api-gen print item.tera
```
2 changes: 1 addition & 1 deletion crates/bevy_api_gen/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
components = ["clippy", "rust-src", "rustc-dev", "llvm-tools"]
channel = "nightly-2024-01-24"
channel = "nightly-2024-05-20"
1 change: 1 addition & 0 deletions crates/bevy_api_gen/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ impl Verbosity {

fn default_ignored_types() -> String {
[
"std::any::TypeId",
"bevy_reflect::DynamicArray",
"bevy_reflect::DynamicList",
"bevy_reflect::DynamicMap",
Expand Down
99 changes: 87 additions & 12 deletions crates/bevy_api_gen/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ use std::{
collections::HashMap,
env,
fs::{create_dir_all, File},
io::Write,
path::Path,
io::{BufRead, Write},
path::{Path, PathBuf},
process::{Command, Stdio},
};

use bevy_api_gen::*;
use cargo_metadata::camino::Utf8Path;
use clap::Parser;
use log::{debug, info};
use log::{debug, error, info};
use strum::VariantNames;
use tera::Context;

Expand All @@ -25,19 +25,34 @@ fn main() {
}
env_logger::init();

info!("Computing crate metadata");
let metadata = cargo_metadata::MetadataCommand::new()
.no_deps()
.other_options(["--all-features".to_string(), "--offline".to_string()])
.exec()
.unwrap();

let crates = metadata
.workspace_packages()
.iter()
.map(|p| p.name.to_owned())
.collect::<Vec<_>>();

info!("Computing active features");
let include_crates = match (&args.workspace_root, args.cmd.is_generate()) {
(Some(root), true) => {
let feature_graph = FeatureGraph::from_metadata(&metadata, root);
info!(
"Using workspace root: {}, found {} crates",
feature_graph.workspace_root,
feature_graph.crates.len()
);

info!(
"Computing all transitive dependencies for enabled top-level features: {}",
args.features.join(",")
);

let dependencies = feature_graph
.dependencies_for_features(args.features.as_ref(), !args.no_default_features)
.into_iter()
Expand All @@ -52,6 +67,8 @@ fn main() {
let plugin_subdir = format!("plugin-{}", env!("RUSTC_CHANNEL"));
let plugin_target_dir = metadata.target_directory.join(plugin_subdir);

info!("Computing wokrspace metadata");

// inform the deps about the workspace crates, this is going to be useful when working with meta files as we will be able to
// know when to panic if a crate is not found
// it's also useful to pass around the output directory for our Args default values to be able to compute them
Expand Down Expand Up @@ -132,14 +149,13 @@ fn main() {
_ => {}
}

let temp_dir = tempdir::TempDir::new("bevy_api_gen_bootstrap")
.expect("Error occured when trying to acquire temp file");
let temp_dir = find_bootstrap_dir();

debug!("Temporary directory: {}", &temp_dir.path().display());
debug!("Bootstrap directory: {}", &temp_dir.as_path().display());

write_bootstrap_files(temp_dir.path());
write_bootstrap_files(temp_dir.as_path());

let bootstrap_rlibs = build_bootstrap(temp_dir.path(), &plugin_target_dir.join("bootstrap"));
let bootstrap_rlibs = build_bootstrap(temp_dir.as_path(), &plugin_target_dir.join("bootstrap"));

if bootstrap_rlibs.len() == BOOTSTRAP_DEPS.len() {
let extern_args = bootstrap_rlibs
Expand Down Expand Up @@ -195,20 +211,50 @@ fn build_bootstrap(
let mut cmd = Command::new("cargo")
.current_dir(temp_dir)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.args(["build", "--message-format=json"])
.spawn()
.unwrap();

info!(
"cd {} && cargo build --message-format=json",
temp_dir.display()
);

let reader = std::io::BufReader::new(cmd.stdout.take().unwrap());
let err_reader = std::io::BufReader::new(cmd.stderr.take().unwrap());

std::fs::create_dir_all(cache_dir).unwrap();

let mut bootstrap_rlibs = HashMap::with_capacity(BOOTSTRAP_DEPS.len());
for msg in cargo_metadata::Message::parse_stream(reader) {
if let cargo_metadata::Message::CompilerArtifact(artifact) = msg.unwrap() {
let msg = msg.unwrap();
if let cargo_metadata::Message::CompilerArtifact(artifact) = msg {
for artifact in artifact.filenames.into_iter() {
process_artifact(artifact, &mut bootstrap_rlibs);
}
} else {
match msg {
cargo_metadata::Message::BuildFinished(finished) => {
if !finished.success {
error!("Bootstrapping crate failed to build artifact");
}
}
cargo_metadata::Message::TextLine(t) => {
info!("{t}");
}
cargo_metadata::Message::CompilerMessage(msg) => {
info!("{msg}");
}
_ => {}
}
}
}
for msg in err_reader.lines() {
if let Ok(line) = msg {
info!("{line}");
} else {
panic!("Failed to read cargo stderr");
}
}

Expand All @@ -224,10 +270,17 @@ fn build_bootstrap(
std::fs::copy(path, dest).unwrap();
}
}
match cmd.wait() {
Ok(status) => {
if !status.success() {
panic!("Building bootstrap crate returned a failure status code");
}
}
Err(e) => {
panic!("Failed to wait on cargo build process: {}", e);
}
}

if !cmd.wait().unwrap().success() {
panic!("Building bootstrap crate returned a failure status code");
};
bootstrap_rlibs
}

Expand All @@ -248,6 +301,28 @@ fn process_artifact(
}
}

/// finds best location for bootstrapping crate
/// this will be the nearest target/bevy_api_gen_bootstrap directory
fn find_bootstrap_dir() -> PathBuf {
let mut path = env::current_dir().unwrap();
loop {
if path.join("target").exists() {
break;
} else if let Some(parent) = path.parent() {
path = parent.to_path_buf();
} else {
panic!("Could not find `target` directory");
}
}

path.push("target");
path.push("bevy_api_gen_bootstrap");

// create all the directories
create_dir_all(&path).unwrap();
path
}

/// Generate bootstrapping crate files
fn write_bootstrap_files(path: &Path) {
// write manifest file 'Cargo.toml'
Expand Down
Loading

0 comments on commit 5d775dc

Please sign in to comment.