File: utils-io.R

package info (click to toggle)
r-cran-reprex 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 1,584 kB
  • sloc: sh: 13; makefile: 2
file content (135 lines) | stat: -rw-r--r-- 3,275 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
readLines <- function(...) {
  stop("In this house, we use read_lines() for UTF-8 reasons.")
}

writeLines <- function(...) {
  stop("In this house, we use write_lines() for UTF-8 reasons.")
}

read_lines <- function(path, n = -1L) {
  if (is.null(path)) return(NULL)
  base::readLines(path, n = n, encoding = "UTF-8", warn = FALSE)
}

write_lines <- function(text, path, sep = "\n") {
  path <- file(path, open = "wb")
  on.exit(close(path), add = TRUE)
  base::writeLines(enc2utf8(text), con = path, sep = sep, useBytes = TRUE)
}

reprex_default_filebase <- function(in_temp_dir) {
  if (in_temp_dir) {
    # outfile is NULL --> reprex in sub-directory, within session temp directory
    # example: /private/var/.../.../.../reprex97d77de2835c/reprex
    target_dir <- path_real(dir_create(file_temp("reprex")))
    path(target_dir, "reprex")
  } else {
    # infile = NULL, outfile is NA --> reprex in working directory
    # TODO: I'd love to devise a better way to express "work in current working
    # directory", i.e. I've concluded that `outfile = NA` is yucky
    # example: reprexbfa165580676
    path_file(file_temp("reprex"))
  }
}

make_filebase <- function(outfile = NULL, infile = NULL) {
  if (is.null(outfile)) {
    return(reprex_default_filebase(in_temp_dir = TRUE))
  }

  if (!is.na(outfile)) {
    return(path_ext_remove(outfile))
  }

  if (is.null(infile)) {
    return(reprex_default_filebase(in_temp_dir = FALSE))
  } else {
    ## outfile = NA, infile = "sthg" --> follow infile's lead
    ## example: basename_of_infile
    ## example: path/to/infile
    path_ext_remove(infile)
  }
}

add_suffix <- function(x, suffix = "reprex") {
  orig_ext <- path_ext(x)
  filebase <- path_ext_remove(x)
  if (nzchar(suffix)) {
    filebase <- paste0(filebase, "_", suffix)
  }
  path_ext_set(filebase, orig_ext)
}

path_mutate <- function(path, suffix = "", ext = NULL) {
  if (nzchar(suffix)) {
    path <- add_suffix(path, suffix)
  }
  if (!is.null(ext)) {
    path <- path_ext_set(path, ext)
  }
  path
}

r_file <- function(path) {
  path_mutate(path, suffix = "reprex", ext = "R")
}

r_file_clean <- function(path) {
  path_mutate(path, suffix = "clean", ext = "R")
}

r_file_rendered <- function(path) {
  path_mutate(path, suffix = "rendered", ext = "R")
}

md_file <- function(path) {
  path_mutate(path, ext = "md")
}

md_file_slack <- function(path) {
  path_mutate(path, suffix = "slack", ext = "md")
}

std_file <- function(path) {
  path_mutate(path, suffix = "std_out_err", ext = "txt")
}

html_file <- function(path) {
  path_mutate(path, suffix = "reprex", ext = "html")
}

rtf_file <- function(path) {
  path_mutate(path, ext = "rtf")
}

rmd_file <- function(path) {
  path_mutate(path, suffix = "reprex", ext = "Rmd")
}

force_tempdir <- function(path) {
  if (is_in_tempdir(path)) {
    path
  } else {
    file_copy(path, path_temp(path_file(path)), overwrite = TRUE)
  }
}

is_in_tempdir <- function(path) {
  identical(
    path_real(path_temp()),
    path_common(path_real(c(path, path_temp())))
  )
}

would_clobber <- function(path) {
  if (!file_exists(path)) {
    return(FALSE)
  }
  if (!is_interactive()) {
    return(TRUE)
  }
  nope(
    "Oops, file already exists:\n  * ", path, "\n",
    "Carry on and overwrite it?"
  )
}