Skip to content

Commit 2d0dfc0

Browse files
WIP: Update to 0.9.1 (#38)
* Update to 0.9.1 * formatting * Fixed some requested things Docs now partially work project changed to package unneeded reference removed Resource moved to ScriptHost definition. * New path generator * No type registry wrapper * Kinda close to working All the imports are actual paths, but the code that gets generated is wrong. * Generation now works * Small fixes * fix certain paths not being found + minor improvements * formatting * New clippy changes Co-authored-by: makspll <[email protected]>
1 parent d377eef commit 2d0dfc0

File tree

40 files changed

+1067
-501
lines changed

40 files changed

+1067
-501
lines changed

Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ bevy_mod_scripting_rhai = { path = "languages/bevy_mod_scripting_rhai", version
5151
bevy_script_api = { path = "bevy_script_api", version = "0.1.1", optional = true }
5252

5353
[dev-dependencies]
54-
bevy = { version = "0.8.0"}
54+
bevy = { version = "0.9.1"}
5555
rand = "0.8.5"
5656

5757

@@ -62,7 +62,7 @@ rand = "0.8.5"
6262
# bevy_mod_scripting_lua = {path="languages/bevy_mod_scripting_lua", version="0.1.1", features=["lua54"]}
6363
# bevy_mod_scripting_lua_derive = {path="languages/bevy_mod_scripting_lua_derive", version="0.1.1"}
6464

65-
# bevy = { version = "0.8.0"}
65+
# bevy = { version = "0.9.1"}
6666
# serde = "1.0.137"
6767
# criterion = "0.3"
6868

@@ -89,7 +89,7 @@ opt-level = 1
8989
opt-level = 3
9090

9191

92-
# needs bevy 0.8 support from console
92+
# needs bevy 0.9 support from console
9393
# [[example]]
9494
# name = "console_integration_lua"
9595
# path = "examples/console_integration_lua.rs"

api_gen_config.toml

+7-12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::{script_ref::{ReflectedValue,ValueIndex},
1616
use std::sync::Mutex;
1717
use bevy_mod_scripting_core::prelude::*;
1818
use bevy::prelude::App;
19+
use bevy::reflect::Enum;
1920
"""
2021

2122
other = """
@@ -156,10 +157,6 @@ source="bevy_ui"
156157
type="Style"
157158
source="bevy_ui"
158159

159-
[[types]]
160-
type="UiColor"
161-
source="bevy_ui"
162-
163160
[[types]]
164161
type="UiImage"
165162
source="bevy_ui"
@@ -398,9 +395,7 @@ source="bevy_sprite"
398395
type="Sprite"
399396
source="bevy_sprite"
400397

401-
[[types]]
402-
type="Rect"
403-
source="bevy_sprite"
398+
404399

405400
## BEVY_RENDER
406401

@@ -486,10 +481,6 @@ traits=[
486481
{name="CameraProjection", import_path="bevy::render::camera::CameraProjection"}
487482
]
488483

489-
[[types]]
490-
type="DepthCalculation"
491-
source="bevy_render"
492-
493484
[[types]]
494485
type="CameraRenderGraph"
495486
source="bevy_render"
@@ -915,4 +906,8 @@ import_path="glam::f64::DQuat"
915906
[[types]]
916907
type="EulerRot"
917908
source="bevy_math"
918-
import_path="glam::EulerRot"
909+
import_path="glam::EulerRot"
910+
911+
[[types]]
912+
type="Rect"
913+
source="bevy_math"

bevy_api_gen/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ path = "src/main.rs"
2121

2222

2323
[dependencies]
24-
rustdoc-types = "0.11.0"
24+
rustdoc-types = "0.19.0"
2525
clap = { version = "3.2.6", features = ["derive"] }
2626
serde_json = "1.0.81"
2727
toml = "0.5.9"

bevy_api_gen/src/arg_validator.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ impl TryFrom<&Type> for ArgType {
6767

6868
fn try_from(value: &Type) -> Result<Self, Self::Error> {
6969
match value {
70-
Type::ResolvedPath { name, args, .. } => {
70+
Type::ResolvedPath(path) => {
7171
let mut processed_args = Vec::default();
7272

73-
for a in args {
73+
for a in &path.args {
7474
if let GenericArgs::AngleBracketed { args, bindings } = a.as_ref() {
7575
for generic in args {
7676
match generic {
@@ -89,7 +89,7 @@ impl TryFrom<&Type> for ArgType {
8989
return Err("Parenthesised generics are not supported".to_owned());
9090
}
9191
}
92-
let base = Type::Primitive(name.to_string()).try_into()?;
92+
let base = Type::Primitive(path.name.to_string()).try_into()?;
9393
if let base @ ArgType::Base(_) = base {
9494
if !processed_args.is_empty() {
9595
Ok(Self::Generic {
@@ -174,7 +174,7 @@ impl ArgWrapperType {
174174
let base_ident = type_.base_ident().unwrap_or(self_type);
175175
type_
176176
.is_self()
177-
.then(|| ArgWrapperType::None)
177+
.then_some(ArgWrapperType::None)
178178
.or_else(|| {
179179
config
180180
.primitives

bevy_api_gen/src/cratepath.rs

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use rustdoc_types::{Crate, Id, ItemEnum, Visibility};
2+
3+
pub(crate) fn get_path(id: &Id, source: &Crate) -> Option<Vec<Id>> {
4+
match source.paths.get(id) {
5+
Some(_) => return Some(vec![id.to_owned()]),
6+
None => {
7+
let ind = source.index.get(id)?;
8+
if let Visibility::Restricted { parent, .. } = &ind.visibility {
9+
if let Some(p_path) = get_path(parent, source) {
10+
return Some(p_path);
11+
}
12+
}
13+
let parents = source.index.iter().filter(|(_, p_item)| {
14+
if let Some(name) = &ind.name {
15+
if p_item.links.contains_key(name) {
16+
return true;
17+
}
18+
}
19+
if let ItemEnum::Impl(p_impl) = &p_item.inner {
20+
return p_impl.items.contains(id);
21+
}
22+
if let ItemEnum::Import(p_import) = &p_item.inner {
23+
if let Some(p_inner) = &p_import.id {
24+
return p_inner == id;
25+
}
26+
return false;
27+
}
28+
if let ItemEnum::Module(p_mod) = &p_item.inner {
29+
return p_mod.items.contains(id);
30+
}
31+
false
32+
});
33+
34+
for (parent, _) in parents {
35+
let path_o = get_path(parent, source);
36+
if let Some(mut path) = path_o {
37+
path.push(id.to_owned());
38+
return Some(path);
39+
}
40+
}
41+
}
42+
};
43+
None
44+
}
45+
46+
pub(crate) fn path_to_import(path: Vec<Id>, source: &Crate) -> Vec<String> {
47+
path.iter()
48+
.rev()
49+
.enumerate()
50+
.rev()
51+
.enumerate()
52+
.map(|(starti, (endi, id))| {
53+
let ind = source.index.get(id).unwrap();
54+
if starti == 0 {
55+
return source.paths.get(id).unwrap().path.clone();
56+
} else if endi == 0 {
57+
if let Some(name) = &ind.name {
58+
return vec![name.to_owned()];
59+
}
60+
} else if let Visibility::Restricted { parent: _, path } = &ind.visibility {
61+
return path[2..].split("::").map(|x| x.to_string()).collect();
62+
} else if let ItemEnum::Module(module) = &ind.inner {
63+
if !module.is_stripped {
64+
return vec![source.index.get(id).unwrap().name.clone().unwrap()];
65+
} else {
66+
return vec![];
67+
}
68+
}
69+
vec![]
70+
})
71+
.reduce(|mut x, y| {
72+
x.extend(y);
73+
x
74+
})
75+
.unwrap()
76+
}

bevy_api_gen/src/lib.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@ pub use {arg_validator::*, config::*, wrapper::*, writer::*};
1010
/// Currently only used for stringifying simple trait names
1111
pub fn stringify_type(type_: &Type) -> Option<String> {
1212
match type_ {
13-
Type::ResolvedPath {
14-
name,
15-
id: _,
16-
args: _,
17-
param_names: _,
18-
} => Some(name.to_owned()),
13+
Type::ResolvedPath(path) => Some(path.name.to_owned()),
1914
Type::Generic(s) | Type::Primitive(s) => Some(s.to_owned()),
2015
Type::QualifiedPath {
2116
name,

bevy_api_gen/src/main.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
use bevy_api_gen_lib::{stringify_type, Args, Config, PrettyWriter, WrappedItem, WRAPPER_PREFIX};
1+
pub mod cratepath;
2+
3+
use bevy_api_gen_lib::{Args, Config, PrettyWriter, WrappedItem, WRAPPER_PREFIX};
24

35
use clap::Parser;
6+
use cratepath::{get_path, path_to_import};
47
use indexmap::{IndexMap, IndexSet};
58
use rustdoc_types::{Crate, Impl, Item, ItemEnum};
69
use serde_json::from_reader;
710
use std::{
11+
borrow::Cow,
812
collections::HashSet,
913
fs::{read_to_string, File},
1014
io::{self, BufReader},
@@ -70,7 +74,7 @@ pub(crate) fn generate_macros(
7074
if let ItemEnum::Impl(i) = &source.index.get(id).unwrap().inner {
7175
match &i.trait_ {
7276
Some(t) => {
73-
stringify_type(t).map(|str_| implemented_traits.insert(str_));
77+
implemented_traits.insert(t.name.to_owned());
7478
}
7579
None => self_impl = Some(i),
7680
}
@@ -89,14 +93,20 @@ pub(crate) fn generate_macros(
8993

9094
let config = config.types.get(item.name.as_ref().unwrap()).unwrap();
9195

92-
let path_components = &source.paths.get(id).unwrap().path;
96+
//let path_components = &source.paths.get(id).unwrap().path;
97+
let path_components = get_path(id, source).unwrap_or_else(|| {
98+
panic!("path not found for {:?} in {:?}", id, source.root)
99+
});
100+
//eprintln!("{:?}", path_components);
101+
let path_components = path_to_import(path_components, source);
102+
//eprintln!("{:?}", path_components);
93103

94104
let wrapper_name = format!("{WRAPPER_PREFIX}{}", item.name.as_ref().unwrap());
95105
let wrapped_type = item.name.as_ref().unwrap();
96106
WrappedItem {
97107
wrapper_name,
98108
wrapped_type,
99-
path_components,
109+
path_components: Cow::Owned(path_components),
100110
source,
101111
config,
102112
item,
@@ -333,7 +343,7 @@ pub fn main() -> Result<(), io::Error> {
333343
.json
334344
.iter()
335345
.map(|json| {
336-
let f = File::open(&json).unwrap_or_else(|_| panic!("Could not open {}", &json));
346+
let f = File::open(json).unwrap_or_else(|_| panic!("Could not open {}", &json));
337347
let rdr = BufReader::new(f);
338348
from_reader(rdr)
339349
})

0 commit comments

Comments
 (0)