Skip to content

Commit efbc220

Browse files
feat: refactor generated bindings to use new derive macro (#268)
* feat: refactor generated bindings to use new derive macro * remove semicolon * Update bevy bindings feat/refactor generated bindings use new macro (#269) chore(codegen): update bevy bindings Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * add core path * update branch name * simplify * chore(codegen): update bevy bindings * add _functions postfix * chore(codegen): update bevy bindings * add docstrings * add output type * chore(codegen): update bevy bindings * make return type matter in the macro and fix resulting errors --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 324e5e1 commit efbc220

File tree

17 files changed

+30374
-29996
lines changed

17 files changed

+30374
-29996
lines changed

.github/workflows/bevy_mod_scripting.yml

+3-14
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ jobs:
144144
steps:
145145
- name: Checkout
146146
uses: actions/checkout@v4
147+
with:
148+
ref: ${{ github.head_ref || github.ref_name }}
147149
- name: Setup Bot GitHub Credentials
148150
run: |
149151
git config user.name "github-actions[bot]"
@@ -163,19 +165,6 @@ jobs:
163165
- name: Commit Changes
164166
if: steps.check_changes.outputs.changes
165167
run: |
166-
git checkout -b ${{ env.CODEGEN_BRANCH_NAME }} || git checkout ${{ env.CODEGEN_BRANCH_NAME }}
167168
git add -A
168169
git commit -m "chore(codegen): update bevy bindings"
169-
git push -u origin ${{ env.CODEGEN_BRANCH_NAME }} --force
170-
- uses: jwalton/gh-find-current-pr@master
171-
if: steps.check_changes.outputs.changes
172-
id: findPR
173-
with:
174-
state: all
175-
- name: Create Or Update PR
176-
if: steps.check_changes.outputs.changes && success() && steps.findPR.outputs.number
177-
run: |
178-
gh pr list --base ${{ github.ref }} --search "chore(codegen): update bevy bindings" --json number > prs.json
179-
if [ $(jq '. | length' prs.json) -eq 0 ]; then
180-
gh pr create --title "chore(codegen): update bevy bindings" --body "This PR updates the bevy bindings for #${{ steps.findPR.outputs.number }}" --base ${{ github.ref }} --head ${{ env.CODEGEN_BRANCH_NAME }} || true
181-
fi
170+
git push

crates/bevy_api_gen/src/template.rs

-6
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ pub enum TemplateKind {
3232
SharedModule,
3333
#[strum(to_string = "crate.tera")]
3434
CrateArtifact,
35-
#[strum(to_string = "field.tera")]
36-
Field,
37-
#[strum(to_string = "function.tera")]
38-
Function,
39-
#[strum(to_string = "item.tera")]
40-
Item,
4135
#[strum(to_string = "header.tera")]
4236
Header,
4337
#[strum(to_string = "footer.tera")]

crates/bevy_api_gen/templates/field.tera

-13
This file was deleted.

crates/bevy_api_gen/templates/footer.tera

+45-31
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,56 @@
77

88
pub struct {{ "ScriptingPlugin" | prefix_cratename | convert_case(case="upper_camel")}};
99

10+
{% for item in items %}
11+
#[script_bindings(
12+
remote,
13+
name = "{{ item.ident | convert_case(case="snake") }}_functions",
14+
bms_core_path="bevy_mod_scripting_core"
15+
)]
16+
impl {{item.import_path}} {
17+
{% for function in item.functions %}
18+
19+
{% for docstring in function.docstrings %}
20+
/// {{ docstring }}
21+
{% endfor %}
22+
fn {{ function.ident }} (
23+
{%- for arg in function.args -%}
24+
{%- if arg.proxy_ty is matching("Mut.*")-%}
25+
mut {% endif -%}
26+
{{- arg.ident | to_arg_pattern() -}}
27+
: {{- arg.proxy_ty -}},
28+
{%- endfor -%}
29+
) -> {{ function.output.proxy_ty }} {
30+
let output: {{ function.output.proxy_ty }} =
31+
{%- if function.from_trait_path -%}
32+
{{- function_call_expression(type=item.import_path, trait=function.from_trait_path, function=function.ident) -}}
33+
{%- else -%}
34+
{{- function_call_expression(type=item.import_path, function=function.ident) -}}
35+
{%- endif -%}
36+
(
37+
{%- for arg in function.args -%}
38+
{%- if arg.proxy_ty is matching("Ref.*")-%}
39+
&{% endif -%}
40+
{%- if arg.proxy_ty is matching ("Mut.*")-%}
41+
&mut {% endif -%}
42+
{{- arg.ident | to_arg_pattern() -}}
43+
{%- if arg.proxy_ty is matching("Val.*")-%}
44+
.into_inner()
45+
{%- endif -%},
46+
{%- endfor -%}
47+
).into();
48+
output
49+
}
50+
{% endfor %}
51+
}
52+
{% endfor %}
53+
1054
impl ::bevy::app::Plugin for {{ "ScriptingPlugin" | prefix_cratename | convert_case(case="upper_camel")}} {
1155
fn build(&self, app: &mut ::bevy::prelude::App) {
1256
let mut world = app.world_mut();
1357

1458
{% for item in items %}
15-
NamespaceBuilder::<::{{ item.import_path }}>::new(world)
16-
{%- for function in item.functions -%}
17-
.register("{{ function.ident }}", |
18-
{%- for arg in function.args -%}
19-
{%- if arg.proxy_ty is matching("Mut.*")-%}
20-
mut {% endif -%}
21-
{{- arg.ident | to_arg_pattern() -}}
22-
: {{- arg.proxy_ty -}},
23-
{%- endfor -%}
24-
| {
25-
let output: {{ function.output.proxy_ty }} =
26-
{%- if function.from_trait_path -%}
27-
{{- function_call_expression(type=item.import_path, trait=function.from_trait_path, function=function.ident) -}}
28-
{%- else -%}
29-
{{- function_call_expression(type=item.import_path, function=function.ident) -}}
30-
{%- endif -%}
31-
(
32-
{%- for arg in function.args -%}
33-
{%- if arg.proxy_ty is matching("Ref.*")-%}
34-
&{% endif -%}
35-
{%- if arg.proxy_ty is matching ("Mut.*")-%}
36-
&mut {% endif -%}
37-
{{- arg.ident | to_arg_pattern() -}}
38-
{%- if arg.proxy_ty is matching("Val.*")-%}
39-
.into_inner()
40-
{%- endif -%},
41-
{%- endfor -%}
42-
).into();
43-
output
44-
})
45-
{%- endfor -%};
59+
register_{{ item.ident | convert_case(case="snake") }}_functions(&mut world);
4660
{% endfor %}
4761
}
4862
}

crates/bevy_api_gen/templates/function.tera

-53
This file was deleted.

crates/bevy_api_gen/templates/header.tera

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use bevy_mod_scripting_core::{
88
function::{from::{Ref, Mut, Val}, namespace::{NamespaceBuilder}}
99
}
1010
};
11+
12+
use bevy_mod_scripting_derive::script_bindings;
13+
1114
{% if args.self_is_bms_lua %}
1215
use crate::*;
1316
{% else %}

crates/bevy_api_gen/templates/item.tera

-93
This file was deleted.

crates/bevy_mod_scripting_derive/src/lib.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,21 @@ fn process_impl_fn(fun: &ImplItemFn) -> TokenStream {
166166
.unwrap_or(syn::LitStr::new("", Span::call_site()));
167167
let fun_name = syn::LitStr::new(&fun.sig.ident.to_string(), Span::call_site());
168168
let fun_span = fun.sig.ident.span();
169+
let out_type = match &fun.sig.output {
170+
syn::ReturnType::Default => quote_spanned! {fun_span=>
171+
()
172+
},
173+
syn::ReturnType::Type(_, ty) => quote_spanned! {fun_span=>
174+
#ty
175+
},
176+
};
169177
quote_spanned! {fun_span=>
170178
.register_documented(
171179
#fun_name,
172-
|#args| #body,
180+
|#args| {
181+
let output: #out_type = {#body};
182+
output
183+
},
173184
#docstring,
174185
&[#(#args_names),*]
175186
)

crates/bevy_mod_scripting_functions/src/bevy_bindings/bevy_core.rs

+26-24
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,36 @@ use bevy_mod_scripting_core::bindings::{
99
namespace::NamespaceBuilder,
1010
},
1111
};
12+
use bevy_mod_scripting_derive::script_bindings;
1213
use crate::*;
1314
pub struct BevyCoreScriptingPlugin;
15+
#[script_bindings(
16+
remote,
17+
name = "name_functions",
18+
bms_core_path = "bevy_mod_scripting_core"
19+
)]
20+
impl bevy::core::prelude::Name {
21+
fn clone(_self: Ref<bevy::core::prelude::Name>) -> Val<bevy::core::prelude::Name> {
22+
let output: Val<bevy::core::prelude::Name> = <bevy::core::prelude::Name as std::clone::Clone>::clone(
23+
&_self,
24+
)
25+
.into();
26+
output
27+
}
28+
fn eq(
29+
_self: Ref<bevy::core::prelude::Name>,
30+
other: Ref<bevy::core::prelude::Name>,
31+
) -> bool {
32+
let output: bool = <bevy::core::prelude::Name as std::cmp::PartialEq<
33+
bevy::core::prelude::Name,
34+
>>::eq(&_self, &other)
35+
.into();
36+
output
37+
}
38+
}
1439
impl ::bevy::app::Plugin for BevyCoreScriptingPlugin {
1540
fn build(&self, app: &mut ::bevy::prelude::App) {
1641
let mut world = app.world_mut();
17-
NamespaceBuilder::<::bevy::core::prelude::Name>::new(world)
18-
.register(
19-
"clone",
20-
|_self: Ref<bevy::core::prelude::Name>| {
21-
let output: Val<bevy::core::prelude::Name> = <bevy::core::prelude::Name as std::clone::Clone>::clone(
22-
&_self,
23-
)
24-
.into();
25-
output
26-
},
27-
)
28-
.register(
29-
"eq",
30-
|
31-
_self: Ref<bevy::core::prelude::Name>,
32-
other: Ref<bevy::core::prelude::Name>|
33-
{
34-
let output: bool = <bevy::core::prelude::Name as std::cmp::PartialEq<
35-
bevy::core::prelude::Name,
36-
>>::eq(&_self, &other)
37-
.into();
38-
output
39-
},
40-
);
42+
register_name_functions(&mut world);
4143
}
4244
}

0 commit comments

Comments
 (0)