~hrbrmstr/cloc

cloc/R/cloc-recognized-languages.r -rw-r--r-- 2.1 KiB
0350958fhrbrmstr Working on #10 2 years ago
                                                                                
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
#' Return a data frame of `cloc` recognized languages and associated extensions
#'
#' Some file extensions map to multiple languages:
#'
#' - `.cl` files could be Lisp or OpenCL
#' - `.d` files could be D or dtrace
#' - `.f` or `.for` files could be Fortran or Forth
#' - `.fs` files could be Forth or F#
#' - `.inc` files could be PHP or Pascal
#' - `.jl` files could be Lisp or Julia
#' - `.m` files could be MATLAB, Mathematica, Mercury, MUMPS, or Objective C
#' - `.pl` files could be Perl or Prolog
#' - `.pp` files could be Pascal or Puppet
#' - `.pro` files could be IDL, Prolog, or a Qt Project
#' - `.ts` files could be TypeScript or Qt Linguist
#' - `.v` files Coq or Verilog/SystemVerilog
#'
#' `cloc` has subroutines that attempt to identify the correct language based
#' on the file's contents for these special cases. Language identification
#' accuracy is a function of how much code the file contains; .m files with
#' just one or two lines for example, seldom have enough information to
#' correctly distinguish between MATLAB, Mercury, MUMPS, or Objective C.
#' @md
#' @return tibble
#' @export
#' @examples
#' cloc_recognized_languages()
cloc_recognized_languages <- function() {

  perl <- Sys.which("perl")

  if (perl == "") {
    stop(
      "Cannot find 'perl'. cloc requires perl to be installed and on the PATH.",
      call. = FALSE
    )
  }

  c(
    system.file("bin/cloc.pl", package = "cloc"),
    "--show-lang"
  ) -> args

  processx::run(
    command = perl,
    args = args
  ) -> res

  dat <- res$stdout

  dat <- unlist(strsplit(dat, "\r?\n"))

  # sprintf(
  #   "%s %s --show-lang",
  #   perl,
  #   shQuote(system.file("bin/cloc.pl", package = "cloc")),
  #   source
  # ) -> cmd
  #
  # dat <- system(cmd, intern = TRUE)

  do.call(rbind.data.frame,
    lapply(
      strsplit(dat, "\\("),
      function(.x) {
        lang <- trimws(.x[1])
        extensions <- trimws(.x[2])
        extensions <- sub("\\)", "", extensions)
        data.frame(
          lang = lang,
          extensions = extensions,
          stringsAsFactors = FALSE
        )
      }
    )
  ) -> out

  class(out) <- c("tbl_df", "tbl", "data.frame")

  out

}