Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#5 Create New Project #7

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
.Rproj.user
.Rhistory
.Rdata
.httr-oauth
.DS_Store
.quarto
local/
secrets/
7 changes: 6 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ Description: We find ourselves repeating the same simple tasks or running a seri
License: GPL (>= 3)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
Imports:
cli,
dbplyr,
dplyr,
fs,
gert,
gh,
readr,
renv,
rlang,
stringr,
usethis,
withr
Suggests:
testthat (>= 3.0.0),
Expand Down
99 changes: 99 additions & 0 deletions R/project.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#' @title Create Project
#' @description Creates a new R project with a standard directory structure
#'
ojo_create_project <- function(name = NULL, description = NULL, dir = ".", private = TRUE, packages = NULL) {
# Get the initial working directory
init_wd <- fs::path_wd()

# TODO: Default to using OJO_PATH env var for project location

# Get full path of project directory
project_dir <- fs::path_abs(paste0(dir, "/", name))

# If name is NULL, first try to use interactive CLI, otherwise fail
if (is.null(name)) {
if (rlang::is_interactive()) {
# TODO: Get variable interactively
} else {
rlang::abort("No project name provided, and no support for interactive definition available in this environment.")
}
}

# TODO: Check name matches conventions and isn't taken
# TODO: If interactive, offer to change

# TODO: Check description format
# TODO: If description NULL offer to create

# TODO: Wrap API call and issue custom error message
# Create Github repo from project template
gh_resp <- gh::gh(
"POST /repos/openjusticeok/ojo-project-template/generate",
owner = "openjusticeok",
name = name,
description = description,
private = private
)

# Have to give github the time to transfer the repo
# TODO: Base this on an API call?
Sys.sleep(1)

usethis::create_from_github(
repo_spec = glue::glue("[email protected]:openjusticeok/{name}.git"),
destdir = dir,
fork = FALSE,
rstudio = TRUE,
open = FALSE,
protocol = "ssh"
)

# Prefill readme with title line
readr::write_lines(
x = c(
paste0("# ", name),
description
),
sep = "\n\n",
file = fs::path(project_dir, "README.md"),
append = FALSE
)

# TODO: Ask in cli if you want to edit the readme further

# Init renv
renv::init(
project = project_dir,
bare = TRUE,
load = FALSE,
restart = FALSE
)

# Add pak
renv::install(
packages = c(
"pak"
),
prompt = FALSE,
lock = TRUE,
project = project_dir
)

# Stage all changed files
gert::git_add(
files = c("*", ".*"),
repo = project_dir
)

# Commit changes
gert::git_commit(
message = "Scaffold project",
repo = project_dir
)

# Push changes to Github
gert::git_push(repo = project_dir)

# Return the directory of the project invisibly
invisible(project_dir)
}
11 changes: 11 additions & 0 deletions man/ojo_create_project.Rd

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