Skip to content

Commit a7a7337

Browse files
committed
Remove dependency on CaptainHook
1 parent 69deb6b commit a7a7337

File tree

8 files changed

+178
-396
lines changed

8 files changed

+178
-396
lines changed

.php-cs-fixer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
$finder = Finder::create()
1010
->in(__DIR__ . '/src')
11-
->append([__DIR__ . '/.php-cs-fixer.php']);
11+
->append([
12+
__DIR__ . '/bin/readme-examples-sync',
13+
__DIR__ . '/.php-cs-fixer.php',
14+
]);
1215

1316
return PhpCsFixerConfigFactory::create(TicketSwapRuleSet::create())->setFinder($finder);

README.md

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,75 @@
1-
# README Examples Sync Hook
1+
# README Examples Sync
22

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.
45

56
## Installation
67

78
```bash
89
composer require --dev ruudk/readme-examples-sync-hook
910
```
1011

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
2663
```
2764

2865
## How It Works
2966

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:
3168

3269
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
3471

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.
3873

3974
### Syncing Source Code
4075

bin/readme-examples-sync

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
declare(strict_types=1);
5+
6+
// Find and include the Composer autoloader
7+
$autoloadPaths = [
8+
__DIR__ . '/../../../autoload.php', // When installed as a dependency
9+
__DIR__ . '/../vendor/autoload.php', // When in the project root
10+
];
11+
12+
$autoloaded = false;
13+
foreach ($autoloadPaths as $file) {
14+
if (file_exists($file)) {
15+
require $file;
16+
$autoloaded = true;
17+
18+
break;
19+
}
20+
}
21+
22+
if ( ! $autoloaded) {
23+
fwrite(STDERR, "Error: Could not find Composer autoload file.\n");
24+
exit(1);
25+
}
26+
27+
use Ruudk\ReadmeExamplesSyncHook\SyncReadmeExamples;
28+
29+
// Determine the repository root (current directory by default)
30+
$repositoryRoot = getcwd();
31+
32+
if ($repositoryRoot === false) {
33+
fwrite(STDERR, "Error: Could not determine current directory.\n");
34+
exit(1);
35+
}
36+
37+
$syncer = new SyncReadmeExamples();
38+
$exitCode = $syncer->sync($repositoryRoot);
39+
40+
exit($exitCode);

captainhook.json

Lines changed: 0 additions & 23 deletions
This file was deleted.

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"php": "^8.4"
1717
},
1818
"require-dev": {
19-
"captainhook/captainhook": "^5.25",
2019
"ergebnis/composer-normalize": "^2.48",
2120
"friendsofphp/php-cs-fixer": "^3.85",
2221
"phpstan/extension-installer": "^1.4",
@@ -29,10 +28,18 @@
2928
"Ruudk\\ReadmeExamplesSyncHook\\": "src/"
3029
}
3130
},
31+
"bin": [
32+
"bin/readme-examples-sync"
33+
],
3234
"config": {
3335
"allow-plugins": {
3436
"ergebnis/composer-normalize": true,
3537
"phpstan/extension-installer": true
3638
}
39+
},
40+
"scripts": {
41+
"post-install-cmd": [
42+
"command -v lefthook >/dev/null 2>&1 && lefthook install || true"
43+
]
3744
}
3845
}

0 commit comments

Comments
 (0)