R/safety.R

first_time <- function(path) {
  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 <- vapply(generated, made_by_roxygen, logical(1))
  all(!roxy)
}

made_by_roxygen <- function(path) {
  if (!file.exists(path)) return(TRUE)

  first <- readLines(path, n = 1)
  check_made_by(first)
}

add_made_by_roxygen <- function(path, comment) {
  if (!file.exists(path)) stop("Can't find ", path, call. = FALSE)

  lines <- readLines(path, warn = FALSE)
  if (check_made_by(lines[1])) return()

  writeLines(c(made_by(comment), lines), path)
}

check_made_by <- function(first) {
  if (length(first) == 0L) return(FALSE)
  grepl("^. Generated by roxydoxy", first)
}

made_by <- function(comment) {
  paste0(comment, " Generated by roxydoxy: do not edit by hand\n")
}

update_roxygen_version <- function(base_path) {
  desc_path <- file.path(base_path, "DESCRIPTION")

  cur <- as.character(utils::packageVersion("roxydoxy"))
  prev <- desc::desc_get("RoxygenNote", file = desc_path)[[1]]

  if (!is.na(cur) && !is.na(prev) && package_version(cur) < package_version(prev)) {
    warning("Version of roxydoxy last used with this package is ", prev, ". ",
      " You only have version ", cur, call. = FALSE)
  } else if (!identical(cur, prev)) {
    message("Updating roxygen version in ", desc_path)
    desc::desc_set(RoxygenNote = cur, file = desc_path)
  }
}
klmr/roxydoxy documentation built on May 20, 2019, 4:09 p.m.