Skip to content

Commit

Permalink
feat: draw tree works for classical package without fusen
Browse files Browse the repository at this point in the history
closes #189
  • Loading branch information
statnmap committed May 7, 2024
1 parent a444d8c commit 7c4ebde
Show file tree
Hide file tree
Showing 13 changed files with 257 additions and 112 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

## New features

- `draw_the_tree()` along with `get_package_structure()` allows to draw the package structure with
all functions created in each R file, and whether they are exported (#189)
- `rename_flat_file()` allows to rename a flat file, and deals with config and inflated files
- `deprecate_flat_file()` helps properly deprecate a flat file, modifies the config file
and cleans the previously inflated files
Expand Down
2 changes: 1 addition & 1 deletion R/get_all_created_funs.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
get_all_created_funs <- function(file) {
stopifnot(tools::file_ext(file) %in% c("R", "r"))
# Get each bloc of code
parts_parsed <- parse(file)
parts_parsed <- parse(file, keep.source = TRUE)
# We cannot directly get as.character as it would
# remove character quotes from text only lines
# like a line with : "_example" fails
Expand Down
46 changes: 33 additions & 13 deletions R/get_package_structure.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

#' Get structure and information of a 'fusen' built package for developers
#'
#' @param path The path to the yaml file
#' @param config_file Path to a source configuration file
#' to get the structure from
#' @param pkg The package directory
#' @param emoji Add emojis to the output
#' @param silent Do not print messages
#'
#' @return A list of information about the package
#' @export
Expand All @@ -29,6 +29,18 @@
#' flat_file <- dev_file[grepl("flat_", dev_file)]
#'
#' usethis::with_project(dummypackage, {
#' # Add an extra R file with internal function
#' # to list in "keep"
#' dir.create("R")
#' cat("extra_fun <- function() {1}\n", file = "R/my_extra_fun.R")
#'
#' # Works with classical package
#' pkg_structure <- get_package_structure()
#' draw_the_tree(pkg_structure)
#' })
#'
#' usethis::with_project(dummypackage, {
#' # Works with 'fusen' package
#' suppressMessages(
#' inflate(
#' pkg = dummypackage, flat_file = flat_file,
Expand All @@ -37,27 +49,29 @@
#' )
#' )
#'
#' # Add an extra R file to list in "keep"
#' cat("extra_fun <- function() {1}\n", file = "R/my_extra_fun.R")
#'
#' pkg_structure <- get_package_structure()
#' draw_the_tree(pkg_structure)
#' })
get_package_structure <- function(
path,
pkg = ".",
config_file,
emoji = TRUE,
silent = FALSE) {
if (missing(path)) {
if (missing(config_file)) {
yaml_fusen_file_orig <- getOption(
"fusen_config_file",
default = "dev/config_fusen.yaml"
)
}

# Add not registered files in a copy of the config file
yaml_fusen_file <- tempfile(fileext = ".yaml")
file.copy(yaml_fusen_file_orig, yaml_fusen_file)
if (!file.exists(yaml_fusen_file_orig)) {
# Not 'fusen' package or not inflated
file.create(yaml_fusen_file)
} else {
file.copy(yaml_fusen_file_orig, yaml_fusen_file)
}

# Add not registered files in a copy of the config file
suppressMessages(
register_all_to_config(
pkg = ".",
Expand All @@ -75,8 +89,9 @@ get_package_structure <- function(
cat_rule("Reading NAMESPACE file")
}
} else {
namespace <- NULL
if (isFALSE(silent)) {
cat_rule("No NAMESPACE file found there: ", getwd())
cat_rule(paste("No NAMESPACE file found there: ", getwd()))
}
}

Expand All @@ -102,7 +117,8 @@ get_package_structure <- function(
if (emoji) {
flat_state <- yaml_fusen[[flat_file]]$state
yaml_fusen[[flat_file]]$state <-
paste(ifelse(flat_state == "active",
paste(ifelse(
flat_state == "active",
"\U0001f34f", "\U0001f6d1"
), flat_state)
}
Expand Down Expand Up @@ -143,11 +159,15 @@ get_package_structure <- function(
#'
#' @param structure_list A list of information about the package as issued
#' from `[get_package_structure()]`
#' @param silent Do not print messages
#'
#' @export
#' @rdname get_package_structure
#'
draw_the_tree <- function(structure_list) {
draw_the_tree <- function(structure_list, silent) {
if (missing(structure_list)) {
structure_list <- get_package_structure(silent = silent)
}
# Calculate the depth of a list
depth <- function(structure_list) {
if (!is.list(structure_list)) {
Expand Down
68 changes: 41 additions & 27 deletions R/register_config_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,21 @@ check_not_registered_files <- function(path = ".", config_file, guess = TRUE, to
if (file.exists(config_file)) {
# Read config file, and remove those already there
config_list <- yaml::read_yaml(config_file)
config_list_path <- normalize_path_winslash(get_list_paths(config_list), mustWork = FALSE)
res_existing <- res[res$path %in% config_list_path, ]
res_new <- res[!res$path %in% config_list_path, ]
config_flat_paths <- sapply(config_list, function(x) x[["path"]])
config_flat_paths <- config_flat_paths[!names(config_flat_paths) == "keep"]
all_paths_in_config <- get_list_paths(config_list)
if (any(!is.null(all_paths_in_config))) {
config_list_path <- normalize_path_winslash(
all_paths_in_config,
mustWork = FALSE
)
res_new <- res[!res$path %in% config_list_path, ]
config_flat_paths <- sapply(config_list, function(x) x[["path"]])
config_flat_paths <-
config_flat_paths[!names(config_flat_paths) == "keep"]
} else {
res_new <- res
config_flat_paths <- NULL
}
} else {
res_existing <- res[FALSE, ]
res_new <- res
config_flat_paths <- NULL
}
Expand Down Expand Up @@ -427,28 +435,32 @@ df_to_config <- function(df_files,
complete_yaml <- read_yaml(config_file)

yaml_paths <- get_list_paths(complete_yaml)
yaml_paths <- yaml_paths[!grepl("inflate\\.", names(yaml_paths))]
all_exists <- file.exists(yaml_paths)
if (!all(all_exists)) {
msg <- paste(
"Some paths in config_file do not exist:",
paste(
yaml_paths[!all_exists],
collapse = ", "
), ".\n",
"Please open the configuration file: ",
config_file,
" to verify, and delete the non-existing files if needed."
)
if (isTRUE(force)) {
cli_alert_warning(
if (all(is.null(yaml_paths))) {
complete_yaml <- list()
} else if (any(!is.null(yaml_paths))) {
yaml_paths <- yaml_paths[!grepl("inflate\\.", names(yaml_paths))]
all_exists <- file.exists(yaml_paths)
if (!all(all_exists)) {
msg <- paste(
"Some paths in config_file do not exist:",
paste(
msg,
"However, you forced to write it in the yaml file with `force = TRUE`."
)
yaml_paths[!all_exists],
collapse = ", "
), ".\n",
"Please open the configuration file: ",
config_file,
" to verify, and delete the non-existing files if needed."
)
} else {
stop(msg)
if (isTRUE(force)) {
cli_alert_warning(
paste(
msg,
"However, you forced to write it in the yaml file with `force = TRUE`."
)
)
} else {
stop(msg)
}
}
}
} else {
Expand Down Expand Up @@ -778,7 +790,9 @@ register_all_to_config <- function(pkg = ".", config_file) {
}

# Use the function to check the list of files
out_df <- check_not_registered_files(pkg, config_file = config_file, to_csv = FALSE, open = FALSE)
out_df <- check_not_registered_files(pkg,
config_file = config_file, to_csv = FALSE, open = FALSE
)


if (is.null(out_df)) {
Expand Down
6 changes: 3 additions & 3 deletions dev/README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ output: github_document

```{r, message=FALSE, results='asis'}
pkgload::load_all()
withr::with_dir(here::here(), {
pkg_structure <- get_package_structure(silent = TRUE)
draw_the_tree(pkg_structure)
usethis::with_project(here::here(), {
draw_the_tree(silent = TRUE)
})
```
6 changes: 3 additions & 3 deletions dev/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

``` r
pkgload::load_all()
withr::with_dir(here::here(), {
pkg_structure <- get_package_structure(silent = TRUE)
draw_the_tree(pkg_structure)

usethis::with_project(here::here(), {
draw_the_tree(silent = TRUE)
})
```

Expand Down
3 changes: 1 addition & 2 deletions dev/dev_history_cran.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ Sys.setenv("FUSEN_TEST_PUBLISH" = "FALSE")
devtools::check()

# Update the map of the package
pkg_structure <- get_package_structure()
draw_the_tree(pkg_structure)
fusen::draw_the_tree()

rmarkdown::render("dev/README.Rmd",
output_format = "github_document", output_file = "README.md"
Expand Down
Loading

0 comments on commit 7c4ebde

Please sign in to comment.