-
Notifications
You must be signed in to change notification settings - Fork 1
/
reload
executable file
·98 lines (81 loc) · 2.33 KB
/
reload
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
#!/usr/bin/env Rscript
find_root <- function(args = commandArgs()) {
re <- "^--file=(.+)$"
i <- grep(re, args)
stopifnot(length(i) >= 1)
path_script <- normalizePath(sub(re, "\\1", args[[i[[1]]]]), mustWork = TRUE)
dirname(path_script)
}
reload <- function(root) {
paths <- dirname(list.files(root, recursive = TRUE, pattern = "^app.R$"))
ok <- file.create(file.path(root, paths, "restart.txt"))
if (any(ok)) {
message(sprintf("Reloaded %d apps:\n%s",
sum(ok), paste(" -", paths[ok], collapse = "\n")))
}
if (any(!ok)) {
message(sprintf("Failed to reload %d apps:\n%s",
sum(!ok), paste(" -", paths[!ok], collapse = "\n")))
}
}
clear <- function(root) {
paths <- list.files(root, recursive = TRUE, pattern = "^restart.txt$",
full.names = TRUE)
unlink(paths)
message(sprintf("Removed %d reload files", length(paths)))
}
parse_args <- function(args) {
if (length(args) == 0) {
clear <- FALSE
} else if (identical(args, "--clear")) {
clear <- TRUE
} else {
stop("Usage:\n reload [--clear]")
}
list(clear = clear)
}
die <- function(msg) {
message(msg)
q(save = "no", status = 1)
}
prepare <- function(root, file) {
if (file.exists(file.path(root, file))) {
paths <- dirname(list.files(root, recursive = TRUE, pattern = "^app.R$"))
n <- lengths(strsplit(paths, "/"))
for (p in paths) {
n <- length(strsplit(p, "/")[[1]])
from <- file.path(paste(rep("..", n), collapse = "/"), file)
to <- file.path(root, p, file)
if (!file.exists(to)) {
message(sprintf("Creating %s for %s", file, p))
file.symlink(from, to)
}
}
}
}
staff_pages <- function(root) {
files <- c("week1.html", "week2.html")
message("Writing staff pages")
for (f in files) {
txt <- readLines(file.path(root, f))
writeLines(txt[!grepl("static/after.js", txt, fixed = TRUE)],
file.path(root, sub(".html$", "-staff.html", f)))
}
}
main <- function(args = commandArgs(TRUE), root = find_root()) {
opts <- tryCatch(
parse_args(args),
error = function(e) die(e$message))
message("Working in ", root)
if (opts$clear) {
clear(root)
} else {
reload(root)
prepare(root, ".lib")
prepare(root, ".Renviron")
staff_pages(root)
}
}
if (!interactive()) {
main()
}