|
9 | 9 | #' @export |
10 | 10 | #' |
11 | 11 | #' @param path (string) The full file path to the CmdStan installation. If |
12 | | -#' `NULL` (the default) then the path is set to the default path used by |
| 12 | +#' `NULL` (the default) then the path is set using the `"CMDSTAN"` |
| 13 | +#' environment variable when available, otherwise the default path used by |
13 | 14 | #' [install_cmdstan()] if it exists. |
14 | 15 | #' @return A string. Either the file path to the CmdStan installation or the |
15 | 16 | #' CmdStan version number. |
|
39 | 40 | #' |
40 | 41 | set_cmdstan_path <- function(path = NULL) { |
41 | 42 | if (is.null(path)) { |
42 | | - path <- cmdstan_default_path() |
| 43 | + env_path <- Sys.getenv("CMDSTAN") |
| 44 | + path <- if (nzchar(env_path)) env_path else cmdstan_default_path() |
43 | 45 | } |
44 | 46 | if (dir.exists(path)) { |
45 | 47 | path <- absolute_path(path) |
@@ -219,22 +221,56 @@ cmdstan_default_path <- function(dir = NULL) { |
219 | 221 | latest_cmdstan |
220 | 222 | } |
221 | 223 |
|
| 224 | +is_wsl_unc_path <- function(path) { |
| 225 | + is.character(path) && |
| 226 | + length(path) == 1 && |
| 227 | + !is.na(path) && |
| 228 | + startsWith(repair_path(path), "//wsl$/") |
| 229 | +} |
| 230 | + |
| 231 | +cmdstan_version_from_path <- function(path) { |
| 232 | + path <- repair_path(path) |
| 233 | + match <- regmatches( |
| 234 | + path, |
| 235 | + regexpr("cmdstan-[0-9]+\\.[0-9]+\\.[0-9]+(?:-rc[0-9]+)?$", path) |
| 236 | + ) |
| 237 | + if (!length(match) || is.na(match) || !nzchar(match)) { |
| 238 | + return(NULL) |
| 239 | + } |
| 240 | + sub("^cmdstan-", "", match) |
| 241 | +} |
| 242 | + |
222 | 243 |
|
223 | 244 | #' Find the version of CmdStan from makefile |
224 | 245 | #' @noRd |
225 | 246 | #' @param path Path to installation. |
226 | 247 | #' @return Version number as a string. |
227 | 248 | read_cmdstan_version <- function(path) { |
228 | 249 | makefile_path <- file.path(path, "makefile") |
229 | | - if (!file.exists(makefile_path)) { |
| 250 | + if (is_wsl_unc_path(path)) { |
| 251 | + makefile <- tryCatch( |
| 252 | + suppressWarnings(readLines(makefile_path, warn = FALSE)), |
| 253 | + error = function(e) NULL |
| 254 | + ) |
| 255 | + } else if (file.exists(makefile_path)) { |
| 256 | + makefile <- readLines(makefile_path) |
| 257 | + } else { |
| 258 | + makefile <- NULL |
| 259 | + } |
| 260 | + if (is.null(makefile)) { |
| 261 | + if (is_wsl_unc_path(path)) { |
| 262 | + version_from_path <- cmdstan_version_from_path(path) |
| 263 | + if (!is.null(version_from_path)) { |
| 264 | + return(version_from_path) |
| 265 | + } |
| 266 | + } |
230 | 267 | warning( |
231 | 268 | "Can't find CmdStan makefile to detect version number. ", |
232 | 269 | "Path may not point to valid installation.", |
233 | 270 | call. = FALSE |
234 | 271 | ) |
235 | 272 | return(NULL) |
236 | 273 | } |
237 | | - makefile <- readLines(makefile_path) |
238 | 274 | version_line <- grep("^CMDSTAN_VERSION :=", makefile, value = TRUE) |
239 | 275 | if (length(version_line) == 0) { |
240 | 276 | stop("CmdStan makefile is missing a version number.", call. = FALSE) |
|
0 commit comments