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

#6 Create New Package #9

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
^ojoutils\.Rproj$
^\.Rproj\.user$
^LICENSE\.md$
^README\.Rmd$
^_pkgdown\.yml$
^docs$
^pkgdown$
^\.github$
1 change: 1 addition & 0 deletions .github/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.html
50 changes: 50 additions & 0 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]
release:
types: [published]
workflow_dispatch:

name: pkgdown.yaml

permissions: read-all

jobs:
pkgdown:
runs-on: ubuntu-latest
# Only restrict concurrency for non-PR jobs
concurrency:
group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }}
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
permissions:
contents: write
steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::pkgdown, local::.
needs: website

- name: Build site
run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE)
shell: Rscript {0}

- name: Deploy to GitHub pages 🚀
if: github.event_name != 'pull_request'
uses: JamesIves/github-pages-deploy-action@v4.5.0
with:
clean: false
branch: gh-pages
folder: docs
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
.Rproj.user
.Rhistory
.Rdata
.httr-oauth
.DS_Store
.quarto
local/
secrets/
docs
11 changes: 10 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@ 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,
heck,
readr,
renv,
rlang,
stringr,
usethis,
withr
Suggests:
testthat (>= 3.0.0),
tibble
Config/testthat/edition: 3
URL: https://github.com/openjusticeok/ojoutils, https://openjusticeok.github.io/ojoutils/
BugReports: https://github.com/openjusticeok/ojoutils/issues
Config/testthat/parallel: true
131 changes: 131 additions & 0 deletions R/package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#' Create Package
#'
ojo_create_package <- function(
name,
description,
dir = ".",
private = TRUE,
imports = NULL,
depends = NULL,
suggests = NULL,
authors = NULL,
rextendr = FALSE,
rstudio = TRUE
) {
# 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))

# Get package name
package_name <- heck::to_lower_camel_case(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

# Create Github repo from package template
gh_resp <- gh::gh(
"POST /repos/openjusticeok/ojo-package-template/generate",
owner = "openjusticeok",
name = name,
description = description,
private = private
)

Sys.sleep(1)

# Clone repo
gert::git_clone(
url = paste0("git@github.com:openjusticeok/", name, ".git"),
path = project_dir
)

usethis::create_package(
path = project_dir,
rstudio = rstudio,
roxygen = TRUE,
check_name = FALSE,
fields = list(
Package = package_name,
Description = description,
Version = "0.0.0.9000"
),
open = FALSE
)

usethis::with_project(
path = project_dir,
code = {
usethis::use_readme_rmd()
usethis::use_github_links()
usethis::use_testthat(parallel = TRUE)
usethis::use_pkgdown_github_pages()
}
)

# # Prefill readme with title line
# readr::write_lines(
# x = c(
# paste0("# {", package_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",
"devtools",
"usethis"
),
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 package",
repo = project_dir
)

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

# Return the directory of the project invisibly
invisible(project_dir)
}
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("git@github.com: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)
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# ojoutils
4 changes: 4 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
url: https://openjusticeok.github.io/ojoutils/
template:
bootstrap: 5

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.

4 changes: 2 additions & 2 deletions tests/testthat.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/tests.html
# * https://testthat.r-lib.org/reference/test_package.html#special-files
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(ojoutils)
Expand Down