Skip to content

Commit

Permalink
feat: WIP Improve create_dependencies_file
Browse files Browse the repository at this point in the history
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
MurielleDelmotte committed Mar 21, 2023
1 parent cf1fd15 commit 771c1b6
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 8 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ Suggests:
renv (>= 0.8.4),
rstudioapi,
testthat (>= 3.0.0)
Config/fusen/version: 0.5.0.9000
17 changes: 11 additions & 6 deletions R/create_dependencies_file.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# WARNING - Generated by {fusen} from dev/flat_create_dependencies_file.Rmd: do not edit by hand

#' Create a dependencies.R in the `inst` folder
#'
#' @param path path to the DESCRIPTION file
Expand All @@ -19,15 +21,14 @@
#' 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)

create_dependencies_file <- function(path = "DESCRIPTION",
field = c("Depends", "Imports"),
to = "inst/dependencies.R",
Expand Down Expand Up @@ -57,18 +58,21 @@ create_dependencies_file <- function(path = "DESCRIPTION",
# deps_orig <- desc$get_deps()
remotes_orig <- desc$get_remotes()
if (length(remotes_orig) != 0) {
remotes_orig_pkg <- gsub("^.*/", "", remotes_orig)
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)
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] <- remotes_orig[!w.github]
inst_remotes[!(w.github | w.local)] <- remotes_orig[!w.github]

# Store content
remotes_content <- paste("# Remotes ----",
Expand Down Expand Up @@ -108,3 +112,4 @@ to_install <- c("*{glue::glue_collapse(as.character(ll), sep="\\", \\"")}*")
utils::file.edit(file, editor = "internal")
}
}

3 changes: 3 additions & 0 deletions dev/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

*.html
*.R
6 changes: 6 additions & 0 deletions dev/config_fusen.yaml
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
196 changes: 196 additions & 0 deletions dev/flat_create_dependencies_file.Rmd
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)
```

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

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

4 changes: 3 additions & 1 deletion tests/testthat/test-create_dependencies_file.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# WARNING - Generated by {fusen} from dev/flat_create_dependencies_file.Rmd: do not edit by hand

# Copy package in a temporary directory
tmpdir <- tempfile(pattern = "pkgdeps")
dir.create(tmpdir)
Expand All @@ -15,6 +17,6 @@ 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)
40 changes: 40 additions & 0 deletions vignettes/create-dependencies-file.Rmd
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)
```

0 comments on commit 771c1b6

Please sign in to comment.