From d6816ed1a6d541f805aebc9f28cd4ac1f99c3c79 Mon Sep 17 00:00:00 2001 From: katieb1 Date: Thu, 30 Nov 2023 10:40:39 -0800 Subject: [PATCH] Remove enableAddon, disableAddon, add addPackage, removePackage --- DESCRIPTION | 4 +- NAMESPACE | 4 +- R/addPackage.R | 73 ++++++++++++++++++++++++++++++++++ R/disableAddon.R | 81 -------------------------------------- R/enableAddon.R | 80 ------------------------------------- R/removePackage.R | 74 ++++++++++++++++++++++++++++++++++ man/addPackage.Rd | 52 ++++++++++++++++++++++++ man/disableAddon.Rd | 52 ------------------------ man/enableAddon.Rd | 52 ------------------------ man/removePackage.Rd | 52 ++++++++++++++++++++++++ tests/testthat/test-unit.R | 19 +-------- 11 files changed, 256 insertions(+), 287 deletions(-) create mode 100644 R/addPackage.R delete mode 100644 R/disableAddon.R delete mode 100644 R/enableAddon.R create mode 100644 R/removePackage.R create mode 100644 man/addPackage.Rd delete mode 100644 man/disableAddon.Rd delete mode 100644 man/enableAddon.Rd create mode 100644 man/removePackage.Rd diff --git a/DESCRIPTION b/DESCRIPTION index d7c2fa70..10a0f7de 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -46,6 +46,7 @@ Suggests: SystemRequirements: SyncroSim (>=2.3.10) Collate: 'AAAClassDefinitions.R' + 'addPackage.R' 'addRow.R' 'autogentags.R' 'backup.R' @@ -59,8 +60,6 @@ Collate: 'delete.R' 'dependency.R' 'description.R' - 'disableAddon.R' - 'enableAddon.R' 'filepath.R' 'folder.R' 'folderId.R' @@ -86,6 +85,7 @@ Collate: 'printCmd.R' 'published.R' 'readOnly.R' + 'removePackage.R' 'rsyncrosim.R' 'run.R' 'runLog.R' diff --git a/NAMESPACE b/NAMESPACE index 209a3b79..3ce40194 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -19,6 +19,7 @@ export(Scenario) export(Session) export(SsimLibrary) export(addBreakpoint) +export(addPackage) export(addRow) export(autogentags) export(backup) @@ -33,8 +34,6 @@ export(delete) export(deleteBreakpoint) export(dependency) export(description) -export(disableAddon) -export(enableAddon) export(filepath) export(folder) export(folderId) @@ -53,6 +52,7 @@ export(project) export(projectId) export(published) export(readOnly) +export(removePackage) export(run) export(runLog) export(runtimeInputFolder) diff --git a/R/addPackage.R b/R/addPackage.R new file mode 100644 index 00000000..15bd5d31 --- /dev/null +++ b/R/addPackage.R @@ -0,0 +1,73 @@ +# Copyright (c) 2023 Apex Resource Management Solution Ltd. (ApexRMS). All rights reserved. +# MIT License +#' @include AAAClassDefinitions.R +NULL + +#' Add SyncroSim package(s) +#' +#' Adds package(s) to a \code{\link{SsimLibrary}}. +#' +#' @param ssimLibrary \code{\link{SsimLibrary}} object +#' @param name character string or vector of package name(s) +#' +#' @return +#' Invisibly returns \code{TRUE} upon success (i.e.successful addition +#' of the package) or \code{FALSE} upon failure. +#' +#' @seealso +#' \code{\link{packages}} +#' +#' @examples +#' \donttest{ +#' # Install "stsim" SyncroSim package +#' installPackage("stsim") +#' +#' # Specify file path and name of new SsimLibrary +#' myLibraryName <- file.path(tempdir(), "testlib") +#' +#' # Set up a SyncroSim Session, SsimLibrary, and Project +#' mySession <- session() +#' myLibrary <- ssimLibrary(name = myLibraryName, session = mySession, +#' package = "stsim") +#' +#' # Add package +#' addPackage(myLibrary, c("stsimsf")) +#' packages(myLibrary) +#' +#' # Remove package +#' removePackage(myLibrary, c("stsimsf")) +#' packages(myLibrary) +#' } +#' +#' @export +setGeneric("addPackage", function(ssimLibrary, name) standardGeneric("addPackage")) + +#' @rdname addPackage +setMethod("addPackage", signature(ssimLibrary = "character"), function(ssimLibrary, name) { + return(SyncroSimNotFound(ssimLibrary)) +}) + +#' @rdname addPackage +setMethod("addPackage", signature(ssimLibrary = "SsimLibrary"), function(ssimLibrary, name) { + cAdds <- packages(ssimLibrary) + retList <- list() + for (i in seq(length.out = length(name))) { + cVal <- name[i] + if (!is.element(cVal, cAdds$name)) { + print(paste0("Warning - ", cVal, " is not among the available packages: ", paste(cAdds$name, collapse = ","))) + retList[[cVal]] <- FALSE + next + } + + tt <- command(list(add = NULL, package = NULL, lib = .filepath(ssimLibrary), pkg = cVal), .session(ssimLibrary)) + if (tt == "saved"){ + message(paste0("Package <", cVal, "> added")) + retList[[cVal]] <- TRUE + } else { + message(tt) + retList[[cVal]] <- FALSE + } + } + + return(invisible(retList)) +}) diff --git a/R/disableAddon.R b/R/disableAddon.R deleted file mode 100644 index a4b4bcd8..00000000 --- a/R/disableAddon.R +++ /dev/null @@ -1,81 +0,0 @@ -# Copyright (c) 2023 Apex Resource Management Solution Ltd. (ApexRMS). All rights reserved. -# MIT License -#' @include AAAClassDefinitions.R -NULL - -#' Disable addon package(s) -#' -#' Disable \code{\link{addon}} package(s) of a \code{\link{SsimLibrary}}. -#' -#' @param ssimLibrary \code{\link{SsimLibrary}} object -#' @param name character string or vector of addon name(s) -#' -#' @return -#' This function invisibly returns \code{TRUE} upon success (i.e.successful deactivation -#' of the addon) or \code{FALSE} upon failure. -#' -#' @seealso -#' \code{\link{addon}} -#' -#' @examples -#' \donttest{ -#' # Install "stsim" SyncroSim package -#' installPackage("stsim") -#' -#' # Specify file path and name of new SsimLibrary -#' myLibraryName <- file.path(tempdir(), "testlib") -#' -#' # Set up a SyncroSim Session, SsimLibrary, and Project -#' mySession <- session() -#' myLibrary <- ssimLibrary(name = myLibraryName, session = mySession, -#' package = "stsim") -#' -#' # Enable addon package -#' enableAddon(myLibrary, c("stsimsf")) -#' addon(myLibrary) -#' -#' # Disable addon package -#' disableAddon(myLibrary, c("stsimsf")) -#' addon(myLibrary) -#' } -#' -#' @export -setGeneric("disableAddon", function(ssimLibrary, name) standardGeneric("disableAddon")) - -#' @rdname disableAddon -setMethod("disableAddon", signature(ssimLibrary = "character"), function(ssimLibrary, name) { - return(SyncroSimNotFound(ssimLibrary)) -}) - -#' @rdname disableAddon -setMethod("disableAddon", signature(ssimLibrary = "SsimLibrary"), function(ssimLibrary, name) { - enabled <- NULL - cAdds <- subset(addon(ssimLibrary)) - retList <- list() - - for (i in seq(length.out = length(name))) { - cVal <- name[i] - if (!is.element(cVal, cAdds$name)) { - print(paste0("Warning - ", cVal, " is not among the available addons: ", paste(cAdds$name[cAdds$enabled == "No"], collapse = ","))) - retList[[cVal]] <- FALSE - next - } - cAddsLess <- subset(cAdds, enabled == TRUE) - if (!is.element(cVal, cAddsLess$name)) { - message(paste0(cVal, " is already disabled.")) - retList[[cVal]] <- FALSE - next - } - - tt <- command(list(delete = NULL, addon = NULL, force = NULL, lib = .filepath(ssimLibrary), name = cVal), .session(ssimLibrary)) - if (tt == "saved"){ - message(paste0("Addon <", cVal, "> disabled")) - retList[[cVal]] <- TRUE - } else { - message(tt) - retList[[cVal]] <- FALSE - } - } - - return(invisible(retList)) -}) diff --git a/R/enableAddon.R b/R/enableAddon.R deleted file mode 100644 index 4fd35651..00000000 --- a/R/enableAddon.R +++ /dev/null @@ -1,80 +0,0 @@ -# Copyright (c) 2023 Apex Resource Management Solution Ltd. (ApexRMS). All rights reserved. -# MIT License -#' @include AAAClassDefinitions.R -NULL - -#' Enable addon package(s) -#' -#' Enable \code{\link{addon}} package(s) of a \code{\link{SsimLibrary}}. -#' -#' @param ssimLibrary \code{\link{SsimLibrary}} object -#' @param name character string or vector of addon name(s) -#' -#' @return -#' Invisibly returns \code{TRUE} upon success (i.e.successful activation -#' of the addon) or \code{FALSE} upon failure. -#' -#' @seealso -#' \code{\link{addon}} -#' -#' @examples -#' \donttest{ -#' # Install "stsim" SyncroSim package -#' installPackage("stsim") -#' -#' # Specify file path and name of new SsimLibrary -#' myLibraryName <- file.path(tempdir(), "testlib") -#' -#' # Set up a SyncroSim Session, SsimLibrary, and Project -#' mySession <- session() -#' myLibrary <- ssimLibrary(name = myLibraryName, session = mySession, -#' package = "stsim") -#' -#' # Enable add on package -#' enableAddon(myLibrary, c("stsimsf")) -#' addon(myLibrary) -#' -#' # Disable add on package -#' disableAddon(myLibrary, c("stsimsf")) -#' addon(myLibrary) -#' } -#' -#' @export -setGeneric("enableAddon", function(ssimLibrary, name) standardGeneric("enableAddon")) - -#' @rdname enableAddon -setMethod("enableAddon", signature(ssimLibrary = "character"), function(ssimLibrary, name) { - return(SyncroSimNotFound(ssimLibrary)) -}) - -#' @rdname enableAddon -setMethod("enableAddon", signature(ssimLibrary = "SsimLibrary"), function(ssimLibrary, name) { - enabled <- NULL - cAdds <- addon(ssimLibrary) - retList <- list() - for (i in seq(length.out = length(name))) { - cVal <- name[i] - if (!is.element(cVal, cAdds$name)) { - print(paste0("Warning - ", cVal, " is not among the available addons: ", paste(cAdds$name[cAdds$enabled == "No"], collapse = ","))) - retList[[cVal]] <- FALSE - next - } - cAddsLess <- subset(cAdds, enabled == FALSE) - if (!is.element(cVal, cAddsLess$name)) { - message(paste0(cVal, " is already enabled.")) - retList[[cVal]] <- FALSE - next - } - - tt <- command(list(create = NULL, addon = NULL, lib = .filepath(ssimLibrary), name = cVal), .session(ssimLibrary)) - if (tt == "saved"){ - message(paste0("Addon <", cVal, "> enabled")) - retList[[cVal]] <- TRUE - } else { - message(tt) - retList[[cVal]] <- FALSE - } - } - - return(invisible(retList)) -}) diff --git a/R/removePackage.R b/R/removePackage.R new file mode 100644 index 00000000..d5bab518 --- /dev/null +++ b/R/removePackage.R @@ -0,0 +1,74 @@ +# Copyright (c) 2023 Apex Resource Management Solution Ltd. (ApexRMS). All rights reserved. +# MIT License +#' @include AAAClassDefinitions.R +NULL + +#' Removes SyncroSim package(s) +#' +#' Removes package(s) from a \code{\link{SsimLibrary}}. +#' +#' @param ssimLibrary \code{\link{SsimLibrary}} object +#' @param name character string or vector of package name(s) +#' +#' @return +#' This function invisibly returns \code{TRUE} upon success (i.e.successful +#' removal of the package) or \code{FALSE} upon failure. +#' +#' @seealso +#' \code{\link{packages}} +#' +#' @examples +#' \donttest{ +#' # Install "stsim" SyncroSim package +#' installPackage("stsim") +#' +#' # Specify file path and name of new SsimLibrary +#' myLibraryName <- file.path(tempdir(), "testlib") +#' +#' # Set up a SyncroSim Session, SsimLibrary, and Project +#' mySession <- session() +#' myLibrary <- ssimLibrary(name = myLibraryName, session = mySession, +#' package = "stsim") +#' +#' # Add package +#' addPackage(myLibrary, c("stsimsf")) +#' packages(myLibrary) +#' +#' # Remove package +#' removePackage(myLibrary, c("stsimsf")) +#' packages(myLibrary) +#' } +#' +#' @export +setGeneric("removePackage", function(ssimLibrary, name) standardGeneric("removePackage")) + +#' @rdname removePackage +setMethod("removePackage", signature(ssimLibrary = "character"), function(ssimLibrary, name) { + return(SyncroSimNotFound(ssimLibrary)) +}) + +#' @rdname removePackage +setMethod("removePackage", signature(ssimLibrary = "SsimLibrary"), function(ssimLibrary, name) { + cAdds <- subset(addon(ssimLibrary)) + retList <- list() + + for (i in seq(length.out = length(name))) { + cVal <- name[i] + if (!is.element(cVal, cAdds$name)) { + print(paste0("Warning - ", cVal, " is not among the available packages: ", paste(cAdds$name, collapse = ","))) + retList[[cVal]] <- FALSE + next + } + + tt <- command(list(remove = NULL, package = NULL, force = NULL, lib = .filepath(ssimLibrary), pkg = cVal), .session(ssimLibrary)) + if (tt == "saved"){ + message(paste0("Package <", cVal, "> removed")) + retList[[cVal]] <- TRUE + } else { + message(tt) + retList[[cVal]] <- FALSE + } + } + + return(invisible(retList)) +}) diff --git a/man/addPackage.Rd b/man/addPackage.Rd new file mode 100644 index 00000000..cd23eb76 --- /dev/null +++ b/man/addPackage.Rd @@ -0,0 +1,52 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/addPackage.R +\name{addPackage} +\alias{addPackage} +\alias{addPackage,character-method} +\alias{addPackage,SsimLibrary-method} +\title{Add SyncroSim package(s)} +\usage{ +addPackage(ssimLibrary, name) + +\S4method{addPackage}{character}(ssimLibrary, name) + +\S4method{addPackage}{SsimLibrary}(ssimLibrary, name) +} +\arguments{ +\item{ssimLibrary}{\code{\link{SsimLibrary}} object} + +\item{name}{character string or vector of package name(s)} +} +\value{ +Invisibly returns \code{TRUE} upon success (i.e.successful addition +of the package) or \code{FALSE} upon failure. +} +\description{ +Adds package(s) to a \code{\link{SsimLibrary}}. +} +\examples{ +\donttest{ +# Install "stsim" SyncroSim package +installPackage("stsim") + +# Specify file path and name of new SsimLibrary +myLibraryName <- file.path(tempdir(), "testlib") + +# Set up a SyncroSim Session, SsimLibrary, and Project +mySession <- session() +myLibrary <- ssimLibrary(name = myLibraryName, session = mySession, + package = "stsim") + +# Add package +addPackage(myLibrary, c("stsimsf")) +packages(myLibrary) + +# Remove package +removePackage(myLibrary, c("stsimsf")) +packages(myLibrary) +} + +} +\seealso{ +\code{\link{packages}} +} diff --git a/man/disableAddon.Rd b/man/disableAddon.Rd deleted file mode 100644 index 1a295b0d..00000000 --- a/man/disableAddon.Rd +++ /dev/null @@ -1,52 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/disableAddon.R -\name{disableAddon} -\alias{disableAddon} -\alias{disableAddon,character-method} -\alias{disableAddon,SsimLibrary-method} -\title{Disable addon package(s)} -\usage{ -disableAddon(ssimLibrary, name) - -\S4method{disableAddon}{character}(ssimLibrary, name) - -\S4method{disableAddon}{SsimLibrary}(ssimLibrary, name) -} -\arguments{ -\item{ssimLibrary}{\code{\link{SsimLibrary}} object} - -\item{name}{character string or vector of addon name(s)} -} -\value{ -This function invisibly returns \code{TRUE} upon success (i.e.successful deactivation -of the addon) or \code{FALSE} upon failure. -} -\description{ -Disable \code{\link{addon}} package(s) of a \code{\link{SsimLibrary}}. -} -\examples{ -\donttest{ -# Install "stsim" SyncroSim package -installPackage("stsim") - -# Specify file path and name of new SsimLibrary -myLibraryName <- file.path(tempdir(), "testlib") - -# Set up a SyncroSim Session, SsimLibrary, and Project -mySession <- session() -myLibrary <- ssimLibrary(name = myLibraryName, session = mySession, - package = "stsim") - -# Enable addon package -enableAddon(myLibrary, c("stsimsf")) -addon(myLibrary) - -# Disable addon package -disableAddon(myLibrary, c("stsimsf")) -addon(myLibrary) -} - -} -\seealso{ -\code{\link{addon}} -} diff --git a/man/enableAddon.Rd b/man/enableAddon.Rd deleted file mode 100644 index f59b01a7..00000000 --- a/man/enableAddon.Rd +++ /dev/null @@ -1,52 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/enableAddon.R -\name{enableAddon} -\alias{enableAddon} -\alias{enableAddon,character-method} -\alias{enableAddon,SsimLibrary-method} -\title{Enable addon package(s)} -\usage{ -enableAddon(ssimLibrary, name) - -\S4method{enableAddon}{character}(ssimLibrary, name) - -\S4method{enableAddon}{SsimLibrary}(ssimLibrary, name) -} -\arguments{ -\item{ssimLibrary}{\code{\link{SsimLibrary}} object} - -\item{name}{character string or vector of addon name(s)} -} -\value{ -Invisibly returns \code{TRUE} upon success (i.e.successful activation -of the addon) or \code{FALSE} upon failure. -} -\description{ -Enable \code{\link{addon}} package(s) of a \code{\link{SsimLibrary}}. -} -\examples{ -\donttest{ -# Install "stsim" SyncroSim package -installPackage("stsim") - -# Specify file path and name of new SsimLibrary -myLibraryName <- file.path(tempdir(), "testlib") - -# Set up a SyncroSim Session, SsimLibrary, and Project -mySession <- session() -myLibrary <- ssimLibrary(name = myLibraryName, session = mySession, - package = "stsim") - -# Enable add on package -enableAddon(myLibrary, c("stsimsf")) -addon(myLibrary) - -# Disable add on package -disableAddon(myLibrary, c("stsimsf")) -addon(myLibrary) -} - -} -\seealso{ -\code{\link{addon}} -} diff --git a/man/removePackage.Rd b/man/removePackage.Rd new file mode 100644 index 00000000..976899a0 --- /dev/null +++ b/man/removePackage.Rd @@ -0,0 +1,52 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/removePackage.R +\name{removePackage} +\alias{removePackage} +\alias{removePackage,character-method} +\alias{removePackage,SsimLibrary-method} +\title{Removes SyncroSim package(s)} +\usage{ +removePackage(ssimLibrary, name) + +\S4method{removePackage}{character}(ssimLibrary, name) + +\S4method{removePackage}{SsimLibrary}(ssimLibrary, name) +} +\arguments{ +\item{ssimLibrary}{\code{\link{SsimLibrary}} object} + +\item{name}{character string or vector of package name(s)} +} +\value{ +This function invisibly returns \code{TRUE} upon success (i.e.successful +removal of the package) or \code{FALSE} upon failure. +} +\description{ +Removes package(s) from a \code{\link{SsimLibrary}}. +} +\examples{ +\donttest{ +# Install "stsim" SyncroSim package +installPackage("stsim") + +# Specify file path and name of new SsimLibrary +myLibraryName <- file.path(tempdir(), "testlib") + +# Set up a SyncroSim Session, SsimLibrary, and Project +mySession <- session() +myLibrary <- ssimLibrary(name = myLibraryName, session = mySession, + package = "stsim") + +# Add package +addPackage(myLibrary, c("stsimsf")) +packages(myLibrary) + +# Remove package +removePackage(myLibrary, c("stsimsf")) +packages(myLibrary) +} + +} +\seealso{ +\code{\link{packages}} +} diff --git a/tests/testthat/test-unit.R b/tests/testthat/test-unit.R index 9f6b59e4..9894d354 100644 --- a/tests/testthat/test-unit.R +++ b/tests/testthat/test-unit.R @@ -54,26 +54,9 @@ test_that("Tests of Library - assumes SyncroSim is installed", { expect_equal(file.exists(filepath(myLibrary)), TRUE) expect_equal(name(myLibrary), "SSimLibrary") - # With addons - expect_equal(nrow(subset(addon(myLibrary), enabled)), 0) - allAdds <- addon(myLibrary) - expect_equal(names(allAdds), c("name", "description", "enabled", "currentVersion", "minimumVersion")) - expect_equal(names(addon(mySsim)), c("name", "description", "version", "extends")) + # Test delete expect_equal(delete(myLibrary, force = TRUE), TRUE) - allAdds <- subset(allAdds, name != "stsim-cbm") # Requires stsim-stockflow to be added first - - if (nrow(allAdds) > 0) { - cAdd <- allAdds$name[1] - # TODO Fails to detect stsimsf is installed => potential bug with console - myLibrary <- ssimLibrary(name = "NewLibrary", addon = c(cAdd), session = mySsim) - expect_equal(subset(addon(myLibrary), enabled)$name, cAdd) - expect_equal(disableAddon(myLibrary, cAdd)[[cAdd]], TRUE) - expect_equal(nrow(subset(addon(myLibrary), enabled)), 0) - expect_equal(enableAddon(myLibrary, cAdd)[[cAdd]], TRUE) - expect_equal(subset(addon(myLibrary), enabled)$name, cAdd) - } - # Get/set the various properties of the library expect_is("session<-"(myLibrary, mySsim), "SsimLibrary")