Skip to content

Commit

Permalink
Merge pull request #45 from katilingban:dev
Browse files Browse the repository at this point in the history
re-factor functions; fix #43; fix #44
  • Loading branch information
ernestguevarra authored Dec 30, 2024
2 parents e23dd03 + 51a1844 commit ce9cdae
Show file tree
Hide file tree
Showing 27 changed files with 874 additions and 272 deletions.
36 changes: 1 addition & 35 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,35 +1 @@
# Contributing

## Bugs

* Submit an issue on the [issues page](https://github.com/katilingban/pakete/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>/pakete.git
```

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

```
git remote add upstream https://github.com/katilingban/pakete.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 `katilingban/pakete`
cli-147-757
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ Description: Tools and utilities for package development currently not
and repetitive tasks easily implementable.
License: GPL (>= 3)
Imports:
cli,
jsonlite,
stringr,
utils,
withr
Suggests:
knitr,
Expand Down
12 changes: 10 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ export(add_contributing)
export(add_github_action)
export(add_issue_template)
export(add_logo)
importFrom(cli,cli_abort)
importFrom(cli,cli_alert_success)
importFrom(cli,cli_alert_warning)
importFrom(cli,cli_bullets)
importFrom(cli,cli_h1)
importFrom(cli,cli_h2)
importFrom(cli,cli_warn)
importFrom(cli,format_inline)
importFrom(cli,style_bold)
importFrom(jsonlite,fromJSON)
importFrom(stringr,str_detect)
importFrom(stringr,str_replace_all)
importFrom(utils,menu)
importFrom(withr,with_output_sink)
12 changes: 12 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

This is a first release of the `{pakete}` package to [R Universe](https://katilingban.r-universe.dev/pakete).

## What's changed

* add development mode in pkgdown.yml by @ernestguevarra in https://github.com/katilingban/pakete/pull/16
* create base vignette by @ernestguevarra in https://github.com/katilingban/pakete/pull/17
* update README by @ernestguevarra in https://github.com/katilingban/pakete/pull/18
* update website by @ernestguevarra in https://github.com/katilingban/pakete/pull/19
* create add_codecov function by @ernestguevarra in https://github.com/katilingban/pakete/pull/21
* refactor `add_codecov()` function by @ernestguevarra in https://github.com/katilingban/pakete/pull/22
* general tidying up of package structure by @ernestguevarra in https://github.com/katilingban/pakete/pull/26
* setup netlify; add_github_action; fix #25; fix #32; fix #33; fix #34 by @ernestguevarra in https://github.com/katilingban/pakete/pull/41
* add NEWS.md; fix #30 by @ernestguevarra in https://github.com/katilingban/pakete/pull/42


# pakete 0.0.0.9000

Expand Down
185 changes: 75 additions & 110 deletions R/add_badge.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,13 @@ add_badge_status <- function(status = c("concept", "wip", "suspended",
"unsupported", "moved"),
path = NULL,
.url = NULL) {
## cli header 1 ----
cli::cli_h1("Add repostatus badge")

## 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"
}
cli::cli_h2("Creating {status} repostatus badge")

## Determine what text to use based on status ----
status_text_url <- paste0(
Expand All @@ -50,42 +47,43 @@ add_badge_status <- function(status = c("concept", "wip", "suspended",
## 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
)
## If status is moved ----
if (status == "moved") {
## Check if .url is provided ----
if (is.null(.url)) {
cli::cli_abort(
c(
'{.var .url} cannot be NULL when {.var status} is "moved"',
"x" = '{.var .url} is NULL and {.var status} is "moved"'
)
)
} else {
## Replace https://example.com ----
badge_text <- gsub(
pattern = "http://example.com", replacement = .url, x = badge_text
)
}
}

cli::cli_bullets(
c(
"v" = "{status} repostatus badge created",
"i" = "Badge: {badge_text}"
)
)

cli::cli_h2("Adding {status} repostatus badge to README file")

## Determine which file to append badge to ----
path <- get_readme_path(path = path)

## 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))]
## Insert badge text to README ----
insert_readme_badge_text(
badge_text = badge_text, readme_lines = readme_lines, path = path
)

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

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


Expand All @@ -96,54 +94,39 @@ add_badge_status <- function(status = c("concept", "wip", "suspended",

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

cli::cli_h2("Creating CodeFactor badge")

## Get repo ----
if (is.null(repo)) repo <- get_github_repository()

## 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, ")")

cli::cli_bullets(
c(
"v" = "CodeFactor badge successfully created.",
"i" = "Badge: {badge_text}"
)
)

cli::cli_h2("Adding CodeFactor badge to README")

## Determine which file to append badge to ----
path <- get_readme_path(path = path)

## 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))]
## Insert badge text to README ----
insert_readme_badge_text(
badge_text = badge_text, readme_lines = readme_lines, path = path
)

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

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

#'
Expand All @@ -153,14 +136,14 @@ add_badge_codefactor <- function(repo = NULL,

add_badge_zenodo <- function(repo = NULL,
path = NULL) {
## Determine repo ----
if (is.null(repo))
stop("Remote git repository required. Try again")
cli::cli_h1("Add Zenodo badge")

cli::cli_h2("Creating Zenodo badge")

if (is.null(repo)) repo <- get_github_repository()

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

## Set CodeFactor defaults ----
badge <- paste0("https://zenodo.org/badge/", repo_id, ".svg")
Expand All @@ -169,41 +152,23 @@ add_badge_zenodo <- function(repo = NULL,
## Create badge text ----
badge_text <- paste0("[![DOI](", badge, ")](", link, ")")

cli::cli_bullets(
c(
"v" = "Zenodo badge successfully created.",
"i" = "Badge: {badge_text}"
)
)

cli::cli_h2("Adding Zenodo badge to README")

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

## 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))]
## Insert badge text to README ----
insert_readme_badge_text(
badge_text = badge_text, readme_lines = readme_lines, path = path
)

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

## Return TRUE if badge was added ----
TRUE
}
36 changes: 15 additions & 21 deletions R/add_codecov.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,20 @@ add_codecov <- function() {

codecov_lines <- readLines("codecov.yml")

if (
any(
stringr::str_detect(
string = codecov_lines, pattern = "token", negate = TRUE
)
if (any(!grepl(pattern = "token", x = codecov_lines))) {
FALSE
} else {
codecov_lines <- c(
"codecov:",
" token: ${{ secrets.CODECOV_TOKEN }}",
"",
codecov_lines
)
)
return (FALSE)


codecov_lines <- c(
"codecov:",
" token: ${{ secrets.CODECOV_TOKEN }}",
"",
codecov_lines
)

## Append replacement text ----
writeLines(text = codecov_lines, con = "codecov.yml")

## Return TRUE if snippet was added ----
TRUE

## Append replacement text ----
writeLines(text = codecov_lines, con = "codecov.yml")

## Return TRUE if snippet was added ----
TRUE
}
}
Loading

0 comments on commit ce9cdae

Please sign in to comment.