Skip to content

Commit 576bfa4

Browse files
authored
Merge pull request #46 from ThinkR-open/renv_en_folie
add create_renv_for_dev function
2 parents db8f59e + d3ae7be commit 576bfa4

24 files changed

+477
-38
lines changed

.Rbuildignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
^renv$
2+
^renv\.lock$
13
^Meta$
24
^doc$
35
^docs$

DESCRIPTION

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: attachment
22
Title: Deal with Dependencies
3-
Version: 0.2.4.9000
3+
Version: 0.2.4.9001
44
Authors@R:
55
c(person(given = "Sébastien",
66
family = "Rochette",
@@ -32,9 +32,11 @@ VignetteBuilder:
3232
Encoding: UTF-8
3333
RoxygenNote: 7.1.2
3434
Config/testthat/edition: 3
35+
Roxygen: list(markdown = TRUE)
3536
Depends:
3637
R (>= 3.4)
3738
Imports:
39+
cli,
3840
desc (>= 1.2.0),
3941
glue (>= 1.3.0),
4042
knitr (>= 1.20),
@@ -46,5 +48,7 @@ Imports:
4648
utils,
4749
withr
4850
Suggests:
51+
lifecycle,
52+
renv (>= 0.8.4),
4953
rstudioapi,
5054
testthat (>= 3.0.0)

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ export(att_to_desc_from_is)
1212
export(att_to_desc_from_pkg)
1313
export(att_to_description)
1414
export(create_dependencies_file)
15+
export(create_renv_for_dev)
16+
export(create_renv_for_prod)
1517
export(find_remotes)
1618
export(install_from_description)
1719
export(install_if_missing)
1820
export(set_remotes_to_desc)
21+
importFrom(cli,cat_bullet)
1922
importFrom(desc,description)
2023
importFrom(glue,glue)
2124
importFrom(glue,glue_collapse)

NEWS.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
# attachment 0.2.4.9000
1+
# attachment 0.2.4.900x
2+
## Major changes
3+
4+
* add `create_renv_for_dev()` and `create_renv_for_prod()` function to create `renv.lock` file based on development project (@VincentGuyader).
25

36
## Minor changes
47

5-
- `att_amend_desc()` now saves file before processing
8+
* `att_amend_desc()` now saves file before processing
69

710
# attachment 0.2.4
811

R/att_to_description.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#' Amend package DESCRIPTION file with the list of dependencies extracted from
44
#' R, tests, vignettes files.
55
#' att_to_desc_from_pkg() is an alias of att_amend_desc(),
6-
#' for the correspondence with \code{\link{att_to_desc_from_is}}.
6+
#' for the correspondence with [att_to_desc_from_is()].
77
#'
88
#' @param path path to the root of the package directory. Default to current directory.
99
#' @param path.n path to namespace file.
@@ -62,7 +62,7 @@ att_amend_desc <- function(path = ".",
6262
"Please fill it. ",
6363
"\nNext time, you may want to use 'usethis::use_description()'")
6464
}
65-
if (path.d == "DESCRIPTION") {path.d <- file.path(normalizePath("."), path.d)}
65+
if (path.d == "DESCRIPTION") {path.d <- file.path(normalizePath(path), path.d)}
6666

6767
# Remove non-existing directories in path.n for Imports
6868
if (!file.exists(path.n)) {
@@ -171,7 +171,7 @@ att_to_desc_from_pkg <- att_amend_desc
171171
#' @param path.d path to description file.
172172
#' @param imports character vector of package names to add in Imports section
173173
#' @param suggests character vector of package names to add in Suggests section
174-
#' @param normalize Logical. Whether to normalize the DESCRIPTION file. See \code{\link[desc]{desc_normalize}}
174+
#' @param normalize Logical. Whether to normalize the DESCRIPTION file. See [desc::desc_normalize()]
175175
#' @param must.exist Logical. If TRUE then an error is given if packages do not exist
176176
#' within installed packages. If NA, a warning.
177177
#'

R/attachment-deprecated.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#'
55
#' @section Details:
66
#' \tabular{rl}{
7-
#' \code{att_to_description} \tab is now called \code{att_amend_desc}
7+
#' `att_to_description` \tab is now called `att_amend_desc`
88
#' so that it is the first function proposed when using auto-completion\cr
99
#' }
1010
NULL

R/create_renv.R

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
extra_dev_pkg <- c(
2+
"renv", "fusen", "devtools",
3+
"roxygen2", "usethis", "pkgload",
4+
"testthat", "remotes", "covr",
5+
"attachment", "pak", "dockerfiler",
6+
"pkgdown"
7+
)
8+
9+
#' Create reproducible environments for your R projects with {renv}
10+
#'
11+
#' @description
12+
#' `r lifecycle::badge("experimental")`
13+
#'
14+
#' Tool to create and maintain renv.lock files.
15+
#' The idea is to have 2 distinct files, one for development and the other for deployment.
16+
#' Indeed, although packages like {attachment} or {pkgload} must be installed to develop,
17+
#' they are not necessary in your project, package or Shiny application.
18+
#'
19+
#'
20+
#' @param path Path to your current package source folder
21+
#' @param dev_pkg Package development toolbox you need. Use `_default`
22+
#' (with underscore before to avoid confusing with a package name), to
23+
#' use the default list. Use `NULL` for no extra package.
24+
#' Use `attachment:::extra_dev_pkg` for the list.
25+
#' @param folder_to_include Folder to scan to detect development packages
26+
#' @param output Path and name of the file created, default is `./renv.lock`
27+
#' @param install_if_missing Logical. Install missing packages. `TRUE` by default
28+
#' @param document Logical. Whether to run [att_amend_desc()] before
29+
#' detecting packages in DESCRIPTION.
30+
#' @param ... Other arguments to pass to [renv::snapshot()]
31+
#'
32+
#' @return a renv.lock file
33+
#'
34+
#'
35+
#' @importFrom cli cat_bullet
36+
#' @export
37+
#'
38+
#' @examples
39+
#' \dontrun{
40+
#' create_renv_for_dev()
41+
#' create_renv_for_dev(dev_pkg = "attachment")
42+
#' create_renv_for_prod()
43+
#' }
44+
create_renv_for_dev <- function(path = ".",
45+
dev_pkg = "_default",
46+
folder_to_include = c("dev", "data-raw"),
47+
output = "renv.lock",
48+
install_if_missing = TRUE,
49+
document = TRUE,
50+
...) {
51+
52+
if (!requireNamespace("renv")) {
53+
stop("'renv' is required. Please install it before.")
54+
}
55+
56+
path <- normalizePath(path)
57+
58+
if (!is.null(dev_pkg) && "_default" %in% dev_pkg) {
59+
cli::cli_alert_info(
60+
paste('`dev_pkg = _default` includes: ',
61+
paste(extra_dev_pkg, collapse = ", ")))
62+
dev_pkg <- c(extra_dev_pkg, dev_pkg[dev_pkg != "_default"])
63+
}
64+
65+
if (isTRUE(document)) {
66+
att_amend_desc(path)
67+
}
68+
69+
pkg_list <-
70+
c(
71+
att_from_description(path = file.path(path, "DESCRIPTION")),
72+
dev_pkg
73+
)
74+
75+
# Extra folders
76+
folder_to_include_relative <- folder_to_include
77+
folder_to_include <- file.path(path, folder_to_include)
78+
folder_exists <- dir.exists(folder_to_include)
79+
80+
if (any(!folder_exists)) {
81+
cli::cli_alert_info(
82+
paste(
83+
"There is no directory named: ",
84+
paste(folder_to_include_relative[!folder_exists], collapse = ", "),
85+
". This is removed from the exploration."
86+
)
87+
)
88+
}
89+
90+
if (any(folder_exists)) {
91+
folder_to_include <- folder_to_include[folder_exists]
92+
93+
# folder_to_include <- folder_to_include[dir.exists(file.path(path, folder_to_include))]
94+
95+
from_r_script <- att_from_rscripts(folder_to_include)
96+
from_rmd <- att_from_rmds(folder_to_include)
97+
98+
pkg_list <- unique(c(pkg_list, from_r_script, from_rmd))
99+
}
100+
101+
# Install
102+
if (install_if_missing) {
103+
install_if_missing(pkg_list)
104+
}
105+
106+
cli::cat_bullet(
107+
sprintf("create renv.lock at %s", output),
108+
bullet = "tick",
109+
bullet_col = "green"
110+
)
111+
112+
renv::snapshot(
113+
packages = pkg_list,
114+
lockfile = output,
115+
prompt = FALSE,
116+
...
117+
# type = "packages"
118+
)
119+
120+
121+
if (!file.exists(output)) {
122+
stop("error during renv.lock creation")
123+
}
124+
125+
output
126+
}
127+
128+
#' @export
129+
#' @rdname create_renv_for_dev
130+
create_renv_for_prod <- function(path = ".", output = "renv.lock.prod", dev_pkg = "remotes", ...) {
131+
create_renv_for_dev(
132+
path = path,
133+
dev_pkg = dev_pkg,
134+
folder_to_include = NULL,
135+
output = output,
136+
...
137+
)
138+
}
139+

R/install_from_description.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#'
33
#' @param path path to the DESCRIPTION file
44
#' @param field DESCRIPTION fields to parse, "Depends", "Imports", "Suggests" by default
5-
#' @param ... Arguments to be passed to \code{\link[utils]{install.packages}}
5+
#' @param ... Arguments to be passed to [utils::install.packages()]
66
#' @export
77
#' @return Used for side effect. Installs R packages from DESCRIPTION file if missing.
88
#' @examples
@@ -22,7 +22,7 @@ install_from_description <- function(path = "DESCRIPTION", field = c("Depends",
2222
#' install packages if missing
2323
#'
2424
#' @param to_be_installed a character vector containing required packages names
25-
#' @param ... Arguments to be passed to \code{\link[utils]{install.packages}}
25+
#' @param ... Arguments to be passed to [utils::install.packages()]
2626
#'
2727
#' @importFrom utils install.packages
2828
#'

R/utils-pipe.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#' Pipe operator
22
#'
3-
#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details.
3+
#' See `magrittr::[\%>\%][magrittr::pipe]` for details.
44
#'
55
#' @name %>%
66
#' @rdname pipe

dev/dev_history.R

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ usethis::use_github_action_check_standard()
6161
usethis::use_github_action("pkgdown")
6262
usethis::use_github_action("test-coverage")
6363

64+
usethis::use_vignette("use_renv")
6465
usethis::use_build_ignore("_pkgdown.yml")
6566
pkgdown::build_site()
6667

67-
# PR
68+
# PR ----
6869
usethis::pr_fetch(28)
6970
usethis::pr_push()
7071

@@ -74,11 +75,14 @@ usethis::pr_push()
7475
# Document ----
7576
# Do not parse dir.t because of tests
7677
attachment::att_from_rscripts("tests")
78+
usethis::use_roxygen_md()
79+
roxygen2md::roxygen2md()
7780
roxygen2::roxygenise()
78-
attachment::att_amend_desc(pkg_ignore = c("remotes", "i", "usethis", "rstudioapi"), #i
79-
extra.suggests = c("testthat", "rstudioapi"), #"pkgdown", "covr",
80-
dir.t = "",
81-
normalize = FALSE)
81+
attachment::att_amend_desc(
82+
pkg_ignore = c("remotes", "i", "usethis", "rstudioapi", "renv"), #i
83+
extra.suggests = c("testthat", "rstudioapi", "renv", "lifecycle"), #"pkgdown", "covr",
84+
dir.t = "",
85+
normalize = FALSE)
8286

8387
attachment::create_dependencies_file(field = c("Depends", "Imports", "Suggests"))
8488

@@ -103,9 +107,7 @@ devtools::test()
103107

104108
# Checks for CRAN release ----
105109
# Check package as CRAN
106-
remotes::install_github("r-lib/roxygen2")
107110
rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"))
108-
install.packages("roxygen2")
109111

110112
# Check content
111113
# remotes::install_github("ThinkR-open/checkhelper")

0 commit comments

Comments
 (0)