Skip to content

Commit

Permalink
Merge pull request #19 from KWB-R/support-unix
Browse files Browse the repository at this point in the history
Support unix
  • Loading branch information
hsonne authored Sep 27, 2023
2 parents 5c91d97 + b9b8a36 commit 0d75c4d
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
matrix:
config:
- {os: macOS-latest, r: 'release'}
- {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
- {os: ubuntu-latest, r: 'release'}
- {os: windows-latest, r: 'devel'}
- {os: windows-latest, r: 'oldrel'}
- {os: windows-latest, r: 'release'}
Expand Down
51 changes: 45 additions & 6 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,31 @@ NULL
# abimo_binary -----------------------------------------------------------------
abimo_binary <- function(tag = latest_abimo_version())
{
file.path(extdata_file(), paste0("abimo_", tag, "_win64"), "Abimo.exe")
name <- "Abimo"

executables <- list(
Windows = paste0(name, ".exe"),
Linux = name,
Darwin = paste0(name, ".app/Contents/MacOS/GNUSparseFile.0/", name)
)

file.path(
extdata_file(),
paste0("abimo_", tag, "_", get_architecture_suffix()),
kwb.utils::selectElements(executables, get_os_type())
)
}

# abimo_help -------------------------------------------------------------------
abimo_help <- function()
abimo_help <- function(...)
{
run_abimo_command_line("--help")
run_abimo_command_line("--help", ...)
}

# abimo_version ----------------------------------------------------------------
abimo_version <- function()
abimo_version <- function(...)
{
run_abimo_command_line("--version")
run_abimo_command_line("--version", ...)
}

# appendSubToFile --------------------------------------------------------------
Expand All @@ -41,6 +53,24 @@ appendSubToFile <- function (filename)
writeBin(as.raw(0x1A), con)
}

# check_abimo_binary -----------------------------------------------------------
check_abimo_binary <- function(tag = latest_abimo_version())
{
file <- abimo_binary(tag)

if (!file.exists(file)) {
install_abimo(tag)
}

if (!file.exists(file)) {
kwb.utils::stopFormatted(
"Could not install or find Abimo (no such file: %s)", file
)
}

file
}

# default_config -----------------------------------------------------------------

#' Default ABIMO config.xml path
Expand Down Expand Up @@ -97,7 +127,16 @@ latest_abimo_version <- function()
#' @export
run_abimo_command_line <- function(args, tag = latest_abimo_version())
{
output <- system2(abimo_binary(tag), args = args, stdout = TRUE)
command <- check_abimo_binary(tag)

output <- try(system2(command, args = args, stdout = TRUE))

if (kwb.utils::isTryError(output)) {
stop(
"system2() failed. Files below ", dirname(command), ":\n",
paste(dir(path, recursive = TRUE), collapse = "\n")
)
}

output
}
Expand Down
18 changes: 15 additions & 3 deletions R/install_abimo.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@

#' @importFrom archive archive_extract
#' @importFrom kwb.utils catAndRun createDirectory
install_abimo <- function(tag = latest_abimo_version(), arch = "win64", ...)
install_abimo <- function(
tag = latest_abimo_version(),
arch = get_architecture_suffix(),
...
)
{
if (arch != "win64") {
stop("Currently, the abimo executable is only available for win64")
expected_architectures <- c("windows", "linux", "macos")

if (!arch %in% expected_architectures) {
stop(
"Currently, the abimo executable is only available for one of these ",
"'architectures': ",
kwb.utils::stringList(expected_architectures),
call. = FALSE

)
}

exdir <- dirname(abimo_binary(tag))
Expand Down
18 changes: 1 addition & 17 deletions R/run_abimo.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ run_abimo <- function(
config_file <- config$save(file = file.path(tempdir(), file_name))
}

if (!check_abimo_binary(tag)) {
stop("Could not install Abimo!")
}

output_file <- kwb.utils::defaultIfNULL(
output_file, default_output_file(input_file)
)
Expand All @@ -72,18 +68,6 @@ run_abimo <- function(
foreign::read.dbf(output_file)
}

# check_abimo_binary -----------------------------------------------------------
check_abimo_binary <- function(tag = latest_abimo_version())
{
file <- abimo_binary(tag)

if (file.exists(file)) {
return(TRUE)
}

file.exists(install_abimo(tag))
}

# default_output_file ----------------------------------------------------------

#' @importFrom kwb.utils replaceFileExtension
Expand All @@ -99,7 +83,7 @@ full_quoted_path <- function(x)
x <- path.expand(x)

# Convert slashes to backslashes on windows
if (Sys.info()["sysname"] == "Windows") {
if (on_windows()) {
x <- kwb.utils::windowsPath(x)
}

Expand Down
40 changes: 40 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,43 @@
#' @inheritParams kwb.utils::extdataFile
#' @export
extdata_file <- kwb.utils::createFunctionExtdataFile("kwb.abimo")

# get_architecture_suffix ------------------------------------------------------
get_architecture_suffix <- function()
{
suffixes <- list(
Linux = "linux",
Darwin = "macos",
Windows = "windows"
)

kwb.utils::selectElements(suffixes, get_os_type())
}

# get_os_type ------------------------------------------------------------------
get_os_type <- function()
{
os_type <- Sys.info()[["sysname"]]

stopifnot(os_type %in% c("Windows", "Linux", "Darwin"))

os_type
}

# on_linux ---------------------------------------------------------------------
on_linux <- function()
{
get_os_type() == "Linux"
}

# on_macos ---------------------------------------------------------------------
on_macos <- function()
{
get_os_type() == "Darwin"
}

# on_windows -------------------------------------------------------------------
on_windows <- function()
{
get_os_type() == "Windows"
}
23 changes: 19 additions & 4 deletions vignettes/running-abimo.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,33 @@ editor_options:
---

```{r, include = FALSE}
is_windows <- .Platform$OS.type == "windows"
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
eval = is_windows
eval = TRUE # kwb.abimo:::on_windows()
)
is_windows <- .Platform$OS.type == "windows"
```

This vignette aims at demonstrating how to work with the kwb.abimo package.

## Install Abimo and check the version

```{r}
tag <- kwb.abimo:::latest_abimo_version()
```

```{r}
command <- kwb.abimo:::check_abimo_binary(tag)
```

```{r}
output <- try(system2(command, args = "--version", stdout = TRUE))
```

```{r install-abimo}
kwb.abimo:::abimo_version()
```

## Basic model run

```{r basic-run}
Expand Down

0 comments on commit 0d75c4d

Please sign in to comment.