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

New parsing functions without {parsermd} #232

Merged
merged 14 commits into from
Dec 5, 2023
Merged
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ rstudio\_dotrstudio
^CONTRIBUTING\.md$
^fusen2\.Rproj$
^CRAN-SUBMISSION$
^.vscode$
79 changes: 79 additions & 0 deletions .github/workflows/rhub.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# R-hub's genetic GitHub Actions workflow file. It's canonical location is at
# https://github.com/r-hub/rhub2/blob/v1/inst/workflow/rhub.yaml
# You can update this file to a newer version using the rhub2 package:
#
# rhub2::rhub_setup()
#
# It is unlikely that you need to modify this file manually.

name: R-hub
run-name: ${{ github.event.inputs.name || format('Manually run by {0}', github.triggering_actor) }} (${{ github.event.inputs.id }})

on:
workflow_dispatch:
inputs:
config:
description: 'A comma separated list of R-hub platforms to use.'
type: string
default: 'linux,windows,macos'
name:
description: 'Run name. You can leave this empty now.'
type: string
id:
description: 'Unique ID. You can leave this empty now.'
type: string

jobs:

setup:
runs-on: ubuntu-latest
outputs:
containers: ${{ steps.rhub-setup.outputs.containers }}
platforms: ${{ steps.rhub-setup.outputs.platforms }}

steps:
# NO NEED TO CHECKOUT HERE
- uses: r-hub/rhub2/actions/rhub-setup@v1
with:
config: ${{ github.event.inputs.config }}
id: rhub-setup

linux-containers:
needs: setup
if: ${{ needs.setup.outputs.containers != '[]' }}
runs-on: ubuntu-latest
name: ${{ matrix.config.label }}
strategy:
fail-fast: false
matrix:
config: ${{ fromJson(needs.setup.outputs.containers) }}
container:
image: ${{ matrix.config.container }}

steps:
- uses: actions/checkout@v3
- uses: r-hub/rhub2/actions/rhub-check@v1
with:
token: ${{ secrets.RHUB_TOKEN }}
job-config: ${{ matrix.config.job-config }}

other-platforms:
needs: setup
if: ${{ needs.setup.outputs.platforms != '[]' }}
runs-on: ${{ matrix.config.os }}
name: ${{ matrix.config.label }}
strategy:
fail-fast: false
matrix:
config: ${{ fromJson(needs.setup.outputs.platforms) }}

steps:
- uses: actions/checkout@v3
- uses: r-hub/rhub2/actions/rhub-setup-r@v1
with:
job-config: ${{ matrix.config.job-config }}
token: ${{ secrets.RHUB_TOKEN }}
- uses: r-hub/rhub2/actions/rhub-check@v1
with:
job-config: ${{ matrix.config.job-config }}
token: ${{ secrets.RHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ check
/Meta/
cran-comments.md
CRAN-SUBMISSION
.vscode
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ Suggests:
rcmdcheck,
rmarkdown,
rstudioapi,
styler,
testthat (>= 3.0.0),
withr
VignetteBuilder:
knitr
Config/fusen/version: 0.5.2
Config/fusen/version: 0.5.2.9000
Config/Needs/website: ThinkR-open/thinkrtemplate
Config/testthat/edition: 3
Config/testthat/parallel: false
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# fusen (development version)

- Allow `organisation` in `init_share_on_github()` to send to a GitHub organisation
- Fix `load_flat_functions()` to work with VSCode

# fusen 0.5.2

## New features
Expand Down
4 changes: 1 addition & 3 deletions R/globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@ globalVariables(unique(c(
# add_fun_to_parsed
"fun_name", "rox_filename", "chunk_filename", ".",
# get_functions
".",
# split_rmd
"type", "lines", "title"
"."
)))
5 changes: 3 additions & 2 deletions R/inflate.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@

# If flat_file empty
if (missing(flat_file) && requireNamespace("rstudioapi") && rstudioapi::isAvailable() &&
rstudioapi::hasFun("documentPath")) {
current_file <- rstudioapi::documentPath()
rstudioapi::hasFun("getSourceEditorContext")) {
curr_editor <- rstudioapi::getSourceEditorContext()
current_file <- curr_editor$path

Check warning on line 99 in R/inflate.R

View check run for this annotation

Codecov / codecov/patch

R/inflate.R#L98-L99

Added lines #L98 - L99 were not covered by tests
if (!is.null(current_file) && grepl("^flat.*[.](R|r|q)md$", basename(current_file))) {
if (overwrite == "ask") {
sure <- paste0(
Expand Down
5 changes: 3 additions & 2 deletions R/init_share_on_github.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#'
#' @param ask Logical. `TRUE` (default) to ask the user to apply the instructions each time needed,
#' or `FALSE` if the user already know what to do.
#' @inheritParams usethis::use_github
#'
#' @details
#'
Expand Down Expand Up @@ -35,7 +36,7 @@
#' # This modifies the current directory and send it on GitHub
#' init_share_on_github()
#' }
init_share_on_github <- function(ask = TRUE) {
init_share_on_github <- function(ask = TRUE, organisation = NULL) {
pkg <- "."

if (!requireNamespace("gert", quietly = TRUE)) {
Expand Down Expand Up @@ -88,7 +89,7 @@
dont_do_it <- FALSE
}
if (do_it) {
usethis::use_github()
usethis::use_github(organisation = organisation)

Check warning on line 92 in R/init_share_on_github.R

View check run for this annotation

Codecov / codecov/patch

R/init_share_on_github.R#L92

Added line #L92 was not covered by tests
} else {
cli::cli_text(
cli::cli_alert_info(
Expand Down
45 changes: 33 additions & 12 deletions R/load_flat_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,54 @@
#'
#' @examples
#' \dontrun{
#' # Use this command directly in the console
#' fusen::load_flat_functions()
#'
#' # Or choose a flat file to load functions from
#' load_flat_functions(flat_file = "dev/flat_full.Rmd")
#' load_flat_functions(flat_file = "dev/flat_clean_fusen_files.Rmd")
#' }
load_flat_functions <- function(flat_file, envir = globalenv()) {
if (missing(flat_file) && requireNamespace("rstudioapi") && rstudioapi::isAvailable() &&
rstudioapi::hasFun("documentPath")) {
current_file <- rstudioapi::documentPath()
if (!is.null(current_file) && grepl("^flat.*[.](R|r|q)md$", basename(current_file))) {
if (
missing(flat_file) && requireNamespace("rstudioapi") &&
rstudioapi::isAvailable() &&
rstudioapi::hasFun("getSourceEditorContext")
) {
curr_editor <- rstudioapi::getSourceEditorContext()
current_file <- curr_editor$path

Check warning on line 26 in R/load_flat_functions.R

View check run for this annotation

Codecov / codecov/patch

R/load_flat_functions.R#L25-L26

Added lines #L25 - L26 were not covered by tests

if (!is.null(current_file)) {

Check warning on line 28 in R/load_flat_functions.R

View check run for this annotation

Codecov / codecov/patch

R/load_flat_functions.R#L28

Added line #L28 was not covered by tests
flat_file <- current_file
}
}

if (!grepl("^(flat|dev).*[.](R|r|q)md$", basename(flat_file))) {
stop(
"Please provide a Rmd or qmd flat file to load functions from",
" or open a flat file in the current IDE editor.",
"\n'flat_file' name should start with 'flat' or 'dev'",
" and end with '.Rmd' or '.qmd'."

Check warning on line 38 in R/load_flat_functions.R

View check run for this annotation

Codecov / codecov/patch

R/load_flat_functions.R#L34-L38

Added lines #L34 - L38 were not covered by tests
)
}

parsed_flat_file <- parse_rmd(flat_file)
parsed_tbl <- as_tibble(parsed_flat_file)
which_parsed_fun <- which(!is.na(parsed_tbl$label) &
grepl(regex_functions, parsed_tbl$label))
which_parsed_fun <- which(
!is.na(parsed_tbl$label) &
grepl(regex_functions, parsed_tbl$label)
)

if (nrow(parsed_tbl) > 0) {
# to_source <- tempfile()
content <- unlist(rmd_node_code(parsed_tbl[which_parsed_fun, ][["ast"]]))

eval(parse(text = content), envir)
# cat(content, file = to_source)
#
# source(to_source, ...)
# file.remove(to_source)
cli_alert_success(paste0("'function' chunks from '", flat_file, "' sourced in global env."))

cli_alert_success(
paste0(
"'function' chunks from '",
flat_file, "' sourced in global env."
)
)
} else {
cli_alert_warning("Nothing to source")
}
Expand Down
23 changes: 0 additions & 23 deletions data-raw/DATASET.R

This file was deleted.

1 change: 1 addition & 0 deletions dev/config_attachment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extra.suggests:
- rmarkdown
- knitr
- gert
- styler
pkg_ignore:
- testthat
- dummypackage
Expand Down
Loading