Skip to content

Commit

Permalink
Add ltex and actionlint (#1)
Browse files Browse the repository at this point in the history
* Add ltex and actionlint

* gh-hosted runnners

* Add concurrency

* No cwd
  • Loading branch information
tdeo authored Oct 16, 2023
1 parent 7816002 commit a164400
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 1 deletion.
29 changes: 29 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Tests
on:
push:

concurrency:
group: ${{ github.workflow }}-${{ github.ref == 'refs/heads/main' && github.run_id || github.ref }}
cancel-in-progress: true

jobs:
check_hooks:
name: Check hooks work
timeout-minutes: 15
runs-on: ubuntu-22.04

steps:
- name: Clone repository
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Install pre-commit
run: |
apk update && apk add python3 python3-dev py3-pip git go build-base
pip install pre-commit --ignore-installed distlib
pre-commit install --install-hooks
- name: Run pre-commit hooks
run: |
pre-commit run --all-files --show-diff-on-failure
30 changes: 30 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
repos:
- repo: meta
hooks:
- id: check-hooks-apply
- id: check-useless-excludes

- repo: local
hooks:
- id: local_hooks
name: All hooks are ran
language: ruby
entry: ./check_up_to_date.rb
pass_filenames: false
files: ^.pre-commit-(config|hooks)\.yaml$

- id: ltex
name: LTeX grammar check
language: script
entry: ltex/ltex.sh
verbose: true
types_or: ['markdown', 'mdx']
args: [--client-configuration, ltex-config.json]

- id: actionlint
name: Lint github workflows
language: golang
additional_dependencies:
- github.com/rhysd/actionlint/cmd/[email protected]
entry: actionlint
files: .github/workflows/.*\.yml
14 changes: 14 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
- id: ltex
name: LTeX grammar check
language: script
entry: ltex/ltex.sh
verbose: true
types_or: ['markdown', 'mdx']

- id: actionlint
name: Lint github workflows
language: golang
additional_dependencies:
- github.com/rhysd/actionlint/cmd/[email protected]
entry: actionlint
files: .github/workflows/.*\.yml
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# pre-commit-hooks
# Pennylane's pre-commit hooks

Homemade pre-commit hooks
Empty file added actionlint/actionlint.sh
Empty file.
12 changes: 12 additions & 0 deletions check_up_to_date.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#! /usr/bin/env ruby

require 'yaml'

hooks = YAML.safe_load_file('.pre-commit-hooks.yaml')
local_config = YAML.safe_load_file('.pre-commit-config.yaml')
.fetch('repos')
.find { _1['repo'] == 'local' }
.fetch('hooks')[1..]
local_config.each { _1.delete('args') }

raise "Update local configuration to include all hooks defined in this repo" unless hooks == local_config
10 changes: 10 additions & 0 deletions ltex-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"ltex": {
"language": "en-US",
"dictionary": {
"en-US": [
"Pennylane"
]
}
}
}
10 changes: 10 additions & 0 deletions ltex/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM openjdk:11.0.16-jre

ARG LTEX_VERSION=16.0.0

RUN <<EOF
wget https://github.com/valentjn/ltex-ls/releases/download/${LTEX_VERSION}/ltex-ls-${LTEX_VERSION}.tar.gz -O - | tar -xz
mv /ltex-ls-${LTEX_VERSION} /ltex-ls
EOF

ENTRYPOINT ["/ltex-ls/bin/ltex-cli"]
51 changes: 51 additions & 0 deletions ltex/ltex.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#! /bin/bash

set -euo pipefail

[ -z "${CI+1}" ] || exit 0

ltex_dir=$(dirname "$0")
ltex_version=""

ltex_tag=$(cd $ltex_dir; tar -cf - ./ | md5sum | cut -d' ' -f1)
ltex_version="ltex:$ltex_tag"

(
cd $ltex_dir
docker inspect "$ltex_version" > /dev/null || docker build . -t "$ltex_version" -t 'ltex:latest'
)

files_to_clean=()
files_to_process=()

function cleanup() {
if (( ${#files_to_clean[@]} )); then
rm "${files_to_clean[@]}"
fi
}

trap cleanup SIGHUP SIGINT SIGTERM

for file in "$@"; do
new_file=${file/%.mdx/.md}

if [ "$file" != "$new_file" ]; then
cp "$file" "$new_file"
files_to_process+=( "$new_file" )
files_to_clean+=( "$new_file" )
else
files_to_process+=( "$file" )
fi
done

echo "${files_to_process[@]}" |
xargs docker run --rm -i \
--user "$UID" \
--volume "$(pwd):/src:rw,Z" \
--workdir /src \
"$ltex_version" |
sed -E 's|(app/javascript/documentation/.*?\.md):|\1x:|g' || true

cleanup

exit 0

0 comments on commit a164400

Please sign in to comment.