Skip to content

Commit ed1d3bc

Browse files
authored
Merge pull request #97 from ThinkR-open/att_config
fix: stop if parameters are not used
2 parents b563bf8 + bd9dfda commit ed1d3bc

25 files changed

+478
-168
lines changed

NEWS.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
# attachment 0.3.1.XXXX
22

3+
## Breaking changes
4+
5+
- When using `att_amend_desc()` without the default parameters, like `pkg_ignore = "x"` will now require `att_amend_desc(pkg_ignore = "x", update.config = TRUE)`, otherwise, it will fail. This allows for the use of parameters stored in the config file when running `att_amend_desc()` directly in the console.
6+
Recommendation: Run `att_amend_desc(pkg_ignore = "x", update.config = TRUE)` if you have to update your config, run `att_amend_desc()` daily as you'll want to use what is stored in the config file.
7+
8+
- `create_dependencies_file()` gets parameter `install_only_if_missing = FALSE` by default to complete the installation instructions packages only if missing. (@MurielleDelmotte)
9+
310
## New features
411

512
- `att_amend_desc()` can run with the last set of parameters stored in a configuration file, without having to call them all each time. See vignettes and documentation of parameters `update.config = FALSE`, `use.config = FALSE` and `path.c = "dev/config_attachment.yaml"`. (@dagousket)
613
- `create_dependencies_file()` now takes other sources into account (git, gitlab, github, bioc, local). (@MurielleDelmotte)
7-
- `create_dependencies_file()` gets parameter `install_only_if_missing = FALSE` by default to complete the installation instructions packages only if missing. (@MurielleDelmotte)
14+
- Use `create_dependencies_file(to = NULL)` to only get the output as character and do not create a file
815

916
## Bug fixes
1017

1118
- `att_amend_desc()` does not modify user `options("warn")` level anymore (#94)
19+
- `att_amend_desc()` allows "Remotes" field to contain `@ref` (#67)
1220

1321
# attachment 0.3.1
1422

@@ -19,7 +27,7 @@
1927
## Minor changes
2028

2129
- a new parameters `check_if_suggests_is_installed` in `att_amend_desc()` allow not to check if suggested package is installed. (thanks to @yogat3ch)
22-
- `create_renv_for_prod()` dont look anymore for suggested packages
30+
- `create_renv_for_prod()` don't look anymore for suggested packages
2331
- Clean a maximum of temp directories after examples and unit tests
2432

2533
# attachment 0.3.0

R/amend_with_config.R

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,48 @@
11
# WARNING - Generated by {fusen} from dev/flat_save_att_params.Rmd: do not edit by hand
22

3+
#' Compare input from the user to config file or default inputs
4+
#'
5+
#' @param local_att_params List of parameters called by the user
6+
#' @inheritParams att_amend_desc
7+
#'
8+
#' @noRd
9+
10+
compare_inputs_load_or_save <- function(path.c, local_att_params, use.config, update.config) {
11+
12+
if (isTRUE(use.config) & file.exists(path.c)) {
13+
# reassign input value to saved parameters
14+
saved_att_params <- load_att_params(path_to_yaml = path.c)
15+
# Check if different from config and as defaults
16+
diff_config <- lapply(names(local_att_params), function(x) {setdiff(local_att_params[x], saved_att_params[x])})
17+
diff_default <- lapply(names(local_att_params), function(x) {setdiff(local_att_params[x], formals(att_amend_desc)[x])})
18+
19+
if (any(lengths(diff_config) != 0) & any(lengths(diff_default) != 0)) {
20+
stop(c("Params in your `att_amend_desc()` and the one in the config file are different. ",
21+
"Please choose among `update.config = TRUE` or `use.config = FALSE`"))
22+
}
23+
24+
params_to_load <- saved_att_params
25+
# for (param_name in names(saved_att_params)){
26+
# assign(param_name, saved_att_params[[param_name]])
27+
# }
28+
# message(c("Documentation parameters were restored from attachment config file.\n"))
29+
} else if (isTRUE(update.config) | !file.exists(path.c)) {
30+
# save current parameters to yaml config - overwrite if already exist
31+
save_att_params(
32+
param_list = local_att_params,
33+
path_to_yaml = path.c,
34+
overwrite = TRUE)
35+
36+
params_to_load <- NULL
37+
} else {
38+
message("attachment config file was not updated. Parameters used this time won't be stored.")
39+
40+
params_to_load <- local_att_params
41+
}
42+
43+
return(params_to_load)
44+
}
45+
346
#' load_att_params
447
#'
548
#' @param path_to_yaml character The path to the yaml file

R/att_to_description.R

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,43 @@
2424
#'
2525
#' @return Update DESCRIPTION file.
2626
#'
27+
#' @details
28+
#'
29+
#' Your daily use is to run `att_amend_desc()`, as is.
30+
#' You will want to run this function sometimes with some extra information like
31+
#' `att_amend_desc(pkg_ignore = "x", update.config = TRUE)` if you have to update
32+
#' the configuration file.
33+
#' Next time `att_amend_desc()` will use these parameters from the configuration
34+
#' file directly.
35+
#'
36+
#'
2737
#' @export
2838
#' @examples
39+
#'
40+
#' # Run on an external "dummypackage" as an example
41+
#' # For your local use, you do not have to specify the `path` as below
42+
#' # By default, `att_amend_desc()` will run on the current working directory
43+
#'
44+
#' # Create a fake package for the example
2945
#' tmpdir <- tempfile(pattern = "description")
3046
#' dir.create(tmpdir)
3147
#' file.copy(system.file("dummypackage",package = "attachment"), tmpdir,
3248
#' recursive = TRUE)
3349
#' dummypackage <- file.path(tmpdir, "dummypackage")
34-
#' # browseURL(dummypackage)
35-
#' att_amend_desc(path = tmpdir)
50+
#'
51+
#' # Update documentation and dependencies
52+
#' att_amend_desc(path = dummypackage)
53+
#'
54+
#' # You can look at the content of this external package
55+
#' #' # browseURL(dummypackage)
56+
#'
57+
#' # Update the config file with extra parameters
58+
#' # We recommend that you store this code in a file in your "dev/" directory
59+
#' # to run it when needed
60+
#' att_amend_desc(path = dummypackage, extra.suggests = "testthat", update.config = TRUE)
61+
#'
62+
#' # Next time, in your daily development
63+
#' att_amend_desc(path = dummypackage)
3664
#'
3765
#' # Clean after examples
3866
#' unlink(tmpdir, recursive = TRUE)
@@ -73,22 +101,17 @@ att_amend_desc <- function(path = ".",
73101
att_params <- att_params[!att_params %in% c("path", "update.config", "use.config", "path.c")]
74102
local_att_params <- mget(att_params)
75103

76-
if (isTRUE(use.config) & file.exists(path.c)) {
77-
# reassign input value to saved parameters
78-
saved_att_params <- load_att_params(path_to_yaml = path.c)
79-
for (param_name in names(saved_att_params)){
80-
assign(param_name, saved_att_params[[param_name]])
81-
}
82-
message(c("Documentation parameters were restored from attachment config file.\n",
83-
"See `use.config` and `update.config` for other behaviour with `att_amend_desc()`\n"))
84-
} else if (isTRUE(update.config) | !file.exists(path.c)) {
85-
# save current parameters to yaml config - overwrite if already exist
86-
save_att_params(
87-
param_list = local_att_params,
88-
path_to_yaml = path.c,
89-
overwrite = TRUE)
90-
} else {
91-
message("attachment config file was not updated. Parameters used this time won't be stored.")
104+
params_to_load <- compare_inputs_load_or_save(
105+
path.c = path.c,
106+
local_att_params = local_att_params,
107+
use.config = use.config,
108+
update.config = update.config)
109+
110+
if (!is.null(params_to_load)) {
111+
for (param_name in names(params_to_load)){
112+
assign(param_name, params_to_load[[param_name]])
113+
}
114+
message(c("Documentation parameters were restored from attachment config file."))
92115
}
93116

94117
# Save all open files ----
@@ -314,7 +337,7 @@ att_to_desc_from_is <- function(path.d = "DESCRIPTION", imports = NULL,
314337

315338
remotes_orig <- desc$get_remotes()
316339
if (length(remotes_orig) != 0) {
317-
remotes_orig_pkg <- gsub("^.*/|[.]git", "", remotes_orig)
340+
remotes_orig_pkg <- gsub("^.*/|[.]git|@.*$", "", remotes_orig)
318341
} else {
319342
remotes_orig_pkg <- NULL
320343
}

R/create_dependencies_file.R

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,93 @@
11
# WARNING - Generated by {fusen} from dev/flat_create_dependencies_file.Rmd: do not edit by hand
22

3-
#' Create a dependencies.R in the `inst` folder
3+
#' Create the list of instructions to install dependencies from a DESCRIPTION file
4+
#'
5+
#' Outputs the list of instructions and a "dependencies.R" file with instructions in the "inst/" directory
46
#'
57
#' @param path path to the DESCRIPTION file
68
#' @param field DESCRIPTION field to parse, "Import" and "Depends" by default. Can add "Suggests"
7-
#' @param to path to dependencies.R. "inst/dependencies.R" by default
9+
#' @param to path where to save the dependencies file. Set to "inst/dependencies.R" by default. Set to `NULL` if you do not want the file, but only the instructions as a list of character.
810
#' @param open_file Logical. Open the file created in an editor
911
#' @param ignore_base Logical. Whether to ignore package coming with base, as they cannot be installed (default TRUE)
1012
#' @param install_only_if_missing Logical Modify the installation instructions to check, beforehand, if the packages are missing . (default FALSE)
1113
#' @export
12-
#' @return Used for side effect. Shows a message with installation instructions and
13-
#' creates a R file containing these instructions.
14+
#' @return List of R instructions to install all dependencies from a DESCRIPTION file. Side effect: creates a R file containing these instructions.
1415
#' @importFrom glue glue glue_collapse
1516
#' @importFrom desc description
1617
#' @importFrom utils packageDescription
1718
#'
1819
#' @examples
19-
#' \dontrun{
20-
#' tmpdir <- tempfile(pattern = "depsfile")
21-
#' dir.create(tmpdir)
22-
#' file.copy(system.file("dummypackage",package = "attachment"), tmpdir,
23-
#' recursive = TRUE)
24-
#' dummypackage <- file.path(tmpdir, "dummypackage")
25-
#'
26-
#' # browseURL(dummypackage)
27-
#' create_dependencies_file(path = file.path(dummypackage,"DESCRIPTION"),
20+
#' # Create a fake package
21+
#' tmpdir <- tempfile(pattern = "depsfile")
22+
#' dir.create(tmpdir)
23+
#' file.copy(system.file("dummypackage",package = "attachment"), tmpdir,
24+
#' recursive = TRUE)
25+
#' dummypackage <- file.path(tmpdir, "dummypackage")
26+
#'
27+
#' # Create the dependencies commands but no file
28+
#' create_dependencies_file(
29+
#' path = file.path(dummypackage,"DESCRIPTION"),
30+
#' to = NULL,
31+
#' open_file = FALSE)
32+
#'
33+
#' # Create the dependencies files in the package
34+
#' create_dependencies_file(
35+
#' path = file.path(dummypackage,"DESCRIPTION"),
2836
#' to = file.path(dummypackage, "inst/dependencies.R"),
2937
#' open_file = FALSE)
30-
#'
31-
#' # Clean temp files after this example
32-
#' unlink(tmpdir, recursive = TRUE)
33-
#' }
38+
#' list.files(file.path(dummypackage, "inst"))
39+
#' # browseURL(dummypackage)
40+
#'
41+
#' # Clean temp files after this example
42+
#' unlink(tmpdir, recursive = TRUE)
3443
create_dependencies_file <- function(path = "DESCRIPTION",
3544
field = c("Depends", "Imports"),
3645
to = "inst/dependencies.R",
3746
open_file = TRUE,
3847
ignore_base = TRUE,
3948
install_only_if_missing = FALSE) {
4049

41-
if (!dir.exists(dirname(to))) {
42-
dir.create(dirname(to), recursive = TRUE, showWarnings = FALSE)
43-
dir_to <- normalizePath(dirname(to))
44-
} else {
45-
dir_to <- normalizePath(dirname(to))
46-
}
47-
4850
# get all packages
4951
ll <- att_from_description(path = path, field = field)
5052
# get pkg in remotes
5153
if (isTRUE(ignore_base)) {
52-
to_remove<- which(lapply(ll,packageDescription,field="Priority")=="base")
53-
if (length(to_remove)>0){
54-
ll<- ll[-to_remove]
54+
to_remove <-
55+
which(lapply(ll , packageDescription, field = "Priority") == "base")
56+
if (length(to_remove) > 0) {
57+
ll <- ll[-to_remove]
5558
}
5659

5760
}
5861

5962
desc <- description$new(path)
6063
# Get previous dependencies in Description in case version is set
61-
# deps_orig <- desc$get_deps()
6264
remotes_orig <- desc$get_remotes()
6365

6466
content <- dependencies_file_text(ll = ll,
6567
remotes_orig = remotes_orig,
6668
install_only_if_missing = install_only_if_missing)
6769

68-
# file <- normalizePath(to, mustWork = FALSE)
69-
file <- file.path(dir_to, basename(to))
70-
file.create(file)
71-
72-
cat(unlist(content), sep = "\n", file = file)
70+
if (!is.null(to)) {
71+
if (!dir.exists(dirname(to))) {
72+
dir.create(dirname(to), recursive = TRUE, showWarnings = FALSE)
73+
dir_to <- normalizePath(dirname(to))
74+
} else {
75+
dir_to <- normalizePath(dirname(to))
76+
}
77+
78+
the_file <- file.path(dir_to, basename(to))
79+
# file.create(the_file)
80+
cat(unlist(content), sep = "\n", file = the_file)
7381

74-
if (interactive() && open_file) {
75-
utils::file.edit(file, editor = "internal")
82+
if (interactive() && open_file) {
83+
utils::file.edit(file, editor = "internal")
84+
}
85+
86+
return(invisible(content))
87+
} else {
88+
return(content)
7689
}
7790

78-
return(invisible(content))
91+
7992
}
8093

R/create_renv.R

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ extra_dev_pkg <- c(
4040
#'
4141
#' @examples
4242
#' \dontrun{
43+
#' # Writes a renv.lock a file in the user directory
4344
#' create_renv_for_dev()
4445
#' create_renv_for_dev(dev_pkg = "attachment")
4546
#' create_renv_for_prod()
@@ -68,7 +69,21 @@ create_renv_for_dev <- function(path = ".",
6869
}
6970

7071
if (isTRUE(document)) {
71-
att_amend_desc(path, check_if_suggests_is_installed = check_if_suggests_is_installed)
72+
# Use a temporary config_file for renv
73+
config_file <- file.path(path, "dev", "config_attachment.yaml")
74+
if (file.exists(config_file)) {
75+
yaml_params <- load_att_params(path_to_yaml = config_file)
76+
yaml_params[["check_if_suggests_is_installed"]] <- check_if_suggests_is_installed
77+
yamlfile <- tempfile(fileext = ".yaml")
78+
save_att_params(path_to_yaml = yamlfile, param_list = yaml_params)
79+
att_amend_desc(path,
80+
use.config = TRUE,
81+
path.c = yamlfile)
82+
} else {
83+
att_amend_desc(path,
84+
check_if_suggests_is_installed = check_if_suggests_is_installed,
85+
use.config = FALSE)
86+
}
7287
}
7388

7489
if ( isTRUE(check_if_suggests_is_installed)){

R/dependencies_file_text.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# WARNING - Generated by {fusen} from /dev/flat_create_dependencies_file.Rmd: do not edit by hand
1+
# WARNING - Generated by {fusen} from dev/flat_create_dependencies_file.Rmd: do not edit by hand
22

33
#' dependencies_file_text
44
#'

R/install_from_description.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
#' @return Used for side effect. Installs R packages from DESCRIPTION file if missing.
88
#' @examples
99
#' \dontrun{
10+
#' # This will install packages on your system
1011
#' dummypackage <- system.file("dummypackage", package = "attachment")
1112
#' # browseURL(dummypackage)
1213
#'
13-
#' install_from_description(path = file.path(dummypackage,"DESCRIPTION"))
14+
#' install_from_description(path = file.path(dummypackage, "DESCRIPTION"))
1415
#' }
1516

1617
install_from_description <- function(path = "DESCRIPTION", field = c("Depends", "Imports", "Suggests"), ...) {
@@ -30,7 +31,8 @@ install_from_description <- function(path = "DESCRIPTION", field = c("Depends",
3031
#' @return Used for side effect. Install missing packages from the character vector input.
3132
#' @examples
3233
#' \dontrun{
33-
#' install_if_missing(c("dplyr","fcuk","rusk"))
34+
#' # This will install packages on your system
35+
#' install_if_missing(c("dplyr", "fcuk", "rusk"))
3436
#' }
3537
#'
3638
install_if_missing <- function(to_be_installed, ...) {

R/set_remotes.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
#' @examples
1212
#' # Find from vector of packages
1313
#' find_remotes(pkg = c("attachment", "desc", "glue"))
14+
#'
1415
#' # Find from Description file
1516
#' dummypackage <- system.file("dummypackage", package = "attachment")
1617
#' att_from_description(
1718
#' path = file.path(dummypackage, "DESCRIPTION")) %>%
1819
#' find_remotes()
20+
#'
1921
#' \dontrun{
2022
#' # For the current package directory
2123
#' att_from_description() %>% find_remotes()

dev/config_attachment.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pkg_ignore:
3535
- ggplot3
3636
- svn
3737
- pkgload
38+
- bookdown
3839
document: yes
3940
normalize: no
4041
inside_rmd: no

0 commit comments

Comments
 (0)