Skip to content

Commit

Permalink
Check for drift between .template files and generated files (#13)
Browse files Browse the repository at this point in the history
This:
- [x] runs generate and diffs the resulting files with a copy, looking
for instances where a PR author might have changed a file without
updating the corresponding template

edit: doesn't really close 5 :)

This approach should also work for `bevy_quickstart`, we just need to
provide all the template placeholder values with
https://cargo-generate.github.io/cargo-generate/templates/template_defined_placeholders.html#--define-or--d-flag.

---------

Co-authored-by: Ben Frankel <[email protected]>
  • Loading branch information
bas-ie and benfrankel authored Sep 19, 2024
1 parent fae1c5d commit 7c14715
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
60 changes: 60 additions & 0 deletions .github/workflows/ci-generate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: CI

on:
pull_request:
branches:
- main

jobs:
# Compare committed files with those generated from templates to ensure no differences occur.
check-generated-files:
runs-on: ubuntu-latest
env:
REPO: ${{ github.event.repository.name }}

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable

- name: Install cargo-generate
uses: taiki-e/install-action@v2
with:
tool: cargo-generate

- name: Retain pre-generated files
run: |
for template_file in $(find . -type f -name '*.template'); do
generated_file="${template_file%.template}"
if [[ -f "${generated_file}" ]]; then
# Keep a copy to diff with the output of the next step.
cp "${generated_file}" "${generated_file}.check"
fi
done
- name: Generate repository
run: |
# Note: we use cargo-generate itself here as bevy_cli's wrapping of the cargo-generate
# library does not allow us to do useful things like --allow-commands and use local
# templates.
cargo generate --path . --name '${{ env.REPO }}' --allow-commands
- name: Diff generated files
run: |
change_detected=false
for check_file in $(find '${{ env.REPO }}' -type f -name '*.check'); do
generated_file="${check_file%.check}"
if ! diff "${check_file}" "${generated_file}" > /dev/null ; then
echo "File ${generated_file} has changed from ${generated_file}.template:"
diff "${check_file}" "${generated_file}" || true
echo "::error file=${generated_file}::Template change detected."
change_detected=true
fi
done
if [[ "${change_detected}" == true ]]; then
exit 1
fi
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "bevy_new"
name = "bevy_new_minimal"
version = "0.1.0"
edition = "2021"

Expand Down
1 change: 1 addition & 0 deletions cargo-generate.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
cargo_generate_version = "0.22"
include = ["*.template"]
ignore = [
".github/workflows/ci-generate.yaml",
"LICENSE-Apache-2.0.txt",
"LICENSE-CC0-1.0.txt",
"LICENSE-MIT.txt",
Expand Down

0 comments on commit 7c14715

Please sign in to comment.