diff --git a/.github/workflows/pr-commands.yaml b/.github/workflows/pr-commands.yaml new file mode 100644 index 00000000..8a86c68e --- /dev/null +++ b/.github/workflows/pr-commands.yaml @@ -0,0 +1,91 @@ +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +on: + issue_comment: + types: [created] + +name: pr-commands.yaml + +permissions: read-all + +jobs: + document: + if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/document') }} + name: document + runs-on: ubuntu-latest + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + permissions: + contents: write + steps: + - uses: actions/checkout@v4 + + - uses: r-lib/actions/pr-fetch@v2 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + + - uses: r-lib/actions/setup-r@v2 + with: + use-public-rspm: true + + - uses: r-lib/actions/setup-r-dependencies@v2 + with: + extra-packages: any::roxygen2 + needs: pr-document + + - name: Document + run: roxygen2::roxygenise() + shell: Rscript {0} + + - name: commit + run: | + git config --local user.name "$GITHUB_ACTOR" + git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" + git add man/\* NAMESPACE + git commit -m 'Document' + + - uses: r-lib/actions/pr-push@v2 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + + style: + if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/style') }} + name: style + runs-on: ubuntu-latest + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + permissions: + contents: write + steps: + - uses: actions/checkout@v4 + + - uses: r-lib/actions/pr-fetch@v2 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + + - uses: r-lib/actions/setup-r@v2 + + - name: Install dependencies + run: install.packages(c("styler", "roxygen2")) + shell: Rscript {0} + + - name: Style + run: | + source("inst/styler/bgms_style.R") + styler::style_pkg( + style = bgms_style, + exclude_files = c("R/RcppExports\\.R", "R/cpp11\\.R", "R/import-standalone.*\\.R", "\\.Rprofile\\.R") + ) + + shell: Rscript {0} + + - name: commit + run: | + git config --local user.name "$GITHUB_ACTOR" + git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" + git add \*.R + git commit -m 'Style' + + - uses: r-lib/actions/pr-push@v2 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/inst/styler/bgms_style.R b/inst/styler/bgms_style.R new file mode 100644 index 00000000..403b4299 --- /dev/null +++ b/inst/styler/bgms_style.R @@ -0,0 +1,30 @@ +bgms_style <- function() { + style <- styler::tidyverse_style() + # style$space$spacing_before_comments() + # "if (condition)" -> "if(condition)" + style$space$add_space_after_for_if_while <- function(pd_flat) { + paren_after <- pd_flat$token %in% c("FOR", "IF", "WHILE") + if (!any(paren_after)) { + return(pd_flat) + } + pd_flat$spaces[paren_after & (pd_flat$newlines == 0L)] <- 0L + pd_flat + } + + # "a <- 1" -> "a = 1" + style$token$force_assignment_op <- function(pd) { + to_replace <- pd$token == "EQ_ASSIGN" + pd$token[to_replace] <- "LEFT_ASSIGN" + pd$text[to_replace] <- "=" + pd + } + style +} + +# run the styler locally via +# note that this cannot be undone! make sure to commit your changes beforehand +# source("inst/styler/bgms_style.R") +# styler::style_pkg( +# style = bgms_style, +# exclude_files = c("R/RcppExports\\.R", "R/cpp11\\.R", "R/import-standalone.*\\.R", "\\.Rprofile\\.R") +# )