forked from rich-iannone/splitr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils-resources.R
156 lines (123 loc) · 4.22 KB
/
utils-resources.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
github_resources <- function(repo,
branch = "master",
...) {
names_paths <- list(...)
names_paths <-
lapply(names_paths, function(x) {
class(x) <- "github_resource"
attr(x, "repo") <- repo
attr(x, "branch") <- branch
x
})
names_paths
}
web_resources <- function(...) {
names_paths <- list(...)
names_paths <-
lapply(names_paths, function(x) {
class(x) <- "web_resource"
x
})
names_paths
}
register_resources <- function(resource_list) {
if (length(resource_list) == 0) {
stop("The resource list must be populated with resources", call. = FALSE)
}
# Unlist by a single level if we have a list of lists where
# the first accessible element is of a recognized class
if (any(vapply(resource_list, is.list, FUN.VALUE = logical(1))) &&
(inherits(resource_list[[1]][[1]], "github_resource") |
inherits(resource_list[[1]][[1]], "web_resource"))) {
resource_list <- unlist(resource_list, recursive = FALSE)
}
# Stop function if any classes are not recognized
lapply(
resource_list, function(x) {
if (!(inherits(x, "github_resource") | inherits(x, "web_resource"))) {
stop("There is an unrecognized resource present", call. = FALSE)
}
}
)
# TODO: Stop function if the list isn't completely named
# TODO: Stop function if there are any duplicate names
# Determine which elements are a `github_resource`
github_resources <-
vapply(
resource_list, function(x) inherits(x, "github_resource"),
FUN.VALUE = logical(1),
USE.NAMES = FALSE
)
# Determine which elements are a `web_resource`
web_resources <-
vapply(
resource_list, function(x) inherits(x, "web_resource"),
FUN.VALUE = logical(1),
USE.NAMES = FALSE
)
# Obtain URIs for a `github_resource`s
resource_list[github_resources] <-
lapply(
resource_list[github_resources],
function(x) {
repo <- attr(x, "repo", exact = TRUE)
branch <- attr(x, "branch", exact = TRUE)
path <- as.character(x)
x[1] <- gh_path_resolver(path = path, repo = repo, branch = branch)
x
}
)
resource_list
}
gh_path_resolver <- function(path, repo, branch) {
file.path("https://raw.githubusercontent.com", repo, branch, path)
}
splitr_resources <-
register_resources(
resource_list =
github_resources(
repo = "rich-iannone/splitr",
branch = "master",
osx_hyts_std = file.path("extras", "osx", "hyts_std"),
osx_hycs_std = file.path("extras", "osx", "hycs_std"),
osx_parhplot = file.path("extras", "osx", "parhplot"),
win_hyts_std = file.path("extras", "win", "hyts_std.exe"),
win_hycs_std = file.path("extras", "win", "hycs_std.exe"),
win_parhplot = file.path("extras", "win", "parhplot.exe"),
l32_hyts_std = file.path("extras", "linux-x86", "hyts_std"),
l32_hycs_std = file.path("extras", "linux-x86", "hycs_std"),
l32_parhplot = file.path("extras", "linux-x86", "parhplot"),
l64_hyts_std = file.path("extras", "linux-amd64", "hyts_std"),
l64_hycs_std = file.path("extras", "linux-amd64", "hycs_std"),
l64_parhplot = file.path("extras", "linux-amd64", "parhplot")
)
)
fetch_resources <- function(resources,
names,
out_dir = NULL) {
if (is.null(out_dir)) {
out_dir <- getwd()
} else {
out_dir <- file.path(getwd(), out_dir)
}
resource_names <- names(resources)
for (name in names) {
if (name %in% resource_names) {
filename <- basename(resources[[name]])
downloader::download(
url = resources[[name]],
destfile = file.path(out_dir, filename)
)
}
}
}
get_resource_names_by_system_os <- function(system_os) {
if (system_os == "mac") {
resource_names <- c("osx_hyts_std", "osx_hycs_std", "osx_parhplot")
} else if (system_os == "win") {
resource_names <- c("win_hyts_std", "win_hycs_std", "win_parhplot")
} else {
resource_names <- c("l64_hyts_std", "l64_hycs_std", "l64_parhplot")
}
resource_names
}