Skip to content

Commit

Permalink
feat(R): check README.Rmd changes are rendered
Browse files Browse the repository at this point in the history
Adds the githook generated by usethis::use_readme_rmd() to pre-commits,
ensuring any changes made to README.Rmd are properly synchronised to the
rendered README.md file.
  • Loading branch information
domodwyer committed Aug 24, 2022
1 parent e145473 commit 533b8a6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@
minimum_pre_commit_version: 0.15.0
description: 'Run lintr against R code'

- id: r-readme
name: 'Generated README.Rmd'
entry: run-r-readme.sh
language: 'script'
files: README\.Rmd$
pass_filenames: false
minimum_pre_commit_version: 0.15.0
description: 'Check README.md in-sync with README.Rmd'

- id: buf-lint
name: 'Lint protobuf files'
entry: buf-lint.sh
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Hooks:
* `rust-doc`: runs `cargo doc` against the workspace - great for linting [intra-doc links]
* `r-stylr`: runs [`stylr`] to format R code
* `r-lintr`: static analysis of R code with [`lintr`]
* `r-readme`: ensure README.Rmd changes are rendered to README.md
* `buf-lint`: runs [`buf`] lints against protobuf files
* `buf-breaking`: protobuf breaking change detection using [`buf`]

Expand Down Expand Up @@ -87,6 +88,8 @@ repos:

- id: r-lintr
stages: [commit, push]

- id: r-readme
stages: [commit, push]

- id: buf-lint
Expand Down
17 changes: 17 additions & 0 deletions run-r-readme.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

set -euo pipefail

README=($(git diff --cached --name-only | grep -Ei '^README\.[R]?md$'))

if [[ ${#README[@]} == 0 ]]; then
exit 0
fi

if [[ README.Rmd -nt README.md ]]; then
echo -e "README.md is out of date; please re-knit README.Rmd"
exit 1
elif [[ ${#README[@]} -lt 2 ]]; then
echo -e "README.Rmd and README.md should be both staged"
exit 1
fi

0 comments on commit 533b8a6

Please sign in to comment.