-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: WIP Improve create_dependencies_file
why: - take in account the local package what: - Move in flat the function create_dependencies_file (test + doc + example) - Improve function to take in account local package Issue #56
- Loading branch information
1 parent
cf1fd15
commit 771c1b6
Showing
8 changed files
with
261 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,3 +52,4 @@ Suggests: | |
renv (>= 0.8.4), | ||
rstudioapi, | ||
testthat (>= 3.0.0) | ||
Config/fusen/version: 0.5.0.9000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
*.html | ||
*.R |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
flat_create_dependencies_file.Rmd: | ||
path: dev/flat_create_dependencies_file.Rmd | ||
state: active | ||
R: R/create_dependencies_file.R | ||
tests: tests/testthat/test-create_dependencies_file.R | ||
vignettes: vignettes/create-dependencies-file.Rmd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
--- | ||
title: "flat_create_dependencies_file.Rmd empty" | ||
output: html_document | ||
editor_options: | ||
chunk_output_type: console | ||
--- | ||
|
||
```{r development, include=FALSE} | ||
library(testthat) | ||
# Copy package in a temporary directory | ||
tmpdir <- tempfile(pattern = "pkgdeps") | ||
dir.create(tmpdir) | ||
file.copy(system.file("dummypackage",package = "attachment"), tmpdir, recursive = TRUE) | ||
dummypackage <- file.path(tmpdir, "dummypackage") | ||
path.d <- file.path(dummypackage, "DESCRIPTION") | ||
new_desc <- readLines(path.d) | ||
new_desc[new_desc == "Depends: "] <- "Depends:\n asupp," | ||
new_desc[new_desc == "Imports: "] <- "Imports:\n appflagquizz," | ||
# Add pkglocal in DESCRIPTION as should be local installed from a path | ||
new_desc <- c(new_desc,"Remotes: \n local::asupp,\n murielledelmotte/appflagquizz") | ||
writeLines(new_desc, con = path.d) | ||
# browseURL(file.path(dummypackage, "DESCRIPTION")) | ||
create_dependencies_file(path = file.path(dummypackage,"DESCRIPTION"), | ||
to = file.path(dummypackage, "inst/dependencies.R"), | ||
field = c("Depends", "Imports", "Suggests"), | ||
open_file = FALSE) | ||
dep_file <- readLines(file.path(tmpdir, "dummypackage", "inst/dependencies.R")) | ||
unlink(tmpdir, recursive = TRUE) | ||
``` | ||
|
||
```{r development-load} | ||
# Load already included functions if relevant | ||
pkgload::load_all(export_all = FALSE) | ||
``` | ||
|
||
# create_dependencies_file | ||
|
||
```{r function-create_dependencies_file} | ||
#' Create a dependencies.R in the `inst` folder | ||
#' | ||
#' @param path path to the DESCRIPTION file | ||
#' @param field DESCRIPTION field to parse, "Import" and "Depends" by default. Can add "Suggests" | ||
#' @param to path to dependencies.R. "inst/dependencies.R" by default | ||
#' @param open_file Logical. Open the file created in an editor | ||
#' @param ignore_base Logical. Whether to ignore package coming with base, as they cannot be installed | ||
#' | ||
#' @export | ||
#' @return Used for side effect. Shows a message with installation instructions and | ||
#' creates a R file containing these instructions. | ||
#' @importFrom glue glue glue_collapse | ||
#' @importFrom desc description | ||
#' @importFrom utils packageDescription | ||
#' | ||
#' @examples | ||
create_dependencies_file <- function(path = "DESCRIPTION", | ||
field = c("Depends", "Imports"), | ||
to = "inst/dependencies.R", | ||
open_file = TRUE, | ||
ignore_base = TRUE) { | ||
if (!dir.exists(dirname(to))) { | ||
dir.create(dirname(to), recursive = TRUE, showWarnings = FALSE) | ||
dir_to <- normalizePath(dirname(to)) | ||
} else { | ||
dir_to <- normalizePath(dirname(to)) | ||
} | ||
# get all packages | ||
ll <- att_from_description(path = path, field = field) | ||
# get pkg in remotes | ||
if (isTRUE(ignore_base)) { | ||
to_remove<- which(lapply(ll,packageDescription,field="Priority")=="base") | ||
if (length(to_remove)>0){ | ||
ll<- ll[-to_remove] | ||
} | ||
} | ||
desc <- description$new(path) | ||
# Get previous dependencies in Description in case version is set | ||
# deps_orig <- desc$get_deps() | ||
remotes_orig <- desc$get_remotes() | ||
if (length(remotes_orig) != 0) { | ||
remotes_orig_pkg <- gsub("^.*/|^local::", "", remotes_orig) | ||
# Remove remotes from ll | ||
ll <- ll[!ll %in% remotes_orig_pkg] | ||
# Install script | ||
inst_remotes <- remotes_orig | ||
# _If no (), then github | ||
w.github <- !grepl("\\(", remotes_orig) & !grepl("local::", remotes_orig) | ||
inst_remotes[w.github] <- glue("remotes::install_github('{remotes_orig[w.github]}')") | ||
# _If no (), then local | ||
w.local <- grepl("local::", remotes_orig) | ||
inst_remotes[w.local] <- glue("remotes::install_local('{remotes_orig[w.local]}')") | ||
# _Others (WIP...) | ||
inst_remotes[!(w.github | w.local)] <- remotes_orig[!w.github] | ||
# Store content | ||
remotes_content <- paste("# Remotes ----", | ||
"install.packages(\"remotes\")", | ||
paste(inst_remotes, collapse = "\n"), | ||
sep = "\n") | ||
} else { | ||
remotes_content <- "# No Remotes ----" | ||
} | ||
if (length(ll) != 0) { | ||
content <- glue::glue( | ||
'*{remotes_content}* | ||
# Attachments ---- | ||
to_install <- c("*{glue::glue_collapse(as.character(ll), sep="\\", \\"")}*") | ||
for (i in to_install) { | ||
message(paste("looking for ", i)) | ||
if (!requireNamespace(i)) { | ||
message(paste(" installing", i)) | ||
install.packages(i) | ||
} | ||
}\n\n', .open = "*{", .close = "}*") | ||
} else { | ||
content <- glue::glue( | ||
'*{remotes_content}* | ||
# No attachments ---- | ||
\n\n', .open = "*{", .close = "}*") | ||
} | ||
# file <- normalizePath(to, mustWork = FALSE) | ||
file <- file.path(dir_to, basename(to)) | ||
file.create(file) | ||
cat(content, file = file) | ||
if (open_file) { | ||
utils::file.edit(file, editor = "internal") | ||
} | ||
} | ||
``` | ||
|
||
```{r examples-create_dependencies_file} | ||
tmpdir <- tempfile(pattern = "depsfile") | ||
dir.create(tmpdir) | ||
file.copy(system.file("dummypackage",package = "attachment"), tmpdir, | ||
recursive = TRUE) | ||
dummypackage <- file.path(tmpdir, "dummypackage") | ||
# browseURL(dummypackage) | ||
create_dependencies_file(path = file.path(dummypackage,"DESCRIPTION"), | ||
to = file.path(dummypackage, "inst/dependencies.R"), | ||
open_file = FALSE) | ||
# Clean temp files after this example | ||
unlink(tmpdir, recursive = TRUE) | ||
``` | ||
|
||
```{r tests-create_dependencies_file} | ||
# Copy package in a temporary directory | ||
tmpdir <- tempfile(pattern = "pkgdeps") | ||
dir.create(tmpdir) | ||
file.copy(system.file("dummypackage",package = "attachment"), tmpdir, recursive = TRUE) | ||
dummypackage <- file.path(tmpdir, "dummypackage") | ||
# browseURL(dummypackage) | ||
create_dependencies_file(path = file.path(dummypackage,"DESCRIPTION"), | ||
to = file.path(dummypackage, "inst/dependencies.R"), | ||
field = c("Depends", "Imports", "Suggests"), | ||
open_file = FALSE) | ||
dep_file <- readLines(file.path(tmpdir, "dummypackage", "inst/dependencies.R")) | ||
test_that("create-dependencies-file works", { | ||
expect_equal(dep_file[1], "# No Remotes ----") | ||
expect_equal(dep_file[3], "to_install <- c(\"knitr\", \"magrittr\", \"rmarkdown\", \"testthat\")") | ||
}) | ||
# Clean temp files after this example | ||
unlink(tmpdir, recursive = TRUE) | ||
``` | ||
|
||
|
||
```{r development-inflate, eval=FALSE} | ||
# Run but keep eval=FALSE to avoid infinite loop | ||
# Execute in the console directly | ||
fusen::inflate(flat_file = "dev/flat_create_dependencies_file.Rmd", vignette_name = "Create dependencies file", | ||
check = FALSE, | ||
open_vignette = FALSE, | ||
overwrite = TRUE) | ||
``` | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
title: "Create dependencies file" | ||
output: rmarkdown::html_vignette | ||
vignette: > | ||
%\VignetteIndexEntry{create-dependencies-file} | ||
%\VignetteEngine{knitr::rmarkdown} | ||
%\VignetteEncoding{UTF-8} | ||
--- | ||
|
||
```{r, include = FALSE} | ||
knitr::opts_chunk$set( | ||
collapse = TRUE, | ||
comment = "#>" | ||
) | ||
``` | ||
|
||
```{r setup} | ||
library(attachment) | ||
``` | ||
|
||
<!-- WARNING - This vignette is generated by {fusen} from dev/flat_create_dependencies_file.Rmd: do not edit by hand --> | ||
|
||
# create_dependencies_file | ||
|
||
```{r examples-create_dependencies_file} | ||
tmpdir <- tempfile(pattern = "depsfile") | ||
dir.create(tmpdir) | ||
file.copy(system.file("dummypackage",package = "attachment"), tmpdir, | ||
recursive = TRUE) | ||
dummypackage <- file.path(tmpdir, "dummypackage") | ||
# browseURL(dummypackage) | ||
create_dependencies_file(path = file.path(dummypackage,"DESCRIPTION"), | ||
to = file.path(dummypackage, "inst/dependencies.R"), | ||
open_file = FALSE) | ||
# Clean temp files after this example | ||
unlink(tmpdir, recursive = TRUE) | ||
``` | ||
|