1proj_line_ending <- function() {
2  # First look in .Rproj file
3  proj_path <- proj_path(paste0(project_name(), ".Rproj"))
4  if (file_exists(proj_path)) {
5    config <- read_utf8(proj_path)
6
7    if (any(grepl("^LineEndingConversion: Posix", config))) {
8      return("\n")
9    } else if (any(grepl("^LineEndingConversion: Windows", config))) {
10      return("\r\n")
11    }
12  }
13
14  # Then try DESCRIPTION
15  desc_path <- proj_path("DESCRIPTION")
16  if (file_exists(desc_path)) {
17    return(detect_line_ending(desc_path))
18  }
19
20  # Then try any .R file
21  r_path <- proj_path("R")
22  if (dir_exists(r_path)) {
23    r_files <- dir_ls(r_path, regexp = "[.][rR]$")
24    if (length(r_files) > 0) {
25      return(detect_line_ending(r_files[[1]]))
26    }
27  }
28
29  # Then give up - this is used (for example), when writing the
30  # first file into the package
31  platform_line_ending()
32}
33
34platform_line_ending <- function() {
35  if (.Platform$OS.type == "windows") "\r\n" else "\n"
36}
37
38detect_line_ending <- function(path) {
39  samp <- suppressWarnings(readChar(path, nchars = 500))
40  if (isTRUE(grepl("\r\n", samp))) "\r\n" else "\n"
41}
42