Skip to content

Commit d1c37cd

Browse files
committed
create_package checks you're not inside an existing project
* remove vcs from project consideration * correct evaluation of force * error if no project root found
1 parent 39f0712 commit d1c37cd

File tree

4 files changed

+59
-23
lines changed

4 files changed

+59
-23
lines changed

R/create.R

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1-
#' Setup a complete package
1+
#' Create a package from scratch
2+
#'
3+
#' This changes the active project to the new package so that subsequent
4+
#' `use_` calls will affect the project that you've just created.
25
#'
36
#' @param path A path. If it exists, it will be used. If it does not
47
#' exist, it will be created (providing that the parent path exists).
58
#' @param rstudio If `TRUE`, run [use_rstudio()].
69
#' @param open If `TRUE`, will automatically open
710
#' @inheritParams use_description
811
#' @export
9-
create_package <- function(path,
12+
create_package <- function(path = ".",
1013
fields = getOption("devtools.desc"),
1114
rstudio = rstudioapi::isAvailable(),
1215
open = interactive()) {
1316

14-
name <- basename(normalizePath(path, mustWork = FALSE))
17+
path <- normalizePath(path, mustWork = FALSE)
18+
19+
name <- basename(path)
1520
check_package_name(name)
21+
check_not_nested(dirname(path))
1622

1723
create_directory(dirname(path), name)
1824
cat_line(crayon::bold("Changing active project to", crayon::red(name)))
19-
proj_set(path)
25+
proj_set(path, force = TRUE)
2026

2127
use_directory("R")
2228
use_directory("man")
@@ -32,9 +38,32 @@ create_package <- function(path,
3238
project_path <- file.path(normalizePath(path), paste0(name, ".Rproj"))
3339
utils::browseURL(project_path)
3440
} else {
35-
todo("Please change working directory to ", value(path))
41+
setwd(path)
42+
done("Changing working directory to ", value(path))
3643
}
3744
}
3845

3946
invisible(TRUE)
4047
}
48+
49+
check_not_nested <- function(path) {
50+
proj_root <- proj_find(path)
51+
52+
if (is.null(proj_root))
53+
return()
54+
55+
message <- paste0(
56+
"Project is nested inside an existing project (", value(proj_root), ")"
57+
)
58+
if (!interactive()) {
59+
stop(message, call. = FALSE)
60+
}
61+
62+
cat_line(message)
63+
cat_line("Creating a project inside another project is usually ill-advised.")
64+
65+
if (yesno("Do you wish to create anyway?")) {
66+
stop(message, call. = FALSE)
67+
}
68+
69+
}

R/proj.R

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,50 @@
11
proj <- new.env(parent = emptyenv())
22

3-
proj_find <- function(path = ".") {
4-
criteria <- rprojroot::has_file(".here") |
3+
proj_crit <- function() {
4+
rprojroot::has_file(".here") |
55
rprojroot::is_rstudio_project |
66
rprojroot::is_r_package |
77
rprojroot::is_remake_project |
8-
rprojroot::is_projectile_project |
9-
rprojroot::is_vcs_root
10-
11-
rprojroot::find_root(criteria, path = path)
8+
rprojroot::is_projectile_project
129
}
1310

11+
proj_find <- function(path = ".") {
12+
tryCatch(
13+
rprojroot::find_root(proj_crit(), path = path),
14+
error = function(e) NULL
15+
)
16+
}
1417

1518
#' Get and set currently active project
1619
#'
1720
#' When attached, usethis uses rprojroot to find the project root of the
1821
#' current working directory. It establishes the project root by looking for
1922
#' for a `.here` file, an RStudio project, a package `DESCRIPTION`, a
20-
#' `remake.yml`, `.projectile` file, or `.git`/`.svn` directories. It then
21-
#' stores the project directory for use for the remainder of the session.
22-
#' If needed, you can manually override by running `proj_set()`.
23+
#' `remake.yml`, or a `.projectile` file. It then stores the project directory
24+
#' for use for the remainder of the session. If needed, you can manually
25+
#' override by running `proj_set()`.
2326
#'
2427
#' @param path Path to set.
2528
#' @param force If `TRUE`, uses this path without checking if any parent
2629
#' directories are existing projects.
2730
#' @keywords internal
2831
#' @export
2932
proj_get <- function() {
33+
if (is.null(proj$cur)) {
34+
stop("No active project found", call. = FALSE)
35+
}
3036
proj$cur
3137
}
3238

3339
#' @export
3440
#' @rdname proj_get
3541
proj_set <- function(path = ".", force = FALSE) {
36-
old <- proj_get()
42+
old <- proj$cur
3743

3844
if (force) {
39-
proj$cur <- proj_find(path)
40-
} else {
4145
proj$cur <- path
46+
} else {
47+
proj$cur <- proj_find(path)
4248
}
4349

4450
invisible(old)

man/create_package.Rd

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/proj_get.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)