|
1 | | -# README Examples Sync Hook |
| 1 | +# README Examples Sync |
2 | 2 |
|
3 | | -A [CaptainHook](https://github.com/captainhook-git/captainhook) action that automatically syncs PHP code examples in your README.md with actual source files. This ensures your documentation always shows up-to-date, working code examples. |
| 3 | +A PHP script that automatically syncs code examples in your README.md with actual source files. |
| 4 | +This ensures your documentation always shows up-to-date, working code examples. |
4 | 5 |
|
5 | 6 | ## Installation |
6 | 7 |
|
7 | 8 | ```bash |
8 | 9 | composer require --dev ruudk/readme-examples-sync-hook |
9 | 10 | ``` |
10 | 11 |
|
11 | | -## Configuration |
12 | | - |
13 | | -Add the hook to your `captainhook.json` configuration file in the `pre-commit` section: |
14 | | - |
15 | | -```json |
16 | | -{ |
17 | | - "pre-commit": { |
18 | | - "enabled": true, |
19 | | - "actions": [ |
20 | | - { |
21 | | - "action": "\\Ruudk\\ReadmeExamplesSyncHook\\SyncReadmeExamples" |
22 | | - } |
23 | | - ] |
24 | | - } |
25 | | -} |
| 12 | +## Usage |
| 13 | + |
| 14 | +Run the script from your project root: |
| 15 | + |
| 16 | +```bash |
| 17 | +vendor/bin/readme-examples-sync |
| 18 | +``` |
| 19 | + |
| 20 | +### Git Hook Integration |
| 21 | + |
| 22 | +The easiest way to automatically sync your README on commit is using [Lefthook](https://lefthook.dev): |
| 23 | + |
| 24 | +1. Install Lefthook (if not already installed): |
| 25 | + ```bash |
| 26 | + # macOS |
| 27 | + brew install lefthook |
| 28 | + ``` |
| 29 | + |
| 30 | +2. Create a `lefthook.yml` file in your project root: |
| 31 | + ```yaml |
| 32 | + pre-commit: |
| 33 | + parallel: false |
| 34 | + commands: |
| 35 | + sync-readme-examples: |
| 36 | + glob: |
| 37 | + - "*.php" |
| 38 | + - "*.md" |
| 39 | + run: vendor/bin/readme-examples-sync |
| 40 | + stage_fixed: true |
| 41 | + ``` |
| 42 | +
|
| 43 | +3. Install the hooks: |
| 44 | + ```bash |
| 45 | + lefthook install |
| 46 | + ``` |
| 47 | + |
| 48 | +That's it! Now your README will automatically sync whenever you commit changes to PHP or Markdown files. |
| 49 | + |
| 50 | +#### Alternative: Manual Git Hook |
| 51 | + |
| 52 | +If you prefer not to use Lefthook, you can manually create a `.git/hooks/pre-commit` file: |
| 53 | + |
| 54 | +```bash |
| 55 | +#!/bin/bash |
| 56 | +vendor/bin/readme-examples-sync |
| 57 | +``` |
| 58 | + |
| 59 | +Don't forget to make the hook executable: |
| 60 | + |
| 61 | +```bash |
| 62 | +chmod +x .git/hooks/pre-commit |
26 | 63 | ``` |
27 | 64 |
|
28 | 65 | ## How It Works |
29 | 66 |
|
30 | | -This hook scans your README.md file for special HTML comments that mark code examples: |
| 67 | +This script scans your README.md file for special HTML comments that mark code examples: |
31 | 68 |
|
32 | 69 | 1. **Source code sync**: Updates code blocks marked with `<!-- source: ... -->` with the actual content from source files |
33 | | -2. **Output sync** (optional): When you add `<!-- output: ... -->` comments, the hook executes PHP files and captures their output to display results |
| 70 | +2. **Output sync** (optional): When you add `<!-- output: ... -->` comments, the script executes PHP files and captures their output to display results |
34 | 71 |
|
35 | | -The hook automatically stages the updated README.md if changes are detected, ensuring your documentation stays in sync with your code. |
36 | | - |
37 | | -## Usage |
| 72 | +The script automatically stages the updated README.md if changes are detected (when run in a git repository), ensuring your documentation stays in sync with your code. |
38 | 73 |
|
39 | 74 | ### Syncing Source Code |
40 | 75 |
|
|
0 commit comments