1
###########################################################################/**
2
# @RdocDefault findAsciiDoc
3
#
4
# @title "Locates the asciidoc executable"
5
#
6
# \description{
7
#  @get "title" on the current system.
8
# }
9
#
10
# @synopsis
11
#
12
# \arguments{
13
#   \item{mustExist}{If @TRUE, an exception is thrown if the executable
14
#      could not be located.}
15
#   \item{...}{Not used.}
16
#   \item{verbose}{See @see "R.utils::Verbose".}
17
# }
18
#
19
# \value{
20
#   Returns the pathname to the executable, or @NULL if not found.
21
# }
22
#
23
# \details{
24
#  The 'asciidoc' executable is searched for as follows:
25
#  \enumerate{
26
#   \item \code{Sys.which("asciidoc")}
27
#  }
28
# }
29
#
30
# @author
31
#
32
# @keyword file
33
# @keyword IO
34
# @keyword internal
35
#*/###########################################################################
36
setMethodS3("findAsciiDoc", "default", function(mustExist=TRUE, ..., verbose=FALSE) {
37
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
38
  # Validate arguments
39
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
40
  # Argument 'mustExist':
41 1
  mustExist <- Arguments$getLogical(mustExist)
42

43
  # Argument 'verbose':
44 1
  verbose <- Arguments$getVerbose(verbose)
45 1
  if (verbose) {
46 0
    pushState(verbose)
47 0
    on.exit(popState(verbose))
48
  }
49

50 1
  command <- "asciidoc"
51

52 1
  verbose && enter(verbose, "Locating external software")
53 1
  verbose && cat(verbose, "Command: ", command)
54

55 1
  bin <- Sys.getenv("R_ASCIIDOC")
56 1
  if (identical(bin, "")) bin <- Sys.which(command)
57 0
  if (identical(bin, "")) bin <- NULL
58 1
  if (!isFile(bin)) bin <- NULL
59

60 1
  verbose && cat(verbose, "Located pathname: ", bin)
61

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

66
  # Validate by retrieving version information
67 1
  if (isFile(bin)) {
68 0
    output <- tryCatch({
69 0
      system2(bin, args="--version", stdout=TRUE)
70 0
    }, error = function(ex) {
71 0
      NULL
72
    })
73

74 0
    if (!is.null(output)) {
75 0
      name <- "asciidoc"
76 0
      ver <- trim(gsub("asciidoc", "", output))
77
      
78
      ## No matching output?
79 0
      if (length(ver) == 0) {
80 0
        stop(sprintf("Failed to infer version of %s based on captured output: ", sQuote(name), paste(dQuote(output), collapse=", ")))
81
      }
82

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

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

95 0
      attr(bin, "version") <- ver
96
    }
97
  }
98

99 1
  verbose && exit(verbose)
100

101 1
  bin
102
}) # findAsciiDoc()

Read our documentation on viewing source code .

Loading