Repository navigation
Expand file tree
/
Copy path_software.R
More file actions
362 lines (331 loc) · 12.1 KB
/
Copy path_software.R
File metadata and controls
362 lines (331 loc) · 12.1 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# Shared code for the software pages (software.qmd for R, julia.qmd for
# Julia): fetching package details from GitHub and rendering them.
library(magrittr)
# Hide software not updated in this many months (set to NULL to disable)
stale_months <- 24
# Team members keyed by GitHub username, to pick out group contributors
load_team_by_github <- function() {
fs::dir_ls("_data/team", regexp = "\\w+\\-\\w+\\.yml") |>
purrr::map(yaml::read_yaml) %>%
purrr::keep(~ !is.null(.x$github) && .x$github != "") %>%
purrr::set_names(purrr::map_chr(., "github"))
}
# Weekly CRAN download figures, collected by
# .github/workflows/update-stats.yaml. Absent until that job has run at least
# once, so treated as optional.
load_downloads <- function() {
# The weekly job records to the stats-data branch. Read from there, falling
# back to a local file so the page still renders before the branch exists
# or if the fetch fails.
stats_url <- paste0(
"https://raw.githubusercontent.com/epiforecasts/epiforecasts.github.io/",
"stats-data/_data/package-stats.csv"
)
stats <- tryCatch(
read.csv(stats_url, stringsAsFactors = FALSE),
error = function(err) NULL
)
if (is.null(stats) && file.exists("_data/package-stats.csv")) {
stats <- read.csv("_data/package-stats.csv", stringsAsFactors = FALSE)
}
if (is.null(stats) || nrow(stats) == 0) {
return(list())
}
# Each repository's most recent figure: a week where cranlogs is
# unreachable writes NAs for everything, and the previous week's figures
# are still usable. Bounded so a long outage shows nothing rather than
# something months out of date.
stats <- stats[!is.na(stats$downloads_last_month), ]
stats <- stats[as.Date(stats$date) > Sys.Date() - 35, ]
stats <- stats[order(stats$date, decreasing = TRUE), ]
stats <- stats[!duplicated(tolower(stats$repo)), ]
# keyed by repository, not package name: the two can differ and the
# repository is what both sides can agree on
split(stats$downloads_last_month, tolower(stats$repo))
}
# The first of `paths` that exists in the repository, with its download URL
find_file <- function(repo, paths) {
for (f in paths) {
hit <- tryCatch(
gh::gh("/repos/{repo}/contents/{f}", repo = repo, f = f),
error = function(err) NULL
)
if (!is.null(hit$download_url)) {
return(list(path = f, url = hit$download_url))
}
}
NULL
}
fetch_package_info <- function(repo, team_by_github) {
repo_info <- tryCatch(
gh::gh("/repos/{repo}", repo = repo),
error = function(err) list(
name = basename(repo),
description = "No description available",
html_url = paste0("https://github.com/", repo),
stargazers_count = 0,
language = NA,
pushed_at = NA
)
)
contributors <- tryCatch(
gh::gh("/repos/{repo}/contributors", repo = repo, per_page = 100),
error = function(err) list()
)
team_contributors <- contributors %>%
purrr::keep(~ .x$login %in% names(team_by_github)) %>%
purrr::map(function(c) {
tm <- team_by_github[[c$login]]
list(
name = tm$name,
github = c$login,
avatar_url = c$avatar_url
)
})
# R packages keep their logo in man/figures, Julia packages in the
# Documenter assets folder. There the SVG is the sticker, while a PNG
# alongside it can be a different mark.
logo <- find_file(repo, c(
"man/figures/logo.png", "man/figures/logo.svg",
"docs/src/assets/logo.svg", "docs/src/assets/logo.png"
))
logo_url <- logo$url %||% NA_character_
# A dark-mode variant sits next to the logo, as pkgdown's logo-dark does
logo_dark_url <- NA_character_
if (!is.null(logo)) {
dark <- find_file(repo, file.path(
dirname(logo$path), c("logo-dark.svg", "logo-dark.png")
))
logo_dark_url <- dark$url %||% NA_character_
}
list(
Package = repo_info$name,
logo_url = logo_url,
logo_dark_url = logo_dark_url,
description = if (nzchar(repo_info$description %||% "")) {
repo_info$description
} else {
"No description available"
},
github_url = repo_info$html_url,
homepage = repo_info$homepage %||% "",
stars = repo_info$stargazers_count %||% 0,
language = repo_info$language,
updated = if (!is.null(repo_info$pushed_at)) substr(repo_info$pushed_at, 1, 10) else NA,
team_contributors = team_contributors
)
}
# Packages listed in a YAML file rather than a registry
fetch_listed_packages <- function(path, team_by_github) {
purrr::map(yaml::read_yaml(path), function(e) {
info <- fetch_package_info(e$repo, team_by_github)
# marks a package belonging to a separate community project
info$part_of <- e$part_of
if (!is.null(e$description)) info$description <- e$description
info$hex <- !isFALSE(e$hex)
if (!is.null(e$logo)) {
info$logo_url <- paste0(
"https://raw.githubusercontent.com/", e$repo, "/HEAD/", e$logo
)
# a dark variant found at the default path would belong to another
# logo, so look for one named after this logo instead
dark <- find_file(e$repo, sub("(\\.[[:alnum:]]+)$", "-dark\\1", e$logo))
info$logo_dark_url <- dark$url %||% NA_character_
}
# not in our r-universe, so there is no page there to link to
info$from_extras <- TRUE
info
})
}
# The R packages on software.qmd: those our r-universe marks for the website,
# and those listed in software-extras.yml
r_packages <- function(team_by_github = load_team_by_github()) {
pkg_descriptions <- jsonlite::fromJSON("https://epiforecasts.r-universe.dev/api/packages")
extra_metadata <- jsonlite::read_json("https://github.com/epiforecasts/universe/raw/main/packages.json") %>%
dplyr::bind_rows() %>%
dplyr::mutate(
Package = package,
display_website = display_website & !is.na(display_website),
.keep = "unused"
)
universe_packages <- pkg_descriptions %>%
dplyr::left_join(extra_metadata, by = "Package") %>%
dplyr::filter(display_website) %>%
split(rownames(.)) %>%
purrr::map(unlist) %>%
purrr::map(function(e) {
repo <- paste0(e[["_owner"]], "/", e[["Package"]])
fetch_package_info(repo, team_by_github)
})
extra_packages <- fetch_listed_packages(
"_data/software-extras.yml", team_by_github
)
tidy_packages(c(universe_packages, extra_packages))
}
# The Julia packages on julia.qmd
julia_packages <- function(team_by_github = load_team_by_github()) {
fetch_listed_packages("_data/julia-packages.yml", team_by_github) |>
tidy_packages()
}
# Drop stale packages and sort by stars
tidy_packages <- function(packages) {
packages %>%
purrr::keep(~ !is.null(.x)) %>%
purrr::keep(~ {
if (is.null(stale_months) || is.na(.x$updated)) return(TRUE)
as.Date(.x$updated) > Sys.Date() - (stale_months * 30)
}) %>%
{ .[order(purrr::map_dbl(., "stars"), decreasing = TRUE)] }
}
# A logo as one <img>, or as a light and a dark one when the package has a
# dark-mode variant; styles.css shows the one matching the colour scheme
logo_img <- function(e, alt, attrs = "") {
light <- sprintf('<img src="%s" alt="%s"%s>', e$logo_url, alt, attrs)
if (is.null(e$logo_dark_url) || is.na(e$logo_dark_url)) {
return(light)
}
paste0(
sprintf('<img class="logo-for-light" src="%s" alt="%s"%s>', e$logo_url, alt, attrs),
sprintf('<img class="logo-for-dark" src="%s" alt="%s"%s>', e$logo_dark_url, alt, attrs)
)
}
# Honeycomb index: every package with a hex logo, in offset rows so the hexes
# tessellate. Rows alternate in length by one, which is what produces the
# half-width stagger without any positioning maths.
hex_wall <- function(packages) {
with_logo <- purrr::keep(
packages, ~ !is.na(.x$logo_url) && !isFALSE(.x$hex)
)
if (length(with_logo) == 0) {
return(invisible())
}
# Adjacent rows must differ by exactly one hex, which is what offsets them
# by half a width and makes the shapes interlock. Enumerate the row patterns
# that satisfy that and sum exactly, preferring a symmetric one so the
# widest row sits in the middle.
patterns <- function(total, rows) {
out <- list()
for (first in 7:3) {
steps <- as.matrix(expand.grid(rep(list(c(-1, 1)), rows - 1)))
for (k in seq_len(nrow(steps))) {
widths <- cumsum(c(first, steps[k, ]))
if (sum(widths) == total && min(widths) >= 3) {
out[[length(out) + 1]] <- as.integer(widths)
}
}
}
out
}
n <- length(with_logo)
options <- c(patterns(n, 3), patterns(n, 4), patterns(n, 2))
palindromic <- purrr::keep(options, ~ identical(.x, rev(.x)))
sizes <- if (length(palindromic) > 0) {
palindromic[[1]]
} else if (length(options) > 0) {
options[[1]]
} else {
# nothing interlocks exactly, so fill rows greedily and accept the shape
widths <- integer(0)
left <- n
wide <- FALSE
while (left > 0) {
take <- min(if (wide) 6 else 5, left)
widths <- c(widths, take)
left <- left - take
wide <- !wide
}
widths
}
idx <- 0
rows <- character(0)
for (n in sizes) {
cells <- character(0)
for (i in seq_len(n)) {
idx <- idx + 1
e <- with_logo[[idx]]
cells <- c(cells, sprintf(
'<a href="%s" title="%s">%s</a>',
e$github_url, e$Package, logo_img(e, paste(e$Package, "hex logo"))
))
}
rows <- c(rows, paste0(
'<div class="hex-row">', paste(cells, collapse = ""), '</div>'
))
}
cat(paste0(
'<div class="hex-wall">',
paste(rows, collapse = ""), '</div>\n\n'
))
}
# Package cards
render_packages <- function(packages, downloads_by_package = list()) {
packages %>%
purrr::map_chr(function(e) {
contributors_section <- ""
if (length(e$team_contributors) > 0) {
contributor_items <- purrr::map_chr(e$team_contributors, function(c) {
sprintf(
'<a href="https://github.com/%s" title="%s"><img src="%s" alt="%s" width="32" height="32"></a>',
c$github, c$name, c$avatar_url, c$name
)
})
contributors_section <- paste(contributor_items, collapse = " ")
}
logo_html <- ""
if (!is.na(e$logo_url)) {
logo_html <- sprintf(
'<div class="pkg-logo">%s</div>',
logo_img(e, paste(e$Package, "logo"), ' loading="lazy"')
)
}
part_of_html <- ""
if (!is.null(e$part_of)) {
part_of_html <- sprintf(
'<div class="pkg-part-of">Part of <a href="%s">%s</a></div>',
e$part_of$url, e$part_of$name
)
}
slug <- sub("^https://github.com/", "", e$github_url)
downloads <- downloads_by_package[[tolower(slug)]]
# Naming the source matters: a package with no CRAN figure is
# distributed elsewhere, not unused.
downloads_text <- if (!is.null(downloads)) {
paste(
formatC(downloads, big.mark = ",", format = "d"),
"CRAN downloads/month"
)
} else if (isTRUE(e$from_extras)) {
# Only link where the registry is known. A registry package is
# served by the epiforecasts universe whatever repo it lives in, so
# epimixr at sbfnk/epimixr still resolves there. A listed package
# could be in any universe or none.
""
} else {
# single-quoted attribute: substituted into R source
sprintf(
"<a href='https://epiforecasts.r-universe.dev/%s'>On r-universe</a>",
e$Package
)
}
# a GitHub homepage that is the repository itself adds nothing
docs_text <- ""
if (nzchar(e$homepage) && !startsWith(e$homepage, e$github_url)) {
docs_text <- sprintf("<a href='%s'>Documentation</a>", e$homepage)
}
knitr::knit_expand(
"_software-item.Rmd",
Package = e$Package,
downloads_text = downloads_text,
logo_html = logo_html,
part_of_html = part_of_html,
description = e$description,
github_url = e$github_url,
language = e$language %||% "",
updated = e$updated %||% "",
contributors_section = contributors_section,
docs_text = docs_text
)
}) %>%
{ knitr::knit_child(text = unlist(.), quiet = TRUE) } %>%
cat(sep = "\n")
}