Skip to content

Commit 324e5e1

Browse files
authored
feat: refactor core bindings to use new derive macro (#267)
* feat: refactor core bindings to use new derive macro * make CI run
1 parent d52012b commit 324e5e1

File tree

4 files changed

+573
-486
lines changed

4 files changed

+573
-486
lines changed

.github/workflows/bevy_mod_scripting.yml

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ jobs:
4343
filters: |
4444
src:
4545
- 'src/**'
46+
- 'crates/**'
47+
- 'examples/**'
48+
- 'assets/**'
4649
- 'docs/**'
4750
- '.github/workflows/bevy_mod_scripting.yml'
4851

crates/bevy_mod_scripting_derive/src/lib.rs

+22-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use syn::{spanned::Spanned, ImplItemFn, ItemImpl};
1212
/// - `name`: the name to use to suffix the generated function, i.e. `test_fn` will generate `register_test_fn. Defaults to `functions`
1313
/// - `remote`: If true the original impl block will be ignored, and only the function registrations will be generated
1414
/// - `bms_core_path`: If set the path to override bms imports, normally only used internally
15+
/// - `unregistered`: If set, will use `new_unregistered` instead of `new` for the namespace builder
1516
#[proc_macro_attribute]
1617
pub fn script_bindings(
1718
args: proc_macro::TokenStream,
@@ -45,10 +46,15 @@ pub fn script_bindings(
4546
let bms_core_path = &args.bms_core_path;
4647

4748
let function_name = format_ident!("register_{}", args.name);
49+
let builder_function_name = if args.unregistered {
50+
format_ident!("new_unregistered")
51+
} else {
52+
format_ident!("new")
53+
};
4854

4955
let out = quote_spanned! {impl_span=>
5056
fn #function_name(world: &mut bevy::ecs::world::World) {
51-
#bms_core_path::bindings::function::namespace::NamespaceBuilder::<#type_ident_with_generics>::new(world)
57+
#bms_core_path::bindings::function::namespace::NamespaceBuilder::<#type_ident_with_generics>::#builder_function_name(world)
5258
#(#function_registrations)*;
5359
}
5460

@@ -65,6 +71,8 @@ struct Args {
6571
pub remote: bool,
6672
/// If set the path to override bms imports
6773
pub bms_core_path: syn::Path,
74+
/// If true will use `new_unregistered` instead of `new` for the namespace builder
75+
pub unregistered: bool,
6876
}
6977

7078
impl syn::parse::Parse for Args {
@@ -75,6 +83,7 @@ impl syn::parse::Parse for Args {
7583

7684
let mut name = syn::Ident::new("functions", Span::call_site());
7785
let mut remote = false;
86+
let mut unregistered = false;
7887
let mut bms_core_path =
7988
syn::Path::from(syn::Ident::new("bevy_mod_scripting", Span::call_site()));
8089
bms_core_path.segments.push(syn::PathSegment {
@@ -88,6 +97,9 @@ impl syn::parse::Parse for Args {
8897
if path.is_ident("remote") {
8998
remote = true;
9099
continue;
100+
} else if path.is_ident("unregistered") {
101+
unregistered = true;
102+
continue;
91103
}
92104
}
93105
syn::Meta::NameValue(name_value) => {
@@ -107,23 +119,24 @@ impl syn::parse::Parse for Args {
107119
}
108120
}
109121
}
110-
_ => {}
122+
_ => {
123+
unknown_spans.push((pair.span(), "Unsupported meta kind for script_bindings"));
124+
continue;
125+
}
111126
}
112127

113-
unknown_spans.push(pair.span());
128+
unknown_spans.push((pair.span(), "Unknown argument to script_bindings"));
114129
}
115130

116131
if !unknown_spans.is_empty() {
117-
return Err(syn::Error::new(
118-
unknown_spans[0],
119-
"Unknown argument to script_bindings",
120-
));
132+
return Err(syn::Error::new(unknown_spans[0].0, unknown_spans[0].1));
121133
}
122134

123135
Ok(Self {
124136
remote,
125137
bms_core_path,
126138
name,
139+
unregistered,
127140
})
128141
}
129142
}
@@ -152,7 +165,8 @@ fn process_impl_fn(fun: &ImplItemFn) -> TokenStream {
152165
.map(|s| syn::LitStr::new(&s, Span::call_site()))
153166
.unwrap_or(syn::LitStr::new("", Span::call_site()));
154167
let fun_name = syn::LitStr::new(&fun.sig.ident.to_string(), Span::call_site());
155-
quote_spanned! {Span::call_site()=>
168+
let fun_span = fun.sig.ident.span();
169+
quote_spanned! {fun_span=>
156170
.register_documented(
157171
#fun_name,
158172
|#args| #body,

crates/bevy_mod_scripting_functions/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ profiling = { workspace = true }
3434
uuid = "1.11"
3535
smol_str = "0.2.2"
3636
bevy_mod_scripting_core = { workspace = true }
37+
bevy_mod_scripting_derive = { workspace = true }
3738

3839
[lints]
3940
workspace = true

0 commit comments

Comments
 (0)