diff --git a/NAMESPACE b/NAMESPACE index d98e277..a2e6532 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -14,6 +14,7 @@ export(project_data_path) export(project_extdata_path) export(project_path) export(use_data_object) +export(use_ignore) export(use_processing_script) export(use_raw_dataset) export(yml_add_files) diff --git a/NEWS.md b/NEWS.md index 31bb34e..a5f62dc 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,6 +8,7 @@ * Fix the documentation for datapackager_object_read() and "Migrating old packages". * code argument no longer required for construct_yml_config * Add option to overwrite (or not) via use_processing_script. Provide warning. +* Add use_ignore() to ignore files and data sets in .Rbuildignore and .gitignore and added ignore argument to use_raw_dataset(). # DataPackageR 0.15.3 * conditional tests when pandoc is missing (ropensci/DataPackager/issues/46) diff --git a/R/ignore.R b/R/ignore.R new file mode 100644 index 0000000..2887cec --- /dev/null +++ b/R/ignore.R @@ -0,0 +1,20 @@ +#' Ignore specific files by git and R build. +#' +#' @param file \code{character} File to ignore. +#' @param path \code{character} Path to the file. +#' +#' @return invisibly returns 0. +#' @export +#' +#' @examples +#' use_ignore("foo", ".") +use_ignore <- function(file = NULL, path = NULL){ + if (is.null(file)) { + message("No file name provided to ignore.") + invisible(0) + } + proj_path <- usethis::proj_get() + usethis::use_build_ignore(files = file.path(path,file), escape = TRUE) + usethis::use_git_ignore(ignores = file, directory = path) + invisible(0) +} \ No newline at end of file diff --git a/R/use.R b/R/use.R index 8ad0154..1f515d1 100644 --- a/R/use.R +++ b/R/use.R @@ -4,7 +4,8 @@ #' the inst/extdata directory. #' #' @param path \code{character} path to file or directory. -#' +#' @param ignore \code{logical} whether to ignore the path or file in git and R build. +#' #' @return invisibly returns TRUE for success. Stops on failure. #' @importFrom usethis proj_get proj_set create_package use_data_raw #' @importFrom utils file_test @@ -25,7 +26,7 @@ #' r_object_names = "data") #' use_raw_dataset(raw_data) #' } -use_raw_dataset <- function(path = NULL) { +use_raw_dataset <- function(path = NULL, ignore = FALSE) { if (is.null(path)) { stop("You must provide a full path to a file or directory.") } @@ -40,6 +41,11 @@ use_raw_dataset <- function(path = NULL) { to = file.path(proj_path, "inst", "extdata"), overwrite = TRUE ) + if (ignore) { + # inst/extdata is a path relative to the project root + # as needed by git_ignore + use_ignore(basename(raw_file), path = file.path("inst", "extdata")) + } return(invisible(TRUE)) } else if (utils::file_test("-d", raw_file)) { file.copy( @@ -47,6 +53,10 @@ use_raw_dataset <- function(path = NULL) { to = file.path(proj_path, "inst", "extdata"), recursive = TRUE, overwrite = TRUE ) + if (ignore) { + #should work and the directory should be ignored + use_ignore(basename(raw_file), path = file.path("inst", "extdata")) + } return(invisible(TRUE)) } else { stop("path must be a path to an existing file or directory.") diff --git a/man/use_ignore.Rd b/man/use_ignore.Rd new file mode 100644 index 0000000..d49f0d4 --- /dev/null +++ b/man/use_ignore.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ignore.R +\name{use_ignore} +\alias{use_ignore} +\title{Ignore specific files by git and R build.} +\usage{ +use_ignore(file = NULL, path = NULL) +} +\arguments{ +\item{file}{\code{character} File to ignore.} + +\item{path}{\code{character} Path to the file.} +} +\value{ +invisibly returns 0. +} +\description{ +Ignore specific files by git and R build. +} +\examples{ +use_ignore("foo", ".") +} diff --git a/man/use_raw_dataset.Rd b/man/use_raw_dataset.Rd index 91ae411..76b291b 100644 --- a/man/use_raw_dataset.Rd +++ b/man/use_raw_dataset.Rd @@ -4,10 +4,12 @@ \alias{use_raw_dataset} \title{Add a raw data set to inst/extdata} \usage{ -use_raw_dataset(path = NULL) +use_raw_dataset(path = NULL, ignore = FALSE) } \arguments{ \item{path}{\code{character} path to file or directory.} + +\item{ignore}{\code{logical} whether to ignore the path or file in git and R build.} } \value{ invisibly returns TRUE for success. Stops on failure. diff --git a/tests/testthat/test-ignore.R b/tests/testthat/test-ignore.R new file mode 100644 index 0000000..791f04b --- /dev/null +++ b/tests/testthat/test-ignore.R @@ -0,0 +1,17 @@ +context("ignore") + +test_that("use_ignore works", { + file <- system.file("extdata", "tests", "subsetCars.Rmd", + package = "DataPackageR" + ) + expect_null( + datapackage_skeleton( + name = "subsetCars", + path = tempdir(), + code_files = c(file), + force = TRUE, + r_object_names = c("cars_over_20") + ) + ) + expect_output(use_ignore(file = "mydata.csv",path = "inst/extdata"),"Adding 'mydata.csv' to 'inst/extdata/\\.gitignore'|Adding '\\^inst/extdata/mydata\\\\.csv\\$' to '\\.Rbuildignore'") +}) diff --git a/tests/testthat/test-use_raw_data.R b/tests/testthat/test-use_raw_data.R index 5fbed2a..25ef664 100644 --- a/tests/testthat/test-use_raw_data.R +++ b/tests/testthat/test-use_raw_data.R @@ -36,6 +36,7 @@ test_that("use_raw_data works as expected", { recursive = TRUE ) expect_true(use_raw_dataset(myfile)) + expect_true(use_raw_dataset(myfile, ignore = TRUE)) expect_true(file_test( "-f", file.path(