File: findAsciiDoc.R

package info (click to toggle)
r-cran-r.rsp 0.46.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,472 kB
  • sloc: javascript: 612; tcl: 304; sh: 18; makefile: 16
file content (105 lines) | stat: -rw-r--r-- 2,835 bytes parent folder | download | duplicates (2)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
###########################################################################/**
# @RdocDefault findAsciiDoc
#
# @title "Locates the asciidoc executable"
#
# \description{
#  @get "title" on the current system.
# }
#
# @synopsis
#
# \arguments{
#   \item{mustExist}{If @TRUE, an exception is thrown if the executable
#      could not be located.}
#   \item{...}{Not used.}
#   \item{verbose}{See @see "R.utils::Verbose".}
# }
#
# \value{
#   Returns the pathname to the executable, or @NULL if not found.
# }
#
# \details{
#  The 'asciidoc' executable is searched for as follows:
#  \enumerate{
#   \item \code{Sys.which("asciidoc")}
#  }
# }
#
# @author
#
# @keyword file
# @keyword IO
# @keyword internal
#*/###########################################################################
setMethodS3("findAsciiDoc", "default", function(mustExist=TRUE, ..., verbose=FALSE) {
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Validate arguments
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Argument 'mustExist':
  mustExist <- Arguments$getLogical(mustExist)

  # Argument 'verbose':
  verbose <- Arguments$getVerbose(verbose)
  if (verbose) {
    pushState(verbose)
    on.exit(popState(verbose))
  }

  command <- "asciidoc"

  verbose && enter(verbose, "Locating external software")
  verbose && cat(verbose, "Command: ", command)

  bin <- Sys.getenv("R_ASCIIDOC")
  if (identical(bin, "")) bin <- Sys.which(command)
  if (identical(bin, "")) bin <- NULL
  if (!isFile(bin)) bin <- NULL

  verbose && cat(verbose, "Located pathname: ", bin)

  if (mustExist && !isFile(bin)) {
    throw(sprintf("Failed to located external executable: '%s'", command))
  }

  # Validate by retrieving version information
  if (isFile(bin)) {
    output <- tryCatch({
      system2(bin, args="--version", stdout=TRUE)
    }, error = function(ex) {
      NULL
    })

    if (!is.null(output)) {
      name <- "asciidoc"
      ver <- gsub("asciidoc", "", output)
      ver <- trim(ver)
      ver <- gsub("rc.?$", "", ver)        ## e.g. asciidoc 9.0.0rc1
      ver <- trim(ver)
      
      ## No matching output?
      if (length(ver) == 0) {
        stop(sprintf("Failed to infer version of %s based on captured output: %s", sQuote(name), paste(dQuote(output), collapse=", ")))
      }

      ## Try to coerce to version objects
      ver <- numeric_version(ver, strict = FALSE)
      ver <- ver[!is.na(ver)]
      
      ## Failed to coerce?
      if (length(ver) == 0) {
        stop(sprintf("Failed to parse version of %s based on captured output: ", sQuote(name), paste(dQuote(output), collapse=", ")))
      }

      ## If more than one match, use the first one
      ver <- ver[[1]]

      attr(bin, "version") <- ver
    }
  }

  verbose && exit(verbose)

  bin
}) # findAsciiDoc()