Skip to content

Commit

Permalink
clean up package detection
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau committed Nov 14, 2024
1 parent 37eac27 commit 546debc
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 30 deletions.
8 changes: 4 additions & 4 deletions R/class_builder.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ target_prepare.tar_builder <- function(
meta,
pending = FALSE
) {
if (installed_autometric) {
if (package_installed("autometric (>= 0.1.0)")) {
phase <- paste("prepare:", target_get_name(target))
autometric::log_phase_set(phase = phase)
on.exit(autometric::log_phase_reset())
Expand Down Expand Up @@ -134,7 +134,7 @@ target_needs_worker.tar_builder <- function(target) {

#' @export
target_run.tar_builder <- function(target, envir, path_store) {
if (installed_autometric) {
if (package_installed("autometric (>= 0.1.0)")) {
autometric::log_phase_set(phase = target_get_name(target))
on.exit(autometric::log_phase_reset())
}
Expand Down Expand Up @@ -167,7 +167,7 @@ target_run_worker.tar_builder <- function(
options,
envvars
) {
if (installed_autometric) {
if (package_installed("autometric (>= 0.1.0)")) {
autometric::log_phase_set(phase = target_get_name(target))
on.exit(autometric::log_phase_reset())
}
Expand Down Expand Up @@ -214,7 +214,7 @@ target_skip.tar_builder <- function(

#' @export
target_conclude.tar_builder <- function(target, pipeline, scheduler, meta) {
if (installed_autometric) {
if (package_installed("autometric (>= 0.1.0)")) {
phase <- paste("conclude:", target_get_name(target))
autometric::log_phase_set(phase = phase)
on.exit(autometric::log_phase_reset())
Expand Down
6 changes: 0 additions & 6 deletions R/class_runtime.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ runtime_new <- function(
file_exist = NULL,
file_info = NULL,
file_info_exist = NULL,
nanonext = NULL,
inventories = NULL,
traceback = NULL,
pid_parent = NULL,
Expand All @@ -32,7 +31,6 @@ runtime_new <- function(
out$file_exist <- file_exist
out$file_info <- file_info
out$file_info_exist <- file_info_exist
out$nanonext <- nanonext
out$inventories <- inventories
out$traceback <- traceback
out$pid_parent <- pid_parent
Expand Down Expand Up @@ -105,10 +103,6 @@ runtime_validate_extras <- function(x) {
if (!is.null(x$file_info_exist)) {
tar_assert_envir(x$file_info_exist)
}
if (!is.null(x$nanonext)) {
tar_assert_scalar(x$nanonext)
tar_assert_lgl(x$nanonext)
}
if (!is.null(x$inventories)) {
tar_assert_list(x$inventories)
}
Expand Down
17 changes: 13 additions & 4 deletions R/utils_packages.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ load_packages <- function(packages, library) {
tar_assert_true(all(out), msg)
}

installed_autometric <- rlang::is_installed(
pkg = "autometric",
version = "0.1.0"
)
package_installed <- function(package) {
installed_packages <- .subset2(tar_runtime, "installed_packages")
if (is.null(installed_packages)) {
installed_packages <- lookup_new()
tar_runtime$installed_packages <- installed_packages
}
result <- .subset2(installed_packages, package)
if (is.null(result)) {
result <- rlang::is_installed(pkg = package)
installed_packages[[package]] <- result
}
result
}
2 changes: 1 addition & 1 deletion R/utils_rstudio.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ rstudio_symbol_at_cursor <- function(context) {
# nocov start
rstudio_available <- function(verbose = TRUE) {
available <- TRUE
if (!rlang::is_installed("rstudioapi")) {
if (!package_installed("rstudioapi")) {
available <- FALSE
reason <- "package {rstudioapi} is not installed."
}
Expand Down
11 changes: 4 additions & 7 deletions R/utils_time.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ time_seconds <- function() {
}

time_seconds_local <- function() {
if (is.null(tar_runtime$nanonext)) {
tar_runtime$nanonext <- rlang::is_installed("nanonext")
if (package_installed("nanonext")) {
nanonext::mclock() / 1e3
} else {
as.numeric(proc.time()["elapsed"]) # nocov
}
if_any(
tar_runtime$nanonext,
nanonext::mclock() / 1e3,
as.numeric(proc.time()["elapsed"])
)
}
8 changes: 0 additions & 8 deletions tests/testthat/test-class_runtime.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,6 @@ tar_test("file_info_exist", {
expect_silent(runtime_validate(x))
})

tar_test("nanonext", {
x <- runtime_new()
expect_null(x$nanonext)
x$nanonext <- TRUE
expect_true(x$nanonext)
expect_silent(runtime_validate(x))
})

tar_test("traceback", {
x <- runtime_new()
expect_null(x$traceback)
Expand Down

0 comments on commit 546debc

Please sign in to comment.