Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format R #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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/[email protected]", Ncpus = 8)'

# Python packages
RUN pip3 install --upgrade pip && pip3 install \
Expand All @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const enum HookName {
Scalafmt = "scalafmt",
Sed = "sed",
Shfmt = "shfmt",
Styler = "styler",
Svgo = "SVGO",
TerraformFmt = "terraform fmt",
}
Expand Down Expand Up @@ -310,6 +311,36 @@ const HOOKS: Record<HookName, LockableHook> = {
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(
Expand Down