-
Notifications
You must be signed in to change notification settings - Fork 153
/
install-github.R
6582 lines (5538 loc) · 203 KB
/
install-github.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Autogenerated from contents in the package's R directory, do not edit!
# Run make to update.
function(...) {
## This is the code of the package, put in here by brew
# Contents of R/bioc-standalone.R
#' Tools for Bioconductor versions and repositories
#'
#' \section{API:}
#'
#' ```
#' get_yaml_config(forget = FALSE)
#' set_yaml_config(text)
#'
#' get_release_version(forget = FALSE)
#' get_devel_version(forget = FALSE)
#'
#' get_version_map(forget = FALSE)
#' get_matching_bioc_version(r_version = getRversion(), forget = FALSE)
#' get_bioc_version(r_version = getRversion(), forget = FALSE)
#'
#' get_repos(bioc_version = "auto", forget = FALSE)
#' ```
#'
#' * `forget`: Whether to forget the cached version of the Bioconductor
#' config YAML file and download it again.
#' * `text`: character vector (linewise) or scalar, the contents of the
#' `config.yaml` file, if obtained externally, to be used as a cached
#' version in the future.
#' * `r_version`: R version string, or `package_version` object.
#' * `bioc_version`: Bioc version string or `package_version` object,
#' or the string `"auto"` to use the one matching the current R version.
#'
#' `get_yaml_config()` returns the raw contents of the `config.yaml` file,
#' linewise. It is typically not needed, except if one needs information
#' that cannot be surfaces via the other API functions.
#'
#' `set_yaml_config()` can be used to _set_ the contents of the
#' `config.yaml` file. This is useful, if one has already obtained it
#' externally, but wants to use the obtained file with the rest of the
#' bioc standalone code.
#'
#' `get_release_version()` returns the version of the current Bioconductor
#' release.
#'
#' `get_devel_version()` returns the version of the current development
#' version of Bioconductor.
#'
#' `get_version_map()` return the mapping between R versions and
#' Bioconductor versions. Note that this is not a one to one mapping.
#' E.g. currently R `3.6.x` maps to both Bioc `3.9` (Bioc release) and
#' `3.10` (Bioc devel); and also Bioc `3.10` maps to both R `3.6.x` and
#' R `3.7.x` (current R-devel). It returns a data frame with three columns:
#' `bioc_version`, `r_version` and `bioc_status`. The first two columns
#' contain `package_vesion` objects, the third is a factor with levels:
#' `out-of-date`, `release`, `devel`, `future`.
#'
#' `get_matching_bioc_version()` returns the matching Bioc version for an
#' R version. If the R version matches to both a released and a devel
#' version, then the released version is chosen.
#'
#' `get_bioc_version()` returns the matching Bioc version for the
#' specified R version. It does observe the `R_BIOC_VERSION` environment
#' variable, which can be used to force a Bioconductor version. If this is
#' not set, it just calls `get_matching_bioc_version()`.
#'
#' `get_repos()` returns the Bioc repositories of the specified Bioc
#' version. It defaults to the Bioc version that matches the calling R
#' version. It returns a named character vector.
#'
#' \section{NEWS:}
#' * 2019-05-30 First version in remotes.
#' * 2020-03-22 get_matching_bioc_version() is now correct if the current
#' R version is not in the builtin mapping.
#' * 2020-11-21 Update internal map for 3.12.
#' * 2023-05-08 Add 'books' repo.
#'
#' @name bioconductor
#' @keywords internal
#' @noRd
NULL
bioconductor <- local({
# -------------------------------------------------------------------
# Configuration that does not change often
config_url <- function() {
Sys.getenv(
"R_BIOC_CONFIG_URL",
"https://bioconductor.org/config.yaml"
)
}
builtin_map <- list(
"2.1" = package_version("1.6"),
"2.2" = package_version("1.7"),
"2.3" = package_version("1.8"),
"2.4" = package_version("1.9"),
"2.5" = package_version("2.0"),
"2.6" = package_version("2.1"),
"2.7" = package_version("2.2"),
"2.8" = package_version("2.3"),
"2.9" = package_version("2.4"),
"2.10" = package_version("2.5"),
"2.11" = package_version("2.6"),
"2.12" = package_version("2.7"),
"2.13" = package_version("2.8"),
"2.14" = package_version("2.9"),
"2.15" = package_version("2.11"),
"3.0" = package_version("2.13"),
"3.1" = package_version("3.0"),
"3.2" = package_version("3.2"),
"3.3" = package_version("3.4"),
"3.4" = package_version("3.6"),
"3.5" = package_version("3.8"),
"3.6" = package_version("3.10"),
"4.0" = package_version("3.12"),
"4.1" = package_version("3.14"),
"4.2" = package_version("3.16"),
"4.3" = package_version("3.18")
# Do not include R 4.4 <-> Bioc 3.19, because R 4.4 will use
# Bioc 3.20 eventually.
)
# -------------------------------------------------------------------
# Cache
devel_version <- NULL
release_version <- NULL
version_map <- NULL
yaml_config <- NULL
clear_cache <- function() {
devel_version <<- NULL
release_version <<- NULL
version_map <<- NULL
yaml_config <<- NULL
}
# -------------------------------------------------------------------
# API
get_yaml_config <- function(forget = FALSE) {
if (forget || is.null(yaml_config)) {
new <- tryCatch(read_url(config_url()), error = function(x) x)
if (inherits(new, "error")) {
http_url <- sub("^https", "http", config_url())
new <- tryCatch(read_url(http_url), error = function(x) x)
}
if (inherits(new, "error")) stop(new)
yaml_config <<- new
}
yaml_config
}
set_yaml_config <- function(text) {
if (length(text) == 1) text <- strsplit(text, "\n", fixed = TRUE)[[1]]
yaml_config <<- text
}
get_release_version <- function(forget = FALSE) {
if (forget || is.null(release_version)) {
yaml <- get_yaml_config(forget)
pattern <- "^release_version: \"(.*)\""
release_version <<- package_version(
sub(pattern, "\\1", grep(pattern, yaml, value=TRUE))
)
}
release_version
}
get_devel_version <- function(forget = FALSE) {
if (forget || is.null(devel_version)) {
yaml <- get_yaml_config(forget)
pattern <- "^devel_version: \"(.*)\""
devel_version <<- package_version(
sub(pattern, "\\1", grep(pattern, yaml, value=TRUE))
)
}
devel_version
}
get_version_map <- function(forget = FALSE) {
if (forget || is.null(version_map)) {
txt <- get_yaml_config(forget)
grps <- grep("^[^[:blank:]]", txt)
start <- match(grep("r_ver_for_bioc_ver", txt), grps)
map <- txt[seq(grps[start] + 1, grps[start + 1] - 1)]
map <- trimws(gsub("\"", "", sub(" #.*", "", map)))
pattern <- "(.*): (.*)"
bioc <- package_version(sub(pattern, "\\1", map))
r <- package_version(sub(pattern, "\\2", map))
status <- rep("out-of-date", length(bioc))
release <- get_release_version()
devel <- get_devel_version()
status[bioc == release] <- "release"
status[bioc == devel] <- "devel"
# append final version for 'devel' R
bioc <- c(
bioc, max(bioc)
)
r <- c(r, package_version(paste(unlist(max(r)) + 0:1, collapse = ".")))
status <- c(status, "future")
version_map <<- rbind(
.VERSION_MAP_SENTINEL,
data.frame(
bioc_version = bioc, r_version = r,
bioc_status = factor(
status,
levels = c("out-of-date", "release", "devel", "future")
)
)
)
}
version_map
}
get_matching_bioc_version <- function(r_version = getRversion(),
forget = FALSE) {
minor <- as.character(get_minor_r_version(r_version))
if (minor %in% names(builtin_map)) return(builtin_map[[minor]])
# If we are not in the map, then we need to look this up in
# YAML data. It is possible that the current R version matches multiple
# Bioc versions. Then we choose the latest released version. If none
# of them were released (e.g. they are 'devel' and 'future'), then
# we'll use the 'devel' version.
map <- get_version_map(forget = forget)
mine <- which(package_version(minor) == map$r_version)
if (length(mine) == 0) {
mine <- NA
} else if (length(mine) > 1) {
if ("release" %in% map$bioc_status[mine]) {
mine <- mine["release" == map$bioc_status[mine]]
} else if ("devel" %in% map$bioc_status[mine]) {
mine <- mine["devel" == map$bioc_status[mine]]
} else {
mine <- rev(mine)[1]
}
}
if (!is.na(mine)) return(map$bioc_version[mine])
# If it is not even in the YAML, then it must be some very old
# or very new version. If old, we fail. If new, we assume bioc-devel.
if (package_version(minor) < "2.1") {
stop("R version too old, cannot run Bioconductor")
}
get_devel_version()
}
get_bioc_version <- function(r_version = getRversion(),
forget = FALSE) {
if (nzchar(v <- Sys.getenv("R_BIOC_VERSION", ""))) {
return(package_version(v))
}
get_matching_bioc_version(r_version, forget = forget)
}
get_repos <- function(bioc_version = "auto", forget = FALSE) {
if (identical(bioc_version, "auto")) {
bioc_version <- get_bioc_version(getRversion(), forget)
} else {
bioc_version <- package_version(bioc_version)
}
mirror <- Sys.getenv("R_BIOC_MIRROR", "https://bioconductor.org")
mirror <- getOption("BioC_mirror", mirror)
repos <- c(
BioCsoft = "{mirror}/packages/{bv}/bioc",
BioCann = "{mirror}/packages/{bv}/data/annotation",
BioCexp = "{mirror}/packages/{bv}/data/experiment",
BioCworkflows =
if (bioc_version >= "3.7") "{mirror}/packages/{bv}/workflows",
BioCextra =
if (bioc_version <= "3.5") "{mirror}/packages/{bv}/extra",
BioCbooks =
if (bioc_version >= "3.12") "{mirror}/packages/{bv}/books"
)
## It seems that if a repo is not available yet for bioc-devel,
## they redirect to the bioc-release version, so we do not need to
## parse devel_repos from the config.yaml file
sub("{mirror}", mirror, fixed = TRUE,
sub("{bv}", bioc_version, repos, fixed = TRUE))
}
# -------------------------------------------------------------------
# Internals
read_url <- function(url) {
tmp <- tempfile()
on.exit(unlink(tmp), add = TRUE)
suppressWarnings(download.file(url, tmp, quiet = TRUE))
if (!file.exists(tmp) || file.info(tmp)$size == 0) {
stop("Failed to download `", url, "`")
}
readLines(tmp, warn = FALSE)
}
.VERSION_SENTINEL <- local({
version <- package_version(character())
class(version) <- c("unknown_version", class(version))
version
})
.VERSION_MAP_SENTINEL <- data.frame(
bioc_version = .VERSION_SENTINEL,
r_version = .VERSION_SENTINEL,
bioc_status = factor(
factor(),
levels = c("out-of-date", "release", "devel", "future")
)
)
get_minor_r_version <- function (x) {
package_version(x)[,1:2]
}
# -------------------------------------------------------------------
structure(
list(
.internal = environment(),
get_yaml_config = get_yaml_config,
set_yaml_config = set_yaml_config,
get_release_version = get_release_version,
get_devel_version = get_devel_version,
get_version_map = get_version_map,
get_matching_bioc_version = get_matching_bioc_version,
get_bioc_version = get_bioc_version,
get_repos = get_repos
),
class = c("standalone_bioc", "standalone"))
})
# Contents of R/bioc.R
#' @export
#' @rdname bioc_install_repos
#' @keywords internal
#' @examples
#' bioc_version()
#' bioc_version("3.4")
bioc_version <- function(r_ver = getRversion()) {
bioconductor$get_bioc_version(r_ver)
}
#' Tools for Bioconductor repositories
#'
#' `bioc_version()` returns the Bioconductor version for the current or the
#' specified R version.
#'
#' `bioc_install_repos()` deduces the URLs of the Bioconductor repositories.
#'
#' @details
#' Both functions observe the `R_BIOC_VERSION` environment variable, which
#' can be set to force a Bioconductor version. If this is set, then the
#' `r_ver` and `bioc_ver` arguments are ignored.
#'
#' `bioc_install_repos()` observes the `R_BIOC_MIRROR` environment variable
#' and also the `BioC_mirror` option, which can be set to the desired
#' Bioconductor mirror. The option takes precedence if both are set. Its
#' default value is `https://bioconductor.org`.
#'
#' @return
#' `bioc_version()` returns a Bioconductor version, a `package_version`
#' object.
#'
#' `bioc_install_repos()` returns a named character vector of the URLs of
#' the Bioconductor repositories, appropriate for the current or the
#' specified R version.
#'
#' @param r_ver R version to use. For `bioc_install_repos()` it is
#' ignored if `bioc_ver` is specified.
#' @param bioc_ver Bioconductor version to use. Defaults to the default one
#' corresponding to `r_ver`.
#'
#' @export
#' @keywords internal
#' @examples
#' bioc_install_repos()
bioc_install_repos <- function(r_ver = getRversion(),
bioc_ver = bioc_version(r_ver)) {
bioconductor$get_repos(bioc_ver)
}
# Contents of R/circular.R
## A environment to hold which packages are being installed so packages
## with circular dependencies can be skipped the second time.
installing <- new.env(parent = emptyenv())
is_root_install <- function() is.null(installing$packages)
exit_from_root_install <- function() installing$packages <- NULL
check_for_circular_dependencies <- function(pkgdir, quiet) {
pkgdir <- normalizePath(pkgdir)
pkg <- get_desc_field(file.path(pkgdir, "DESCRIPTION"), "Package")
if (pkg %in% installing$packages) {
if (!quiet) message("Skipping ", pkg, ", it is already being installed")
TRUE
} else {
installing$packages <- c(installing$packages, pkg)
FALSE
}
}
# Contents of R/cran.R
cache <- new.env(parent = emptyenv())
#' @rdname available_packages
#' @export
available_packages_set <- function(repos, type, db) {
signature <- rawToChar(serialize(list(repos, type), NULL, ascii = TRUE))
if (is.null(cache[[signature]])) {
cache[[signature]] <- db
}
cache[[signature]]
}
#' @rdname available_packages
#' @export
available_packages_reset <- function() {
rm(list = ls(envir = cache), envir = cache)
}
#' Simpler available.packages
#'
#' This is mostly equivalent to [utils::available.packages()] however it also
#' caches the full result. Additionally the cache can be assigned explicitly with
#' [available_packages_set()] and reset (cleared) with [available_packages_reset()].
#'
#' @inheritParams utils::available.packages
#' @keywords internal
#' @seealso [utils::available.packages()] for full documentation on the output format.
#' @export
available_packages <- function(repos = getOption("repos"), type = getOption("pkgType")) {
available_packages_set(
repos, type,
suppressWarnings(utils::available.packages(utils::contrib.url(repos, type), type = type))
)
}
# Contents of R/dcf.R
read_dcf <- function(path) {
fields <- colnames(read.dcf(path))
as.list(read.dcf(path, keep.white = fields)[1, ])
}
write_dcf <- function(path, desc) {
write.dcf(
rbind(unlist(desc)),
file = path,
keep.white = names(desc),
indent = 0
)
}
get_desc_field <- function(path, field) {
dcf <- read_dcf(path)
dcf[[field]]
}
# Contents of R/decompress.R
# Decompress pkg, if needed
source_pkg <- function(path, subdir = NULL) {
if (!dir.exists(path)) {
bundle <- path
outdir <- tempfile(pattern = "remotes")
dir.create(outdir)
path <- decompress(path, outdir)
} else {
bundle <- NULL
}
pkg_path <- if (is.null(subdir)) path else file.path(path, subdir)
# Check it's an R package
if (!file.exists(file.path(pkg_path, "DESCRIPTION"))) {
stop("Does not appear to be an R package (no DESCRIPTION)", call. = FALSE)
}
# Check configure is executable if present
config_path <- file.path(pkg_path, "configure")
if (file.exists(config_path)) {
Sys.chmod(config_path, "777")
}
pkg_path
}
decompress <- function(src, target) {
stopifnot(file.exists(src))
if (grepl("\\.zip$", src)) {
my_unzip(src, target)
outdir <- getrootdir(as.vector(utils::unzip(src, list = TRUE)$Name))
} else if (grepl("\\.(tar|tar\\.gz|tar\\.bz2|tgz|tbz)$", src)) {
untar(src, exdir = target)
outdir <- getrootdir(untar(src, list = TRUE))
} else {
ext <- gsub("^[^.]*\\.", "", src)
stop("Don't know how to decompress files with extension ", ext,
call. = FALSE)
}
file.path(target, outdir)
}
# Returns everything before the last slash in a filename
# getdir("path/to/file") returns "path/to"
# getdir("path/to/dir/") returns "path/to/dir"
getdir <- function(path) sub("/[^/]*$", "", path)
# Given a list of files, returns the root (the topmost folder)
# getrootdir(c("path/to/file", "path/to/other/thing")) returns "path/to"
# It does not check that all paths have a common prefix. It fails for
# empty input vector. It assumes that directories end with '/'.
getrootdir <- function(file_list) {
stopifnot(length(file_list) > 0)
slashes <- nchar(gsub("[^/]", "", file_list))
if (min(slashes) == 0) return(".")
getdir(file_list[which.min(slashes)])
}
my_unzip <- function(src, target, unzip = getOption("unzip", "internal")) {
if (unzip %in% c("internal", "")) {
return(utils::unzip(src, exdir = target))
}
args <- paste(
"-oq", shQuote(src),
"-d", shQuote(target)
)
system_check(unzip, args)
}
# Contents of R/deps.R
#' Find all dependencies of a CRAN or dev package.
#'
#' Find all the dependencies of a package and determine whether they are ahead
#' or behind CRAN. A `print()` method identifies mismatches (if any)
#' between local and CRAN versions of each dependent package; an
#' `update()` method installs outdated or missing packages from CRAN.
#'
#' @param packages A character vector of package names.
#' @param pkgdir Path to a package directory, or to a package tarball.
#' @param dependencies Which dependencies do you want to check?
#' Can be a character vector (selecting from "Depends", "Imports",
#' "LinkingTo", "Suggests", or "Enhances"), or a logical vector.
#'
#' `TRUE` is shorthand for "Depends", "Imports", "LinkingTo" and
#' "Suggests". `NA` is shorthand for "Depends", "Imports" and "LinkingTo"
#' and is the default. `FALSE` is shorthand for no dependencies (i.e.
#' just check this package, not its dependencies).
#'
#' The value "soft" means the same as `TRUE`, "hard" means the same as `NA`.
#'
#' You can also specify dependencies from one or more additional fields,
#' common ones include:
#' - Config/Needs/website - for dependencies used in building the pkgdown site.
#' - Config/Needs/coverage for dependencies used in calculating test coverage.
#' @param quiet If `TRUE`, suppress output.
#' @param upgrade Should package dependencies be upgraded? One of "default", "ask", "always", or "never". "default"
#' respects the value of the `R_REMOTES_UPGRADE` environment variable if set,
#' and falls back to "ask" if unset. "ask" prompts the user for which out of
#' date packages to upgrade. For non-interactive sessions "ask" is equivalent
#' to "always". `TRUE` and `FALSE` are also accepted and correspond to
#' "always" and "never" respectively.
#' @param repos A character vector giving repositories to use.
#' @param type Type of package to `update`.
#' @param remote_precedence A logical flag specifying whether remote sources should take precedence over
#' CRAN when both were found.
#' @param additional_repositories A logical flag specifying whether `Additional_repositories` should
#' be extracted from the DESCRIPTION and appended to `repos`.
#' @param object A `package_deps` object.
#' @param ... Additional arguments passed to `install_packages`.
#' @inheritParams install_github
#'
#' @return
#'
#' A `data.frame` with columns:
#'
#' \tabular{ll}{
#' `package` \tab The dependent package's name,\cr
#' `installed` \tab The currently installed version,\cr
#' `available` \tab The version available on CRAN,\cr
#' `diff` \tab An integer denoting whether the locally installed version
#' of the package is newer (1), the same (0) or older (-1) than the version
#' currently available on CRAN.\cr
#' }
#'
#' @export
#' @examples
#' \dontrun{
#' package_deps("devtools")
#' # Use update to update any out-of-date dependencies
#' update(package_deps("devtools"))
#' }
package_deps <- function(packages, dependencies = NA,
repos = getOption("repos"),
type = getOption("pkgType")) {
repos <- fix_repositories(repos)
cran <- available_packages(repos, type)
deps <- find_deps(packages, available = cran, top_dep = dependencies)
# Remove base packages
inst <- utils::installed.packages()
base <- unname(inst[inst[, "Priority"] %in% c("base", "recommended"), "Package"])
deps <- setdiff(deps, base)
# get remote types
remote <- structure(lapply(deps, package2remote, repos = repos, type = type), class = "remotes")
inst_ver <- vapply(deps, local_sha, character(1))
cran_ver <- vapply(remote, function(x) remote_sha(x), character(1))
is_cran_remote <- vapply(remote, inherits, logical(1), "cran_remote")
diff <- compare_versions(inst_ver, cran_ver, is_cran_remote)
res <- structure(
data.frame(
package = deps,
installed = inst_ver,
available = cran_ver,
diff = diff,
is_cran = is_cran_remote,
stringsAsFactors = FALSE
),
class = c("package_deps", "data.frame")
)
res$remote <- remote
res
}
#' `local_package_deps` extracts dependencies from a
#' local DESCRIPTION file.
#'
#' @export
#' @rdname package_deps
local_package_deps <- function(pkgdir = ".", dependencies = NA) {
pkg <- load_pkg_description(pkgdir)
dependencies <- tolower(standardise_dep(dependencies))
dependencies <- intersect(dependencies, names(pkg))
parsed <- lapply(pkg[tolower(dependencies)], parse_deps)
unlist(lapply(parsed, `[[`, "name"), use.names = FALSE)
}
#' `dev_package_deps` lists the status of the dependencies
#' of a local package.
#'
#' @export
#' @rdname package_deps
dev_package_deps <- function(pkgdir = ".", dependencies = NA,
repos = getOption("repos"),
type = getOption("pkgType"),
remote_precedence = TRUE,
additional_repositories = TRUE) {
pkg <- load_pkg_description(pkgdir)
repos <- c(repos, if (additional_repositories) parse_additional_repositories(pkg))
deps <- local_package_deps(pkgdir = pkgdir, dependencies = dependencies)
if (is_bioconductor(pkg)) {
bioc_repos <- bioc_install_repos()
missing_repos <- setdiff(names(bioc_repos), names(repos))
if (length(missing_repos) > 0)
repos[missing_repos] <- bioc_repos[missing_repos]
}
cran_deps <- package_deps(deps, repos = repos, type = type)
res <- combine_remote_deps(cran_deps, extra_deps(pkg, "remotes"), remote_precedence)
res <- do.call(rbind, c(list(res), lapply(get_extra_deps(pkg, dependencies), extra_deps, pkg = pkg), stringsAsFactors = FALSE))
res[is.na(res$package) | !duplicated(res$package, fromLast = TRUE), ]
}
combine_remote_deps <- function(cran_deps, remote_deps, remote_precedence) {
# If there are no dependencies there will be no remote dependencies either,
# so just return them (and don't force the remote_deps promise)
if (nrow(cran_deps) == 0) {
return(cran_deps)
}
# Only keep the remotes that are specified in the cran_deps or are NA
remote_deps <- remote_deps[is.na(remote_deps$package) | remote_deps$package %in% cran_deps$package, ]
# If there are remote deps remove the equivalent CRAN deps
if (remote_precedence) {
cran_deps <- cran_deps[!(cran_deps$package %in% remote_deps$package), ]
# Otherwise remove remotes already covered by CRAN
} else {
remote_deps <- remote_deps[!(remote_deps$package %in% cran_deps$package), ]
}
rbind(remote_deps, cran_deps)
}
## -2 = not installed, but available on CRAN
## -1 = installed, but out of date
## 0 = installed, most recent version
## 1 = installed, version ahead of CRAN
## 2 = package not on CRAN
compare_versions <- function(inst, remote, is_cran) {
stopifnot(length(inst) == length(remote) && length(inst) == length(is_cran))
compare_var <- function(i, c, cran) {
if (!cran) {
if (identical(i, c)) {
return(CURRENT)
} else {
return(BEHIND)
}
}
if (is.na(c)) return(UNAVAILABLE) # not on CRAN
if (is.na(i)) return(UNINSTALLED) # not installed, but on CRAN
i <- package_version(i)
c <- package_version(c)
if (i < c) {
BEHIND # out of date
} else if (i > c) {
AHEAD # ahead of CRAN
} else {
CURRENT # most recent CRAN version
}
}
vapply(seq_along(inst),
function(i) compare_var(inst[[i]], remote[[i]], is_cran[[i]]),
integer(1))
}
has_extra_deps <- function(pkg, dependencies) {
any(dependencies %in% names(pkg))
}
get_extra_deps <- function(pkg, dependencies) {
dependencies <- tolower(dependencies)
dependencies <- intersect(dependencies, names(pkg))
#remove standard dependencies
setdiff(dependencies, tolower(standardise_dep(c("Depends", "Imports", "LinkingTo", "Suggests", "Enhances"))))
}
#' @export
print.package_deps <- function(x, show_ok = FALSE, ...) {
class(x) <- "data.frame"
x$remote <-lapply(x$remote, format)
ahead <- x$diff > 0L
behind <- x$diff < 0L
same_ver <- x$diff == 0L
x$diff <- NULL
x[] <- lapply(x, format_str, width = 12)
if (any(behind)) {
cat("Needs update -----------------------------\n")
print(x[behind, , drop = FALSE], row.names = FALSE, right = FALSE)
}
if (any(ahead)) {
cat("Not on CRAN ----------------------------\n")
print(x[ahead, , drop = FALSE], row.names = FALSE, right = FALSE)
}
if (show_ok && any(same_ver)) {
cat("OK ---------------------------------------\n")
print(x[same_ver, , drop = FALSE], row.names = FALSE, right = FALSE)
}
}
## -2 = not installed, but available on CRAN
## -1 = installed, but out of date
## 0 = installed, most recent version
## 1 = installed, version ahead of CRAN
## 2 = package not on CRAN
UNINSTALLED <- -2L
BEHIND <- -1L
CURRENT <- 0L
AHEAD <- 1L
UNAVAILABLE <- 2L
#' @export
#' @rdname package_deps
#' @importFrom stats update
update.package_deps <- function(object,
dependencies = NA,
upgrade = c("default", "ask", "always", "never"),
force = FALSE,
quiet = FALSE,
build = TRUE, build_opts = c("--no-resave-data", "--no-manual", "--no-build-vignettes"),
build_manual = FALSE, build_vignettes = FALSE,
repos = getOption("repos"),
type = getOption("pkgType"),
...) {
dependencies <- standardise_dep(dependencies)
object <- upgradable_packages(object, upgrade, quiet)
unavailable_on_cran <- object$diff == UNAVAILABLE & object$is_cran
unknown_remotes <- (object$diff == UNAVAILABLE | object$diff == UNINSTALLED) & !object$is_cran
if (any(unavailable_on_cran) && !quiet) {
message("Skipping ", sum(unavailable_on_cran), " packages not available: ",
paste(object$package[unavailable_on_cran], collapse = ", "))
}
if (any(unknown_remotes)) {
install_remotes(object$remote[unknown_remotes],
dependencies = dependencies,
upgrade = upgrade,
force = force,
quiet = quiet,
build = build,
build_opts = build_opts,
build_manual = build_manual,
build_vignettes = build_vignettes,
repos = repos,
type = type,
...)
}
ahead_of_cran <- object$diff == AHEAD & object$is_cran
if (any(ahead_of_cran) && !quiet) {
message("Skipping ", sum(ahead_of_cran), " packages ahead of CRAN: ",
paste(object$package[ahead_of_cran], collapse = ", "))
}
ahead_remotes <- object$diff == AHEAD & !object$is_cran
if (any(ahead_remotes)) {
install_remotes(object$remote[ahead_remotes],
dependencies = dependencies,
upgrade = upgrade,
force = force,
quiet = quiet,
build = build,
build_opts = build_opts,
build_manual = build_manual,
build_vignettes = build_vignettes,
repos = repos,
type = type,
...)
}
behind <- is.na(object$installed) | object$diff < CURRENT
if (any(object$is_cran & !unavailable_on_cran & behind)) {
# get the first cran-like remote and use its repos and pkg_type
r <- object$remote[object$is_cran & behind][[1]]
install_packages(object$package[object$is_cran & behind], repos = r$repos,
type = r$pkg_type, dependencies = dependencies, quiet = quiet, ...)
}
install_remotes(object$remote[!object$is_cran & behind],
dependencies = dependencies,
upgrade = upgrade,
force = force,
quiet = quiet,
build = build,
build_opts = build_opts,
build_manual = build_manual,
build_vignettes = build_vignettes,
repos = repos,
type = type,
...)
invisible()
}
install_packages <- function(packages, repos = getOption("repos"),
type = getOption("pkgType"), ...,
dependencies = FALSE, quiet = NULL) {
# We want to pass only args that exist in the downstream functions
args_to_keep <-
unique(
names(
c(
formals(utils::install.packages),
formals(utils::download.file)
)
)
)
args <- list(...)
args <- args[names(args) %in% args_to_keep]
if (is.null(quiet))
quiet <- !identical(type, "source")
message("Installing ", length(packages), " packages: ",
paste(packages, collapse = ", "))
do.call(
safe_install_packages,
c(list(
packages,
repos = repos,
type = type,
dependencies = dependencies,
quiet = quiet
),
args
)
)
}
find_deps <- function(packages, available = available_packages(),
top_dep = TRUE, rec_dep = NA, include_pkgs = TRUE) {
if (length(packages) == 0 || identical(top_dep, FALSE))
return(character())
top_dep <- standardise_dep(top_dep)
rec_dep <- standardise_dep(rec_dep)
top <- tools::package_dependencies(packages, db = available, which = top_dep)
top_flat <- unlist(top, use.names = FALSE)
if (length(rec_dep) != 0 && length(top_flat) > 0) {
rec <- tools::package_dependencies(top_flat, db = available, which = rec_dep,
recursive = TRUE)
rec_flat <- unlist(rec, use.names = FALSE)
} else {
rec_flat <- character()
}
# We need to return these in reverse order, so that the packages furthest
# down in the tree are installed first.
unique(rev(c(if (include_pkgs) packages, top_flat, rec_flat)))
}
#' Standardise dependencies using the same logical as [install.packages]
#'
#' @param x The dependencies to standardise.
#' A character vector (selecting from "Depends", "Imports",
#' "LinkingTo", "Suggests", or "Enhances"), or a logical vector.
#'
#' `TRUE` is shorthand for "Depends", "Imports", "LinkingTo" and
#' "Suggests". `NA` is shorthand for "Depends", "Imports" and "LinkingTo"
#' and is the default. `FALSE` is shorthand for no dependencies.
#'
#' The value "soft" means the same as `TRUE`, "hard" means the same as `NA`.
#'
#' Any additional values that don't match one of the standard dependency
#' types are filtered out.
#'
#' @seealso <https://r-pkgs.org/description.html> for
#' additional information on what each dependency type means.
#' @keywords internal
#' @export
standardise_dep <- function(x) {
if (identical(x, NA)) {
c("Depends", "Imports", "LinkingTo")
} else if (isTRUE(x)) {
c("Depends", "Imports", "LinkingTo", "Suggests")
} else if (identical(x, FALSE)) {
character(0)
} else if (is.character(x)) {
if (any(x == "hard")) {