From ed0b628902c640877f9e98e539ffbcd0f25e8a3c Mon Sep 17 00:00:00 2001 From: Philipp Baumann Date: Tue, 16 Jan 2024 20:15:46 +0100 Subject: [PATCH] Implement `"append"` option --- R/find_rev.R | 28 +++++++++++++++------------- dev/build_envs.Rmd | 28 +++++++++++++++------------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/R/find_rev.R b/R/find_rev.R index 658f17e4..e60ed523 100644 --- a/R/find_rev.R +++ b/R/find_rev.R @@ -844,10 +844,6 @@ init <- function(project_path = ".", "\n* Keep existing `.Rprofile`. in `project_path`:\n", paste0(project_path, "/"), "\n" ) - if (message_type == "verbose") { - cat("\n* Current lines of local `.Rprofile` are:\n\n") - cat(readLines(con = file(rprofile_file)), sep = "\n") - } } else { write_rprofile(rprofile_text, rprofile_file) message_rprofile(action_string = "Added", project_path = project_path) @@ -856,10 +852,6 @@ init <- function(project_path = ".", }, create_backup = { if (isTRUE(rprofile_exists)) { - if (message_type == "verbose") { - cat("\n* Current lines of local `.Rprofile` are\n:") - cat(readLines(con = file(rprofile_file)), sep = "\n") - } file.copy(from = rprofile_file, to = rprofile_backup) cat( "\n==> Backed up existing `.Rprofile` in file:\n", rprofile_backup, @@ -870,6 +862,10 @@ init <- function(project_path = ".", action_string = "Overwrote", project_path = project_path ) + if (message_type == "verbose") { + cat("\n* Current lines of local `.Rprofile` are\n:") + cat(readLines(con = file(rprofile_file)), sep = "\n") + } set_message_session_PATH(message_type = message_type) } }, @@ -884,12 +880,19 @@ init <- function(project_path = ".", action_string = "Added", project_path = project_path ) } - if (message_type == "verbose") { - cat("\n* Current lines of local `.Rprofile` are:\n\n") - cat(readLines(con = file(rprofile_file)), sep = "\n") - } + }, + append = { + cat(paste0(rprofile_text, "\n"), file = rprofile_file, append = TRUE) + message_rprofile( + action_string = "Appended", project_path = project_path + ) } ) + + if (message_type == "verbose") { + cat("\n* Current lines of local `.Rprofile` are:\n\n") + cat(readLines(con = file(rprofile_file)), sep = "\n") + } on.exit(close(file(rprofile_file))) } @@ -910,7 +913,6 @@ get_rprofile_text <- function(rprofile_deparsed) { ) } - #' @noRd message_rprofile <- function(action_string = "Added", project_path = ".") { diff --git a/dev/build_envs.Rmd b/dev/build_envs.Rmd index fda724b4..61950173 100644 --- a/dev/build_envs.Rmd +++ b/dev/build_envs.Rmd @@ -943,10 +943,6 @@ init <- function(project_path = ".", "\n* Keep existing `.Rprofile`. in `project_path`:\n", paste0(project_path, "/"), "\n" ) - if (message_type == "verbose") { - cat("\n* Current lines of local `.Rprofile` are:\n\n") - cat(readLines(con = file(rprofile_file)), sep = "\n") - } } else { write_rprofile(rprofile_text, rprofile_file) message_rprofile(action_string = "Added", project_path = project_path) @@ -955,10 +951,6 @@ init <- function(project_path = ".", }, create_backup = { if (isTRUE(rprofile_exists)) { - if (message_type == "verbose") { - cat("\n* Current lines of local `.Rprofile` are\n:") - cat(readLines(con = file(rprofile_file)), sep = "\n") - } file.copy(from = rprofile_file, to = rprofile_backup) cat( "\n==> Backed up existing `.Rprofile` in file:\n", rprofile_backup, @@ -969,6 +961,10 @@ init <- function(project_path = ".", action_string = "Overwrote", project_path = project_path ) + if (message_type == "verbose") { + cat("\n* Current lines of local `.Rprofile` are\n:") + cat(readLines(con = file(rprofile_file)), sep = "\n") + } set_message_session_PATH(message_type = message_type) } }, @@ -983,12 +979,19 @@ init <- function(project_path = ".", action_string = "Added", project_path = project_path ) } - if (message_type == "verbose") { - cat("\n* Current lines of local `.Rprofile` are:\n\n") - cat(readLines(con = file(rprofile_file)), sep = "\n") - } + }, + append = { + cat(paste0(rprofile_text, "\n"), file = rprofile_file, append = TRUE) + message_rprofile( + action_string = "Appended", project_path = project_path + ) } ) + + if (message_type == "verbose") { + cat("\n* Current lines of local `.Rprofile` are:\n\n") + cat(readLines(con = file(rprofile_file)), sep = "\n") + } on.exit(close(file(rprofile_file))) } @@ -1009,7 +1012,6 @@ get_rprofile_text <- function(rprofile_deparsed) { ) } - #' @noRd message_rprofile <- function(action_string = "Added", project_path = ".") {