Skip to content

Commit 81dde08

Browse files
committed
🔥 Change all shell scripts to perl
Add eval-bash Update CI Remove shfmt in github workflow Add cache-dependency-path in github workflow Fix templates/class.txt Update README.md
1 parent dd6b1f2 commit 81dde08

File tree

12 files changed

+106
-83
lines changed

12 files changed

+106
-83
lines changed

.github/workflows/main.yml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,9 @@ jobs:
3131
with:
3232
python-version: ${{ env.python-version }}
3333
cache: ${{ env.cache }}
34-
- name: Install dependencies for macOS
35-
if: runner.os == 'macOS'
36-
run: |
37-
brew update
38-
brew install shellcheck shfmt
39-
- name: Install dependencies for Linux
40-
if: runner.os == 'Linux'
41-
run: |
42-
sudo apt update
43-
sudo apt install shfmt
34+
cache-dependency-path: |-
35+
requirements.txt
36+
requirements/dev.txt
4437
- name: Install dependencies
4538
run: |
4639
pip install -e '.[dev]'
@@ -68,6 +61,9 @@ jobs:
6861
with:
6962
python-version: ${{ env.python-version }}
7063
cache: ${{ env.cache }}
64+
cache-dependency-path: |-
65+
requirements.txt
66+
requirements/dev.txt
7167
- name: Install dependencies
7268
run: |
7369
pip install build

.pre-commit-config.yaml

100644100755
Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
#!/usr/bin/env -S pre-commit run -ac
12
---
23
repos:
34
- repo: https://github.com/pre-commit/pre-commit-hooks
4-
rev: v4.3.0
5+
rev: v4.4.0
56
hooks:
67
- id: check-added-large-files
78
- id: fix-byte-order-marker
@@ -11,6 +12,7 @@ repos:
1112
- id: trailing-whitespace
1213
- id: mixed-line-ending
1314
- id: end-of-file-fixer
15+
exclude: ^templates/class\.txt$
1416
- id: detect-private-key
1517
- id: check-symlinks
1618
- id: check-ast
@@ -31,21 +33,19 @@ repos:
3133
additional_dependencies:
3234
- tomli
3335
- repo: https://github.com/jorisroovers/gitlint
34-
rev: v0.17.0
36+
rev: v0.18.0
3537
hooks:
3638
- id: gitlint
3739
args:
3840
- --msg-filename
3941
- repo: https://github.com/editorconfig-checker/editorconfig-checker.python
40-
rev: 2.6.1
42+
rev: 2.6.2
4143
hooks:
4244
- id: editorconfig-checker
4345
- repo: https://github.com/jumanjihouse/pre-commit-hooks
4446
rev: 3.0.0
4547
hooks:
4648
- id: check-mailmap
47-
- id: shellcheck
48-
- id: shfmt
4949
- repo: https://github.com/adrienverge/yamllint
5050
rev: v1.28.0
5151
hooks:
@@ -57,28 +57,24 @@ repos:
5757
args:
5858
- --number
5959
additional_dependencies:
60-
- mdformat-gfm
6160
- mdformat-myst
62-
- mdformat-tables
6361
- mdformat-toc
64-
- mdformat-footnote
65-
- mdformat-frontmatter
6662
- mdformat-deflist
6763
- mdformat-beautysh
6864
- mdformat-black
6965
- mdformat-config
7066
- repo: https://github.com/DavidAnson/markdownlint-cli2
71-
rev: v0.5.1
67+
rev: v0.6.0
7268
hooks:
7369
- id: markdownlint-cli2
7470
additional_dependencies:
7571
7672
- repo: https://github.com/psf/black
77-
rev: 22.10.0
73+
rev: 22.12.0
7874
hooks:
7975
- id: black
8076
- repo: https://github.com/PyCQA/isort
81-
rev: 5.10.1
77+
rev: 5.11.4
8278
hooks:
8379
- id: isort
8480
- repo: https://github.com/pycqa/pydocstyle
@@ -88,7 +84,7 @@ repos:
8884
additional_dependencies:
8985
- toml
9086
- repo: https://github.com/kumaraditya303/mirrors-pyright
91-
rev: v1.1.278
87+
rev: v1.1.286
9288
hooks:
9389
- id: pyright
9490
- repo: https://github.com/PyCQA/bandit
@@ -102,6 +98,4 @@ repos:
10298

10399
ci:
104100
skip:
105-
- shellcheck
106-
- shfmt
107101
- pyright

README.md

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,28 @@ Result:
8989
My OS is linux-gnu.
9090
```
9191

92+
**NOTE:** the current working directory depends on you. That is, if you run
93+
`cd docs && sphinx-build . _build/html && cd -`, CWD will be `docs`, which is
94+
the default setting of <https://readthedocs.org>. So if your code structure is
95+
like
96+
97+
```console
98+
$ tree --level 1
99+
 .
100+
├──  docs
101+
├──  scripts
102+
├──  src
103+
└──  tests
104+
```
105+
106+
And you want to run `scripts/*.sh`, you need `cd ..` firstly from `docs` to
107+
`.` else you have to run `../scripts/*.sh`.
108+
92109
### Advanced Usages
93110

94111
All of the following examples are myst. The corresponding examples of rst are
95-
similar. Remember to add `scripts` to your `$PATH` in `docs/conf.py`.
96-
Click the hyperlink of title to see the actual example.
112+
similar. Click the hyperlinks of the titles and scripts to see the actual
113+
examples.
97114

98115
#### [Generate API Document](https://github.com/Freed-Wu/translate-shell/tree/main/docs/api/translate_shell.md)
99116

@@ -118,28 +135,21 @@ Now
118135

119136
````{eval-rst}
120137
```{eval-sh}
121-
generate-api.md.sh '' -maxdepth 1
138+
cd ..
139+
scripts/generate-api.md.pl src/*/*.py
122140
```
123141
````
124142
`````
125143

126-
Where `scripts/generate-api.md.sh` is a script which search all python file and
127-
replace them from `src/translate_shell/XXX.py` to
144+
Where
145+
[`scripts/generate-api.md.pl`](https://github.com/Freed-Wu/translate-shell/blob/main/scripts/generate-api.md.pl)
146+
replaces all `src/translate_shell/XXX.py` to
128147

129148
```rst
130149
.. automodule:: translate_shell.XXX
131150
:members:
132151
```
133152

134-
```bash
135-
#!/usr/bin/env bash
136-
# shellcheck disable=SC2086
137-
cd "$(dirname "$(dirname "$0")")" || exit 1
138-
find src/translate_shell/$1 $2 $3 -name '*.py' |
139-
perl -pe's=src/=.. automodule:: =g;
140-
s=\.py$=\n :members:=g;s=/__init__==g;s=/=.=g'
141-
```
142-
143153
#### [Generate TODO Document](https://github.com/Freed-Wu/translate-shell/tree/main/docs/misc/todo.md)
144154

145155
Before:
@@ -156,26 +166,20 @@ Before:
156166
- ...
157167
```
158168

159-
Now:
169+
Now: (notice `eval-bash` because readthedocs uses dash as their default `$SHELL`)
160170

161171
````markdown
162172
# TODO
163173

164-
```{eval-sh}
165-
generate-todo.md.sh
174+
```{eval-bash}
175+
cd ..
176+
scripts/generate-todo.md.pl src/**/*
166177
```
167178
````
168179

169-
Where `scripts/generate-todo.md.sh` searches all `TODO`s in code then convert
170-
them to correct hyperlinks.
171-
172-
```sh
173-
#!/usr/bin/env bash
174-
cd "$(dirname "$(dirname "$0")")" || exit 1
175-
grep -RIn TODO: src | perl -pe's/:/#L/;s/:\s*#?\s*TODO:\s*/ /;
176-
s=^=- https://github.com/Freed-Wu/tranlate-shell/tree/main/=g;
177-
s=(https://\S+)=<\1>=g;s=^(- \S+)=\1\n=g'
178-
```
180+
Where
181+
[`scripts/generate-todo.md.pl`](https://github.com/Freed-Wu/translate-shell/blob/main/scripts/generate-todo.md.pl)
182+
searches all `TODO`s in code then convert them to correct hyperlinks.
179183

180184
#### [Generate Requirements Document](https://github.com/Freed-Wu/translate-shell/tree/main/docs/resources/requirements.md)
181185

@@ -199,23 +203,14 @@ Now
199203
# Requirements
200204

201205
```{eval-sh}
202-
generate-requirements.md.sh
206+
cd ..
207+
generate-requirements.md.pl
203208
```
204209
````
205210

206-
Where `scripts/generate-requirements.md.sh` searches all `requirements/*.txt`s:
207-
208-
```bash
209-
#!/usr/bin/env bash
210-
cd "$(dirname "$(dirname "$0")")" || exit 1
211-
for file in requirements/*.txt ; do
212-
filename="${file##*/}"
213-
perl -pe's=^([^#\n]\S*)=- [\1](https://pypi.org/project/\1)=g;
214-
s/^#\s*//g;s/^!.*/## '"${filename%%.txt}"'/g' < "$file"
215-
done
216-
```
217-
218-
And `requirements/completion.txt` is:
211+
Where
212+
[`scripts/generate-requirements.md.pl`](https://github.com/Freed-Wu/translate-shell/blob/main/scripts/generate-requirements.md.pl)
213+
searches all `requirements/*.txt`s and `requirements/completion.txt` is:
219214

220215
```unixconfig
221216
#!/usr/bin/env -S pip install -r

docs/api/eval.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
````{eval-rst}
44
```{eval-sh}
5-
generate-api.md.sh
5+
cd ..
6+
generate-api.md.pl src/*/*/*.py
67
```
78
````

docs/resources/requirements.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Requirements
22

33
```{eval-sh}
4-
generate-requirements.md.sh
4+
cd ..
5+
generate-requirements.md.pl requirements/*.txt
56
```

scripts/generate-api.md.pl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env -S perl -n
2+
$. = 1 unless $#ARGV == $oldargc;
3+
$oldargc = $#ARGV;
4+
next unless $. == 1;
5+
$_ = $ARGV;
6+
s=src/=.. automodule:: =;
7+
s=\.py$=\n :members:\n=;
8+
s=/__init__==;
9+
s=/=.=g;
10+
print;

scripts/generate-api.md.sh

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

scripts/generate-requirements.md.pl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env -S perl -p
2+
$title = $ARGV;
3+
$title =~ s=.*?requirements/(.*?)\.txt=$1=;
4+
s`^([^#\n][^\s>=<^!^]*)`- [$1](https://pypi.org/project/$1)`;
5+
s/^#\s*//;
6+
s/^!.*/## $title/;

scripts/generate-requirements.md.sh

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

src/sphinxcontrib/eval/__init__.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ def eval_sh(input: str) -> str:
3434
return output
3535

3636

37+
def eval_bash(input: str) -> str:
38+
"""Eval bash.
39+
40+
:param input:
41+
:type input: str
42+
:rtype: str
43+
"""
44+
try:
45+
output = check_output( # nosec B603 B607
46+
["bash", "-c", input], universal_newlines=True
47+
)
48+
except Exception:
49+
output = ""
50+
return output
51+
52+
3753
def eval_python(input: str) -> str:
3854
"""Eval python.
3955
@@ -52,7 +68,7 @@ def eval_python(input: str) -> str:
5268
return output
5369

5470

55-
EVAL_FUNCS = {"sh": eval_sh, "python": eval_python}
71+
EVAL_FUNCS = {"sh": eval_sh, "bash": eval_bash, "python": eval_python}
5672

5773

5874
def setup(app: Sphinx) -> dict[str, Any]:

0 commit comments

Comments
 (0)