Skip to content

Commit b563bf8

Browse files
authored
Merge pull request #96 from ThinkR-open/att_config
Att config
2 parents 338ae82 + b075c4c commit b563bf8

23 files changed

+742
-590
lines changed

DESCRIPTION

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
11
Package: attachment
22
Title: Deal with Dependencies
3-
Version: 0.3.1.9002
4-
Authors@R:
5-
c(person(given = "Sébastien",
6-
family = "Rochette",
7-
role = c("cre", "aut"),
8-
email = "sebastien@thinkr.fr",
9-
comment = c(ORCID = "0000-0002-1565-9313")),
10-
person(given = "Vincent",
11-
family = "Guyader",
12-
role = c("aut"),
13-
email = "vincent@thinkr.fr",
14-
comment = c(ORCID = "0000-0003-0671-9270",
15-
"previous maintainer")),
16-
person(given = "ThinkR",
17-
role = c("cph", "fnd")))
18-
Description: Manage dependencies during package
19-
development. This can retrieve all dependencies that are used in ".R"
20-
files in the "R/" directory, in ".Rmd" files in "vignettes/" directory and
21-
in 'roxygen2' documentation of functions. There is a function to
22-
update the "DESCRIPTION" file of your package with 'CRAN' packages or any other remote package.
23-
All functions to retrieve dependencies of ".R" scripts and ".Rmd" or ".qmd" files can
24-
be used independently of a package development.
3+
Version: 0.3.1.9003
4+
Authors@R: c(
5+
person("Sébastien", "Rochette", , "sebastien@thinkr.fr", role = c("cre", "aut"),
6+
comment = c(ORCID = "0000-0002-1565-9313")),
7+
person("Vincent", "Guyader", , "vincent@thinkr.fr", role = "aut",
8+
comment = c(ORCID = "0000-0003-0671-9270", "previous maintainer")),
9+
person("Murielle", "Delmotte", , "murielle@thinkr.fr", role = "aut",
10+
comment = c(ORCID = "0000-0002-1339-2424")),
11+
person("Swann", "Floc'hlay", , "swann@thinkr.fr", role = "aut",
12+
comment = c(ORCID = "0000-0003-1477-830X")),
13+
person("ThinkR", role = c("cph", "fnd"))
14+
)
15+
Description: Manage dependencies during package development. This can
16+
retrieve all dependencies that are used in ".R" files in the "R/"
17+
directory, in ".Rmd" files in "vignettes/" directory and in 'roxygen2'
18+
documentation of functions. There is a function to update the
19+
"DESCRIPTION" file of your package with 'CRAN' packages or any other
20+
remote package. All functions to retrieve dependencies of ".R"
21+
scripts and ".Rmd" or ".qmd" files can be used independently of a
22+
package development.
2523
License: GPL-3
2624
URL: https://thinkr-open.github.io/attachment/,
2725
https://github.com/ThinkR-open/attachment
2826
BugReports: https://github.com/ThinkR-open/attachment/issues
29-
Config/Needs/website: ThinkR-open/thinkrtemplate
3027
VignetteBuilder:
3128
knitr
32-
Encoding: UTF-8
33-
RoxygenNote: 7.2.3
29+
Config/fusen/version: 0.5.0.9000
30+
Config/Needs/website: ThinkR-open/thinkrtemplate
3431
Config/testthat/edition: 3
32+
Encoding: UTF-8
3533
Roxygen: list(markdown = TRUE)
36-
Config/fusen/version: 0.5.0.9000
34+
RoxygenNote: 7.2.3
3735
Depends:
3836
R (>= 3.4)
3937
Imports:

NAMESPACE

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ export(create_renv_for_prod)
1919
export(find_remotes)
2020
export(install_from_description)
2121
export(install_if_missing)
22-
export(load_att_params)
23-
export(save_att_params)
2422
export(set_remotes_to_desc)
2523
importFrom(cli,cat_bullet)
2624
importFrom(desc,description)

NEWS.md

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

3-
- `att_amend_desc()` gets parameters `update.config = FALSE`, `use.config = FALSE` and `path.c = "dev/config_attachment.yaml"` by default. It allows to store and re-use the provided set of parameters.
3+
## New features
4+
5+
- `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)
6+
- `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)
48

5-
# attachment 0.3.1.9000
9+
## Bug fixes
610

7-
- `create_dependencies_file()` now takes other sources into account (git, gitlab, github, bioc, local)
8-
- `create_dependencies_file()` gets parameter `install_only_if_missing = FALSE` by default to complete the installation instructions packages only if missing.
11+
- `att_amend_desc()` does not modify user `options("warn")` level anymore (#94)
912

1013
# attachment 0.3.1
1114

R/amend_with_config.R

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# WARNING - Generated by {fusen} from dev/flat_save_att_params.Rmd: do not edit by hand
2+
3+
#' load_att_params
4+
#'
5+
#' @param path_to_yaml character The path to the yaml file
6+
#'
7+
#' @importFrom yaml read_yaml
8+
#' @importFrom glue glue
9+
#'
10+
#' @return list A named list of att_amend_desc parameters
11+
#'
12+
#' @noRd
13+
#' @examples
14+
#' # create a list of parameters and tmp file name
15+
#' parameter_list <- list(
16+
#' pkg_ignore = c("remotes", "i"),
17+
#' extra.suggests = c("testthat", "rstudioapi")
18+
#' )
19+
#' yaml_path <- paste0(tempfile(pattern = "save_att"), ".yaml")
20+
#'
21+
#' # save params
22+
#' save_att_params(param_list = parameter_list,
23+
#' path_to_yaml = yaml_path)
24+
#'
25+
#' # read yaml file
26+
#' config_params <- load_att_params(path_to_yaml = yaml_path)
27+
#'
28+
#' # clear created yaml file
29+
#' unlink(yaml_path)
30+
#'
31+
load_att_params <- function(
32+
path_to_yaml = "dev/config_attachment.yaml",
33+
verbose = FALSE
34+
){
35+
36+
# check yaml file exist
37+
if (isFALSE(file.exists(path_to_yaml))){
38+
stop(glue("The att_amend_desc() config file {path_to_yaml} does not exist"))
39+
}
40+
41+
# read yaml
42+
param_list <- read_yaml(file = path_to_yaml)
43+
44+
# Check each name in list corresponds to a parameter name
45+
att_param_names <- names(formals(att_amend_desc))
46+
input_names <- names(param_list)
47+
all_inputs_are_params <- all(input_names %in% att_param_names)
48+
if (isFALSE(all_inputs_are_params)){
49+
bad_names <- input_names[!input_names %in% att_param_names]
50+
stop(paste0("Unexpected parameters in config : ", paste0(bad_names, collapse = " ; ")))
51+
}
52+
53+
# Show parameters used from config
54+
if (isTRUE(verbose)) {
55+
message("att_amend_desc() parameter loaded are : \n",
56+
paste0(
57+
glue("{names(param_list)} = {param_list}"),
58+
collapse = "\n"
59+
)
60+
)
61+
}
62+
63+
# return parameters
64+
return(param_list)
65+
66+
}
67+
68+
#' save_att_params
69+
#'
70+
#' @param param_list list A named list of all parameters to save
71+
#' @param path_to_yaml character The path to the yaml file
72+
#' @param overwrite logical Whether to overwrite the yaml file if it already exists
73+
#'
74+
#' @importFrom yaml write_yaml
75+
#'
76+
#' @return character The path to the yaml file
77+
#' @noRd
78+
#' @examples
79+
#' # create a list of parameters and tmp file name
80+
#' parameter_list <- list(
81+
#' pkg_ignore = c("remotes", "i"),
82+
#' extra.suggests = c("testthat", "rstudioapi")
83+
#' )
84+
#' yaml_path <- paste0(tempfile(pattern = "save_att"), ".yaml")
85+
#'
86+
#' # save params
87+
#' save_att_params(param_list = parameter_list,
88+
#' path_to_yaml = yaml_path)
89+
#'
90+
#' yaml::read_yaml(yaml_path)
91+
#' # rstudioapi::navigateToFile(yaml_path)
92+
#'
93+
#' # clear created yaml file
94+
#' unlink(yaml_path)
95+
save_att_params <- function(
96+
param_list,
97+
path_to_yaml = "dev/config_attachment.yaml",
98+
overwrite = FALSE
99+
) {
100+
101+
# Check each name in list corresponds to a parameter name
102+
att_param_names <- names(formals(att_amend_desc))
103+
input_names <- names(param_list)
104+
all_inputs_are_params <- all(input_names %in% att_param_names)
105+
if (isFALSE(all_inputs_are_params)){
106+
bad_names <- input_names[!input_names %in% att_param_names]
107+
stop(paste0("Unexpected parameters to save : ", paste0(bad_names, collapse = " ; ")))
108+
}
109+
110+
# Create dir if missing
111+
dir_yaml <- normalizePath(dirname(path_to_yaml), mustWork = FALSE)
112+
if (!dir.exists(dir_yaml)) {dir.create(dir_yaml)}
113+
114+
# Write params to yaml
115+
yaml_exists <- file.exists(path_to_yaml)
116+
117+
if (isTRUE(yaml_exists & !overwrite)) {
118+
stop("yaml file already exists and overwriting is not permitted")
119+
} else {
120+
write_yaml(
121+
x = param_list,
122+
file = path_to_yaml,
123+
indent.mapping.sequence = TRUE
124+
)
125+
message("Saving attachment parameters to yaml config file")
126+
}
127+
128+
return(path_to_yaml)
129+
}

R/att_from_rmds.R

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,19 @@ att_from_rmd <- function(path, temp_dir = tempdir(), warn = -1,
3333

3434
# Need an external script to run on windows because of \\ path
3535
runR <- tempfile(fileext = "run.R")
36+
3637
cat(
37-
paste0('options(warn=', warn,
38-
');invisible(knitr::purl("', gsub("\\", "\\\\", path, fixed = TRUE), '"',
38+
paste0('warn_user <- getOption("warn");',
39+
'options(warn=', warn, ');',
40+
'invisible(knitr::purl("', gsub("\\", "\\\\", path, fixed = TRUE), '"',
3941
', output = "', gsub("\\", "\\\\", r_file, fixed = TRUE), '"',
4042
', encoding = "', encoding, '"',
41-
', documentation = 0, quiet = TRUE))')
43+
', documentation = 0, quiet = TRUE));',
44+
'options(warn=warn_user)'
45+
)
4246
, file = runR)
4347

48+
4449
if (isTRUE(inside_rmd)) {
4550
# Purl in a new environment to avoid knit inside knit if function is inside Rmd file
4651
file <- system(

R/att_to_description.R

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#' @param dir.t path to tests directory. Set to empty (dir.t = "") to ignore.
1313
#' @param extra.suggests vector of other packages that should be added in Suggests (pkgdown, covr for instance)
1414
#' @param pkg_ignore vector of packages names to ignore.
15-
#' @param update.config logical Should the parameters used in this call be saved in the config file of the pkg
16-
#' @param use.config logical Should the command use the parameters from the config file to run
15+
#' @param update.config logical. Should the parameters used in this call be saved in the config file of the package
16+
#' @param use.config logical. Should the command use the parameters from the config file to run
1717
#' @param path.c character Path to the yaml config file where parameters are saved
1818
#'
1919
#' @inheritParams att_from_namespace
@@ -51,40 +51,50 @@ att_amend_desc <- function(path = ".",
5151
must.exist = TRUE,
5252
check_if_suggests_is_installed = TRUE,
5353
update.config = FALSE,
54-
use.config = FALSE,
54+
use.config = TRUE,
5555
path.c = "dev/config_attachment.yaml"
5656
) {
5757

58-
# decide whether to use or update config file
59-
if (isTRUE(update.config & use.config)) {
60-
stop("Cannot use and update config at the same time")
61-
} else if (isTRUE(use.config)) {
62-
# reassign input value to saved parameters
63-
saved_att_params <- load_att_params(path_to_yaml = path.c)
64-
for (param_name in names(saved_att_params)){
65-
assign(param_name, saved_att_params[[param_name]])
66-
}
67-
} else if (isTRUE(update.config)) {
68-
# extract all current parameter values - ignore config parameters - save also default
69-
att_params <- names(formals(att_amend_desc))
70-
att_params <- att_params[!att_params %in% c("update.config", "use.config", "path.c")]
71-
att_params <- mget(att_params)
58+
if (path != ".") {
59+
old <- setwd(normalizePath(path))
60+
on.exit(setwd(old))
61+
}
62+
63+
path <- normalizePath(path)
64+
65+
# decide whether to use or update config file ----
66+
if (isTRUE(update.config) & isTRUE(use.config)) {
67+
use.config <- FALSE
68+
message("'update.config' was set to TRUE, hence, 'use.config' was forced to FALSE")
69+
}
70+
71+
# extract all current parameter values - ignore config parameters - save also default
72+
att_params <- names(formals(att_amend_desc))
73+
att_params <- att_params[!att_params %in% c("path", "update.config", "use.config", "path.c")]
74+
local_att_params <- mget(att_params)
75+
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)) {
7285
# save current parameters to yaml config - overwrite if already exist
7386
save_att_params(
74-
param_list = att_params,
87+
param_list = local_att_params,
7588
path_to_yaml = path.c,
7689
overwrite = TRUE)
90+
} else {
91+
message("attachment config file was not updated. Parameters used this time won't be stored.")
7792
}
7893

94+
# Save all open files ----
7995
save_all()
8096

81-
if (path != ".") {
82-
old <- setwd(normalizePath(path))
83-
on.exit(setwd(old))
84-
}
85-
86-
path <- normalizePath(path)
87-
97+
# Update description ----
8898
if (!file.exists(path.d)) {
8999
x3 <- description$new("!new")
90100
x3$set("Package", basename(path))

R/load_att_params.R

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)