File: roxygenize-setup.R

package info (click to toggle)
r-cran-roxygen2 7.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,940 kB
  • sloc: cpp: 351; sh: 14; makefile: 2
file content (90 lines) | stat: -rw-r--r-- 2,487 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
80
81
82
83
84
85
86
87
88
89
90
roxygen_setup <- function(path = ".",
                          cur_version = NULL,
                          frame = caller_env()) {
  if (!file.exists(file.path(path, "DESCRIPTION"))) {
    cli::cli_abort(
      "{.arg package.dir} ({.path {path}}) does not contain a DESCRIPTION"
    )
  }

  is_first <- first_time(path)
  if (is_first) {
    cli::cli_inform("First time using {.pkg roxygen2}. Upgrading automatically...")
  }

  update_roxygen_version(path, cur_version = cur_version)

  encoding <- desc::desc_get("Encoding", path)[[1]]
  if (!identical(encoding, "UTF-8")) {
    cli::cli_inform(c(
      x = "{.pkg roxygen2} requires {.val Encoding: UTF-8}",
      i = "Current encoding is {.val {encoding}}"
    ))
  }

  man_path <- file.path(path, "man")
  dir.create(man_path, recursive = TRUE, showWarnings = FALSE)

  withr::local_envvar(
    ROXYGEN_PKG = desc::desc_get("Package", path),
    .local_envir = frame
  )

  is_first
}

peek_roxygen_pkg <- function() {
  pkg <- Sys.getenv("ROXYGEN_PKG")

  if (nzchar(pkg)) {
    pkg
  } else {
    NULL
  }
}

update_roxygen_version <- function(path, cur_version = NULL) {
  cur <- cur_version %||% as.character(utils::packageVersion("roxygen2"))
  prev <- roxygen_version(path)

  if (!is.na(cur) && !is.na(prev) && package_version(cur) < package_version(prev)) {
    cli::cli_inform(c(
      x = "Installed {.pkg roxygen2} is older than the version used with this package",
      i = "You have {.val {cur}} but you need {.val {prev}}"
    ))
  } else if (!identical(cur, prev)) {
    if (!is.na(prev) && numeric_version(prev) <= "6.1.99") {
      cli::cli_rule()
      cli::cli_inform(c(
        "Changes in {.pkg roxygen2} 7.0.0:",
        "* `%` is now escaped automatically in Markdown mode.",
        "Please carefully check .Rd files for changes"
      ))
      cli::cli_rule()
    }

    cli::cli_inform(c(i = "Setting {.field RoxygenNote} to {.val {cur}}"))
    desc::desc_set(RoxygenNote = cur, file = path)
  }
}

first_time <- function(path) {
  if (!is.na(roxygen_version(path))) {
    return(FALSE)
  }

  generated <- dir(file.path(path, "man"), full.names = TRUE)
  generated <- generated[!file.info(generated)$isdir]

  namespace <- file.path(path, "NAMESPACE")
  if (file.exists(namespace)) {
    generated <- c(generated, namespace)
  }

  roxy <- map_lgl(generated, made_by_roxygen)
  all(!roxy)
}

roxygen_version <- function(path = ".") {
  stringr::str_trim(desc::desc_get("RoxygenNote", path)[[1]])
}