Skip to content

Commit

Permalink
Merge pull request #60 from maelle/master
Browse files Browse the repository at this point in the history
add pre-commit hook for DESCRIPTION vs codemeta.json cf #59
  • Loading branch information
maelle authored Apr 9, 2018
2 parents 98e17f8 + e4c9ca1 commit b369b76
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 19 deletions.
19 changes: 14 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,36 @@ Authors@R: c(person("Carl", "Boettiger",
comment=c(ORCID = "0000-0002-2378-4915")),
person("Toph", "Allen",
role = "rev",
comment=c(ORCID = "0000-0003-4580-091X")))
comment=c(ORCID = "0000-0003-4580-091X")),
person("Maëlle", "Salmon",
role = c("ctb"),
comment = c(ORCID = "0000-0002-2815-0399")),
person("rOpenSci",
role = c("fnd"),
comment = c("https://ropensci.org/")))
Description: The 'Codemeta' Project defines a 'JSON-LD' format for describing
software metadata, as detailed at <https://codemeta.github.io>. This package
provides utilities to generate, parse, and modify 'codemeta.json' files
automatically for R packages, as well as tools and examples for working with
'codemeta.json' 'JSON-LD' more generally.
License: MIT + file LICENSE
License: GPL-3
URL: https://github.com/ropensci/codemetar
BugReports: https://github.com/ropensci/codemetar/issues
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.0.1
RoxygenNote: 6.0.1.9000
Depends: R (>= 3.0.0)
Imports: jsonlite (>= 1.3),
Imports:
jsonlite (>= 1.3),
jsonld,
git2r,
devtools,
methods,
stats,
stringi,
readr
readr,
usethis,
whisker
Suggests: testthat,
jsonvalidate,
covr,
Expand Down
2 changes: 0 additions & 2 deletions LICENSE

This file was deleted.

2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ export(crosswalk)
export(crosswalk_table)
export(drop_context)
export(write_codemeta)
importFrom(devtools,build)
importFrom(devtools,use_build_ignore)
importFrom(git2r,branches)
importFrom(git2r,repository)
importFrom(jsonld,jsonld_compact)
Expand Down
2 changes: 1 addition & 1 deletion R/codemeta_description.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ codemeta_description <-
codemeta$description <- descr$Description
codemeta$name <- paste0(descr$Package, ": ", descr$Title)

## Will later guess these these a la devtools::use_github_links
## Will later guess these à la usethis::use_github_links
codemeta$codeRepository <- descr$URL
codemeta$issueTracker <- descr$BugReports

Expand Down
1 change: 1 addition & 0 deletions R/create_codemeta.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ create_codemeta <- function(pkg = ".",
## no cm, no existing codemeta.json found, start fresh
} else {
cm <- new_codemeta()

}

## we got an existing codemeta object as pkg
Expand Down
5 changes: 4 additions & 1 deletion R/guess_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,16 @@ guess_orcids <- function(codemeta) {
NULL
}

# from devtools https://github.com/r-lib/devtools/blob/21fe55a912ca4eaa49ef5b7d891ff3e2aae7a370/R/git.R#L1
# GPL>=2 code
uses_git <- function(root) {
!is.null(git2r::discover_repository(root, ceiling = 0))
}

guess_github <- function(root = ".") {
## from devtools
# https://github.com/r-lib/devtools/blob/21fe55a912ca4eaa49ef5b7d891ff3e2aae7a370/R/git.R#L130
# GPL>=2 code
remote_urls <- function (r) {
remotes <- git2r::remotes(r)
stats::setNames(git2r::remote_url(r, remotes), remotes)
Expand Down Expand Up @@ -159,7 +163,6 @@ guess_releaseNotes <- function(root = ".") {

}

#' @importFrom devtools build
guess_fileSize <- function(root = ".") {
if (is.null(root)) {
return(NULL)
Expand Down
29 changes: 29 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ get_root_path <- function(pkg){


## based on devtools::read_dcf
# https://github.com/r-lib/devtools/blob/50d2dee67922e304f494e2d5ffb1c5e61aa8e860/R/utils.r#L64
# this is GPL>=2 code
cm_read_dcf <- function(dcf) {

fields <- colnames(read.dcf(dcf))
Expand Down Expand Up @@ -46,3 +48,30 @@ is_IRI <- function(string){
grepl("^http[s]?://", string)
}


# from usethis cf https://github.com/r-lib/usethis/blob/2abb0422a97808cc573fa5900a8efcfed4c2d5b4/R/git.R#L68
# this is GPL-3 code
uses_git <- function(path = usethis::proj_get()) {
!is.null(git2r::discover_repository(path))
}

# from usethis cf https://github.com/r-lib/usethis/blob/4fb556788d2588facaaa8560242d2c83f2261d6e/R/helpers.R#L55
# this is GPL-3 code
render_template <- function(template, data = list(), package = "codemetar") {
template_path <- find_template(template, package = package)
strsplit(whisker::whisker.render(readLines(template_path), data), "\n")[[1]]
}

# from usethis cf https://github.com/r-lib/usethis/blob/4fb556788d2588facaaa8560242d2c83f2261d6e/R/helpers.R#L60
# this is GPL-3 code
find_template <- function(template_name, package = "usethis") {
path <- system.file("templates", template_name, package = package)
if (identical(path, "")) {
stop(
"Could not find template ", value(template_name),
" in package ", value(package),
call. = FALSE
)
}
path
}
26 changes: 21 additions & 5 deletions R/write_codemeta.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
#' @export
#'
#' @importFrom jsonlite write_json
#' @importFrom devtools use_build_ignore
# upcoming devtools release will switch this to:
# @importFrom usethis use_build_ignore
#' @examples
#' write_codemeta("codemeta", tempfile())
#' @details
#'
#' When creating and writing a codemeta.json for the first time,
#' the function adds "codemeta.json" to .Rbuildignore and, if the
#' project uses git, adds a pre-commit hook ensuring that if DESCRIPTION changes,
#' the codemeta.json will be updated as well unless the DESCRIPTION change is committed
#' with 'git commit --no-verify'.
write_codemeta <- function(pkg = ".",
path = "codemeta.json",
root = ".",
Expand All @@ -36,8 +40,20 @@ write_codemeta <- function(pkg = ".",
...) {

if(length(pkg) <= 1 && file.exists(file.path(pkg, "DESCRIPTION"))){
devtools::use_build_ignore("codemeta.json", pkg = pkg)
## usethis::use_build_ignore("codemeta.json", base_path = pkg)
usethis::use_build_ignore("codemeta.json")
# add the git pre-commit hook
# https://github.com/r-lib/usethis/blob/master/inst/templates/readme-rmd-pre-commit.sh#L1
# this is GPL-3 code
if (uses_git()) {
if(!file.exists(file.path(pkg, "codemeta.json"))){
message("Adding a pre-commit git hook ensuring that codemeta.json will be synchronized with DESCRIPTION") # nolint
usethis::use_git_hook(
"pre-commit",
render_template("description-codemetajson-pre-commit.sh")
)
}

}
}
cm <- create_codemeta(pkg = pkg, root = root)
jsonlite::write_json(cm, path, pretty=TRUE, auto_unbox = TRUE, ...)
Expand Down
2 changes: 1 addition & 1 deletion inst/extdata/licenses.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ spdx <- readxl::read_excel("spdx.xls")
## Manual synthesis happens here ##

cran_to_spdx <- readr::read_csv("data-raw/cran-to-spdx.csv")
devtools::use_data(cran_to_spdx, overwrite = TRUE)
usethis::use_data(cran_to_spdx, overwrite = TRUE)
12 changes: 12 additions & 0 deletions inst/templates/description-codemetajson-pre-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
DESCRIPTION=($(git diff --cached --name-only | grep 'DESCRIPTION'))
MSG="use 'git commit --no-verify' to override this check"

if [[ ${#DESCRIPTION[@]} == 0 ]]; then
exit 0
fi

if [[ DESCRIPTION -nt codemeta.json ]]; then
echo -e "codemeta.json is out of date; please re-run codemetar::write_codemeta\n$MSG"
exit 1
fi
6 changes: 4 additions & 2 deletions man/codemetar-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions man/write_codemeta.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b369b76

Please sign in to comment.