Skip to content

Commit

Permalink
Merge pull request #131 from jhudsl/cansavvy/gha
Browse files Browse the repository at this point in the history
Adding github actions and some reorganizations
  • Loading branch information
cansavvy authored Jun 26, 2024
2 parents eb7aa75 + 9b09767 commit 4413ed1
Show file tree
Hide file tree
Showing 37 changed files with 253 additions and 645 deletions.
31 changes: 31 additions & 0 deletions .github/check_testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Check up on the testthat results
# C. Savonen
# Nov 2023

library(magrittr)


# Find .git root directory
root_dir <- rprojroot::find_root(rprojroot::has_dir(".git"))

out_file <- list.files(pattern = "testthat.Rout$|Rout.fail$", file.path(root_dir, "check"),
recursive = TRUE, full.names = TRUE)

check_content <- readLines(out_file)
test_result <- grep("\\[ FAIL", check_content, value = TRUE)[1]
test_result <- unlist(strsplit(test_result, "\\||\\[|\\]"))

# Format the data into a dataframe
test_result_df <- data.frame(result = trimws(test_result)) %>%
dplyr::filter(result != "") %>%
tidyr::separate(result, sep = " ", into = c("test_name", "num")) %>%
dplyr::mutate(num = as.numeric(num))

fail_num <- test_result_df %>%
dplyr::filter(test_name %in% c("FAIL", "WARN")) %>%
dplyr::summarize(total = sum(num))

fail_num <- as.character(fail_num$total)

# Spit the number out
writeLines(fail_num, con = stdout())
26 changes: 17 additions & 9 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ on:
push:
branches:
- main
- master
pull_request:
branches:
- main
- master

name: R-CMD-check

Expand All @@ -33,13 +31,13 @@ jobs:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}

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

- name: Query dependencies
run: |
Expand All @@ -66,18 +64,28 @@ jobs:
- name: Install dependencies
run: |
remotes::install_deps(dependencies = TRUE)
remotes::install_cran("rcmdcheck")
remotes::install_deps(dependencies = TRUE, force = TRUE)
remotes::install_cran("devtools")
sessionInfo()
shell: Rscript {0}

- name: Check
env:
_R_CHECK_CRAN_INCOMING_REMOTE_: false
run: |
options(crayon.enabled = TRUE)
rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), check_dir = "check")
rcmdcheck::rcmdcheck(args = c("--no-manual"), check_dir = "check")
shell: Rscript {0}

- name: Check testthat
if: runner.os != 'Windows'
id: check_check
run: |
error_num=$(Rscript --vanilla '.github/workflows/check_testthat.R')
echo "error_num=$error_num" >> $GITHUB_OUTPUT
- name: Stop if there are errors!
if: ${{ steps.check_check.outputs.error_num != '0' }}
run: exit 1

- name: Upload check results
if: failure()
uses: actions/upload-artifact@main
Expand Down
62 changes: 62 additions & 0 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Candace Savonen Jan 2024

name: Build Docker

on:
workflow_dispatch:
inputs:
dockerhubpush:
description: 'Push to Dockerhub?'
required: true
default: 'false'

jobs:
build-docker:
name: Build Docker image
runs-on: ubuntu-latest

steps:
- name: checkout repo
uses: actions/checkout@v3
with:
token: ${{secrets.GH_PAT}}
fetch-depth: 0

# Set up Docker build
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

# Setup layer cache
- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Get token
run: echo ${{ secrets.GH_PAT }} > ${{ github.workspace }}/inst/extdata/docker/git_token.txt

# Build docker image
- name: Build Docker image
uses: docker/build-push-action@v5
with:
push: false
load: true
context: inst/extdata/docker
tags: cansav09/metricminer

# Login to Dockerhub
- name: Login to DockerHub
if: ${{ github.event.inputs.dockerhubpush != 'false' }}
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

# Push the Docker image if set to true from a manual trigger
- name: Push Docker image if manual trigger set to true
if: ${{ github.event.inputs.dockerhubpush != 'false' }}
run: |
docker push cansav09/metricminer
48 changes: 48 additions & 0 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 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

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@v3

- 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/[email protected]
with:
clean: false
branch: gh-pages
folder: docs
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export(bookdown_path)
export(bookdown_rmd_files)
export(bookdown_to_book_txt)
export(bookdown_to_embed_leanpub)
export(bookdown_to_leanpub)
export(check_all_questions)
export(check_question)
export(check_quiz)
Expand All @@ -20,6 +19,7 @@ export(check_quizzes)
export(convert_coursera_quizzes)
export(convert_quiz)
export(convert_utube_link)
export(download_ottr_template)
export(example_repo_cleanup)
export(example_repo_setup)
export(extract_meta)
Expand Down
128 changes: 0 additions & 128 deletions R/bookdown_to_leanpub.R
Original file line number Diff line number Diff line change
@@ -1,134 +1,6 @@
#' Convert Bookdown to Leanpub
#'
#' @param path path to the bookdown book, must have a `_bookdown.yml` file
#' @param output_dir output directory to put files. It should likely be
#' relative to path
#' @param render if `TRUE`, then [bookdown::render_book()] will be run on each Rmd.
#' @param verbose print diagnostic messages
#' @param remove_resources_start remove the word `resources/` at the front
#' of any image path.
#' @param run_quiz_checks TRUE/FALSE run quiz checks
#' @param make_book_txt Should [ottrpal::bookdown_to_book_txt()] be run
#' to create a `Book.txt` in the output directory?
#' @param quiz_dir directory that contains the quiz .md files that should be
#' checked and incorporated into the Book.txt file. If you don't have quizzes,
#' set this to NULL
#' @param clean_up TRUE/FALSE the old output directory should be deleted and
#' everything created fresh.
#' @param footer_text Optionally can add a bit of text that will be added to the
#' end of each file before the references section.
#'
#' @return A list of output files and diagnostics
#' @export
#'
bookdown_to_leanpub <- function(path = ".",
render = TRUE,
output_dir = "manuscript",
make_book_txt = FALSE,
quiz_dir = "quizzes",
run_quiz_checks = FALSE,
remove_resources_start = FALSE,
verbose = TRUE,
footer_text = NULL,
clean_up = FALSE) {
# Run the set up
set_up_leanpub(
path = path,
embed = FALSE,
clean_up = clean_up,
render = render,
output_dir = output_dir,
make_book_txt = make_book_txt,
quiz_dir = quiz_dir,
run_quiz_checks = run_quiz_checks,
remove_resources_start = remove_resources_start,
verbose = verbose,
footer_text = footer_text
)

# Establish path
path <- bookdown_path(path)

rmd_regex <- "[.][R|r]md$"

# Extract the names of the Rmd files (the chapters)
rmd_files <- bookdown_rmd_files(path = path)

bib_files <- list.files(pattern = "[.]bib$")

if (length(bib_files) > 0) {
pandoc_args <- paste0("--bibliography=", path.expand(normalizePath(bib_files)))
} else {
pandoc_args <- NULL
}

# run_env = new.env()
md_files <- sub(rmd_regex, ".md", rmd_files, ignore.case = TRUE)
md_files <- file.path(output_dir, basename(md_files))

for (file in md_files) {
if (verbose > 1) {
message("Replacing HTML for ", file)
}
infile <- normalizePath(file)

infile <- replace_single_html(infile,
verbose = verbose > 1,
remove_resources_start = remove_resources_start,
footer_text = footer_text
)

if (length(bib_files) > 0) {
if (verbose > 1) {
message("Making references for ", file)
}
writeLines(simple_references(infile, bib_files, add_reference_header = TRUE),
con = infile, sep = "\n"
)
}
}
####################### Book.txt creation ####################################
out <- NULL
if (make_book_txt) {
if (verbose > 1) {
message("Running bookdown_to_book_txt")
}
bookdown_to_book_txt(
md_files = rmd_files,
output_dir = output_dir,
quiz_dir = quiz_dir,
verbose = verbose
)
out <- book_txt_file <- file.path(output_dir, "Book.txt")
} else {
# If false, look for Book.txt file to copy to output folder.
book_txt_file <- file.path(path, "Book.txt")

if (file.exists(book_txt_file)) {
# Copy over an existing book.txt file if it exists
file.copy(from = book_txt_file, to = file.path(output_dir, "Book.txt"))

out <- book_txt_file <- file.path(output_dir, "Book.txt")
} else {
# If none exists and make_book_txt is false: stop.
stop(paste0(
"Book.txt file does not exist in the main directory: ", path, "and make_book_txt is set to FALSE.",
"There is no Book.txt file. Leanpub needs one. Either make one and place it in the directory path or ",
"use make_book_txt = TRUE and one will be generated for you."
))
}
}
############################# Wrapping up ####################################
message(paste(
"Leanpub ready files are saved to",
output_dir,
"Go to https://leanpub.com/ to publish them using the GitHub writing mode."
))
}

#' Convert Bookdown to Embed version of Leanpub
#'
#' @param path path to the bookdown book, must have a `_bookdown.yml` file
#' @param chapt_img_key File path to a TSV whose contents are the chapter urls (`url`),
#' the chapter titles (`chapt_title`), the file path to the image to be used for the chapter (`img_path`).
#' Column names `url`, `chapt_title`, and `img_path` must be used.
Expand Down
File renamed without changes.
Loading

0 comments on commit 4413ed1

Please sign in to comment.