Skip to content

Commit

Permalink
Merge pull request #70 from ropensci/I41
Browse files Browse the repository at this point in the history
start using desc cf #41
  • Loading branch information
maelle authored Apr 9, 2018
2 parents b369b76 + f879691 commit c56c484
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 97 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Imports:
stats,
stringi,
readr,
desc,
usethis,
whisker
Suggests: testthat,
Expand Down
53 changes: 26 additions & 27 deletions R/codemeta_description.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,39 @@ new_codemeta <- function() {
codemeta_description <-
function(f, id = NULL, codemeta = new_codemeta()) {
if (file.exists(f)) {
descr <- cm_read_dcf(f)
descr <- desc::desc(f)
} else {
return(codemeta)
}

if (is.null(descr) || length(descr) == 0) {
return(codemeta)
}

## FIXME define an S3 class based on the codemeta list of lists?
if (is.null(id)) {
id <- descr$Package
id <- descr$get("Package")
}

if (is_IRI(id)) {
codemeta$`@id` <- id
}

codemeta$identifier <- descr$Package
codemeta$description <- descr$Description
codemeta$name <- paste0(descr$Package, ": ", descr$Title)
codemeta$identifier <- descr$get("Package")
codemeta$description <- descr$get("Description")
codemeta$name <- paste0(descr$get("Package"), ": ",
descr$get("Title"))


## Will later guess these these a la devtools::use_github_links
codemeta$codeRepository <- descr$get("URL")
codemeta$issueTracker <- descr$get("BugReports")

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

## According to crosswalk, codemeta$dateModified and
## codemeta$dateCreated are not crosswalked in DESCRIPTION
codemeta$datePublished <-
descr$Date # probably not avaialable as descr$Date.
codemeta$datePublished <- NULL

codemeta$license <- spdx_license(descr$License)
codemeta$license <- spdx_license(descr$get("License"))

codemeta$version <- descr$Version
codemeta$version <- as.character(descr$get_version())
codemeta$programmingLanguage <-
list(
"@type" = "ComputerLanguage",
Expand All @@ -64,23 +63,23 @@ codemeta_description <-
codemeta$runtimePlatform <- R.version.string

if (is.null(codemeta$provider))
codemeta$provider <- guess_provider(descr$Package)
if ("Authors@R" %in% names(descr)) {
codemeta$provider <- guess_provider(descr$get("Package"))
authors <- try(descr$get_authors(), silent = TRUE)
if (!inherits(authors,'try-error')) {
codemeta <-
parse_people(eval(parse(text = descr$`Authors@R`)), codemeta)
parse_people(authors, codemeta)
} else {
codemeta <- parse_people(as.person(descr$Author), codemeta)
## maintainer must come second in case Author list also specifies
## maintainer by role [cre] without email
codemeta$maintainer <-
person_to_schema(as.person(descr$Maintainer))
stop("No correct Authors@R field in DESCRIPTION, please add authors via Authors@R") # nolint

}

codemeta$softwareSuggestions <- parse_depends(descr$Suggests)
codemeta$softwareRequirements <-
c(parse_depends(descr$Imports),
parse_depends(descr$Depends))
dependencies <- descr$get_deps()
suggests <- dependencies[dependencies$type == "Suggests",]
requirements <- dependencies[dependencies$type %in%
c("Imports", "Depends"),]

codemeta$softwareSuggestions <- parse_depends(suggests)
codemeta$softwareRequirements <- parse_depends(requirements)



Expand Down
2 changes: 1 addition & 1 deletion R/create_codemeta.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#' @return a codemeta list object
#' @export
#' @examples
#' cm <- create_codemeta("jsonlite")
#' cm <- create_codemeta("jqr")
#' cm$keywords <- list("metadata", "ropensci")
#' @importFrom jsonlite read_json
create_codemeta <- function(pkg = ".",
Expand Down
2 changes: 1 addition & 1 deletion R/parse_citation.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ guess_citation <- function(pkg){
installed <- installed.packages()
if(file.exists(file.path(pkg, "inst/CITATION"))){
bib <- readCitationFile(file.path(pkg, "inst/CITATION"),
meta = cm_read_dcf(file.path(pkg, "DESCRIPTION")))
meta = desc::desc(file.path(pkg, "DESCRIPTION")))
lapply(bib, parse_citation)
} else if(pkg %in% installed[,1]){
bib <- suppressWarnings(citation(pkg)) # don't worry if no date
Expand Down
51 changes: 18 additions & 33 deletions R/parse_depends.R
Original file line number Diff line number Diff line change
@@ -1,41 +1,26 @@
## internal method for parsing a list of package dependencies into pkg URLs

format_depend <- function(package, version){
dep <- list("@type" = "SoftwareApplication",
identifier = package,
## FIXME technically the name includes the title
name = package)

## Add Version if available
if (version != "*"){
dep$version <- version
}

dep$provider <- guess_provider(package)

## implemention could be better, e.g. support versioning
# dep$`@id` <- guess_dep_id(dep)
dep
}

parse_depends <- function(deps) {
if (!is.null(deps))
str <- strsplit(deps, ",\n*")[[1]]
else
str <- NULL

lapply(str, function(str) {
#if (length(str) > 1) {
# warning(paste0("package depends", str, "may be multiple packages?"))
#}

pkg <- gsub("\\s*(\\w+)\\s.*", "\\1", str)
pkg <- gsub("\\s+", "", pkg)

dep <- list("@type" = "SoftwareApplication",
identifier = pkg,
## FIXME technically the name includes the title
name = pkg)

## Add Version if available
pattern <- "\\s*\\w+\\s+\\([><=]+\\s([1-9.\\-]*)\\)*"
version <- gsub(pattern, "\\1", str)
version <-
gsub("\\)$", "", version) ## hack, avoid extraneous ending )
has_version <- grepl(pattern, str)
if (has_version)
dep$version <- version

dep$provider <- guess_provider(pkg)

## implemention could be better, e.g. support versioning
# dep$`@id` <- guess_dep_id(dep)
dep
})

unname(mapply(format_depend, deps$package, deps$version))
}


Expand Down
10 changes: 0 additions & 10 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@ 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))
as.list(read.dcf(dcf, keep.white = fields)[1, ])

}

## Like system.file, but pkg can instead be path to package root directory
get_file <- function(FILE, pkg = "."){
f <- file.path(pkg, FILE)
Expand Down
36 changes: 27 additions & 9 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"identifier": "codemetar",
"description": "The 'Codemeta' Project defines a 'JSON-LD' format for describing\n software metadata, as detailed at <https://codemeta.github.io>. This package\n provides utilities to generate, parse, and modify 'codemeta.json' files \n automatically for R packages, as well as tools and examples for working with\n 'codemeta.json' 'JSON-LD' more generally.",
"name": "codemetar: Generate 'CodeMeta' Metadata for R Packages",
"issueTracker": "https://github.com/ropensci/codemetar/issues",
"license": "https://spdx.org/licenses/MIT",
"version": "0.1.5",
"programmingLanguage": {
Expand Down Expand Up @@ -141,7 +140,7 @@
"@type": "SoftwareApplication",
"identifier": "dplyr",
"name": "dplyr",
"version": "0.7.0",
"version": ">= 0.7.0",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
Expand Down Expand Up @@ -184,11 +183,17 @@
}
],
"softwareRequirements": [
{
"@type": "SoftwareApplication",
"identifier": "R",
"name": "R",
"version": ">= 3.0.0"
},
{
"@type": "SoftwareApplication",
"identifier": "jsonlite",
"name": "jsonlite",
"version": "1.3",
"version": ">= 1.3",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
Expand Down Expand Up @@ -263,18 +268,31 @@
},
{
"@type": "SoftwareApplication",
"identifier": "R",
"name": "R",
"version": "3.0.0"
"identifier": "desc",
"name": "desc",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Central R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
}
}
],
"codeRepository": "https://github.com/ropensci/codemetar",
"isPartOf": "https://ropensci.org",
"keywords": ["metadata", "codemeta", "ropensci", "citation", "credit", "linked-data"],
"keywords": [
"metadata",
"codemeta",
"ropensci",
"citation",
"credit",
"linked-data"
],
"relatedLink": "https://codemeta.github.io/codemetar",
"contIntegration": "https://travis-ci.org/ropensci/codemetar",
"developmentStatus": "active",
"releaseNotes": "https://github.com/ropensci/codemetar/blob/master/NEWS.md",
"readme": "https://github.com/ropensci/codemetar/blob/master/README.md",
"fileSize": "266.748KB"
"fileSize": "266.286KB",
"codeRepository": "https://github.com/ropensci/codemetar",
"issueTracker": "https://github.com/ropensci/codemetar/issues"
}
2 changes: 1 addition & 1 deletion man/codemetar-package.Rd

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

2 changes: 1 addition & 1 deletion man/create_codemeta.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-codemeta_description.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ testthat::test_that("We can use a preset id", {

testthat::test_that("We can parse plain Authors: & Maintainers: entries", {
f <- system.file("examples/DESCRIPTION_ex1.dcf", package = "codemetar")
codemeta_description(f)
expect_error(codemeta_description(f))
})

27 changes: 14 additions & 13 deletions tests/testthat/test-parse_depends.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@ testthat::context("parse_depends.R")

testthat::test_that("Test the various cases for dependencies", {

a <- parse_depends(NULL)
testthat::expect_length(a, 0)
a <- parse_depends(deps = "a4") # BIOC provider
testthat::expect_gt(length(a[[1]]), 1)
a <- parse_depends(deps = "httr") # CRAN provider
testthat::expect_gt(length(a[[1]]), 1)
a <- parse_depends(deps = "R")
testthat::expect_equal(a[[1]]$name, "R")
a <- parse_depends(deps = "not-a-package")
testthat::expect_error(format_depend(NULL))
a <- format_depend(package = "a4",
version = "*") # BIOC provider
testthat::expect_equal(a$provider$`@id`, "https://www.bioconductor.org")
a <- format_depend(package = "httr",
version = "*") # CRAN provider
testthat::expect_equal(a$provider$`@id`, "https://cran.r-project.org")
a <- format_depend(package = "R",
version = ">= 3.0.0")
testthat::expect_equal(a$name, "R")

})

testthat::test_that("Test the various cases for ids (NOT used currently)", {

a <- guess_dep_id(parse_depends("a4")[[1]]) # BIOC provider
a <- guess_dep_id(parse_depends("httr")[[1]]) # CRAN provider
a <- guess_dep_id(parse_depends("R")[[1]])
a <- guess_dep_id(parse_depends("not-a-package")[[1]])
# a <- guess_dep_id(parse_depends("a4")[[1]]) # BIOC provider
# a <- guess_dep_id(parse_depends("httr")[[1]]) # CRAN provider
# a <- guess_dep_id(parse_depends("R")[[1]])
# a <- guess_dep_id(parse_depends("not-a-package")[[1]])

})

0 comments on commit c56c484

Please sign in to comment.