Skip to content

Commit

Permalink
add set_license()
Browse files Browse the repository at this point in the history
  • Loading branch information
ThierryO committed Jul 17, 2024
1 parent d82f04d commit 368b6cf
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 21 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export(organisation)
export(prepare_ghpages)
export(read_checklist)
export(read_organisation)
export(set_license)
export(set_tag)
export(setup_package)
export(setup_project)
Expand Down
18 changes: 1 addition & 17 deletions R/create_package.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,23 +185,7 @@ create_package <- function(
git_add("README.Rmd", repo = repo)

# add LICENSE.md
license_file <- path(path, "LICENSE.md")
switch(
license, "GPL-3" = path("generic_template", "gplv3.md"),
"MIT" = path("generic_template", "mit.md")
) |>
system.file(package = "checklist") |>
file_copy(license_file)
if (license == "MIT") {
paste0("YEAR: ", format(Sys.Date(), "%Y")) |>
c(sprintf("COPYRIGHT HOLDER: %s", org$get_rightsholder)) |>
writeLines(path(path, "LICENSE"))
git_add("LICENSE", repo = repo)
mit <- readLines(license_file)
mit[3] <- gsub("<YEAR>", format(Sys.Date(), "%Y"), mit[3])
mit[3] <- gsub("<COPYRIGHT HOLDER>", org$get_rightsholder, mit[3])
writeLines(mit, license_file)
}
set_license(x)
git_add("LICENSE.md", repo = repo)

# Add code of conduct
Expand Down
51 changes: 51 additions & 0 deletions R/set_license.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#' Set the proper license
#' @inheritParams read_checklist
#' @family setup
#' @export
#' @importFrom assertthat assert_that
#' @importFrom desc description
#' @importFrom fs file_copy file_exists path
set_license <- function(x = ".") {
x <- read_checklist(x = x)
license_file <- path(x$get_path, "LICENSE.md")
if (x$package) {
assert_that(
file_exists(path(x$get_path, "DESCRIPTION")),
msg = sprintf("No `DESCRIPTION` file found at %s", x$get_path)
)
this_desc <- description$new(file = path(x$get_path, "DESCRIPTION"))
assert_that(
this_desc$has_fields("License"),
msg = "`DESCRIPTION` has no `License`field."
)
switch(
this_desc$get_field("License"),
"GPL-3" = path("generic_template", "gplv3.md"),
"MIT" = path("generic_template", "mit.md"),
"MIT + file LICENSE" = path("generic_template", "mit.md"),
stop(
sprintf("`%s` license is not available", this_desc$get_field("License"))
)
) |>
system.file(package = "checklist") |>
file_copy(license_file)
if (!grepl("^MIT", this_desc$get_field("License"))) {
return(invisible(NULL))
}
this_desc$get_author(role = "cph") |>
format(include = c("given", "family")) |>
paste(collapse = ", ") -> cph
paste0("YEAR: ", format(Sys.Date(), "%Y")) |>
c(sprintf("COPYRIGHT HOLDER: %s", cph)) |>
writeLines(path(x$get_path, "LICENSE"))
mit <- readLines(license_file)
mit[3] <- gsub("<YEAR>", format(Sys.Date(), "%Y"), mit[3])
mit[3] <- gsub("<COPYRIGHT HOLDER>", cph, mit[3])
writeLines(mit, license_file)
return(invisible(NULL))
}
path("generic_template", "cc_by_4_0.md") |>
system.file(package = "checklist") |>
file_copy(license_file)
return(invisible(NULL))
}
5 changes: 1 addition & 4 deletions R/setup_project.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ setup_project <- function(path = ".") {
x$set_default(c("en-GB", "nl-BE", "fr-FR")[answer])

if ("license" %in% checks && !file_exists(path(path, "LICENSE.md"))) {
insert_file(
repo = repo, filename = "cc_by_4_0.md", template = "generic_template",
target = path, new_name = "LICENSE.md"
)
set_license(x)
}

x$set_required(checks = checks)
Expand Down
1 change: 1 addition & 0 deletions man/create_package.Rd

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

1 change: 1 addition & 0 deletions man/create_project.Rd

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

1 change: 1 addition & 0 deletions man/prepare_ghpages.Rd

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

25 changes: 25 additions & 0 deletions man/set_license.Rd

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

1 change: 1 addition & 0 deletions man/setup_package.Rd

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

1 change: 1 addition & 0 deletions man/setup_project.Rd

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

1 change: 1 addition & 0 deletions man/setup_source.Rd

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

0 comments on commit 368b6cf

Please sign in to comment.