Skip to content

Commit

Permalink
Merge pull request #246 from ThinkR-open/clean-obsolete-inflate
Browse files Browse the repository at this point in the history
Clean obsolete files during inflate when possible
  • Loading branch information
statnmap authored Apr 30, 2024
2 parents a3c26c9 + 029bd8b commit efd0e79
Show file tree
Hide file tree
Showing 26 changed files with 1,077 additions and 269 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Suggests:
rmarkdown,
rstudioapi,
styler,
testthat (>= 3.0.0),
testthat (>= 3.2.0),
withr
VignetteBuilder:
knitr
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
## Breaking changes

- {fusen} now relies on {lightparser} instead of {parsermd} to parse flat file. This allows to avoid installation problems with {parsermd}, which is not updated anymore. As {lightparser} is lighter, this may have unattended effects on specific flat file cases. Please report any issue you may encounter. (#233)
- `inflate_all*()` does not use parameter `clean` anymore. Use `check_unregistered` instead to check if all files are registered in the configuration file.

## New features

- `inflate()` detects functions renamed or removed and allow to clean the package repository (#24)
- Allow `organisation` in `init_share_on_github()` to send to a GitHub organisation
- Fix `load_flat_functions()` to work with VSCode

Expand Down
2 changes: 1 addition & 1 deletion R/fill_description.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,5 @@ fill_description <- function(pkg = ".", fields, overwrite = FALSE) {

desc$write(file = desc_file)

desc_file
invisible(desc_file)
}
72 changes: 52 additions & 20 deletions R/inflate.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ regex_example <- paste(regex_example_vec, collapse = "|")
#' @param document Logical. Whether to document your package using \code{\link[attachment:att_amend_desc]{att_amend_desc}}
#' @param overwrite Logical (TRUE, FALSE) or character ("ask", "yes", "no).
#' Whether to overwrite vignette and functions if already exists.
#' @param clean Logical (TRUE, FALSE) or character ("ask", "yes", "no)
#' Whether to delete files that are not anymore created by the current
#' flat file. Typically, if you have deleted or renamed a function
#' in the flat file. Default to "ask".
#' @param ... Arguments passed to `devtools::check()`.
#' For example, you can do `inflate(check = TRUE, quiet = TRUE)`, where `quiet` is
#' passed to `devtools::check()`.
Expand Down Expand Up @@ -64,19 +68,23 @@ regex_example <- paste(regex_example_vec, collapse = "|")
inflate <- function(pkg = ".", flat_file,
vignette_name = "Get started",
open_vignette = TRUE,
check = TRUE, document = TRUE,
check = TRUE,
document = TRUE,
overwrite = "ask",
clean = "ask",
...) {
if (!is.null(list(...)[["name"]])) {
stop(paste0(
"The `name` argument to `inflate()` is deprecated since {fusen} version 0.3.0.",
"The `name` argument to `inflate()`",
" is deprecated since {fusen} version 0.3.0.",
"\nPlease use `vignette_name = '", list(...)[["name"]], "'` instead.\n"
))
vignette_name <- list(...)[["name"]]
}
if (!is.null(list(...)[["rmd"]])) {
stop(paste0(
"The `rmd` argument to `inflate()` is deprecated since {fusen} version 0.3.0.",
"The `rmd` argument to `inflate()`",
" is deprecated since {fusen} version 0.3.0.",
"\nPlease use `flat_file = '", list(...)[["rmd"]], "'` instead.\n"
))
flat_file <- list(...)[["rmd"]]
Expand All @@ -92,21 +100,24 @@ inflate <- function(pkg = ".", flat_file,
}

# If flat_file empty
if (missing(flat_file) && requireNamespace("rstudioapi") && rstudioapi::isAvailable() &&
if (missing(flat_file) &&
requireNamespace("rstudioapi") && rstudioapi::isAvailable() &&
rstudioapi::hasFun("getSourceEditorContext")) {
curr_editor <- rstudioapi::getSourceEditorContext()
current_file <- curr_editor$path
if (!is.null(current_file) && grepl("^flat.*[.](R|r|q)md$", basename(current_file))) {
if (!is.null(current_file) &&
grepl("^flat.*[.](R|r|q)md$", basename(current_file))) {
if (overwrite == "ask") {
sure <- paste0(
"You did not specify parameter 'flat_file'. The current file will be inflated:\n",
"You did not specify parameter 'flat_file'.",
" The current file will be inflated:\n",
current_file, ".\n",
"With vignette name: ", vignette_name, "\n",
"Are you sure this is what you planned? (y/n)\n"
)
do_it <- readline(sure) == "y"
do_it <- readline(sure) == "y" || readline(sure) == "yes"
} else {
do_it <- isTRUE(overwrite)
do_it <- isTRUE(overwrite) || overwrite == "yes"
}
if (do_it) {
message(
Expand All @@ -127,7 +138,10 @@ inflate <- function(pkg = ".", flat_file,
}

old <- setwd(pkg)
if (normalizePath(old, mustWork = FALSE) != normalizePath(pkg, mustWork = FALSE)) {
if (
normalizePath(old, mustWork = FALSE) !=
normalizePath(pkg, mustWork = FALSE)
) {
if (dir.exists(old)) {
on.exit(setwd(old))
} else {
Expand All @@ -136,7 +150,10 @@ inflate <- function(pkg = ".", flat_file,
}

old_proj <- usethis::proj_get()
if (normalizePath(old_proj, mustWork = FALSE) != normalizePath(pkg, mustWork = FALSE)) {
if (
normalizePath(old_proj, mustWork = FALSE) !=
normalizePath(pkg, mustWork = FALSE)
) {
if (dir.exists(old_proj)) {
on.exit(usethis::proj_set(old_proj))
} else {
Expand All @@ -151,16 +168,17 @@ inflate <- function(pkg = ".", flat_file,

if (!file.exists(file.path(normalizePath(pkg), "DESCRIPTION"))) {
stop(
"DESCRIPTION file does not exist in your directory:", normalize_path_winslash(pkg), ".\n",
"Have you run the content of the 'description' chunk of your {fusen} template?"
"DESCRIPTION file does not exist in your directory:",
normalize_path_winslash(pkg), ".\n",
"Have you run the content of the 'description'",
" chunk of your {fusen} template?"
)
}

if (length(list.files(pkg, pattern = ".Rproj")) > 0) {
if (!file.exists(".Rbuildignore")) {
file.create(".Rbuildignore")
}
# usethis::use_build_ignore(basename(flat_file))
usethis::use_build_ignore(paste0(basename(pkg), ".Rproj"))
usethis::use_build_ignore(".Rproj.user")
}
Expand All @@ -173,7 +191,10 @@ inflate <- function(pkg = ".", flat_file,
}

if (!file.exists(flat_file_path)) {
stop(flat_file, " does not exists, please use fusen::add_flat_template() to create it.")
stop(
flat_file, " does not exists, ",
"please use fusen::add_flat_template() to create it."
)
}

# Are you sure ?
Expand All @@ -182,10 +203,14 @@ inflate <- function(pkg = ".", flat_file,
}
overwrite <- match.arg(overwrite, choices = c("ask", "yes", "no"))
cleaned_vignette_name <- asciify_name(vignette_name)
vignette_path <- file.path(pkg, "vignettes", paste0(cleaned_vignette_name, ".Rmd"))
vignette_path <- file.path(
pkg, "vignettes",
paste0(cleaned_vignette_name, ".Rmd")
)
if (file.exists(vignette_path)) {
if (overwrite == "ask") {
rm_exist_vignette <- getFromNamespace("can_overwrite", "usethis")(vignette_path)
rm_exist_vignette <-
getFromNamespace("can_overwrite", "usethis")(vignette_path)
} else {
rm_exist_vignette <- ifelse(overwrite == "yes", TRUE, FALSE)
}
Expand Down Expand Up @@ -242,14 +267,21 @@ inflate <- function(pkg = ".", flat_file,

# Get functions and create R and tests files ----s
if (!is.null(fun_code)) {
script_files <- create_functions_all(parsed_tbl, fun_code, pkg, relative_flat_file)
script_files <- create_functions_all(
parsed_tbl, fun_code, pkg, relative_flat_file
)
} else {
message("No chunks named 'function-xx' or 'fun-xx' were found in the Rmarkdown file: ", flat_file)
message(
"No chunks named 'function-xx' or 'fun-xx'",
" were found in the Rmarkdown file: ", flat_file
)
script_files <- tibble::tibble(type = character(0), path = character(0))
}

# Create vignette ----
if (!(is.null(vignette_name) || is.na(vignette_name) || vignette_name == "")) {
if (!(is.null(vignette_name) ||
is.na(vignette_name) ||
vignette_name == "")) {
vignette_file <- create_vignette(
parsed_tbl = parsed_tbl,
pkg = pkg,
Expand Down Expand Up @@ -295,7 +327,7 @@ inflate <- function(pkg = ".", flat_file,
config_file <- df_to_config(
df_files = all_files,
flat_file_path = relative_flat_file,
clean = TRUE,
clean = clean,
state = "active",
# TODO - Set to force = FALSE when there is a possibility to clean the config
# when there are manually deleted file ----
Expand Down
54 changes: 41 additions & 13 deletions R/inflate_all.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
#' Inflate all the flat files stored in "dev/" and starting with "flat_"
#'
#' @param pkg Path to package
#' @param clean Logical. Whether to help detect unregistered files.
#' @param stylers Function to be run at the end of the process, like `styler::style_pkg` or `lintr::lint_package` or a lambda function combining functions like: `function() {styler::style_pkg(); lintr::lint_package()}`. For a unique function, use the format without parenthesis `()` at the end of the command.
#' @param check_unregistered Logical. Whether to help detect unregistered files.
#' Typically files not created from a flat file and added manually in the repository.
#' @param stylers Function to be run at the end of the process,
#' like `styler::style_pkg` or `lintr::lint_package`
#' or a lambda function combining functions like:
#' `function() {styler::style_pkg(); lintr::lint_package()}`.
#' For a unique function, use the format without parenthesis `()`
#' at the end of the command.
#' @inheritParams inflate
#'
#' @importFrom yaml read_yaml
Expand All @@ -15,16 +21,26 @@
#'
#' @return side effect. Inflates all your flat files that can be inflated.
#'
#' @details This requires to [inflate()] all flat files individually at least once, so that their specific inflate configurations are stored.
#' @details This requires to [inflate()] all flat files
#' individually at least once, so that their specific
#' inflate configurations are stored.
#'
#' This also requires to register all R, tests and vignettes files of your package, even if not created with an inflate. Run [inflate_all()] once and read the messages. The first time, you will probably need to run [register_all_to_config()] if your package is not new.
#' This also requires to register all R,
#' tests and vignettes files of your package,
#' even if not created with an inflate.
#' Run [inflate_all()] once and read the messages.
#' The first time, you will probably need to run
#' [register_all_to_config()] if your package is not new.
#'
#' For more information, read the `vignette("inflate-all-your-flat-files", package = "fusen")`
#' For more information, read the
#' `vignette("inflate-all-your-flat-files", package = "fusen")`
#'
#' @seealso
#' [inflate()] for the options of a single file inflate,
#' [check_not_registered_files()] for the list of files not already associated with a flat file in the config file,
#' [register_all_to_config()] for automatically registering all files already present in the project before the first `inflate_all()`
#' [check_not_registered_files()] for the list of files
#' not already associated with a flat file in the config file,
#' [register_all_to_config()] for automatically registering
#' all files already present in the project before the first `inflate_all()`
#'
#' @export
#' @examples
Expand Down Expand Up @@ -83,7 +99,14 @@
#'
#' # Clean the temporary directory
#' unlink(dummypackage, recursive = TRUE)
inflate_all <- function(pkg = ".", document = TRUE, check = TRUE, open_vignette = FALSE, overwrite = TRUE, clean = TRUE, stylers, ...) {
inflate_all <- function(
pkg = ".",
document = TRUE,
check = TRUE,
open_vignette = FALSE,
overwrite = TRUE,
check_unregistered = TRUE,
stylers, ...) {
config_file <- getOption("fusen.config_file", default = "dev/config_fusen.yaml")

if (!file.exists(config_file)) {
Expand Down Expand Up @@ -120,7 +143,9 @@ inflate_all <- function(pkg = ".", document = TRUE, check = TRUE, open_vignette
message("No flat files were inflated")
} else {
apply_inflate <- function(inflate_params, pkg, overwrite, open_vignette) {
config_file <- getOption("fusen.config_file", default = "dev/config_fusen.yaml")
config_file <- getOption("fusen.config_file",
default = "dev/config_fusen.yaml"
)
# Change config option temporary, to be able to modify it on the fly
config_file_tmp <- tempfile(pattern = "tempconfig")
on.exit(file.remove(config_file_tmp))
Expand All @@ -140,10 +165,13 @@ inflate_all <- function(pkg = ".", document = TRUE, check = TRUE, open_vignette
)
}

apply_inflate(inflate_params, pkg = pkg, overwrite = overwrite, open_vignette = open_vignette)
apply_inflate(inflate_params,
pkg = pkg, overwrite = overwrite,
open_vignette = open_vignette
)
}

if (isTRUE(clean)) {
if (isTRUE(check_unregistered)) {
cli::cat_rule("check not registered files")
invisible(check_not_registered_files(path = pkg))
}
Expand Down Expand Up @@ -172,6 +200,6 @@ inflate_all <- function(pkg = ".", document = TRUE, check = TRUE, open_vignette

#' @rdname inflate_all
#' @export
inflate_all_no_check <- function(pkg = ".", document = TRUE, open_vignette = FALSE, overwrite = TRUE, clean = TRUE, stylers, ...) {
inflate_all(pkg = pkg, document = document, check = FALSE, open_vignette = open_vignette, overwrite = overwrite, clean = clean, stylers, ...)
inflate_all_no_check <- function(pkg = ".", document = TRUE, open_vignette = FALSE, overwrite = TRUE, check_unregistered = TRUE, stylers, ...) {
inflate_all(pkg = pkg, document = document, check = FALSE, open_vignette = open_vignette, overwrite = overwrite, check_unregistered = check_unregistered, stylers, ...)
}
Loading

0 comments on commit efd0e79

Please sign in to comment.