Skip to content

Commit 4468334

Browse files
committed
build(examples): Add pre-commit lockfile copy script
1 parent 5f9eeb2 commit 4468334

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

.pre-commit-config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,12 @@ repos:
2828
- id: check-executables-have-shebangs
2929
- id: check-toml
3030
exclude: ^(rust|rust+wasm)
31+
32+
- repo: local
33+
hooks:
34+
- id: copy-example-lockfiles
35+
name: copy example cargo lockfiles
36+
entry: scripts/copy-example-lockfiles.sh
37+
language: script
38+
files: ^examples/(Cargo\.(toml|lock)|.*/Cargo\.toml)$
39+
pass_filenames: false

examples/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@ This workspace contains numerous examples demonstrating template-generated
22
code and combinations. Each example is setup as its own crate, with its own
33
dependencies.
44

5+
## Cargo.lock Synchronization
6+
7+
The workspace automatically maintains synchronized `Cargo.lock` files for all
8+
example templates. When dependencies are updated at the workspace level, a
9+
pre-commit hook automatically copies the workspace `Cargo.lock` to each example
10+
directory that contains a `Cargo.toml` file. This ensures all templates have
11+
up-to-date dependency resolution for use with `cargo generate`.
12+
13+
This synchronization happens automatically during commits when `Cargo.toml` or
14+
`Cargo.lock` files are modified. You can also run it manually:
15+
16+
```console
17+
./scripts/copy-example-lockfiles.sh
18+
```
19+
520
## Running Examples
621

722
To run any example from this top-level:

scripts/copy-example-lockfiles.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
3+
# Script to copy workspace Cargo.lock to all example template directories
4+
# This ensures templates have up-to-date lock files for cargo generate
5+
6+
set -e
7+
8+
# Define paths
9+
WORKSPACE_LOCK="examples/Cargo.lock"
10+
EXAMPLES_DIR="examples"
11+
12+
# Check if workspace lock file exists
13+
if [[ ! -f "$WORKSPACE_LOCK" ]]; then
14+
echo "Error: Workspace Cargo.lock not found at $WORKSPACE_LOCK"
15+
exit 1
16+
fi
17+
18+
# Check if examples directory exists
19+
if [[ ! -d "$EXAMPLES_DIR" ]]; then
20+
echo "Error: Examples directory not found at $EXAMPLES_DIR"
21+
exit 1
22+
fi
23+
24+
# Find all subdirectories in examples/ that contain a Cargo.toml (excluding target)
25+
example_dirs=()
26+
while IFS= read -r -d '' dir; do
27+
# Skip the target directory
28+
if [[ "$(basename "$dir")" == "target" ]]; then
29+
continue
30+
fi
31+
32+
# Check if the directory contains a Cargo.toml file
33+
if [[ -f "$dir/Cargo.toml" ]]; then
34+
example_dirs+=("$dir")
35+
fi
36+
done < <(find "$EXAMPLES_DIR" -mindepth 1 -maxdepth 1 -type d -print0)
37+
38+
# Check if we found any example directories
39+
if [[ ${#example_dirs[@]} -eq 0 ]]; then
40+
echo "No example directories with Cargo.toml found in $EXAMPLES_DIR"
41+
exit 0
42+
fi
43+
44+
# Copy workspace lock to each example directory
45+
copied_count=0
46+
for example_dir in "${example_dirs[@]}"; do
47+
target_lock="$example_dir/Cargo.lock"
48+
cp "$WORKSPACE_LOCK" "$target_lock"
49+
echo "Copied $WORKSPACE_LOCK to $target_lock"
50+
((copied_count++))
51+
done
52+
53+
echo "Successfully copied Cargo.lock to $copied_count example template(s)"

0 commit comments

Comments
 (0)