File: appMetadata-quarto.R

package info (click to toggle)
r-cran-rsconnect 1.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,044 kB
  • sloc: python: 185; sh: 13; makefile: 5
file content (79 lines) | stat: -rw-r--r-- 1,962 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Called only when the content is known to be Quarto.
inferQuartoInfo <- function(metadata, appDir, appPrimaryDoc) {
  if (hasQuartoMetadata(metadata)) {
    return(list(
      version = metadata[["quarto_version"]],
      engines = metadata[["quarto_engines"]]
    ))
  }

  # If we don't yet have Quarto details, run quarto inspect ourselves
  inspect <- quartoInspect(
    appDir = appDir,
    appPrimaryDoc = appPrimaryDoc
  )
  list(
    version = inspect[["quarto"]][["version"]],
    engines = I(inspect[["engines"]])
  )
}

hasQuartoMetadata <- function(x) {
  !is.null(x$quarto_version)
}

# Run "quarto inspect" on the target and returns its output as a parsed object.
quartoInspect <- function(appDir = NULL, appPrimaryDoc = NULL) {
  # If "quarto inspect appDir" fails, we will try "quarto inspect
  # appPrimaryDoc", so that we can support single files as well as projects.
  quarto <- quarto_path()
  if (is.null(quarto)) {
    cli::cli_abort(c(
      "`quarto` not found.",
      i = "Check that it is installed and available on your {.envvar PATH}."
    ))
  }

  json <- suppressWarnings(
    system2(
      quarto, c("inspect", shQuote(appDir)),
      stdout = TRUE, stderr = TRUE
    )
  )
  status <- attr(json, "status")

  if (!is.null(status) && !is.null(appPrimaryDoc)) {
    json <- suppressWarnings(
      system2(
        quarto, c("inspect", shQuote(file.path(appDir, appPrimaryDoc))),
        stdout = TRUE, stderr = TRUE
      )
    )
    status <- attr(json, "status")
  }

  if (!is.null(status)) {
    cli::cli_abort(
      c(
        "Failed to run `quarto inspect` against your content:",
        json
      )
    )
  }
  jsonlite::fromJSON(json)
}

# inlined from quarto::quarto_path()
quarto_path <- function() {
  path_env <- Sys.getenv("QUARTO_PATH", unset = NA)
  if (is.na(path_env)) {
    path <- unname(Sys.which("quarto"))
    if (nzchar(path)) {
      path
    } else {
      NULL
    }
  } else {
    path_env
  }
}