Skip to content

Commit b683358

Browse files
authored
feat: Add functionality to use code-embedder as a pre-commit hook
feat: Add functionality to use code-embedder as a pre-commit hook
2 parents 747bb5c + 1c4a3b5 commit b683358

17 files changed

+350
-210
lines changed

.github/workflows/release.yml

+9-1
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,21 @@ jobs:
2727
sed -i "s/^version = .*/version = \"$TAG\"/" pyproject.toml
2828
cat pyproject.toml
2929
30-
- name: Update README.md
30+
- name: Update README.md - Github Action version
3131
env:
3232
TAG: ${{ steps.read_tag.outputs.TAG }}
3333
run: |
3434
sed -i "s/kvankova\/code-embedder@.*/kvankova\/code-embedder@$TAG/" README.md
3535
cat README.md
3636
37+
- name: Update README.md - Pre-commit version
38+
env:
39+
TAG: ${{ steps.read_tag.outputs.TAG }}
40+
run: |
41+
sed -i "s/code-embedder==.*/code-embedder==$TAG/" README.md
42+
sed -i "s/rev: v[0-9]\+\.[0-9]\+\.[0-9]\+/rev: $TAG/" README.md
43+
cat README.md
44+
3745
- name: Commit and push changes
3846
env:
3947
TAG: ${{ steps.read_tag.outputs.TAG }}

.pre-commit-hooks.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
- id: code-embedder
2+
name: Code embedder
3+
description: >
4+
Keep your code snippets in README up-to-date!
5+
entry: code-embedder
6+
language: python

README.md

+61-17
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@
55
## **Code Embedder**
66
Seamlessly update code snippets in your **README** files! 🔄📝🚀
77

8-
[Description](#-description)[How it works](#-how-it-works)[Setup](#-setup)[Examples](#-examples)[Contributing](#-contributing)[Development](#️-development)
8+
[Description](#-description)[How it works](#-how-it-works)[Setup - Github Action](#-setup---github-action)[Setup - Pre-commit Hook](#-setup---pre-commit-hook)[Examples](#-examples)[Contributing](#-contributing)[Development](#️-development)
99
</div>
1010

1111

1212
## 📚 Description
1313

14-
**Code Embedder** is a GitHub Action that automatically updates code snippets in your markdown (`README`) files. It finds code blocks in your `README` that reference specific scripts, then replaces these blocks with the current content of those scripts. This keeps your documentation in sync with your code.
14+
**Code Embedder** is a **GitHub Action** and a **pre-commit hook** that automatically updates code snippets in your markdown (`README`) files. It finds code blocks in your `README` that reference specific scripts, then replaces these blocks with the current content of those scripts. This keeps your documentation in sync with your code.
1515

1616
### ✨ Key features
1717
- 🔄 **Automatic synchronization**: Keep your `README` code examples up-to-date without manual intervention.
18-
- 🛠️ **Easy setup**: Simply add the action to your GitHub workflow and format your `README` code blocks.
19-
- 📝 **Section support**: Update only specific sections of the script in the `README`.
18+
- 🛠️ **Easy setup**: Simply add the action to your GitHub workflow / pre-commit hook and format your `README` code blocks.
19+
- 📝 **Section support**: Update only specific sections of the script in the `README`. This is **language agnostic**.
2020
- 🧩 **Object support**: Update only specific objects (functions, classes) in the `README`. *The latest version supports only 🐍 Python objects (other languages to be added soon).*
2121

2222

23-
By using **Code Embedder**, you can focus on writing and updating your actual code 💻, while letting the action take care of keeping your documentation current 📚🔄. This reduces the risk of outdated or incorrect code examples in your project documentation.
23+
By using **Code Embedder**, you can focus on writing and updating your actual code 💻, while letting the Code-Embedder take care of keeping your documentation current 📚🔄. This reduces the risk of outdated or incorrect code examples in your project documentation.
2424

2525
## 🔍 How it works
2626

27-
The action looks for specific tags in all markdown (`README`) files, which indicate the script file path (and optionally the section to update), then it updates the code block sections in the `README` files with the content. The changes are then pushed to the repository 🚀.
27+
The **Code Embedder** scans markdown files for special tags that specify which parts of your scripts to embed. When it finds these tags, it automatically updates the code blocks with the latest content from your source files. When used as a GitHub Action, any changes are automatically committed and pushed to your repository 🚀.
2828

2929
### 📄 **Full script** updates
3030
In the `README` (or other markdown) file, the full script is marked with the following tag:
@@ -47,7 +47,9 @@ You must also add the following comment tags in the script file `path/to/script`
4747
...
4848
[Comment sign] code_embedder:section_name end
4949
```
50-
The comment sign is the one that is used in the script file, e.g. `#` for Python, or `//` for JavaScript. The `section_name` must be unique in the file, otherwise the action will use the first section found.
50+
The comment sign is the one that is used in the script file, e.g. `#` for Python, or `//` for JavaScript. For example, in python script you add `# code_embedder:A start` and `# code_embedder:A end` to new lines to mark the start and end of the section `A` you want to include in the `README`.
51+
52+
The `section_name` must be unique in the file, otherwise the Code-Embedder will use the first section found.
5153

5254
### 🧩 **Object** updates
5355
In the `README` (or other markdown) file, the object of the script is marked with the following tag:
@@ -59,10 +61,10 @@ In the `README` (or other markdown) file, the object of the script is marked wit
5961
> Notice that the `path/to/script` is followed by `o:` in the tag to indicate that the object `object_name` is being updated.
6062
6163
> [!Note]
62-
> The object name must match exactly the name of the object (function, class) in the script file, including the case (e.g. `Person` not `person`). Currently, only 🐍 Python objects are supported.
64+
> The object name must match exactly the name of the object (function, class) in the script file, including the case (e.g. if class is `Person` then object name must be `Person`, not `person`). Currently, only 🐍 Python objects are supported.
6365
64-
## 🔧 Setup
65-
To use this action, you need to configure a yaml workflow file in `.github/workflows` folder (e.g. `.github/workflows/code-embedder.yaml`) with the following content:
66+
## 🔧 Setup - Github Action
67+
Use **Code Embedder** as a Github Action by adding the following to your `.github/workflows/code-embedder.yaml` file:
6668

6769
```yaml
6870
name: Code Embedder
@@ -89,6 +91,48 @@ jobs:
8991

9092
```
9193

94+
## 🔧 Setup - Pre-commit Hook
95+
You can set up **Code Embedder** as a pre-commit hook using either:
96+
<ol type="A">
97+
<li>Installation via PyPI</li>
98+
<li>Direct repository reference in your <code>.pre-commit-config.yaml</code> file</li>
99+
</ol>
100+
101+
### A. Installation via PyPI
102+
Install the package:
103+
```bash
104+
pip install code-embedder==0.5.2
105+
```
106+
107+
Your `.pre-commit-config.yaml` file should look like this:
108+
```yaml
109+
- repo: local
110+
hooks:
111+
- id: code-embedder
112+
name: Code embedder
113+
entry: code-embedder run
114+
language: python
115+
```
116+
117+
### B. Direct repository reference
118+
Alternatively, you can reference the repository directly in your `.pre-commit-config.yaml` file:
119+
```yaml
120+
- repo: https://github.com/kvankova/code-embedder
121+
rev: v0.5.2
122+
hooks:
123+
- id: code-embedder
124+
name: Code embedder
125+
entry: code-embedder run
126+
language: python
127+
```
128+
129+
### 🔧 Options
130+
Command `code-embedder run` has the following options:
131+
132+
| Option | Description |
133+
| ------ | ----------- |
134+
| `--all-files` | Process all files in the repository. In pre-commit hook, it by default checks only the changed files. |
135+
92136
## 💡 Examples
93137

94138
### 📄 Full script update
@@ -107,7 +151,7 @@ The `main.py` file contains the following code:
107151
print("Embedding successful")
108152
```
109153

110-
Once the workflow runs, the code block sections are filled with the content of the script located at `main.py` and updated in the `README` file.
154+
Once code-embedder runs, the code block sections are filled with the content of the script located at `main.py` and updated in the `README` file.
111155

112156
````md
113157
# README
@@ -118,7 +162,7 @@ This is a readme.
118162
print("Embedding successful")
119163
```
120164
````
121-
With any changes to `main.py`, the code block section is updated in the `README` file with the next workflow run.
165+
With any changes to `main.py`, the code block section is updated in the `README` file with the next code-embedder run.
122166

123167
### 📂 Section update
124168

@@ -140,7 +184,7 @@ print("Embedding successful")
140184
# code_embedder:A end
141185
```
142186

143-
Once the workflow runs, the code block section will be updated in the `README` file with the content of the section `A` from the script located at `main.py` and pushed to the repository 🚀.
187+
Once code-embedder runs, the code block section will be updated in the `README` file with the content of the section `A` from the script located at `main.py` (in case of using it as a Github Action, the changes are then pushed to the repository 🚀).
144188

145189
````md
146190
# README
@@ -152,7 +196,7 @@ print("Embedding successful")
152196
```
153197
````
154198

155-
With any changes to the section `A` in `main.py`, the code block section is updated in the `README` file with the next workflow run.
199+
With any changes to the section `A` in `main.py`, the code block section is updated in the `README` file with the next code-embedder run.
156200

157201
### 🧩 Object update
158202
The tag used for object update follows the same convention as the tag for section update with the following changes:
@@ -192,7 +236,7 @@ class Person:
192236
...
193237
```
194238

195-
Once the workflow runs, the code block section will be updated in the `README` file with the content of the function `print_hello` and class `Person` from the script located at `main.py` and pushed to the repository 🚀.
239+
Once code-embedder runs, the code block section will be updated in the `README` file with the content of the function `print_hello` and class `Person` from the script located at `main.py` (in case of using it as a Github Action, the changes are then pushed to the repository 🚀).
196240

197241
````md
198242
# README
@@ -215,10 +259,10 @@ class Person:
215259
```
216260
````
217261

218-
With any changes to the function `print_hello` or class `Person` in `main.py`, the code block sections are updated in the `README` file with the next workflow run.
262+
With any changes to the function `print_hello` or class `Person` in `main.py`, the code block sections are updated in the `README` file with the next code-embedder run.
219263

220264
## 🤝 Contributing
221-
We welcome contributions to improve this tool!
265+
We welcome contributions to improve this package!
222266
- If you have an idea for a **new feature** ✨, open a [new feature request](https://github.com/kvankova/code-embedder/issues/new?labels=enhancement&template=feature_request.yaml) on GitHub.
223267
- If you spot a **bug** 🐛, open a [new issue](https://github.com/kvankova/code-embedder/issues/new/choose) on GitHub.
224268
- If you want to **contribute to the code**, please pick an issue that is not assigned to anyone and comment on it, so that we know you are working on it.

entrypoint.sh

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ git config --global user.name "github-actions"
44
git config --global user.email "github-actions@github.com"
55
git remote set-url origin https://x-access-token:$GITHUB_TOKEN@github.com/$GITHUB_REPOSITORY.git
66

7-
README_PATHS=$(find . -name "*.md" -print)
8-
97
BRANCH_NAME=${GITHUB_HEAD_REF:-$GITHUB_REF_NAME}
108

119
git pull origin ${BRANCH_NAME}
1210

1311
echo "Searching for code snippets in $README_PATHS..."
1412

15-
poetry run python /app/src/main.py --readme-paths $README_PATHS
13+
poetry run python /app/src/main.py
14+
15+
if [ $? -ne 0 ]; then
16+
exit 1
17+
fi
1618

1719
if [ -n "$(git status -s)" ]; then
1820

0 commit comments

Comments
 (0)