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

create add_badge functions #9

Merged
merged 2 commits into from
Apr 22, 2024
Merged
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
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/R-CMD-check.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]

name: R-CMD-check

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: macos-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v4

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

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck
needs: check

- uses: r-lib/actions/check-r-package@v2
with:
upload-snapshots: true
build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")'
50 changes: 50 additions & 0 deletions .github/workflows/test-coverage.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]

name: test-coverage

jobs:
test-coverage:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v4

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

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::covr
needs: coverage

- name: Test coverage
run: |
covr::codecov(
quiet = FALSE,
clean = FALSE,
install_path = file.path(normalizePath(Sys.getenv("RUNNER_TEMP"), winslash = "/"), "package")
)
shell: Rscript {0}

- name: Show testthat output
if: always()
run: |
## --------------------------------------------------------------------
find '${{ runner.temp }}/package' -name 'testthat.Rout*' -exec cat '{}' \; || true
shell: bash

- name: Upload test results
if: failure()
uses: actions/upload-artifact@v4
with:
name: coverage-test-failures
path: ${{ runner.temp }}/package
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Description: Tools and utilities for package development currently not
preferences during the development process. They assist in making routine
and repetitive tasks easily implementable.
License: GPL (>= 3)
Imports:
stringr
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.1

4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(add_badge_codefactor)
export(add_badge_status)
importFrom(stringr,str_detect)
importFrom(stringr,str_replace_all)
168 changes: 168 additions & 0 deletions R/add_badge.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#'
#' Add repostatus badge
#'
#' @param status A character value for status to be assigned to project. This
#' can be either *"concept"*, *"wip"*, *"suspended"*, *"abandoned"*,
#' *"active"*, *"inactive"*, *"unsupported"*, or *"moved"*.
#' @param path Path to file to add repostatus badge to. Set to NULL by default
#' which would indicate that a README file in the root directory of the
#' project is the target file.
#' @param .url If `status` is "moved", the URL to which the repository has moved
#' to. Otherwise NULL.
#'
#' @returns An entry to the badge section of the README of the repository.
#' Otherwise, a print of the markdown text for the status badge.
#'
#' @examples
#' add_badge_status(
#' "wip",
#' path = system.file("examples", "README.Rmd", package = "pakete")
#' )
#'
#' @export
#'

add_badge_status <- function(status = c("concept", "wip", "suspended",
"abandoned", "active", "inactive",
"unsupported", "moved"),
path = NULL,
.url = NULL) {
## Determine status ----
status <- match.arg(status)

## Determine which file to append badge to ----
if (is.null(path)) {
if (file.exists("README.Rmd"))
path <- "README.Rmd"
else
path <- "README.md"
}

## Determine what text to use based on status ----
status_text_url <- paste0(
"https://www.repostatus.org/badges/latest/", status, "_md.txt"
)

## Read the badge text ----
badge_text <- readLines(con = status_text_url)

## Replace http://example.com with .url ----
if (!is.null(.url)) {
badge_text <- stringr::str_replace_all(
string = badge_text, pattern = "http://example.com", replacement = .url
)
}

## Read file in path ----
readme_lines <- readLines(path, encoding = "UTF-8")

if (all(badge_text %in% readme_lines))
return(FALSE)

## Get start and end line of badges ----
badges_start_line <- stringr::str_detect(
readme_lines, pattern = "badges: start"
) |>
(\(x) seq_len(length(x))[x])()

badges_end_line <- stringr::str_detect(
readme_lines, pattern = "badges: end"
) |>
(\(x) seq_len(length(x))[x])()

## Create replacement text ----
readme_lines <- c(
readme_lines[seq_len(badges_end_line - 1)],
badge_text,
readme_lines[seq(from = badges_end_line, to = length(readme_lines))]
)

## Append replacement text ----
writeLines(
text = readme_lines,
con = "README.Rmd"
)

TRUE
}


#'
#' Add CodeFactor badget
#'
#' Assumes that repository is already activated in CodeFactor.
#'
#' @param repo Short remote git repository name. If NULL, is determined based
#' on current git settings.
#' @param path Path to file to add repostatus badge to. Set to NULL by default
#' which would indicate that a README file in the root directory of the
#' project is the target file.
#'
#' @returns An entry to the badge section of the README of the repository.
#' Otherwise, a print of the markdown text for the status badge.
#'
#' @examples
#' if (FALSE) {add_badge_codefactor(repo = "katilingban/pakete")}
#'
#' @export
#'

add_badge_codefactor <- function(repo = NULL,
path = NULL) {
## Determine repo ----
if (is.null(repo))
stop("Remote git repository required. Try again")

## Set CodeFactor defaults ----
badge <- paste0("https://www.codefactor.io/repository/github/", repo, "/badge")
link <- paste0("https://www.codefactor.io/repository/github/", repo)

## Determine which file to append badge to ----
if (is.null(path)) {
if (file.exists("README.Rmd"))
path <- "README.Rmd"
else
path <- "README.md"
}

## Create badge_text ----
badge_text <- paste0(
"[![CodeFactor](",
badge,
")](",
link,
")"
)

## Read file in path ----
readme_lines <- readLines(path, encoding = "UTF-8")

if (all(badge_text %in% readme_lines))
return(FALSE)

## Get start and end line of badges ----
badges_start_line <- stringr::str_detect(
readme_lines, pattern = "badges: start"
) |>
(\(x) seq_len(length(x))[x])()

badges_end_line <- stringr::str_detect(
readme_lines, pattern = "badges: end"
) |>
(\(x) seq_len(length(x))[x])()

## Create replacement text ----
readme_lines <- c(
readme_lines[seq_len(badges_end_line - 1)],
badge_text,
readme_lines[seq(from = badges_end_line, to = length(readme_lines))]
)

## Append replacement text ----
writeLines(
text = readme_lines,
con = "README.Rmd"
)

TRUE
}
1 change: 1 addition & 0 deletions R/pakete-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
#' @docType package
#' @keywords internal
#' @name pakete
#' @importFrom stringr str_detect str_replace_all
#'
"_PACKAGE"
31 changes: 31 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#
# Get current remote repository
#

get_remote_repository <- function(full = FALSE) {
git_repo <- system("git remote -v", intern = TRUE) |>
(\(x) x[stringr::str_detect(string = x, pattern = "push")])() |>
stringr::str_remove_all(pattern = "origin\t| \\(push\\)")

if (!full) {
if (stringr::str_detect(string = git_repo, pattern = "@")) {
git_repo <- stringr::str_extract_all(
string = git_repo, pattern = "[^:]*$", simplify = TRUE
) |>
stringr::str_remove_all(pattern = "[^.]*$|\\.") |>
(\(x) x[1])()
} else {
git_repo <- stringr::str_extract_all(
string = git_repo, pattern = "[^com]*$", simplify = TRUE
) |>
stringr::str_remove_all(pattern = "[^.]*$|\\.") |>
stringr::str_remove(pattern = "/") |>
(\(x) x[1])()
}
}

## Return git_repo
git_repo
}


10 changes: 9 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ knitr::opts_chunk$set(
# pakete: Utilities for Package Development

<!-- badges: start -->
[![Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public.](https://www.repostatus.org/badges/latest/wip.svg)](https://www.repostatus.org/#wip)
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[![R-CMD-check](https://github.com/katilingban/pakete/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/katilingban/pakete/actions/workflows/R-CMD-check.yaml)
[![CodeFactor](https://www.codefactor.io/repository/github/katilingban/pakete/badge)](https://www.codefactor.io/repository/github/katilingban/pakete)
<!-- badges: end -->

Tools and utilities for package development currently not available from usual development tools. These are mostly linked to personal preferences during the development process. They assist in making routine and repetitive tasks easily implementable.
Expand All @@ -30,7 +34,11 @@ Currently, `pakete` includes functions for:

3. creating line snippets for `DESCRIPTION` file;

## Installation
4. creating `CONTRIBUTING.md` file;

5. adding `repostatus` badge;

## Installation·

You can install `pakete` via the [Katilingban R-universe](https://katilingban.r-universe.dev) as follows:

Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
# pakete: Utilities for Package Development

<!-- badges: start -->

[![Project Status: WIP – Initial development is in progress, but there
has not yet been a stable, usable release suitable for the
public.](https://www.repostatus.org/badges/latest/wip.svg)](https://www.repostatus.org/#wip)
[![Lifecycle:
experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[![R-CMD-check](https://github.com/katilingban/pakete/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/katilingban/pakete/actions/workflows/R-CMD-check.yaml)
[![CodeFactor](https://www.codefactor.io/repository/github/katilingban/pakete/badge)](https://www.codefactor.io/repository/github/katilingban/pakete)
<!-- badges: end -->

Tools and utilities for package development currently not available from
Expand All @@ -22,7 +30,11 @@ Currently, `pakete` includes functions for:

3. creating line snippets for `DESCRIPTION` file;

## Installation
4. creating `CONTRIBUTING.md` file;

5. adding `repostatus` badge;

## Installation·

You can install `pakete` via the [Katilingban
R-universe](https://katilingban.r-universe.dev) as follows:
Expand Down
Loading