Skip to content

Commit 21add8a

Browse files
committed
Initial {renv} hook
1 parent 1905f39 commit 21add8a

File tree

5 files changed

+146
-3
lines changed

5 files changed

+146
-3
lines changed

.pre-commit-hooks.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,10 @@
120120
language: script
121121
minimum_pre_commit_version: "2.13.0"
122122
files: '^man/|_pkgdown\.yml'
123+
- id: validate-renv-lock
124+
name: validate-renv-lock
125+
description: Validate that your `renv.lock`` file is valid json and fits the {renv} schema
126+
entry: inst/hooks/exported/validate-renv-lock.R
127+
language: r
128+
minimum_pre_commit_version: "2.13.0"
129+
files: '^renv\.lock$'
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env Rscript
2+
3+
if (!require(readr, quietly = TRUE)) {
4+
stop("{readr} could not be loaded, please install it.")
5+
}
6+
7+
if (!require(jsonvalidate, quietly = TRUE)) {
8+
stop("{jsonvalidate} could not be loaded, please install it.")
9+
}
10+
11+
library(readr)
12+
library(jsonvalidate)
13+
14+
files <- commandArgs(trailing = TRUE)
15+
path_renv_schema <- file.path("inst", "renv.schema.json")
16+
renv_schema <- readr::read_file(path_renv_schema)
17+
18+
validate_renv_lock <- function(renv_path, renv_lock_schema) {
19+
renv_json <- readr::read_file(renv_path)
20+
21+
jsonvalidate::json_validate(
22+
renv_json,
23+
renv_lock_schema,
24+
engine = "ajv",
25+
greedy = TRUE,
26+
error = TRUE,
27+
verbose = TRUE
28+
)
29+
}
30+
31+
#files <- list.files(recursive = TRUE)
32+
out <- lapply(files, function(path) {
33+
is_renv_lock <- grepl("^.*renv\\.lock$", path)
34+
if (is_renv_lock) {
35+
print(paste0(path, " is a renv.lock file"))
36+
tryCatch(
37+
validate_renv_lock(path, renv_schema),
38+
error = function(error) {
39+
cat(c("renv.lock at", path, "failed validation. Full context:\n"))
40+
stop(conditionMessage(error), call. = FALSE)
41+
}
42+
)
43+
}
44+
})

inst/pre-commit-hooks.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,10 @@
120120
language: script
121121
minimum_pre_commit_version: "2.13.0"
122122
files: '^man/|_pkgdown\.yml'
123+
- id: validate-renv-lock
124+
name: validate-renv-lock
125+
description: Validate that your `renv.lock`` file is valid json and fits the {renv} schema
126+
entry: inst/hooks/exported/validate-renv-lock.R
127+
language: r
128+
minimum_pre_commit_version: "2.13.0"
129+
files: '^renv\.lock$'

inst/renv.schema.json

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "renv.lock file.",
4+
"description": "A schema for renv.lock files generated by {renv}",
5+
"type": "object",
6+
"properties": {
7+
"R": {
8+
"type": "object",
9+
"properties": {
10+
"Version": {
11+
"type": "string"
12+
},
13+
"Repositories": {
14+
"type": "array",
15+
"items": {
16+
"type": "object",
17+
"properties": {
18+
"Name": {
19+
"type": "string"
20+
},
21+
"URL": {
22+
"type": "string",
23+
"format": "uri"
24+
}
25+
},
26+
"required": ["Name", "URL"]
27+
}
28+
}
29+
},
30+
"required": ["Version", "Repositories"]
31+
},
32+
"Packages": {
33+
"type": "object",
34+
"additionalProperties": {
35+
"type": "object",
36+
"properties": {
37+
"Package": {
38+
"type": "string"
39+
},
40+
"Version": {
41+
"type": "string"
42+
},
43+
"Source": {
44+
"type": "string"
45+
},
46+
"Repository": {
47+
"type": "string"
48+
},
49+
"Hash": {
50+
"type": "string",
51+
"pattern": "^[a-fA-F0-9]{32}$"
52+
},
53+
"RemoteType": {
54+
"type": "string"
55+
},
56+
"RemoteHost": {
57+
"type": "string",
58+
"format": "uri"
59+
},
60+
"RemoteUsername": {
61+
"type": "string"
62+
},
63+
"RemoteRepo": {
64+
"type": "string"
65+
},
66+
"RemoteRef": {
67+
"type": "string"
68+
},
69+
"RemoteSha": {
70+
"type": "string",
71+
"pattern": "^[a-fA-F0-9]{40}$"
72+
},
73+
"Requirements": {
74+
"type": "array",
75+
"items": {
76+
"type": "string"
77+
}
78+
}
79+
},
80+
"required": ["Package", "Version", "Source", "Hash"]
81+
}
82+
}
83+
},
84+
"required": ["R", "Packages"]
85+
}

renv.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"digest",
2323
"utils"
2424
],
25-
"Hash": "fe539ca3f8efb7410c3ae2cf5fe6c0f8"
25+
"Hash": "fe539c3f8efb7410c3ae2cf5fe6c0f8"
2626
},
2727
"R.methodsS3": {
2828
"Package": "R.methodsS3",
@@ -33,7 +33,7 @@
3333
"R",
3434
"utils"
3535
],
36-
"Hash": "278c286fd6e9e75d0c2e8f731ea445c8"
36+
"Hash": "278c286fd6e9e75d02e8f731ea445c8"
3737
},
3838
"R.oo": {
3939
"Package": "R.oo",
@@ -118,7 +118,7 @@
118118
"Package": "cli",
119119
"Version": "3.6.2",
120120
"Source": "Repository",
121-
"Repository": "RSPM",
121+
"Repository": true,
122122
"Requirements": [
123123
"R",
124124
"utils"

0 commit comments

Comments
 (0)