forked from iNZightVIT/addons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_index.R
79 lines (73 loc) · 2.24 KB
/
create_index.R
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
# creates an index of the modules
`%||%` <- function(a, b)
if (is.null(a)) b else a
## This comes from iNZightModules :
getModules <- function(dir) {
mods <- list.files(dir, pattern = "*.R", full.names = TRUE)
# mods <- mods[!grepl("DemoModule", mods)]
mod_list <- lapply(mods, getmodule)
mod_list <- mod_list[!sapply(mod_list, is.null)]
names(mod_list) <- sapply(mod_list, function(x) x$name)
mod_list
}
getmodule <- function(f) {
## check if file is a Module
t <- readLines(f)
mi <- grep("^#'", t)
meta <- NULL
if (length(mi)) {
meta <- parse_meta(t[mi])
t <- t[-mi]
}
t <- paste(collapse = "\n", t)
if (!grepl("^[a-zA-Z]+[a-zA-Z0-9]*\\s*<-\\s*setRefClass", t)) return(NULL)
# ## load module into an environment to avoid clashes
e <- new.env()
# eval(parse(text = t), e)
# ## fetch the module's name
# objs <- ls(e)
# obj <- objs[which(sapply(objs, function(o) {
# ob <- e[[o]]
# pclass <- try(ob@generator$def@contains$refClass@by, silent = TRUE)
# if (inherits(pclass, "try-error")) return(FALSE)
# pclass == "CustomModule"
# }))]
# if (length(obj) != 1) {
# warning("Couldn't find module class.")
# return(NULL)
# }
# e$name <- obj
# e$display_name <- e[[obj]]@className[1]
e$meta <- meta
# e$module <- e[[obj]]
e$path <- f
e
}
parse_meta <- function(x) {
# remove comment
x <- gsub("^#' ", "", x)
m <- regexpr("^@[a-zA-Z]+", x)
names <- substr(x, m + 1, attr(m, "match.length"))
values <- substr(x, m + attr(m, "match.length") + 1, nchar(x))
names(values) <- names
as.list(values)
}
## ***
create_index <- function() {
mods <- getModules(".")
tbl <- lapply(mods,
function(mod) {
as.data.frame(
list(
Name = mod$meta$name %||% basename(mod$path),
Version = numeric_version(mod$meta$version %||% 0),
Description = mod$meta$desc %||% "",
Author = mod$meta$author %||% "",
filename = gsub("^\\.\\/", "", mod$path)
),
stringsAsFactors = FALSE
)
}
)
do.call(rbind, tbl)
}