Skip to content

Commit

Permalink
Fix #48
Browse files Browse the repository at this point in the history
  • Loading branch information
gfinak committed Sep 27, 2018
1 parent 6567f12 commit 60ef0cc
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 3 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions 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)
}
14 changes: 12 additions & 2 deletions R/use.R
Expand Up @@ -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
Expand All @@ -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.")
}
Expand All @@ -40,13 +41,22 @@ 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(
from = raw_file,
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.")
Expand Down
22 changes: 22 additions & 0 deletions man/use_ignore.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/use_raw_dataset.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions 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'")
})
1 change: 1 addition & 0 deletions tests/testthat/test-use_raw_data.R
Expand Up @@ -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(
Expand Down

0 comments on commit 60ef0cc

Please sign in to comment.