Skip to content

Commit

Permalink
merge: clean-rebase in last version of main
Browse files Browse the repository at this point in the history
Keep both version when conflicts

Issue #24

Merge branch '24-rebase2-clean-yourself' into 24-rebase3-clean-yourself

# Conflicts:
#	R/inflate.R
#	tests/testthat/test-inflate-part2.R
  • Loading branch information
statnmap committed Dec 21, 2022
2 parents 58d72f8 + 898d504 commit b063320
Show file tree
Hide file tree
Showing 28 changed files with 2,569 additions and 99 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ Imports:
tidyr,
tools,
usethis (>= 2.0.0),
utils
utils,
yaml
Suggests:
knitr,
pkgload,
Expand Down
7 changes: 7 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ export(add_flat_template)
export(add_full)
export(add_fusen_chunks)
export(add_minimal)
export(check_not_registered_files)
export(clean_fusen_files)
export(create_fusen)
export(df_to_config)
export(fill_description)
export(inflate)
export(init_share_on_github)
export(load_flat_functions)
export(register_all_to_config)
importFrom(cli,cat_rule)
importFrom(cli,cli_alert_danger)
importFrom(cli,cli_alert_success)
Expand All @@ -23,6 +27,9 @@ importFrom(parsermd,parse_rmd)
importFrom(parsermd,rmd_get_chunk)
importFrom(parsermd,rmd_node_code)
importFrom(stats,na.omit)
importFrom(stats,setNames)
importFrom(stringi,stri_trans_general)
importFrom(tools,file_path_sans_ext)
importFrom(utils,getFromNamespace)
importFrom(utils,read.csv)
importFrom(utils,write.csv)
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

## New features

- Migrate from a non-fusen package or a previous version of {fusen} use with `register_all_to_config()`
- Create or update the config file from a data.frame to list legitimate scripts
- Load all `function` chunks of the flat file currently opened with `load_flat_functions()` (Like a `load_all()` for a flat file)
- Allow multiple examples for the same function (#149)
- Allow to `inflate()` a Quarto ".qmd" flat file (#160)

## Minor
Expand Down
77 changes: 38 additions & 39 deletions R/add_flat_template.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,52 @@
#' @rdname add_flat_template
#' @export
add_additional <- function(
pkg = ".",
pkg = ".",
dev_dir = "dev",
flat_name = "additional",
overwrite = FALSE,
open = TRUE) {
add_flat_template(
template = "additional",
pkg = pkg,
pkg = pkg,
dev_dir = dev_dir,
flat_name = flat_name,
overwrite = overwrite,
open = open)
open = open)
}

#' @rdname add_flat_template
#' @export
add_minimal <- function(
pkg = ".",
pkg = ".",
dev_dir = "dev",
flat_name = "minimal",
overwrite = FALSE,
open = TRUE) {
add_flat_template(
template = "minimal",
pkg = pkg,
pkg = pkg,
dev_dir = dev_dir,
flat_name = flat_name,
overwrite = overwrite,
open = open)
open = open)
}

#' @rdname add_flat_template
#' @export
add_full <- function(
pkg = ".",
pkg = ".",
dev_dir = "dev",
flat_name = "full",
overwrite = FALSE,
open = TRUE) {
add_flat_template(
template = "full",
pkg = pkg,
pkg = pkg,
dev_dir = dev_dir,
flat_name = flat_name,
overwrite = overwrite,
open = open)
open = open)
}

#' Add flat Rmd file that drives package development
Expand All @@ -59,7 +59,7 @@ add_full <- function(
#' @param open Logical. Whether to open file after creation
#' @param dev_dir Name of directory for development Rmarkdown files. Default to "dev".
#' @param flat_name Name of the file to write in dev.
#' Use the name of the main function of your template to get chunks pre-filled with this function name.
#' Use the name of the main function of your template to get chunks pre-filled with this function name.
#'
#' @importFrom tools file_path_sans_ext
#' @details
Expand All @@ -76,7 +76,7 @@ add_full <- function(
#'
#' Abbreviated names can also be used for the different templates:
#' "add" for additional, "min" for minimal, "teach" for teaching, "dev" for "dev_history".
#'
#'
#' `add_additional()`, `add_minimal()`, `add_full()` are wrapper around `add_flat_template("additional")`, ...
#' However, `add_dev_history()` is a deprecated function from a previous version.
#'
Expand All @@ -89,62 +89,62 @@ add_full <- function(
#' # Create a new project
#' dummypackage <- tempfile("dummypackage")
#' dir.create(dummypackage)
#'
#'
#' # Add
#' add_flat_template(template = "teaching", pkg = dummypackage)
#' # Delete dummy package
#' unlink(dummypackage, recursive = TRUE)
#'
#'
#' # For classical use in your package
#' \dontrun{
#' # first time ever using 'fusen'
#' add_flat_template("full")
#'
#' add_flat_template("full")
#'
#' # first time in your new package
#' add_flat_template("minimal")
#'
#'
#' # add new flat file for new functions
#' add_flat_template("add")
#'
#'
#' # add new flat template for teaching (a reduced full template)
#' add_flat_template("teaching")
#' #'}
add_flat_template <- function(
template = c("full", "minimal", "additional", "teaching", "dev_history"),
pkg = ".",
pkg = ".",
dev_dir = "dev",
flat_name = template,
overwrite = FALSE,
open = TRUE) {

project_name <- get_pkg_name(pkg = pkg)

if (project_name != asciify_name(project_name, to_pkg = TRUE)) {
stop("Please rename your project/directory with: `", asciify_name(project_name, to_pkg = TRUE),
"` as a package name should only contain letters, numbers and dots.")
}

template <- match.arg(template)
if (!template %in% c("full", "teaching", "dev_history")
& !flat_name %in% c("minimal", "additional")) {
fun_name <- gsub("-", "_", asciify_name(flat_name))
} else {
fun_name <- NA
}
flat_name <- paste0("flat_",
flat_name <- paste0("flat_",
asciify_name(gsub("[.]Rmd$", "", flat_name[1])), ".Rmd")

pkg <- normalizePath(pkg)
full_dev_dir <- file.path(pkg, dev_dir)
if (!dir.exists(full_dev_dir)) {dir.create(full_dev_dir)}
dev_file_path <- file.path(full_dev_dir, flat_name) #"dev_history.Rmd")

# Which template ----
if (template == "dev_history") {
dev_file_path <- character(0)
} else {
template_file <- system.file(paste0("flat-template-", template, ".Rmd"), package = "fusen")

if (file.exists(dev_file_path) & overwrite == FALSE) {
n <- length(list.files(full_dev_dir, pattern = "^flat_.*[.]Rmd"))
dev_file_path <- file.path(full_dev_dir, paste0(file_path_sans_ext(flat_name), "_", n + 1, ".Rmd"))
Expand All @@ -155,14 +155,14 @@ add_flat_template <- function(
)
}
dev_name <- basename(dev_file_path)

# Change lines asking for pkg name
lines_template <- readLines(template_file)

lines_template[grepl("<my_package_name>", lines_template)] <-
gsub("<my_package_name>", project_name,
lines_template[grepl("<my_package_name>", lines_template)])

# Change flat_template file name
# _inflate
lines_template[grepl("dev/flat_template.Rmd", lines_template)] <-
Expand All @@ -172,17 +172,17 @@ add_flat_template <- function(
lines_template[grepl("flat_template.Rmd", lines_template)] <-
gsub("flat_template.Rmd", dev_name,
lines_template[grepl("flat_template.Rmd", lines_template)])

# Change my_fun to fun_name
if (!is.na(fun_name)) {
lines_template[grepl("my_fun", lines_template)] <-
gsub("my_fun", fun_name,
lines_template[grepl("my_fun", lines_template)])
}

cat(enc2utf8(lines_template), file = dev_file_path, sep = "\n")
}

# Add the-dev-history when needed ----
if (template %in% c("full", "minimal", "dev_history")) {
dev_file <- file.path(full_dev_dir, "0-dev_history.Rmd")
Expand All @@ -200,9 +200,9 @@ add_flat_template <- function(
}
dev_file_path <- c(dev_file_path, dev_file)
}

}

# Add data for the full template exemple
if (template %in% c("full")) {
inst_dir <- file.path(pkg, "inst")
Expand All @@ -213,15 +213,15 @@ add_flat_template <- function(
# Example dataset
file.copy(system.file("nyc_squirrels_sample.csv", package = "fusen"), inst_dir)
}

# .Rbuildignore ----
# usethis::use_build_ignore(dev_dir) # Cannot be used outside project
if (length(list.files(pkg, pattern = "[.]Rproj")) == 0) {
lines <- c(paste0("^", dev_dir, "$"), "^\\.here$")
} else {
lines <- c(paste0("^", dev_dir, "$"))
}

buildfile <- normalizePath(file.path(pkg, ".Rbuildignore"), mustWork = FALSE)
if (!file.exists(buildfile)) {
existing_lines <- ""
Expand All @@ -233,11 +233,11 @@ add_flat_template <- function(
all <- c(existing_lines, new)
cat(enc2utf8(all), file = buildfile, sep = "\n")
}

# Add a gitignore file in dev_dir ----
# Files to ignore
lines <- c("*.html", "*.R")

gitfile <- normalizePath(file.path(full_dev_dir, ".gitignore"), mustWork = FALSE)
if (!file.exists(gitfile)) {
existing_lines <- ""
Expand All @@ -249,11 +249,10 @@ add_flat_template <- function(
all <- c(existing_lines, new)
cat(enc2utf8(all), file = gitfile, sep = "\n")
}

if (length(list.files(pkg, pattern = "[.]Rproj")) == 0) {
here::set_here(pkg)
}
if (isTRUE(open) & interactive()) {usethis::edit_file(dev_file_path)}

dev_file_path
}
Loading

0 comments on commit b063320

Please sign in to comment.