forked from r-lib/desc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnon-oo-api.R
780 lines (650 loc) · 21.1 KB
/
non-oo-api.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
# No coverage calculating here, since this code
# runs during install time only.
# nocov start
#' @include description.R
#' @importFrom utils packageName
generate_api <- function(member, self = TRUE, norm = TRUE,
invisible = FALSE) {
res <- function() { }
func <- description$public_methods[[member]]
## Arguments
xargs <- list(file = ".")
if (self && norm) xargs <- c(xargs, list(normalize = FALSE))
formals(res) <- c(formals(func), xargs)
## Call to member function
member_call <- substitute(
desc[[`_member`]](),
list(`_member` = member)
)
argnames <- names(formals(func))
dargs <- structure(lapply(argnames, as.name), names = argnames)
if (!is.null(formals(func))) {
member_call[1 + 1:length(formals(func))] <- dargs
argnames2 <- c("", ifelse(argnames == "...", "", argnames))
names(member_call) <- argnames2
}
desc_call <- substitute(
result <- `_member`,
list(`_member` = member_call)
)
## Call to write, or just return the result
write_call <- if (self && norm) {
quote({
if (normalize) desc$normalize()
desc$write(file = file)
})
} else if (self) {
quote(desc$write(file = file))
}
## Put together
body(res) <- substitute(
{ `_read`; `_trans`; `_write`; `_return` },
list(
`_read` = quote(desc <- description$new(file = file)),
`_trans` = desc_call,
`_write` = write_call,
`_return` = if (invisible) quote(invisible(result)) else quote(result)
)
)
environment(res) <- asNamespace(packageName())
res
}
## -------------------------------------------------------------------
#' List all fields in a DESCRIPTION file
#'
#' @inheritParams desc_set
#' @return Character vector of fields.
#'
#' @family simple queries
#' @export
desc_fields <- generate_api("fields", self = FALSE)
#' Check if some fields are present in a DESCRIPTION file
#'
#' @param keys Character vector of keys to check.
#' @inheritParams desc_set
#' @return Logical vector.
#'
#' @family simple queries
#' @export
desc_has_fields <- generate_api("has_fields", self = FALSE)
#' Get a field from a DESCRIPTION file
#'
#' @param keys Character vector of fields to get.
#' @inheritParams desc_set
#' @return Character vector, values of the specified keys.
#' Non-existing keys return `NA`.
#'
#' @family simple queries
#' @export
desc_get <- generate_api("get", self = FALSE)
#' Get a single field from a DESCRIPTION file, fail if not found
#'
#' `desc_get_list()` parses a comma separated list into a character
#' vector.
#'
#' @inheritParams desc_get
#' @param key The field to query.
#' @param default Value to return if `key` is not found.
#' By default it throws an error.
#' @param trim_ws Whether to trim leading and trailing whitespace
#' from the value. Defaults to `TRUE`.
#' @param squish_ws Whether to reduce repeated whitespace in the value.
#' Defaults to `trim_ws`.
#' @param sep Separator string for [desc_get_list()].
#' @return Character string, the value of `key`, or `default`
#' if `key` is not found and `default` is specified.
#'
#' @family simple queries
#' @export
desc_get_field <- generate_api("get_field", self = FALSE)
#' @rdname desc_get_field
#' @export
desc_get_or_fail <- generate_api("get_or_fail", self = FALSE)
#' @rdname desc_get_field
#' @export
desc_get_list <- generate_api("get_list", self = FALSE)
#' Set one or more fields in a DESCRIPTION file
#'
#' @details
#' `desc_set()` supports two forms, the first is two unnamed
#' arguments: the key and its value to set.
#'
#' The second form requires named arguments: names are used as keys
#' and values as values to set.
#'
#' `desc_set_list()` collapses a character vector into string,
#' separating the elements by commas.
#'
#' @param ... Values to set, see details below.
#' @param check Whether to check the validity of the new fields.
#' @param file DESCRIPTION file to use. By default the DESCRIPTION
#' file of the current package (i.e. the package the working directory
#' is part of) is used.
#' @param normalize Whether to "normalize" (reorder and reformat) the fields when writing back
#' the result. See [desc_normalize()].
#' @param key Key to set in `desc_set_list()`.
#' @param list_value Character vector, to collapse in
#' `desc_set_list()`.
#' @param sep Separator string for `desc_set_list()` list fields.
#'
#' @family simple queries
#' @export
desc_set <- generate_api("set")
#' @rdname desc_set
#' @export
desc_set_list <- generate_api("set_list")
#' Remove fields from a DESCRIPTION file
#'
#' @param keys Character vector of keys to remove.
#' @inheritParams desc_set
#'
#' @family simple queries
#' @export
desc_del <- generate_api("del")
## -------------------------------------------------------------------
#' Print the contents of a DESCRIPTION file to the screen
#'
#' @inheritParams desc_set
#' @export
desc_print <- generate_api("print", self = FALSE, invisible = TRUE)
#' Converts a DESCRIPTION file to LaTeX
#'
#' Returns the contents of the DESCRIPTION in LaTeX format.
#'
#' @inheritParams desc_set
#' @export
desc_to_latex <- generate_api("to_latex", self = FALSE)
#' Normalize a DESCRIPTION file
#'
#' Re-formats and re-orders fields in DESCRIPTION in a standard way.
#' Reorders the packages alphabetically.
#'
#' @inheritParams desc_set
#' @family repair functions
#' @export
desc_normalize <- generate_api("normalize", self = TRUE, norm = FALSE)
#' Reformat fields in a DESCRIPTION file
#'
#' Reformat the fields in DESCRIPTION in a standard way.
#'
#' @inheritParams desc_set
#' @family repair functions
#' @export
desc_reformat_fields <- generate_api("reformat_fields", self = TRUE, norm = FALSE)
#' Reorder fields in a DESCRIPTION file
#'
#' Reorder the fields in DESCRIPTION in a standard way.
#'
#' @inheritParams desc_set
#' @family repair functions
#' @export
desc_reorder_fields <- generate_api("reorder_fields", self = TRUE, norm = FALSE)
#' Validate a DESCRIPTION file
#'
#' This function is not implemented yet.
#'
#' @inheritParams desc_set
#'
#' @export
desc_validate <- generate_api("validate", self = FALSE)
## -------------------------------------------------------------------
#' List all package dependencies from a DESCRIPTION file
#'
#' @inheritParams desc_set
#' @return Data frame with columns: `type` (dependency type),
#' `package`, and `version`. For non-versioned dependencies
#' `version` is `*`.
#'
#' @family dependencies
#' @export
desc_get_deps <- generate_api("get_deps", self = FALSE)
#' Add a package dependency to a DESCRIPTION file
#'
#' @param package Package to depend on.
#' @param type Dependency type.
#' @param version Version to depend on, for versioned dependencies.
#' @inheritParams desc_set
#'
#' @family dependencies
#' @export
desc_set_dep <- generate_api("set_dep")
#' Set all package dependencies in DESCRIPTION
#'
#' @param deps Package dependency data frame, in the same format
#' returned by [desc_get_deps()].
#' @inheritParams desc_set
#'
#' @family dependencies
#' @export
desc_set_deps <- generate_api("set_deps")
#' Remove a package dependency from DESCRIPTION
#'
#' @param package Package dependency to remove.
#' @param type Dependency type to remove. Sometimes a package is depended
#' on via multiple dependency types, e.g. `LinkingTo` and
#' `Imports`. Defaults to all types.
#' @inheritParams desc_set
#'
#' @family dependencies
#' @export
desc_del_dep <- generate_api("del_dep")
#' Remove all dependencies from DESCRIPTION
#'
#' @inheritParams desc_set
#'
#' @family dependencies
#' @export
desc_del_deps <- generate_api("del_deps")
#' Check for a dependency
#'
#' @inheritParams desc_set
#' @param package The package name.
#' @param type A dependency type or ``any`.
#' @return A logical scalar.
#'
#' @family dependencies
#' @export
desc_has_dep <- generate_api("has_dep", self = FALSE)
## -------------------------------------------------------------------
#' Set the Collate field in DESCRIPTION
#'
#' @param files Collate field to set, as a character vector.
#' @param which Which collate field to use. Collate fields can
#' be operating system type specific.
#' @inheritParams desc_set
#'
#' @family Collate field
#' @export
desc_set_collate <- generate_api("set_collate")
#' Query the Collate field in DESCRIPTION
#'
#' @inheritParams desc_set_collate
#' @return Character vector of file names.
#'
#' @family Collate field
#' @export
desc_get_collate <- generate_api("get_collate", self = FALSE)
#' Delete the Collate field from DESCRIPTION
#'
#' @inheritParams desc_set_collate
#'
#' @family Collate field
#' @export
desc_del_collate <- generate_api("del_collate")
#' Add one or more files to the Collate field, in DESCRIPTION
#'
#' @param files Character vector, files to add.
#' @inheritParams desc_set_collate
#'
#' @family Collate field
#' @export
desc_add_to_collate <- generate_api("add_to_collate")
#' Remove files from the Collate field.
#'
#' @param files Files to remove from the Collate field.
#' @inheritParams desc_set_collate
#'
#' @family Collate field
#' @export
desc_del_from_collate <- generate_api("del_from_collate")
## -------------------------------------------------------------------
#' Query all authors in Authors@R, in DESCRIPTION
#'
#' @inheritParams desc_set
#' @return A person object, see [utils::person()].
#'
#' @family Authors@R
#' @export
desc_get_authors <- generate_api("get_authors", self = FALSE)
#' Query authors by role in Authors@R, in DESCRIPTION
#'
#' @param role Role to query. Defaults to the package maintainer.
#' @inheritParams desc_set
#' @return A person object, see [utils::person()].
#'
#' @family Authors@R
#' @export
desc_get_author <- generate_api("get_author", self = FALSE)
#' Set authors in Authors@R, in DESCRIPTION
#'
#' @param authors Authors, to set, a person object, see [utils::person()].
#' @inheritParams desc_set
#'
#' @family Authors@R
#' @export
desc_set_authors <- generate_api("set_authors")
#' Add an author to Authors@R in DESCRIPTION
#'
#' @param given Given name.
#' @param family Family name.
#' @param email Email address.
#' @param role Role.
#' @param comment Comment.
#' @param orcid ORCID.
#' @param ror ROR ID.
#' @inheritParams desc_set
#'
#' @family Authors@R
#' @export
desc_add_author <- generate_api("add_author")
#' @title Add a role to one or more authors in Authors@R, in DESCRIPTION
#'
#' @description The author(s) can be specified by a combination of the `given`,
#' `family`, `email`, `comment`, `orcid` and `ror` fields.
#' If multiple filters are specified, then all must match
#' to identify the author(s).
#' You cannot both specify `orcid` and `ror`
#' since ORCID is for individuals, ROR for organizations.
#'
#' @param role Role to add.
#' @param given Given name to filter on. Regular expression.
#' @param family Family name to filter on. Regular expression.
#' @param email Email address to filter on. Regular expression.
#' @param comment Comment field to filter on. Regular expression.
#' @param orcid ORCID field to filter on.
#' @param ror ROR field to filter on.
#' @inheritParams desc_set
#'
#' @family Authors@R
#' @export
desc_add_role <- generate_api("add_role")
#' @title Add an ORCID to one or more authors in Authors@R, in DESCRIPTION
#'
#' @description The author(s) can be specified by a combination of the `given`,
#' `family`, `email`, `comment` and `role` fields.
#' If multiple filters are specified, then all must match
#' to identify the author(s).
#'
#' @param orcid orcid to add.
#' @param given Given name to filter on. Regular expression.
#' @param family Family name to filter on. Regular expression.
#' @param email Email address to filter on. Regular expression.
#' @param comment Comment field to filter on. Regular expression.
#' @param role Role field to filter on.
#' @inheritParams desc_set
#'
#' @family Authors@R
#' @export
desc_add_orcid <- generate_api("add_orcid")
#' Remove one or more authors from DESCRIPTION.
#'
#' It uses the Authors@R field. The author(s) to be removed
#' can be specified via any field(s). All authors matching all
#' specifications will be removed. E.g. if only `given = "Joe"`
#' is supplied, then all authors whole given name matches `Joe` will
#' be removed. The specifications can be (PCRE) regular expressions.
#'
#' @param role Role to filter on. Regular expression.
#' @inheritParams desc_add_role
#'
#' @family Authors@R
#' @export
desc_del_author <- generate_api("del_author")
#' @title Add an ROR to one or more authors in Authors@R, in DESCRIPTION
#'
#' @description The author(s) can be specified by a combination of the `given`,
#' `family`, `email`, `comment` and `role` fields.
#' If multiple filters are specified, then all must match
#' to identify the author(s).
#'
#' @param ror ror to add.
#' @param given Given name to filter on. Regular expression.
#' @param family Family name to filter on. Regular expression.
#' @param email Email address to filter on. Regular expression.
#' @param comment Comment field to filter on. Regular expression.
#' @param role Role field to filter on.
#' @inheritParams desc_set
#'
#' @family Authors@R
#' @export
desc_add_ror <- generate_api("add_ror")
#' @title Delete a role of an author, in DESCRIPTION
#'
#' @inherit desc_add_role description
#'
#' @param role Role to remove.
#' @inheritParams desc_add_role
#'
#' @family Authors@R
#' @export
desc_del_role <- generate_api("del_role")
#' Change maintainer of the package, in DESCRIPTION
#'
#' Only works with the Authors@R field.
#'
#' The current maintainer is kept if they have at least another role.
#'
#' @inheritParams desc_add_author
#'
#' @family Authors@R
#' @export
desc_change_maintainer <- generate_api("change_maintainer")
#' Add the current user as an author to DESCRIPTION
#'
#' Uses the Authors@R field.
#'
#' @param role Role to set for the user, defaults to contributor.
#' @param comment Comment, empty by default.
#' @param orcid ORCID, empty by default.
#' @inheritParams desc_set
#'
#' @details
#' `desc_add_me` is a convenience function, it adds the
#' current user as an author, and it needs the
#' `whoami` package to be installed. It'll add your ORCID ID
#' if you provide it as argument or save it as `ORCID_ID` environment
#' variable in .Renviron.
#' The full name is parsed using
#' `as.person` and collapsing the given name and the family name
#' in order to e.g. have the first and middle names together as given
#' name. This approach might be limited to some full name structures.
#'
#' @family Authors@R
#' @export
desc_add_me <- generate_api("add_me")
#' Add a GitHub user as an author to DESCRIPTION
#'
#' Uses the Authors@R field.
#'
#' @param username GitHub username of the GitHub user
#' @param role Role to set for the user, defaults to contributor.
#' @param comment Comment, empty by default.
#' @param orcid ORCID, empty by default.
#' @inheritParams desc_set
#'
#' @details
#' `desc_add_author_gh` is a convenience function, it adds the
#' GitHub user as an author, and it needs the
#' `gh` package to be installed.
#' The full name is parsed using
#' `as.person` and collapsing the given name and the family name
#' in order to e.g. have the first and middle names together as given
#' name. This approach might be limited to some full name structures.
#'
#' @family Authors@R
#' @export
desc_add_author_gh <- generate_api("add_author_gh")
#' Query the package maintainer in DESCRIPTION
#'
#' Either from the `Maintainer` or the `Authors@R` field.
#' @inheritParams desc_set
#' @return A character scalar.
#'
#' @family Authors@R
#' @export
desc_get_maintainer <- generate_api("get_maintainer", self = FALSE)
#' Coerce Author and Maintainer Fields to Authors@R
#'
#' Convert the `Author` and `Maintainer` fields to
#' `Authors@R`, which is necessary for other functions such as
#' `desc_get_authors()`.
#'
#' @inheritParams desc_set
#'
#' @details
#' If the `Authors@R` field does not exist,
#' `desc_coerce_authors_at_r` tries to parse the `Author` and
#' `Maintainer` fields with [utils::as.person()] and writes
#' them to the `Authors@R` field.
#' Note that `Author` and `Maintainer` are free-form fields, so
#' parsing them may fail.
#'
#' @export
#' @family Authors@R
desc_coerce_authors_at_r <- generate_api("coerce_authors_at_r")
## -------------------------------------------------------------------
#' Query the URL field in DESCRIPTION
#'
#' @inheritParams desc_set
#' @return A character vectors or URLs. A length zero vector is returned
#' if there is no URL field in the package.
#'
#' @export
desc_get_urls <- generate_api("get_urls", self = FALSE)
#' Set the URL field in DESCRIPTION
#'
#' The specified urls replace the current ones. The URL field is created
#' if it does not exist currently.
#'
#' @param urls A character vector of urls to set.
#' @inheritParams desc_set
#'
#' @export
desc_set_urls <- generate_api("set_urls")
#' Add URLs to the URL field in DESCRIPTION
#'
#' @param urls Character vector of URLs to add. Duplicate URLs are
#' eliminated.
#' @inheritParams desc_set
#'
#' @export
desc_add_urls <- generate_api("add_urls")
#' Delete URLs from the URL field in DESCRIPTION
#'
#' All URLs matching the specified pattern are deleted.
#'
#' @param pattern Perl-compatible regular expression, all URLs
#' matching this expression will be deleted.
#' @inheritParams desc_set
#'
#' @export
desc_del_urls <- generate_api("del_urls")
#' Remove all URLs from the URL field of DESCRIPTION
#'
#' @inheritParams desc_set
#'
#' @export
desc_clear_urls <- generate_api("clear_urls")
## -------------------------------------------------------------------
#' List the locations in the Remotes field in DESCRIPTION
#'
#' @inheritParams desc_set
#' @return A character vectors or remote locations. A length zero vector
#' is returned if there is no Remotes field in the package.
#'
#' @export
desc_get_remotes <- generate_api("get_remotes", self = FALSE)
#' Set the Remotes field in DESCRIPTION
#'
#' The specified locations replace the current ones. The Remotes field is
#' created if it does not exist currently.
#'
#' @param remotes A character vector of remote locations to set.
#' @inheritParams desc_set
#'
#' @export
desc_set_remotes <- generate_api("set_remotes")
#' Add locations in the Remotes field in DESCRIPTION
#'
#' @param remotes Character vector of remote locations to add.
#' Duplicate locations are eliminated. Note that existing locations
#' are not updated, so if you want to _change_ a remote location
#' of a package, you need to delete the old location first and then add
#' the new one.
#' @inheritParams desc_set
#'
#' @export
desc_add_remotes <- generate_api("add_remotes")
#' Delete locations from the Remotes field in DESCRIPTION
#'
#' All locations matching the specified pattern are deleted.
#'
#' @param pattern Perl-compatible regular expression, all locations
#' matching this expression will be deleted.
#' @inheritParams desc_set
#'
#' @export
desc_del_remotes <- generate_api("del_remotes")
#' Remove all locations from the Remotes field of DESCRIPTION
#'
#' This simply means that the field is deleted.
#'
#' @inheritParams desc_set
#'
#' @export
desc_clear_remotes <- generate_api("clear_remotes")
## -------------------------------------------------------------------
#' Query the package version in DESCRIPTION
#'
#' If the file has no `Version` field, or it is an invalid
#' version string, then it throws an error.
#'
#' @inheritParams desc_set
#' @return A [base::package_version] object.
#'
#' @export
#' @family version numbers
desc_get_version <- generate_api("get_version", self = FALSE)
#' Set the package version in DESCRIPTION
#'
#' Both `$set_version()` and `$bump_version()` use dots to
#' separate the version number components.
#'
#' @param version A string or a [base::package_version] object.
#' @inheritParams desc_set
#'
#' @export
#' @family version numbers
desc_set_version <- generate_api("set_version")
#' Increase the version number in DESCRIPTION
#'
#' The `which` parameter specifies which component to increase.
#' It can be a string referring to a component: `major`,
#' `minor`, `patch` or `dev`, or an integer
#' scalar, for the latter components are counted from one, and the
#' beginning. I.e. component one is equivalent to `major`.
#'
#' If a component is bumped, then the ones after it are zeroed out.
#' Trailing zero components are omitted from the new version number,
#' but if the old version number had at least two or three components, then
#' the one will also have two or three.
#'
#' The bumping of the `dev` version (the fourth component) is
#' special: if the original version number had less than four components,
#' and the `dev` version is bumped, then it is set to `9000`
#' instead of `1`. This is a convention often used by R developers,
#' it was originally invented by Winston Chang.
#'
#' Both `$set_version()` and `$bump_version()` use dots to
#' separate the version number components.
#'
#' @param which Which component to increase. See details below.
#' @inheritParams desc_set
#'
#' @export
#' @family version numbers
desc_bump_version <- generate_api("bump_version")
## -------------------------------------------------------------------
#' Query the built field in DESCRIPTION
#'
#' If the file has no `Built` field then it throws an error.
#'
#' @inheritParams desc_set
#' @return A list with fields `R`, `Platform`, `Date`,
#' `OStype`.
#'
#' @export
#' @family built
desc_get_built <- generate_api("get_built")
## -------------------------------------------------------------------
# nocov end