diff --git a/NAMESPACE b/NAMESPACE index ed0171c39..69b92a95b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,6 +3,7 @@ export(create_package) export(use_apl2_license) export(use_appveyor) +export(use_badge) export(use_build_ignore) export(use_code_of_conduct) export(use_coverage) diff --git a/NEWS.md b/NEWS.md index 2166554e5..66400945e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # usethis 0.0.0.9000 +* New `use_badge()` for adding any badge to a README. Now only prints a + todo message if the badge does not already exist. + * `use_revdep()` creates structure for use with revdepcheck package, the preferred way to run revdepchecks. (#33) diff --git a/R/badge.R b/R/badge.R index 69303e7ba..1e0e549d1 100644 --- a/R/badge.R +++ b/R/badge.R @@ -16,12 +16,31 @@ use_cran_badge <- function(base_path = ".") { invisible(TRUE) } +#' Use a README badge +#' +#' @param badge_name Badge name. Used in error message and alt text +#' @param href,src Badge link and image src +#' @inheritParams use_template +#' @export use_badge <- function(badge_name, href, src, base_path = ".") { + if (has_badge(href, base_path)) { + return(FALSE) + } + img <- paste0("![", badge_name, "](", src, ")") link <- paste0("[", img, "](", href, ")") - todo( - "Add a ", badge_name, " badge by adding the following line to your README:", - link - ) + todo("Add a ", badge_name, " badge by adding the following line to your README:") + code_block(link) +} + +has_badge <- function(href, base_path = ".") { + readme_path <- file.path(base_path, "README.md") + if (!file.exists(readme_path)) { + return(FALSE) + } + + readme <- readLines(readme_path) + any(grepl(href, readme, fixed = TRUE)) + } diff --git a/README.md b/README.md index 0c7d93a6c..4b9d4ff13 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ [![Travis build status](https://travis-ci.org/r-lib/usethis.svg?branch=master)](https://travis-ci.org/r-lib/usethis) [![codecov](https://codecov.io/gh/r-lib/usethis/branch/master/graph/badge.svg)](https://codecov.io/gh/r-lib/usethis) +[![CRAN status](http://www.r-pkg.org/badges/version/usethis)](https://cran.r-project.org/package=usethis) The goal of usethis is to automate many common package and analysis setup tasks. diff --git a/man/use_badge.Rd b/man/use_badge.Rd new file mode 100644 index 000000000..c45225dfe --- /dev/null +++ b/man/use_badge.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/badge.R +\name{use_badge} +\alias{use_badge} +\title{Use a README badge} +\usage{ +use_badge(badge_name, href, src, base_path = ".") +} +\arguments{ +\item{badge_name}{Badge name. Used in error message and alt text} + +\item{href, src}{Badge link and image src} + +\item{base_path}{Path to package root.} +} +\description{ +Use a README badge +}