4
4
# ' @param project Character. Path to a project. By default it is current working
5
5
# ' directory.
6
6
# '
7
- proj_desc_get <- function (key , project = getwd() ) {
8
- stopifnot(is.character( key ) & length( key ) == 1 )
9
- stopifnot(is.character( project ) & length( project ) == 1 )
7
+ proj_desc_get <- function (key , project = " . " ) {
8
+ check_string( key )
9
+ check_string( project )
10
10
validate_desc(project )
11
11
desc_file <- file.path(project , " DESCRIPTION" )
12
12
value <- desc :: desc_get(key , desc_file )
13
13
unname(value )
14
14
}
15
15
16
- # ' @title Warn if cloud function is used not for current working directory
17
- # '
18
- # ' @description Functions for uploading/downloading files from project cloud
19
- # ' locations are designed to synchronize local and cloud folder structures.
20
- # ' That is e.g. when you call `cloud_s3_upload` with `file` parameter set to
21
- # ' "data/demo.csv" and `project` parameter set to something different from the
22
- # ' current working directory it is always assumed that "data/demo.csv" from
23
- # ' **the project's folder** and not from the current working directory needs
24
- # ' to be uploaded to S3. But for development purposes it is handy to be able
25
- # ' to call the functions not only for the current wd. This function checks
26
- # ' that project is set to the current wd. If not, it throws a warning and asks
27
- # ' if user wants to continue.
28
- # '
29
- # ' @param project Path to a project. By default it is current working directory.
30
- # '
31
- cloud_not_wd_warning <- function (project ) {
32
- stopifnot(is.character(project ) & length(project ) == 1 )
33
- if (cloud_talk()) {
34
- wd <- normalizePath(getwd())
35
- project <- normalizePath(project )
36
- if (wd != project ) {
37
- cli :: cli_warn(
38
- " This function is meant to be used without changing the \\
39
- {.arg project} parameter."
40
- )
41
- yeah <- cli_yeah(" Do you want to continue?" )
42
- if (! yeah ) {
43
- cli :: cli_abort(" Aborting" )
44
- }
45
- }
46
- }
47
- }
48
-
49
16
# ' @title Validate file path for cloud functions
50
17
# '
51
18
# ' @description Makes sure that file path passed to a cloud function is in the
@@ -57,7 +24,7 @@ cloud_not_wd_warning <- function(project) {
57
24
# ' file path.
58
25
# '
59
26
cloud_validate_file_path <- function (file , error = TRUE ) {
60
- stopifnot(is.character( file ) )
27
+ check_string( file )
61
28
res <- grepl(" ^([A-Za-z]|[0-9]|-|_|\\ .| |/)+$" , file )
62
29
if (error ) {
63
30
if (file == " " ) stop(" A valid file name should not be empty." )
@@ -81,7 +48,7 @@ cloud_validate_file_path <- function(file, error = TRUE) {
81
48
# '
82
49
# ' @noRd
83
50
cloud_validate_file_names <- function (x ) {
84
- stopifnot(is.character( x ) )
51
+ check_class( x , arg_class = " character " )
85
52
bad_na <- is.na(x )
86
53
bad_symbols <- ! grepl(" ^([A-Za-z]|[0-9]|-|_| |\\ .)+$" , x )
87
54
x_trimmed <- gsub(" ^[ ]+" , " " , gsub(" [ ]+$" , " " , x ))
@@ -98,60 +65,17 @@ cloud_validate_file_names <- function(x) {
98
65
return (invisible (TRUE ))
99
66
}
100
67
101
- # ' @title Assert that a key in project's DESCRIPTION file has a certain value
102
- # '
103
- # ' @description Given a path do DESCRIPTION file or to a project containing such
104
- # ' file makes sure that field `key` in it has value `value`.
105
- # ' - If this field is absent, proposes to populate it with `value`.
106
- # ' - If this field exists, but is populated with a different value, throws an
107
- # ' error.
108
- # ' - If this field exists and is populated with the right value, silently
109
- # ' returns TRUE.
110
- # '
111
- # ' @param key field name, character
112
- # ' @param value required field value, character
113
- # ' @param file path to DESCRIPTION file
114
- # '
115
- # ' @noRd
116
- assert_desc_field <- function (key , value , file ) {
117
- stopifnot(is.character(key ) & length(key ) == 1 )
118
- stopifnot(is.character(value ) & length(value ) == 1 )
119
- desc_value <- desc :: desc_get(keys = key , file = file )
120
- if (is.na(desc_value )) {
121
- cli :: cli_warn(" Field {.field key} does not exist in {.path DESCRIPTION}." )
122
- yeah <- cli_yeah(" Fill it with {.val value}?" , straight = TRUE )
123
- if (yeah ) {
124
- desc :: desc_set_list(key , value , file = file )
125
- return (invisible (TRUE ))
126
- } else {
127
- cli :: cli_abort(" Stopping" )
128
- }
129
- }
130
- if (desc_value != value )
131
- cli :: cli_abort(
132
- " Value found in {.path DESCRIPTION}, {.val {desc_value}}, is different \\
133
- from what should be there - {.val {value}}."
134
- )
135
- return (invisible (TRUE ))
136
- }
137
-
138
68
# ' @title Validate project's DESCRIPTION file
139
69
# '
140
- # ' @description Given a path to a project, figures out project name and base
141
- # ' package. Checks that `Name` and `BasePkg` fields in project's
142
- # ' DESCRIPTION file have corresponding values.
143
- # ' - If DESCRIPTION file is not found, proposes to create one and populate all
144
- # ' the main fields (including `Name` and `BasePkg`) automatically.
145
- # ' - If DESCRIPTION exists but `Name` and/or `BasePkg` are not populated,
146
- # ' proposes to populate these fields.
147
- # ' - If applied to a package folder, throws a warning.
70
+ # ' @description Checks that DESCRIPTION file exists in a project folder. If it's
71
+ # ' not the case, proposes to create a DESCRIPTION file from template.
148
72
# '
149
73
# ' @inheritParams cloud_not_wd_warning
150
74
# '
151
75
# ' @noRd
152
- validate_desc <- function (project = getwd() ) {
76
+ validate_desc <- function (project = " . " ) {
153
77
154
- desc_path <- file.path(project , " DESCRIPTION" )
78
+ desc_path <- normalizePath( file.path(project , " DESCRIPTION" ) )
155
79
156
80
if (! file.exists(desc_path )) {
157
81
@@ -164,16 +88,19 @@ validate_desc <- function(project = getwd()) {
164
88
desc_content <- c(
165
89
" Package: -" ,
166
90
" Name: [Project Name]" ,
167
- " Title: [Description about the project]"
91
+ " Title: [Project Title]" ,
92
+ " Description: [Project Description]"
168
93
)
169
94
170
95
writeLines(con = desc_path , desc_content )
171
96
172
97
cli :: cli_bullets(c(
173
98
" v" = " A sample DESCRIPTION file has been created at \\
174
- {.path {project}/DESCRIPTION}." ,
175
- " " = " Feel free to edit the {.field Name} and {.field Title} fields \\
176
- as needed to reflect your current project (optional)."
99
+ {.path {desc_path}}." ,
100
+ " " = " Feel free to edit the {.field Name}, {.field Title} and \\
101
+ {.field Description} fields as needed to reflect your current project \\
102
+ (optional)." ,
103
+ " " = " Please don't change the {.field Package} field."
177
104
))
178
105
return (invisible (TRUE ))
179
106
} else {
@@ -207,8 +134,12 @@ validate_desc <- function(project = getwd()) {
207
134
# ' names to give a relative file path.
208
135
# '
209
136
cloud_prep_ls <- function (data , path , recursive , full_names ) {
210
- stopifnot(is.data.frame(data ))
211
- stopifnot(all(c(" short_name" , " last_modified" , " size_b" ) %in% names(data )))
137
+ check_class(data , arg_class = " data.frame" )
138
+ required_cols <- c(" short_name" , " last_modified" , " size_b" )
139
+ if (! all(required_cols %in% names(data )))
140
+ cli :: cli_abort(" {.arg data} must contain the following column names: \\
141
+ {.val {required_cols}}" )
142
+
212
143
data <- data [data $ short_name != " " , ]
213
144
214
145
if (nrow(data ) == 0 ) {
0 commit comments