From a8a977e25f90cd39feab10ff37f92c274d9dba2c Mon Sep 17 00:00:00 2001 From: Art Chaidarun Date: Sat, 26 Jun 2021 03:08:16 -0400 Subject: [PATCH] Format R --- Dockerfile | 16 ++++++++++------ README.md | 1 + entry.ts | 31 +++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 853bca8..88470c8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,18 +1,22 @@ FROM alpine:3.12.1 -# Alpine base packages +# Alpine packages RUN apk update && apk upgrade && apk add --no-cache \ bash \ build-base \ + clang \ git \ nodejs-npm \ openjdk11 \ py3-pip \ - python3-dev + python3-dev \ + R \ + R-dev -# Alpine tool packages -RUN apk update && apk upgrade && apk add --no-cache \ - clang +# R packages +RUN Rscript -e '\ + install.packages("remotes", repos = "https://cloud.r-project.org"); \ + remotes::install_github("r-lib/styler@v1.4.1", Ncpus = 8)' # Python packages RUN pip3 install --upgrade pip && pip3 install \ @@ -34,7 +38,7 @@ RUN wget https://releases.hashicorp.com/terraform/0.11.14/terraform_0.11.14_linu # Scala packages RUN wget https://github.com/coursier/coursier/releases/download/v2.0.6/coursier -O /bin/coursier \ - && chmod +x /bin/coursier + && chmod +x /bin/coursier RUN coursier bootstrap org.scalameta:scalafmt-cli_2.13:2.7.5 \ -r sonatype:snapshots --main org.scalafmt.cli.Cli \ --standalone \ diff --git a/README.md b/README.md index 904ea39..f5d729b 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ This repo currently contains a single [pre-commit](https://pre-commit.com/) hook - [ktfmt](https://github.com/facebookincubator/ktfmt) v0.25 for Kotlin - [scalafmt](https://scalameta.org/scalafmt/) v2.7.5 for Scala - [shfmt](https://github.com/mvdan/sh) v3.2.0 for Shell +- [styler](https://github.com/r-lib/styler) v1.4.1 for R - [terraform fmt](https://github.com/hashicorp/terraform) v0.11.14 and v0.12.29 for Terraform - [ClangFormat](https://clang.llvm.org/docs/ClangFormat.html) v10.0.0 for Protobuf - [SVGO](https://github.com/svg/svgo) v1.3.2 for SVG diff --git a/entry.ts b/entry.ts index 06da69d..722aaca 100644 --- a/entry.ts +++ b/entry.ts @@ -76,6 +76,7 @@ const enum HookName { Scalafmt = "scalafmt", Sed = "sed", Shfmt = "shfmt", + Styler = "styler", Svgo = "SVGO", TerraformFmt = "terraform fmt", } @@ -310,6 +311,36 @@ const HOOKS: Record = { include: /./, runAfter: [HookName.Sed], }), + [HookName.Styler]: createLockableHook({ + action: async sources => { + const output = await run( + "Rscript", + "-e", + // We need to set `R.cache.rootPath` ourselves because otherwise Styler + // fails (but only when actually run via pre-commit, not when tested + // directly in a `docker run [...] bash` terminal?!) with this error: + // + // When processing foobar.r: .onLoad failed in loadNamespace + // () for 'R.cache', details: + // call: mkdirs.default(parent, mustWork = mustWork, maxTries = + // maxTries, + // error: Failed to create directory (tried 5 times), most likely + // because of lack of file permissions (directory '/' exists but + // nothing beyond): //.cache + `options(R.cache.rootPath = "/tmp/rcache");${sources + .map(s => `styler::style_file("${s}", scope = "line_breaks")`) + .join(";")}`, + ); + + // R doesn't exit with nonzero code upon failure, so we have to detect + // failure ourselves :/ + if (!/\s0\s+Styling threw an error/.test(output)) { + throw output; + } + }, + include: /\.(R|Rmd|Rnw|Rprofile)$/i, + runAfter: [HookName.Sed], + }), [HookName.Svgo]: createLockableHook({ action: sources => run(