Skip to content

Commit

Permalink
version 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
feddelegrand7 authored and cran-robot committed Feb 13, 2024
1 parent efc985d commit 1b88390
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 76 deletions.
12 changes: 7 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: rfold
Type: Package
Title: Working with many R Folders Within an R Package
Version: 0.1.0
Version: 0.2.0
Authors@R:
person(given = "Mohamed El Fodil",
family = "Ihaddaden",
Expand All @@ -12,14 +12,16 @@ Description:
the R folder while making additional checks.
License: MIT + file LICENSE
Encoding: UTF-8
Imports: cli (>= 3.6.1), fs (>= 1.5.0), here (>= 1.0.1), usethis (>=
2.0.1)
URL: https://github.com/feddelegrand7/rfold
BugReports: https://github.com/feddelegrand7/rfold/issues
Imports: cli (>= 3.6.1), fs (>= 1.5.0), glue, here (>= 1.0.1), usethis
(>= 2.0.1)
RoxygenNote: 7.1.1
Suggests: testthat (>= 3.0.0)
Config/testthat/edition: 3
NeedsCompilation: no
Packaged: 2023-11-01 22:37:29 UTC; Administrateur
Packaged: 2024-02-12 15:37:05 UTC; Administrateur
Author: Mohamed El Fodil Ihaddaden [aut, cre]
Maintainer: Mohamed El Fodil Ihaddaden <ihaddaden.fodeil@gmail.com>
Repository: CRAN
Date/Publication: 2023-11-02 15:50:02 UTC
Date/Publication: 2024-02-12 15:50:02 UTC
8 changes: 4 additions & 4 deletions MD5
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
3680a516b6ed0be936a838738de0db71 *DESCRIPTION
abf5bf582b778402d98b4ce34d177c7e *DESCRIPTION
ce7c21a851fc05d855d30ded2dd4dca4 *LICENSE
818722a25944f8f44c044f54cb89f6cd *NAMESPACE
5793a5608b1f247fa72d16e10bb303a9 *NEWS.md
7453e420754cad0e8d5d7d3e7d719e3a *R/main.R
b4844025274abe17014899b8352c7388 *README.md
e17dd4ea31da3e5898a0e1a0b06c5de7 *NEWS.md
73d1a35eada1c76114066f7492638701 *R/main.R
5ec6100e58c1e1507fc061b32fa7bf60 *README.md
90189bfb82ebe81e65db92e71a32854a *man/figures/figure1.png
e631da678fd55a6dd635e12a7f6a7bac *man/figures/figure2.png
9f02da1d16599452a48c616fe75b2bd6 *man/rfold.Rd
Expand Down
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# rfold 0.2.0

### New features:
* The user can now similarly to script files, refactor `test` files disposition. For example, one might want to put script files and test files within the same folder. By appending the `test-` word to an `R` script, `rfold` will recognise it as a test file and move it to the corresponding folder. Note that for now, `rfold` works with `testthat` only.

# rfold 0.1.0
* initial release

# rfold 0.0.1

* Added a `NEWS.md` file to track changes to the package.
178 changes: 121 additions & 57 deletions R/main.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,55 @@

.get_files_that_are_not_in_r_folder <- function(folders_to_ignore) {
.get_necessary_r_files <- function(folders_to_ignore, type) {

r_folder_exists <- unname(fs::dir_exists(here::here("R")))
r_folder_exists <- unname(fs::dir_exists(here::here(type)))

if (!r_folder_exists) {
message("R folder does not exist, creating it")
fs::dir_create(here::here("R"))
message(glue::glue("{type} folder does not exist, creating it"))

if (type == "R") {
fs::dir_create(here::here("R"))
}

if (type == "tests") {
usethis::use_testthat()
}

}

if (type == "R") {

r_folders <- list.dirs(here::here(type))

if (length(r_folders) > 1) {
stop(glue::glue("Looks like you've folders insider your R folder, please inspect"))
}

}

r_folders <- list.dirs(here::here("R"))
regex_to_use <- "\\.R$|\\.r$"

if (length(r_folders) > 1) {
stop("Looks like you've folders insider your R folder, please inspect")
if (type == "tests") {
regex_to_use <- "test-.*\\.R$|test-.*\\.r$"
}

all_r_files <- fs::dir_ls(
path = ".",
regexp = "\\.R$|\\.r$",
regexp = regex_to_use,
recurse = TRUE
)

all_r_files_base <- basename(all_r_files)

test_files_to_check <- grepl(pattern = "^test-", x = all_r_files_base)

if (type == "R") {
all_r_files <- all_r_files[!test_files_to_check]
}

if (type == "tests") {
all_r_files <- all_r_files[test_files_to_check]
}

tmp_df <- data.frame(
r_files = all_r_files,
dir_name = dirname(all_r_files),
Expand Down Expand Up @@ -48,9 +78,15 @@
r_files_not_in_r_dir <- r_files_not_in_r_dir[!r_check_folder]

if (length(r_files_not_in_r_dir) == 0) {
cli::cli_alert_warning(
"No R files available outside of the R directory, nothing to do"
)

msg <- "No R files available outside of the R directory, nothing to do"

if (type == "tests") {
msg <- "No test files available outside of the tests folder, did you append the name
with the prefix 'test-'? "
}

cli::cli_alert_warning(msg)
return(character(0))
}

Expand All @@ -72,72 +108,46 @@
}


#' Transfer .R files into the R directory
#'
#' @return called for the side effect of transferring all R files
# available inside a project into the R folder
#' @param folders_to_ignore a string vector of plain names folders that should be ignored
#' where transferring .R files into the main R folder (for example a dev folder). Defaults to NULL
#'
#' @param script_name_prefix characters string of length 1 that will be appended to the start of the .R scripts when transferring them to the R folder. Defaults to 'DO_NOT_EDIT', set it to NULL for no appending
#'
#' @export
#'

rfold <- function(
folders_to_ignore = NULL,
script_name_prefix = "DO_NOT_EDIT_"
) {
.move_files_in_dir <- function(r_files, type, script_name_prefix) {
dir_components <- unique(unlist(strsplit(r_files, "/")))

if (length(script_name_prefix) > 1) {
stop("script_name_prefix must be of length 1")
}
dir_components <- dir_components[!grepl("\\.R$", dir_components)]

if (is.null(script_name_prefix)) {
script_name_prefix <- ""
}
usethis::use_build_ignore(dir_components)

if (!is.null(folders_to_ignore)) {
dir_exist <- dir.exists(folders_to_ignore)
r_files_string <- paste(toString(r_files), collapse = ", ")

if (!any(dir_exist)) {
dir_no_exist <- folders_to_ignore[!dir_exist]
stop("The following directories do not exist. Can't ignore them: ", dir_no_exist)
file_names <- basename(r_files)

}
if (type == "R") {
new_file_names <- paste0(script_name_prefix, file_names)
}

r_files <- .get_files_that_are_not_in_r_folder(folders_to_ignore)

if (length(r_files) == 0) {
return(invisible(NULL))
if (type == "tests") {
file_names <- gsub(".R", "", file_names)
file_names <- gsub("test-", "", file_names)
new_file_names <- paste0("test-", script_name_prefix, file_names, ".R")
}

dir_components <- unique(unlist(strsplit(r_files, "/")))

dir_components <- dir_components[!grepl("\\.R$", dir_components)]
dir_to_consider <- "R"

usethis::use_build_ignore(dir_components)

r_files_string <- paste(toString(r_files), collapse = ", ")
if (type == "tests") {
dir_to_consider <- "tests/testthat"
}

cli::cli_alert_info(
"Copying the following R files with prefix '{script_name_prefix}' into the R folder: {r_files_string}"
"Copying the following {type} files with prefix '{script_name_prefix}' into the {dir_to_consider} folder: {r_files_string}"
)

file_names <- basename(r_files)

new_file_names <- paste0(script_name_prefix, file_names)

fs::file_copy(
path = r_files,
new_path = here::here("R", new_file_names),
new_path = here::here(dir_to_consider, new_file_names),
overwrite = TRUE
)

for (i in seq_along(new_file_names)) {

script_to_consider <- here::here("R", new_file_names[i])
script_to_consider <- here::here(dir_to_consider, new_file_names[i])

existing_script_content <- readLines(script_to_consider)

Expand All @@ -161,6 +171,60 @@ rfold <- function(
writeLines(new_content, con = script_to_consider)
}

cli::cli_alert_success("Success")
}

#' Transfer .R files into the R directory
#'
#' @return called for the side effect of transferring all R files
# available inside a project into the R folder
#' @param folders_to_ignore a string vector of plain names folders that should be ignored
#' where transferring .R files into the main R folder (for example a dev folder). Defaults to NULL
#'
#' @param script_name_prefix characters string of length 1 that will be appended to the start of the .R scripts when transferring them to the R folder. Defaults to 'DO_NOT_EDIT', set it to NULL for no appending
#'
#' @export
#'

rfold <- function(
folders_to_ignore = NULL,
script_name_prefix = "DO_NOT_EDIT_"
) {

if (length(script_name_prefix) > 1) {
stop("script_name_prefix must be of length 1")
}

if (is.null(script_name_prefix)) {
script_name_prefix <- ""
}

if (!is.null(folders_to_ignore)) {
dir_exist <- dir.exists(folders_to_ignore)

if (!any(dir_exist)) {
dir_no_exist <- folders_to_ignore[!dir_exist]
stop("The following directories do not exist. Can't ignore them: ", dir_no_exist)

}
}

normal_r_files <- .get_necessary_r_files(folders_to_ignore, type = "R")
test_files <- .get_necessary_r_files(folders_to_ignore, type = "tests")

if (length(normal_r_files) > 0) {
.move_files_in_dir(
r_files = normal_r_files,
type = "R",
script_name_prefix = script_name_prefix
)
}

if (length(test_files) > 0) {
.move_files_in_dir(
r_files = test_files,
type = "tests",
script_name_prefix = script_name_prefix
)
}

}

0 comments on commit 1b88390

Please sign in to comment.