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

Timer bug #20

Closed
wants to merge 3 commits into from
Closed
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
27 changes: 23 additions & 4 deletions R/config_repo.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,28 @@ config_repo_files <- function(config_path) {
file.path(config_path, c("logo.png", "options.yaml", "checklists"))
}

#' @importFrom cli cli_alert_success
#' @importFrom cli cli_blockquote
custom_options_found <- function(yaml_path) {
content <- tryCatch({
ghqcapp_repo <- function(config_path = ghqc_config_path()) {
if (!fs::dir_exists(config_path)) cli::cli_abort("Custom configuration repository is not found locally at {config_path}. The repository from which to install ghqc.app cannot be determined without the repository downloaded.")
yaml_path <- file.path(config_path, "options.yaml")
if (!fs::file_exists(yaml_path)) return(list("unset" = TRUE, "url"="https://a2-ai.r-universe.dev"))
options <- read_custom_options(yaml_path)
if (is.null(options)) cli::cli_abort("{yaml_path} could not be read. Please verify proper structure.")
if (!("ghqc.app_repository" %in% names(options))) return(list("unset" = TRUE, "url"="https://a2-ai.r-universe.dev"))
list("unset" = FALSE, "url" = options["ghqc.app_repository"])
}

read_custom_options <- function(yaml_path) {
tryCatch({
unlist(yaml::yaml.load_file(yaml_path))
}, error = function(e) {
NULL
})
}

#' @importFrom cli cli_alert_success
#' @importFrom cli cli_blockquote
custom_options_found <- function(yaml_path) {
content <- read_custom_options(yaml_path)
if (is.null(content)) {
cli::cli_alert_danger(paste0(cli::col_blue("{basename(yaml_path)}"), " could not be read"))
return()
Expand All @@ -204,6 +218,7 @@ custom_options_found <- function(yaml_path) {
sapply(names(content), function(x) switch(x,
"prepended_checklist_note" = note_found(content[x]),
"checklist_display_name_var" = checklist_display_name_found(content[x]),
"ghqc.app_repository" = ghqcapp_repository_found(content[x]),
cli::cli_alert_info("{x} is not a recognized custom option.")))
cli::cli_end(ul)
if (!("prepended_checklist_note" %in% names(content))) cli::cli_inform("")
Expand All @@ -218,6 +233,10 @@ checklist_display_name_found <- function(checklist_disp_name) {
cli::cli_alert_success("{names(checklist_disp_name)}: {checklist_disp_name}")
}

ghqcapp_repository_found <- function(repo_url) {
cli::cli_alert_success("{names(repo_url)}: {repo_url}")
}

#' @importFrom cli cli_alert_success
#' @importFrom cli cli_h3
checklists_found <- function(config_path) {
Expand Down
32 changes: 28 additions & 4 deletions R/download_packages.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ install_ghqcapp_dependencies <- function(lib_path = ghqc_libpath(),

if (use_pak) {
res <- withr::with_options(list("pkg.sysreqs" = FALSE, repos = setup_rpsm_url(ghqc_depends_snapshot_date)),
pak::pkg_install(pkgs, lib = lib_path, upgrade = TRUE, ask = FALSE)) #blow cache, run this, check description file
pak::pkg_install(pkgs, lib = lib_path, upgrade = TRUE, ask = FALSE))

} else {
if (rlang::is_installed("pak")) cli::cli_alert_warning("pak is installed, but input `use_pak` was set to FALSE. Set `use_pak` to TRUE for better performance.")
Expand Down Expand Up @@ -87,11 +87,11 @@ setup_rpsm_url <- function(snapshot_date) {
system_info <- processx::run(cmd$cmd, args = cmd$args)$stdout
system_info <- gsub(" ", replacement = "_", system_info)
ubuntu_codename <- regmatches(system_info, regexec("\nCodename:\t(.*?)\n", system_info))[[1]][2]
repo <- file.path("https://packagemanager.posit.co/cran/__linux__", tolower(ubuntu_codename),snapshot_date)
}, error = function(e) {
cli::cli_abort(message = "Failed to detect codename via lsb_release")
repo <- file.path("https://packagemanager.posit.co/cran", snapshot_date)
}
)
repo <- file.path("https://packagemanager.posit.co/cran/__linux__", tolower(ubuntu_codename),snapshot_date)
if (rlang::is_installed("pak")) {
repo_status <- withr::with_options(list(repos = repo), pak::repo_status(bioc = FALSE, cran_mirror = repo))$ok
} else {
Expand All @@ -102,11 +102,35 @@ setup_rpsm_url <- function(snapshot_date) {
repo_status = FALSE
})
}
if (!repo_status) cli::cli_abort(message = sprintf("Posit package manager for snapshot date %s and os %s is not available", snapshot_date, ubuntu_codename))
if (!repo_status) cli::cli_abort(message = "{repo} is not available. Verify snapshot date and/or operating system is valid.")

c("CRAN" = repo)
}

install_ghqcapp <- function(lib_path = ghqc_libpath(),
repo = ghqcapp_repo()$url,
use_pak = TRUE) {
if (!(all(ghqc_depends %in% utils::installed.packages(lib_path)))) cli::cli_abort("All package dependencies for ghqc.app are not found in {lib_path}. Run `check_ghqcapp_dependencies` before continuing.")

if (!rlang::is_installed("pak") && use_pak) rlang::abort("pak is not installed. Install pak for better performance. If pak cannot be installed, set `use_pak` = FALSE in `install_ghqcapp_dependencies()` function call")

cli::cli_inform("Installing ghqc.app...")
tryCatch({
if (use_pak) {
res <- withr::with_options(list("pkg.sysreqs" = FALSE, repos = repo),
pak::pkg_install("ghqc.app", lib = lib_path, dependencies = FALSE, ask = FALSE))

} else {
if (rlang::is_installed("pak")) cli::cli_alert_warning("pak is installed, but input `use_pak` was set to FALSE. Set `use_pak` to TRUE for better performance.")
res <- utils::install.packages("ghqc.app", lib = lib_path, repos = repo, dependencies = FALSE)
}
cli::cli_alert_success("ghqc.app successfully installed")
}, error = function(e) {
cli::cli_alert_danger(c("ghqc.app did not install. Failure due to: ", e$message))
})
invisible(res)
}


#' @importFrom rlang is_installed
#' @importFrom rlang abort
Expand Down
2 changes: 1 addition & 1 deletion R/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ run_app <- function(app_name, qc_dir, lib_path, config_path) {
counter <- counter + 1
Sys.sleep(1)
}
if (counter > 19) cli::cli_alert_danger("Shiny app could not be started due to timeout or error")
if (counter > 100) cli::cli_alert_danger("Shiny app could not be started due to timeout or error")


rstudioapi::viewer(url)
Expand Down
213 changes: 167 additions & 46 deletions R/interactive_setup.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.pe <- new.env()

#' Interactive function to set up the ghqc environment, including writing to the .Renviron, custom configuration repository download, and ghqc.app dependency installation/linking, for use of the ghqc application suite
#'
#' @importFrom cli cli_abort
Expand All @@ -10,7 +12,7 @@ setup_ghqc <- function() {
}

renv_text <- interactive_renviron()
interactive_config_download()
if (!exists("no_config_repo", envir = .pe)) interactive_config_download()
check_res <- interactive_depends()
}

Expand All @@ -28,23 +30,57 @@ interactive_renviron <- function() {
#' @importFrom glue glue
interactive_info <- function(renv_text) {
config <- parse_renviron("GHQC_CONFIG_REPO", renv_text)
if (config$val == "") {
cli::cli_inform(c(" ", "GHQC_CONFIG_REPO is not set in your ~/.Renviron"))
config_read <- readline("Provide the URL to the custom configuration repository: ")
} else {
cli::cli_inform(c(" ", "GHQC_CONFIG_REPO is set to {config$val} in your ~/.Renviron"))
config_read <- readline(glue::glue("Custom Configuration Repository ({config$val}) "))
if (config_read == "") config_read <- config$val
val <- if (config$val == "") renviron_not_set() else renviron_set(config$val)
if (!exists("no_config_repo", envir = .pe)) renv_text <- renviron_edit("GHQC_CONFIG_REPO", val, renv_text)
renv_text
}

renviron_set <- function(config_url) {
cli::cli_inform(c(" ", "GHQC_CONFIG_REPO is set to {config_url} in your ~/.Renviron. Would you like to: ",
" " = "1. Proceed",
" " = "2. Replace",
# " " = "3. Delete", Potentially add later
" " = "3. Abort with error"))
input <- readline_and_verify("Input: ", 1:3)
if (input == "3") cli::cli_abort("GHQC_CONFIG_REPO is set in ~/.Renviron. Aborting...")
if (input == "2") return(readline_and_verify_config())
if (input == "1") return(config_url)
}

renviron_not_set <- function() {
cli::cli_inform(c(" ", "GHQC_CONFIG_REPO is not set in your ~/.Renviron. Would you like to: ",
" " = "1. Set value",
" " = "2. Proceed without setting",
" " = "3. Abort with error"))
input <- readline_and_verify("Input: ", 1:3)
if (input == "3") cli::cli_abort("GHQC_CONFIG_REPO not set in ~/.Renviron. Aborting...")
if (input == "2") {
cli::cli_alert_danger("GHQC_CONFIG_REPO not set in ~/.Renviron. The ghqc shiny apps will not function.")
assign("no_config_repo", FALSE, .pe)
}
if (input == "1") readline_and_verify_config()
}

readline_and_verify_config <- function() {
input <- readline("Provide the URL to the custom configuration repository: ")
repeat {
config_read <- gsub('\"', "", config_read)
if (grepl("^https:", config_read)) {
config$val <- config_read
break
}
config_read <- readline(glue::glue("GHQC_CONFIG_REPO does not start with 'https:'. Please provide a valid URL: "))
input <- gsub('\"', "", input)
if (grepl("^https://", input)) break
input <- readline(glue::glue("'{input}' does not start with 'https://'. Please provide a valid URL: "))
}
renviron_edit("GHQC_CONFIG_REPO", config$val, renv_text)
input
}

readline_and_verify <- function(message, input_options) {
if (!is.character(input_options)) input_options <- as.character(input_options)
input <- readline(message)
repeat {
input <- gsub('\"', "", input)
if (input %in% input_options) break
io_str <- paste0(paste0(input_options[-length(input_options)], collapse = ", "), ", or ", input_options[length(input_options)])
input <- readline(glue::glue("Input value of '{input}' is not valid. Please enter {io_str}: "))
}
input
}

write_renv_text <- function(renv_text, val, var_name) {
Expand All @@ -70,12 +106,40 @@ interactive_config_download <- function() {
install.packages("gert")
}

cli::cli_inform(" ")
config_path <- gsub('\"', "", readline(glue::glue("Path to download the custom configuration repository ({ghqc_config_path()}) ")))
if (config_path == "") config_path <- ghqc_config_path()
determine_ghqc_config_download()
}

cli::cli_inform(" ")
check_ghqc_configuration(config_path = config_path)
determine_ghqc_config_download <- function() {
cli::cli_inform(c(" ", "Would you like to download the custom configuration repository to the default location ({ghqc_config_path()})? ",
" " = "1. yes, absolutely!",
" " = "2. can I check the contents in the default location first?",
" " = "3. no, I have other plans and want to specify a different location",
" " = "4. no, I don't want to download right now",
" " = "5. Abort with error"))
input <- readline_and_verify("Input: ", 1:5)
if (input == "5") cli::cli_abort("Custom configuration repository is not downloaded. Aborting...")
if (input == "4") {
cli::cli_alert_danger("Cannot verify custom configuration repository is downloaded. Please ensure before running any ghqc shiny apps")
assign("no_config_repo", TRUE, .pe)
}
if (input == "3") {
cli::cli_alert_warning("NOTE: This is a non-standard option. Please ensure you have an understanding of the effects before continuing.")
yN <- readline_and_verify("Would you like to continue (y/N)? ", c("y", "N"))
if (yN == "N") {
determine_ghqc_config_download()
return(invisible())
}
path <- gsub('\"', "", readline("Provide the path to install the custom configuration repository: "))
assign("config_path", path, .pe)
download_ghqc_configuration(config_path = path, .force = TRUE)
}
assign("config_path", ghqc_config_path(), .pe)
if (input == "2") {
check_ghqc_configuration()
}
if (input == "1") {
download_ghqc_configuration()
}
}

#' @importFrom cli cli_h1
Expand All @@ -88,43 +152,67 @@ interactive_depends <- function() {
"1. INSTALL",
"2. LINK",
"3. Neither"))
inst_method <- readline("Input: ")

repeat {
inst_method <- gsub('\"', "", inst_method)
if ((inst_method %in% c("1", "2", "3"))) break
inst_method <- readline(glue::glue("Input of {inst_method} is not a valid input. Please enter 1, 2, or 3: "))
}
inst_method <- readline_and_verify("Input: ", 1:3)

if (inst_method == "3") {
cli::cli_alert_warning("Ensure that ghqc.app and its dependencies are installed into an isolated ghqc package library path before using any of the ghqc ecosystem apps.")
return(invisible())
}

if (inst_method == "1") return(interactive_install())
if (inst_method == "2") return(interactive_link())
if (inst_method == "1") res <- list("install_results" = interactive_install())
if (inst_method == "2") res <- list("link_results" = interactive_link())
if (!exists("no_config_repo", envir = .pe)) {
list(res, "ghqcapp_results" = interactive_ghqcapp_install())
} else {
cli::cli_alert_warning("The repository from which to install ghqc.app cannot be determined without the custom configuration repository being downloaded.")
}
}

#' @importFrom rlang is_installed
#' @importFrom utils install.packages
#' @importFrom cli cli_inform
#' @importFrom fs file_exists
#' @importFrom fs dir_create
interactive_install <- function() {
interactive_use_pak <- function() {
use_pak <- TRUE
if (!rlang::is_installed("pak")) {
cli::cli_inform(" ")
yN <- gsub('\"', "", readline("Package `pak` is not found in your project package library. To improve performance, would you like to install pak? (y/N) "))
if (yN == "y" || yN == "") utils::install.packages("pak") else use_pak <- FALSE
}
use_pak
}

cli::cli_inform(" ")
lib_path <- gsub('\"', "", readline("Path to install the ghqc.app dependencies (~/.local/share/ghqc/rpkgs) "))
if (lib_path == "") lib_path <- ghqc_libpath()
if (!fs::file_exists(lib_path)) fs::dir_create(lib_path)
#' @importFrom rlang is_installed
#' @importFrom utils install.packages
#' @importFrom cli cli_inform
#' @importFrom fs file_exists
#' @importFrom fs dir_create
interactive_install <- function() {
use_pak <- interactive_use_pak()
assign("use_pak", use_pak, .pe)

cli::cli_inform(" ")
check_ghqcapp_dependencies(lib_path = lib_path, use_pak = use_pak)
cli::cli_inform(c(" ", "Would you like to install the ghqc.app dependencies to the default location ({ghqc_libpath()})?",
" " = "1. yes, absolutely!",
" " = "2. can I check the contents in the default location first?",
" " = "3. no, I have other plans and want to specify a different location",
" " = "4. Abort with error"))
input <- readline_and_verify("Input: ", 1:4)

if (input == "4") cli::cli_abort("Dependency packages not installed. Aborting...")
if (input == "3") {
cli::cli_alert_warning("NOTE: This is a non-standard option. Please ensure you have an understanding of the effects before continuing.")
yN <- readline_and_verify("Would you like to continue (y/N)? ", c("y", "N"))
if (yN == "N") {
interactive_install()
return(invisible())
}
path <- gsub('\"', "", readline("Provide the path to install the dependency packages: "))
assign("lib_path", path, .pe)
install_ghqcapp_dependencies(lib_path = path, use_pak = use_pak)
}
assign("lib_path", ghqc_libpath(), .pe)
if (input == "2") {
check_ghqcapp_dependencies()
}
if (input == "1") {
install_ghqcapp_dependencies()
}
}

#' @importFrom cli cli_inform
Expand All @@ -142,10 +230,43 @@ interactive_link <- function() {
return(invisible())
})

lib_path <- gsub('\"', "", readline("Path to link the ghqc.app dependencies (~/.local/share/ghqc/rpkgs) "))
if (lib_path == "") lib_path <- ghqc_libpath()
if (!fs::file_exists(lib_path)) fs::dir_create(lib_path)
cli::cli_inform(c(" ", "Would you like to link the ghqc.app dependencies to the default location ({ghqc_libpath()})?",
" " = "1. yes, absolutely!",
" " = "2. no, I have other plans and want to specify a different location",
" " = "3. Abort with error"))
input <- readline_and_verify("Input: ", 1:3)
if (input == 3) cli::cli_abort("Dependency packages not symlinked. Aborting...")
if (input == 2) {
cli::cli_alert_warning("NOTE: This is a non-standard option. Please ensure you have an understanding of the effects before continuing.")
yN <- readline_and_verify("Would you like to continue (y/N)? ", c("y", "N"))
if (yN == "N") {
interactive_install()
return(invisible())
}
path <- gsub('\"', "", readline("Provide the path to symlink the dependency packages to: "))
}
if (input == 1) path <- ghqc_libpath()
assign("lib_path", path, .pe)
link_ghqcapp_dependencies(link_path, path)
}

interactive_ghqcapp_install <- function() {
#FOR DEV
# assign("lib_path", ghqc_libpath(), .pe)
# assign("config_path", ghqc_config_path(), .pe)

cli::cli_inform(" ")
link_ghqcapp_dependencies(link_path = link_path, lib_path = lib_path)
cli::cli_h1("GHQC.APP INSTALLATION")
repo <- ghqcapp_repo(config_path = .pe$config_path)
specified <- NULL
if (!repo$unset) specified <- "specified "
cli::cli_inform("ghqc.app will install from {specified}repository: {repo$url}")
yN <- tolower(readline(glue::glue("Would you like to install ghqc.app to {(.pe$lib_path)} (y/N)? ")))

if (yN == "n") {
cli::cli_alert_danger("Please install ghqc.app to {.pe$lib_path} before running any ghqc apps")
return(invisible())
}

if (!exists("use_pak", envir = .pe)) assign("use_pak", interactive_use_pak(), .pe)
install_ghqcapp(lib_path = .pe$lib_path, repo = repo$url, use_pak = .pe$use_pak)
}
Loading