Skip to content

feat: refactor generated bindings to use new derive macro #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions .github/workflows/bevy_mod_scripting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref || github.ref_name }}
- name: Setup Bot GitHub Credentials
run: |
git config user.name "github-actions[bot]"
Expand All @@ -163,19 +165,6 @@ jobs:
- name: Commit Changes
if: steps.check_changes.outputs.changes
run: |
git checkout -b ${{ env.CODEGEN_BRANCH_NAME }} || git checkout ${{ env.CODEGEN_BRANCH_NAME }}
git add -A
git commit -m "chore(codegen): update bevy bindings"
git push -u origin ${{ env.CODEGEN_BRANCH_NAME }} --force
- uses: jwalton/gh-find-current-pr@master
if: steps.check_changes.outputs.changes
id: findPR
with:
state: all
- name: Create Or Update PR
if: steps.check_changes.outputs.changes && success() && steps.findPR.outputs.number
run: |
gh pr list --base ${{ github.ref }} --search "chore(codegen): update bevy bindings" --json number > prs.json
if [ $(jq '. | length' prs.json) -eq 0 ]; then
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
fi
git push
6 changes: 0 additions & 6 deletions crates/bevy_api_gen/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ pub enum TemplateKind {
SharedModule,
#[strum(to_string = "crate.tera")]
CrateArtifact,
#[strum(to_string = "field.tera")]
Field,
#[strum(to_string = "function.tera")]
Function,
#[strum(to_string = "item.tera")]
Item,
#[strum(to_string = "header.tera")]
Header,
#[strum(to_string = "footer.tera")]
Expand Down
13 changes: 0 additions & 13 deletions crates/bevy_api_gen/templates/field.tera

This file was deleted.

76 changes: 45 additions & 31 deletions crates/bevy_api_gen/templates/footer.tera
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,56 @@

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

{% for item in items %}
#[script_bindings(
remote,
name = "{{ item.ident | convert_case(case="snake") }}_functions",
bms_core_path="bevy_mod_scripting_core"
)]
impl {{item.import_path}} {
{% for function in item.functions %}

{% for docstring in function.docstrings %}
/// {{ docstring }}
{% endfor %}
fn {{ function.ident }} (
{%- for arg in function.args -%}
{%- if arg.proxy_ty is matching("Mut.*")-%}
mut {% endif -%}
{{- arg.ident | to_arg_pattern() -}}
: {{- arg.proxy_ty -}},
{%- endfor -%}
) -> {{ function.output.proxy_ty }} {
let output: {{ function.output.proxy_ty }} =
{%- if function.from_trait_path -%}
{{- function_call_expression(type=item.import_path, trait=function.from_trait_path, function=function.ident) -}}
{%- else -%}
{{- function_call_expression(type=item.import_path, function=function.ident) -}}
{%- endif -%}
(
{%- for arg in function.args -%}
{%- if arg.proxy_ty is matching("Ref.*")-%}
&{% endif -%}
{%- if arg.proxy_ty is matching ("Mut.*")-%}
&mut {% endif -%}
{{- arg.ident | to_arg_pattern() -}}
{%- if arg.proxy_ty is matching("Val.*")-%}
.into_inner()
{%- endif -%},
{%- endfor -%}
).into();
output
}
{% endfor %}
}
{% endfor %}

impl ::bevy::app::Plugin for {{ "ScriptingPlugin" | prefix_cratename | convert_case(case="upper_camel")}} {
fn build(&self, app: &mut ::bevy::prelude::App) {
let mut world = app.world_mut();

{% for item in items %}
NamespaceBuilder::<::{{ item.import_path }}>::new(world)
{%- for function in item.functions -%}
.register("{{ function.ident }}", |
{%- for arg in function.args -%}
{%- if arg.proxy_ty is matching("Mut.*")-%}
mut {% endif -%}
{{- arg.ident | to_arg_pattern() -}}
: {{- arg.proxy_ty -}},
{%- endfor -%}
| {
let output: {{ function.output.proxy_ty }} =
{%- if function.from_trait_path -%}
{{- function_call_expression(type=item.import_path, trait=function.from_trait_path, function=function.ident) -}}
{%- else -%}
{{- function_call_expression(type=item.import_path, function=function.ident) -}}
{%- endif -%}
(
{%- for arg in function.args -%}
{%- if arg.proxy_ty is matching("Ref.*")-%}
&{% endif -%}
{%- if arg.proxy_ty is matching ("Mut.*")-%}
&mut {% endif -%}
{{- arg.ident | to_arg_pattern() -}}
{%- if arg.proxy_ty is matching("Val.*")-%}
.into_inner()
{%- endif -%},
{%- endfor -%}
).into();
output
})
{%- endfor -%};
register_{{ item.ident | convert_case(case="snake") }}_functions(&mut world);
{% endfor %}
}
}
53 changes: 0 additions & 53 deletions crates/bevy_api_gen/templates/function.tera

This file was deleted.

3 changes: 3 additions & 0 deletions crates/bevy_api_gen/templates/header.tera
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use bevy_mod_scripting_core::{
function::{from::{Ref, Mut, Val}, namespace::{NamespaceBuilder}}
}
};

use bevy_mod_scripting_derive::script_bindings;

{% if args.self_is_bms_lua %}
use crate::*;
{% else %}
Expand Down
93 changes: 0 additions & 93 deletions crates/bevy_api_gen/templates/item.tera

This file was deleted.

13 changes: 12 additions & 1 deletion crates/bevy_mod_scripting_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,21 @@ fn process_impl_fn(fun: &ImplItemFn) -> TokenStream {
.unwrap_or(syn::LitStr::new("", Span::call_site()));
let fun_name = syn::LitStr::new(&fun.sig.ident.to_string(), Span::call_site());
let fun_span = fun.sig.ident.span();
let out_type = match &fun.sig.output {
syn::ReturnType::Default => quote_spanned! {fun_span=>
()
},
syn::ReturnType::Type(_, ty) => quote_spanned! {fun_span=>
#ty
},
};
quote_spanned! {fun_span=>
.register_documented(
#fun_name,
|#args| #body,
|#args| {
let output: #out_type = {#body};
output
},
#docstring,
&[#(#args_names),*]
)
Expand Down
50 changes: 26 additions & 24 deletions crates/bevy_mod_scripting_functions/src/bevy_bindings/bevy_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,36 @@ use bevy_mod_scripting_core::bindings::{
namespace::NamespaceBuilder,
},
};
use bevy_mod_scripting_derive::script_bindings;
use crate::*;
pub struct BevyCoreScriptingPlugin;
#[script_bindings(
remote,
name = "name_functions",
bms_core_path = "bevy_mod_scripting_core"
)]
impl bevy::core::prelude::Name {
fn clone(_self: Ref<bevy::core::prelude::Name>) -> Val<bevy::core::prelude::Name> {
let output: Val<bevy::core::prelude::Name> = <bevy::core::prelude::Name as std::clone::Clone>::clone(
&_self,
)
.into();
output
}
fn eq(
_self: Ref<bevy::core::prelude::Name>,
other: Ref<bevy::core::prelude::Name>,
) -> bool {
let output: bool = <bevy::core::prelude::Name as std::cmp::PartialEq<
bevy::core::prelude::Name,
>>::eq(&_self, &other)
.into();
output
}
}
impl ::bevy::app::Plugin for BevyCoreScriptingPlugin {
fn build(&self, app: &mut ::bevy::prelude::App) {
let mut world = app.world_mut();
NamespaceBuilder::<::bevy::core::prelude::Name>::new(world)
.register(
"clone",
|_self: Ref<bevy::core::prelude::Name>| {
let output: Val<bevy::core::prelude::Name> = <bevy::core::prelude::Name as std::clone::Clone>::clone(
&_self,
)
.into();
output
},
)
.register(
"eq",
|
_self: Ref<bevy::core::prelude::Name>,
other: Ref<bevy::core::prelude::Name>|
{
let output: bool = <bevy::core::prelude::Name as std::cmp::PartialEq<
bevy::core::prelude::Name,
>>::eq(&_self, &other)
.into();
output
},
);
register_name_functions(&mut world);
}
}
Loading
Loading