From 62e9a9a55f5af35d81b9349bba72f239105f251b Mon Sep 17 00:00:00 2001 From: Valentin Pratz <112951103+vpratz@users.noreply.github.com> Date: Thu, 28 Mar 2024 00:20:14 +0100 Subject: [PATCH] Feature: add support for pre-commit (#56) --- .pre-commit-hooks.yaml | 20 ++++++++++++++++++++ Dockerfile | 2 ++ README.md | 38 ++++++++++++++++++++++++++++++++++++++ panvimdoc.pre-commit.sh | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 .pre-commit-hooks.yaml create mode 100755 panvimdoc.pre-commit.sh diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 0000000..937c77c --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -0,0 +1,20 @@ +- id: panvimdoc-docker + name: pandoc to vimdoc (docker) + description: Convert markdown documentation to vimdoc (using docker) + entry: /panvimdoc.pre-commit.sh + language: docker + files: ^README\.md$ + args: + - '--input-file' + - README.md + pass_filenames: false +- id: panvimdoc + name: pandoc to vimdoc + description: Convert markdown documentation to vimdoc (using local environment) + entry: panvimdoc.pre-commit.sh + language: script + files: ^README\.md$ + args: + - '--input-file' + - README.md + pass_filenames: false diff --git a/Dockerfile b/Dockerfile index 0bd94ac..ffc9ca8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,11 +4,13 @@ RUN apk update && apk upgrade && apk add bash vim neovim # Copies your code file repository to the filesystem COPY panvimdoc.sh /panvimdoc.sh +COPY panvimdoc.pre-commit.sh /panvimdoc.pre-commit.sh COPY scripts/ /scripts/ COPY lib/Demojify.lua /usr/share/lua/common/lib/Demojify.lua # change permission to execute the script and RUN chmod +x /panvimdoc.sh +RUN chmod +x /panvimdoc.pre-commit.sh # file to execute when the docker container starts up ENTRYPOINT ["/panvimdoc.sh"] diff --git a/README.md b/README.md index e18e5a3..0c3b427 100644 --- a/README.md +++ b/README.md @@ -171,6 +171,44 @@ jobs: +### Using pre-commit +[pre-commit](https://pre-commit.com/) lets you easily install and manage pre-commit hooks locally. + +Two hooks are available, differing only in the way dependencies are handled: + +- `panvimdoc-docker`: Requires a running Docker engine on your host. All other dependencies will be loaded inside the container. +- `panvimdoc`: Runs in your local environment, so you have to make sure all dependencies of panvimdoc are installed + (i.e., `pandoc v3.0.0` or greater) + +To use a hook, first install pre-commit. Then, add the following to your `.pre-commit-config.yaml` (here `panvimdoc-docker` is used): + +```yaml +- repo: 'https://github.com/kdheepak/panvimdoc' + rev: v4.0.1 + hooks: + - id: panvimdoc-docker + args: + - '--project-name' + - +``` + +You can specify additional arguments to `panvimdoc.sh` using `args`. See the section below (or run `./panvimdoc.sh`) for the full list of arguments. + +To change the input file, modify the `files` field of the hook and supply the corresponding `--input-file` to `args`. In the example below, the hook will be triggered if any `.md` file changes: + +```yaml +- repo: 'https://github.com/kdhee' + rev: v4.0.1 + hooks: + - id: panvimdoc-docker + files: ^.*\.md$ + args: + - '--project-name' + - + - '--input-file' + - +``` + ### Using it manually The `./panvimdoc.sh` script runs `pandoc` along with all the filters and custom output writer. diff --git a/panvimdoc.pre-commit.sh b/panvimdoc.pre-commit.sh new file mode 100755 index 0000000..02cac07 --- /dev/null +++ b/panvimdoc.pre-commit.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +args=$# # number of command line args +for (( i=1; i<=$args; i+=2 )) # loop from 1 to N (where N is number of args) +do + j=$((i + 1 )) + if [ "${!i}" == "--project-name" ] ; then + PROJECT_NAME="${!j}" + break; + fi +done + +# extract project name to determine file location +if [ -z "$PROJECT_NAME" ]; then + echo "Error: argument --project-name is not set." + exit 1 +fi +# store hash to check whether documentation has changed +if [ -f "doc/$PROJECT_NAME.txt" ]; then + DOC_HASH_PRE="$(md5sum doc/$PROJECT_NAME.txt)" +else + DOC_HASH_PRE="" +fi +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +source "$SCRIPT_DIR/panvimdoc.sh" +DOC_HASH_POST="$(md5sum doc/$PROJECT_NAME.txt)" +# send FAIL when doc has changed +if [ "$DOC_HASH_PRE" != "$DOC_HASH_POST" ] ; then + echo "" + echo 'Updated documentation "doc/$PROJECT_NAME.txt"' + echo 'Use "git add doc/$PROJECT_NAME.txt" to stage the changes' + exit 1 +fi