Skip to content

Commit

Permalink
Allow setting alternate default paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Noam Ross committed Jun 9, 2023
1 parent 401d0c9 commit 2d9d2d2
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 22 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export(any_local_behind_lockfile)
export(capshot)
export(capshot_str)
export(capsule_sources)
export(compare_capsule_to_lockfile)
export(compare_local_to_lockfile)
export(create)
Expand Down
7 changes: 4 additions & 3 deletions R/capshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ BASE_PACKAGES <-
#' be integrated into automated pipelines that build projects or documents.
#'
#' @param dep_source_paths files to scan for project dependencies to write to the lock file.
#' Defaults to './packages.R`. See [capsule_sources()] for setting other defaults.
#' @param lockfile_path output path for the lock file.
#' @param minify a boolean value indicicating if lockfile JSON should have whitespace removed to shrink footprint.
#'
#' @return `lockfile_path`. Writes lockfile as a side effect.
#' @export
capshot <- function(
dep_source_paths = "./packages.R",
dep_source_paths = capsule_sources(),
lockfile_path = "./renv.lock",
minify = FALSE
) {
Expand All @@ -69,7 +70,7 @@ capshot <- function(

#' @describeIn capshot a variation that returns lockfile json as a character vector for further use.
#' @export
capshot_str <- function(dep_source_paths = "./packages.R", minify = FALSE) {
capshot_str <- function(dep_source_paths = capsule_sources(), minify = FALSE) {
generate_lockfile_json(
get_project_deps(detect_dependencies(dep_source_paths)),
minify = minify
Expand Down Expand Up @@ -104,7 +105,7 @@ get_project_dcfs <- function(declared_deps, pkg_names, pkg_dcfs) {
current_deps <- new_current_deps
}
missing <- setdiff(names(project_deps), unlist(pkg_names))
if (length(missing)) stop("Cannot complete capshot(). Project dependencies not installed locally: ",
if (length(missing)) stop("Cannot complete capshot(). Project dependencies not installed locally: ",
paste(missing, collapse = ", "), "\n\n",
"Use dev_mirror_lockfile() to bring the local library up to at least the lockfile versions for all dependencies."
)
Expand Down
20 changes: 20 additions & 0 deletions R/capsule_sources.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
##' Get the default files from which to detect dependencies
##'
##' Dependencies to be encapsulated are detected from R files in your repository.
##' A single `./packages.R` file is the default, but you can provide a
##' vector of paths directly, using the R global option `capsule.sources`, or
##' the environment variable `CAPSULE_SOURCES` (the latter takes only a single
##' file name). Using a single file with all library() calls makes an explicit
##' assertion of your dependencies. This way spurious usages of pkg:: for
##' packages not stated as dependencies will cause errors that can be caught.
##'
##' @title capsule_sources
##' @param paths The default files in which to look for dependencies
##' @author Noam Ross
##' @export
capsule_sources <- function(paths = "./packages.R") {
getOption(
"capsule.sources",
default = Sys.getenv("CAPSULE_SOURCES", unset = paths)
)
}
5 changes: 3 additions & 2 deletions R/create.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
##' file that contains all library() calls - hence this makes an explicit
##' assertion of your dependencies. This way spurious usages of pkg:: for
##' packages not stated as dependencies will cause errors that can be caught.
##'
##'
##' @title create
##' @param dep_source_paths files to find package dependencies in.
##' Defaults to './packages.R`. See [capsule_sources()] for setting other defaults.
##' @return nothing. Creates a capsule as a side effect.
##' @author Miles McBain
##' @export
create <- function(
dep_source_paths = "./packages.R",
dep_source_paths = capsule_sources(),
lockfile_path = "./renv.lock"
) {

Expand Down
7 changes: 4 additions & 3 deletions R/recreate.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
##' Similarly to `create()`, you are expected to supply a vector of files in
##' your project to extract dependencies from. Things work best when this is a
##' single file containing only dependency related code.
##'
##'
##' @title recreate
##' @param dep_source_paths a character vector of project source files to
##' extract dependencies from.
##' extract dependencies from. Defaults to './packages.R`.
##' See [capsule_sources()] for setting other defaults.
##' @return nothing. The capsule is regenerated as a side effect.
##' @author Miles McBain
##' @seealso [create()]
##' @export
recreate <- function(dep_source_paths = "./packages.R") {
recreate <- function(dep_source_paths = capsule_sources()) {

delete()
create(dep_source_paths = dep_source_paths)
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ The following package(s) will be updated in the lockfile:

You supply a vector of file paths to extract dependencies from. The default is
`"./packages.R"`. These dependencies are copied from your regular (dev) library
to your local capsule.
to your local capsule. These can also be set at the user or project level with
`options(capsule.sources = <PATHS>)`, or a `CAPSULE_SOURCES` environment variable.

Notice how this is easier when you keep your library calls all in one place? :wink:

Expand Down
11 changes: 6 additions & 5 deletions man/capshot.Rd

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

26 changes: 26 additions & 0 deletions man/capsule_sources.Rd

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

4 changes: 2 additions & 2 deletions man/compare_lockfile.Rd

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

5 changes: 3 additions & 2 deletions man/create.Rd

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

4 changes: 2 additions & 2 deletions man/get_behind.Rd

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

5 changes: 3 additions & 2 deletions man/recreate.Rd

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

0 comments on commit 2d9d2d2

Please sign in to comment.