Skip to content

Commit e5c0c7b

Browse files
committed
Rough cut of create_from_github
Fixes #109
1 parent 6262e8d commit e5c0c7b

File tree

3 files changed

+72
-12
lines changed

3 files changed

+72
-12
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# usethis 1.0.0.9000
22

3+
* New `create_from_github()` creates a project from an existing GitHub
4+
repository, forking if needed (#109).
5+
36
* `use_r()` will now open the matching `R/` file if you have a test file
47
open (#105).
58

R/create.R

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,7 @@ create_package <- function(path = ".",
3434
use_rstudio()
3535
}
3636
if (open) {
37-
if (rstudio) {
38-
done("Opening project in new session")
39-
project_path <- file.path(normalizePath(path), paste0(name, ".Rproj"))
40-
utils::browseURL(project_path)
41-
} else {
42-
setwd(path)
43-
done("Changing working directory to ", value(path))
44-
}
37+
open_project(path, name, rstudio = rstudio)
4538
}
4639

4740
invisible(TRUE)
@@ -50,6 +43,7 @@ create_package <- function(path = ".",
5043
#' @export
5144
#' @rdname create_package
5245
create_project <- function(path = ".",
46+
rstudio = rstudioapi::isAvailable(),
5347
open = interactive()) {
5448

5549
path <- normalizePath(path, mustWork = FALSE)
@@ -61,18 +55,79 @@ create_project <- function(path = ".",
6155
cat_line(crayon::bold("Changing active project to", crayon::red(name)))
6256
proj_set(path, force = TRUE)
6357

64-
use_rstudio()
6558
use_directory("R")
6659

60+
if (rstudio) {
61+
use_rstudio()
62+
}
6763
if (open) {
68-
done("Opening project in new session")
69-
project_path <- file.path(normalizePath(path), paste0(name, ".Rproj"))
70-
utils::browseURL(project_path)
64+
open_project(path, name, rstudio = rstudio)
7165
}
7266

7367
invisible(TRUE)
7468
}
7569

70+
#' Create a project from a github repository
71+
#'
72+
#' @param repo Full name of repository: `owner/repo`
73+
#' @param path Directory in which to clone repository: will create new
74+
#' directory inside `path`.
75+
#' @param fork Create a fork before cloning? Defaults to `TRUE` if you
76+
#' can't push to `repo`, `FALSE` if you can.
77+
#' @param open Open the new project once cloned?
78+
#' @export
79+
#' @rdname
80+
create_from_github <- function(repo, path = ".", fork = NA, open = TRUE) {
81+
repo <- strsplit(repo, "/")[[1]]
82+
if (length(repo) != 2) {
83+
stop("`repo` must be of form user/reponame", call. = FALSE)
84+
}
85+
owner <- repo[[1]]
86+
repo <- repo[[2]]
87+
repo_info <- gh::gh("GET /repos/:owner/:repo", owner = owner, repo = repo)
88+
89+
check_not_nested(dirname(path), repo)
90+
91+
# By default, fork only if you can't push to the repo
92+
if (is.na(fork)) {
93+
fork <- !repo_info$permissions$push
94+
}
95+
96+
if (fork) {
97+
done("Forking repo")
98+
fork_info <- gh::gh("POST /repos/:owner/:repo/forks", owner = owner, repo = repo)
99+
owner <- fork_info$owner$login
100+
git_url <- fork_info$git_url
101+
} else {
102+
git_url <- repo_info$git_url
103+
}
104+
105+
done("Cloning repo")
106+
repo_path <- create_directory(path, repo)
107+
git2r::clone(git_url, normalizePath(repo_path), progress = FALSE)
108+
proj_set(repo_path)
109+
110+
if (open) {
111+
open_project(repo_path, repo)
112+
}
113+
}
114+
115+
116+
open_project <- function(path, name, rstudio = NA) {
117+
project_path <- file.path(normalizePath(path), paste0(name, ".Rproj"))
118+
if (is.na(rstudio)) {
119+
rstudio <- file.exists(project_path)
120+
}
121+
122+
if (rstudio) {
123+
done("Opening project in new RStudio instance")
124+
utils::browseURL(project_path)
125+
} else {
126+
setwd(path)
127+
done("Changing working directory to ", value(path))
128+
}
129+
invisible(TRUE)
130+
}
76131

77132
check_not_nested <- function(path, name) {
78133
proj_root <- proj_find(path)

R/helpers.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ create_directory <- function(base_path, path) {
161161
stop("Failed to create path", call. = FALSE)
162162
}
163163
}
164+
165+
pkg_path
164166
}
165167

166168
edit_file <- function(base_path, path) {

0 commit comments

Comments
 (0)