Skip to content

Commit

Permalink
Merge pull request #2 from AlexsLemonade/jashapiro/action
Browse files Browse the repository at this point in the history
Make it a full action
  • Loading branch information
jashapiro authored Mar 7, 2024
2 parents 5caac1a + 7200ac8 commit 8a68700
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 14 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
on: [push]

jobs:
spell-check:
runs-on: ubuntu-latest
name: Test the spell check action
steps:
# To use this repository's private action,
# you must check out the repository
- name: Checkout
uses: actions/checkout@v4
- name: Spell check action
uses: ./ # Uses an action in the root directory
id: spell
# Use the output from the `spell check` step
- name: Get the number of errors
run: |
echo "There were ${{ steps.spell.outputs.error_count }} errors"
cat spell_check_errors.tsv
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ LABEL org.opencontainers.image.authors="CCDL ccdl@alexslemonade.org"
LABEL org.opencontainers.image.source="https://github.com/AlexsLemonade/spellcheck/tree/main"

# install the spelling and tidyr packages from CRAN
RUN Rscript -e "install.packages(c('spelling', 'tidyr'))"
RUN Rscript -e "install.packages(c('readr', 'spelling', 'tidyr'))"

# add spell check script and make it executable
COPY spelling.R /usr/local/bin/spell-check.R
RUN chmod +x /usr/local/bin/spell-check.R
COPY spell-check.R /spell-check.R
RUN chmod +x /spell-check.R

ENTRYPOINT ["/spell-check.R"]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
This repository contains a Docker image with the [R `spelling` package](https://cran.r-project.org/web/packages/spelling/index.html) installed.
The role of this repository is to facilitate spell checking actions across `AlexsLemonade` repositories.

speeling
20 changes: 20 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# action.yml
name: 'R spell check'
description: 'Spell check R, Rmd and md files'
inputs:
dictionary: # id of input
description: 'Dictionary file path'
required: true
default: 'components/dictionary.txt'
files:
description: Glob of files to check
required: false
outputs:
error_count:
description: The number of spelling errors
runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.dictionary }}
- ${{ inputs.files }}
27 changes: 16 additions & 11 deletions spell-check.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,33 @@
arguments <- commandArgs(trailingOnly = TRUE)
file_pattern <- "\\.(Rmd|md|rmd)$"

# dictionary is required first argument
dict_file <- arguments[1]

arguments <- arguments[-1]
# if there are arguments, check those files, otherwise check all markdown & rmd files
if (length(arguments) > 0) {
if (length(arguments) > 1) {
files <- arguments[grepl(file_pattern, arguments)]
} else {
files <- list.files(pattern = file_pattern, recursive = TRUE, full.names = TRUE)
}

# Find .git root directory
root_dir <- rprojroot::find_root(rprojroot::has_dir(".git"))

# Read in dictionary
dict_file <- file.path(root_dir, "components", "dictionary.txt")
dictionary <- readLines(dict_file)
if (file.exists(dict_file)) {
dictionary <- readLines(dict_file)
} else {
warning("Dictionary file not found")
dictionary <- ""
}

# Run spell check
spelling_errors <- spelling::spell_check_files(files, ignore = dictionary_plus) |>
spelling_errors <- spelling::spell_check_files(files, ignore = dictionary) |>
data.frame() |>
tidyr::unnest(cols = found) |>
tidyr::separate(found, into = c("file", "lines"), sep = ":")

# Print out how many spell check errors
write(nrow(spelling_errors), stdout())

# Save spell errors to file
# Save spelling errors to file
readr::write_tsv(spelling_errors, "spell_check_errors.tsv")

# Save error count to GITHUB_OUTPUT
system(paste0("echo 'error_count=", nrow(spelling_errors), "'>> $GITHUB_OUTPUT"))

0 comments on commit 8a68700

Please sign in to comment.