Skip to content

Commit

Permalink
create add logo function
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestguevarra committed Apr 22, 2024
1 parent 9240947 commit 74be294
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 71 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Description: Tools and utilities for package development currently not
and repetitive tasks easily implementable.
License: GPL (>= 3)
Imports:
jsonlite,
stringr
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.1

3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@

export(add_badge_codefactor)
export(add_badge_status)
export(add_logo)
export(add_zenodo_badge)
importFrom(jsonlite,fromJSON)
importFrom(stringr,str_detect)
importFrom(stringr,str_replace_all)
111 changes: 76 additions & 35 deletions R/add_badge.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#'
#' Add repostatus badge
#' Add badges to README
#'
#' @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 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.
Expand All @@ -14,11 +16,13 @@
#' Otherwise, a print of the markdown text for the status badge.
#'
#' @examples
#' add_badge_status(
#' "wip",
#' path = system.file("examples", "README.Rmd", package = "pakete")
#' )
#' if (FALSE) {
#' add_badge_status("wip")
#' add_badge_codefactor(repo = "katilingban/pakete")
#' add_badge_zenodo(repo = "katilingban/pakete")
#' }
#'
#' @rdname add_badge
#' @export
#'

Expand Down Expand Up @@ -78,32 +82,15 @@ add_badge_status <- function(status = c("concept", "wip", "suspended",
)

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

## Return TRUE if badge was added ----
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")}
#'
#' @rdname add_badge
#' @export
#'

Expand All @@ -126,13 +113,7 @@ add_badge_codefactor <- function(repo = NULL,
}

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

## Read file in path ----
readme_lines <- readLines(path, encoding = "UTF-8")
Expand All @@ -159,10 +140,70 @@ add_badge_codefactor <- function(repo = NULL,
)

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

## Return TRUE if badge was added ----
TRUE
}

#'
#' @rdname add_badge
#' @export
#'

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

## Get repository ID ----
repo_id <- file.path("https://api.github.com/repos", repo) |>
jsonlite::fromJSON() |>
(\(x) x$id)()

## Set CodeFactor defaults ----
badge <- paste0("https://zenodo.org/badge/", repo_id, ".svg")
link <- paste0("https://zenodo.org/badge/latestdoi/", repo_id)

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

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

## 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 = path)

## Return TRUE if badge was added ----
TRUE
}
52 changes: 52 additions & 0 deletions R/add_logo.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#'
#' Add HTML snippet for package hex sticker in README
#'
#' @param repo Short remote git repository name. If NULL, is determined based
#' on current git settings.
#'
#' @returns An entry to the first level header section of the README of the
#' repository.
#'
#' @examples
#' if (interactive()) add_logo()
#'
#' @export
#'

add_logo <- function(repo = NULL) {
## Get repo name ----
repo_name <- stringr::str_extract(string = repo, pattern = "[^/]*$")

## Check if logo is available ----
#if (file.exists("man/figures/logo.png"))

logo_text <- "<img src='man/figures/logo.png' width='200px' align='right' />"

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

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

if (any(stringr::str_detect(string = readme_lines, pattern = logo_text)))
return(FALSE)

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

## Create replacement text ----
readme_lines[header_line] <- paste0(readme_lines[header_line], " ", logo_text)

## Append replacement text ----
writeLines(text = readme_lines, con = path)

## Return TRUE if badge was added ----
TRUE
}
1 change: 1 addition & 0 deletions R/pakete-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
#' @keywords internal
#' @name pakete
#' @importFrom stringr str_detect str_replace_all
#' @importFrom jsonlite fromJSON
#'
"_PACKAGE"
3 changes: 2 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ knitr::opts_chunk$set(
)
```

# pakete: Utilities for Package Development
# pakete: Utilities for Package Development <img src='man/figures/logo.png' width='200px' align='right' />

<!-- 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)
[![DOI](https://zenodo.org/badge/790010725.svg)](https://zenodo.org/badge/latestdoi/790010725)
<!-- 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 Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<!-- README.md is generated from README.Rmd. Please edit that file -->

# pakete: Utilities for Package Development
# pakete: Utilities for Package Development <img src='man/figures/logo.png' width='200px' align='right' />

<!-- badges: start -->

Expand All @@ -12,6 +12,7 @@ public.](https://www.repostatus.org/badges/latest/wip.svg)](https://www.repostat
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)
[![DOI](https://zenodo.org/badge/790010725.svg)](https://zenodo.org/badge/latestdoi/790010725)
<!-- badges: end -->

Tools and utilities for package development currently not available from
Expand Down
35 changes: 35 additions & 0 deletions inst/templates/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Contributing

## Bugs

* Submit an issue on the [issues page](https://github.com/nutriverse/anthrocheckr/issues)

## Code contributions

* Fork this repository to your Github account

* Clone your version on your account down to your machine from your account

```
git clone https://github.com/<yourgithubusername>/anthrocheckr.git
```

* Make sure to track progress upstream i.e., on our version of `anthrocheckr`
at `nutriverse/anthrocheckr`, by doing

```
git remote add upstream https://github.com/nutriverse/anthrocheckr.git
```

* Before making changes make sure to pull changes in from `upstream` by doing
either `git fetch upstream` then merge later or `git pull upstream` to fetch
and merge in one step

* Make your changes on a new feature branch

* Please write a test or tests for your changes if they affect code and not just
documentation

* Push up changes to your account

* Submit a pull request at `nutriverse/anthrocheckr`
22 changes: 16 additions & 6 deletions man/add_badge_status.Rd → man/add_badge.Rd

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

27 changes: 0 additions & 27 deletions man/add_badge_codefactor.Rd

This file was deleted.

Loading

0 comments on commit 74be294

Please sign in to comment.