|
1 | 1 | # WARNING - Generated by {fusen} from dev/flat_create_dependencies_file.Rmd: do not edit by hand |
2 | 2 |
|
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 |
4 | 6 | #' |
5 | 7 | #' @param path path to the DESCRIPTION file |
6 | 8 | #' @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. |
8 | 10 | #' @param open_file Logical. Open the file created in an editor |
9 | 11 | #' @param ignore_base Logical. Whether to ignore package coming with base, as they cannot be installed (default TRUE) |
10 | 12 | #' @param install_only_if_missing Logical Modify the installation instructions to check, beforehand, if the packages are missing . (default FALSE) |
11 | 13 | #' @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. |
14 | 15 | #' @importFrom glue glue glue_collapse |
15 | 16 | #' @importFrom desc description |
16 | 17 | #' @importFrom utils packageDescription |
17 | 18 | #' |
18 | 19 | #' @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"), |
28 | 36 | #' to = file.path(dummypackage, "inst/dependencies.R"), |
29 | 37 | #' 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) |
34 | 43 | create_dependencies_file <- function(path = "DESCRIPTION", |
35 | 44 | field = c("Depends", "Imports"), |
36 | 45 | to = "inst/dependencies.R", |
37 | 46 | open_file = TRUE, |
38 | 47 | ignore_base = TRUE, |
39 | 48 | install_only_if_missing = FALSE) { |
40 | 49 |
|
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 | | - |
48 | 50 | # get all packages |
49 | 51 | ll <- att_from_description(path = path, field = field) |
50 | 52 | # get pkg in remotes |
51 | 53 | 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] |
55 | 58 | } |
56 | 59 |
|
57 | 60 | } |
58 | 61 |
|
59 | 62 | desc <- description$new(path) |
60 | 63 | # Get previous dependencies in Description in case version is set |
61 | | - # deps_orig <- desc$get_deps() |
62 | 64 | remotes_orig <- desc$get_remotes() |
63 | 65 |
|
64 | 66 | content <- dependencies_file_text(ll = ll, |
65 | 67 | remotes_orig = remotes_orig, |
66 | 68 | install_only_if_missing = install_only_if_missing) |
67 | 69 |
|
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) |
73 | 81 |
|
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) |
76 | 89 | } |
77 | 90 |
|
78 | | - return(invisible(content)) |
| 91 | + |
79 | 92 | } |
80 | 93 |
|
0 commit comments