Skip to content

Commit 1b18e14

Browse files
committed
Implement create_project
And ensure `use_rstudio()` doesn't created unneeded files in a project. Fixes #82
1 parent d0efe98 commit 1b18e14

File tree

6 files changed

+47
-8
lines changed

6 files changed

+47
-8
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(create_package)
4+
export(create_project)
45
export(edit_environ_user)
56
export(edit_git_config_user)
67
export(edit_git_ignore_user)

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This is a new package that extracts out many functions that previously lived in devtools, as well as providing more building blocks so you can create your own helpers.
44

5-
Functions are now designed to work with a directory that's not necessarily a package. This doesn't always make sense but in the long term makes usesthis more flexible for other tasks like (e.g.) data analysis. All `use_` functions have the concept of a active project: rather than supply `base_path`, you can now change the active project by calling `proj_set()`.
5+
Functions are now designed to work with a directory that's not necessarily a package. This doesn't always make sense but in the long term makes usesthis more flexible for other tasks like (e.g.) data analysis. All `use_` functions have the concept of a active project: rather than supply `base_path`, you can now change the active project by calling `proj_set()`. There is also a new `create_project()` which creates a basic RStudio project.
66

77
The output from all usethis commands has been reviewed to be informative but not overwhelming. usethis takes advantage of colour (using the crayon package and features in RStudio 1.1) to help chunk the output and clearly differentiate what you need to do vs. what has been done for you.
88

R/create.R

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
#' Create a package from scratch
1+
#' Create a new package or project
22
#'
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.
3+
#' Both functions change the active project so that subsequent `use_` calls
4+
#' will affect the project that you've just created. See `proj_set()` to
5+
#' manually reset it.
56
#'
67
#' @param path A path. If it exists, it will be used. If it does not
78
#' exist, it will be created (providing that the parent path exists).
@@ -46,6 +47,33 @@ create_package <- function(path = ".",
4647
invisible(TRUE)
4748
}
4849

50+
#' @export
51+
#' @rdname create_package
52+
create_project <- function(path = ".",
53+
open = interactive()) {
54+
55+
path <- normalizePath(path, mustWork = FALSE)
56+
57+
name <- basename(path)
58+
check_not_nested(dirname(path))
59+
60+
create_directory(dirname(path), name)
61+
cat_line(crayon::bold("Changing active project to", crayon::red(name)))
62+
proj_set(path, force = TRUE)
63+
64+
use_rstudio()
65+
use_directory("R")
66+
67+
if (open) {
68+
done("Opening project in new session")
69+
project_path <- file.path(normalizePath(path), paste0(name, ".Rproj"))
70+
utils::browseURL(project_path)
71+
}
72+
73+
invisible(TRUE)
74+
}
75+
76+
4977
check_not_nested <- function(path) {
5078
proj_root <- proj_find(path)
5179

R/helpers.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ find_template <- function(template_name) {
4040
path
4141
}
4242

43+
is_package <- function(base_path = proj_get()) {
44+
file.exists(file.path(base_path, "DESCRIPTION"))
45+
}
46+
4347
package_data <- function(base_path = proj_get()) {
4448
desc <- desc::description$new(base_path)
4549

R/rstudio.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ use_rstudio <- function() {
1212
)
1313

1414
use_git_ignore(".Rproj.user")
15-
use_build_ignore(c("^.*\\.Rproj$", "^\\.Rproj\\.user$"), escape = FALSE)
15+
if (is_package()) {
16+
use_build_ignore(c("^.*\\.Rproj$", "^\\.Rproj\\.user$"), escape = FALSE)
17+
}
1618

1719
invisible(TRUE)
1820
}

man/create_package.Rd

Lines changed: 7 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)