-
Notifications
You must be signed in to change notification settings - Fork 0
/
R6-Repository.R
297 lines (278 loc) · 8.53 KB
/
R6-Repository.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
#' @title
#' R6 Repository class.
#'
#' @description
#' Class representing the Repository
#'
#' @export
#'
#' @family
#' Representations
#'
#' @examples
#' fetchedRepo <- tryCatch(
#' {
#' # Set dir to clone repository to.
#' tempDir <- tempdir()
#' pathToRepo <- file.path(tempDir, "glue")
#'
#' # Clone repo
#' git2r::clone(
#' url = "https://github.com/tidyverse/glue.git",
#' local_path = pathToRepo
#' )
#'
#' # Create instance of Repository object.
#' repo <- PaRe::Repository$new(path = pathToRepo)
#'
#' # Set fetchedRepo to TRUE if all goes well.
#' TRUE
#' },
#' error = function(e) {
#' # Set fetchedRepo to FALSE if an error is encountered.
#' FALSE
#' },
#' warning = function(w) {
#' # Set fetchedRepo to FALSE if a warning is encountered.
#' FALSE
#' }
#' )
#'
#' if (fetchedRepo) {
#' repo
#' }
Repository <- R6::R6Class(
classname = "Repository",
# Public ----
public = list(
#' @description
#' Initializer for Repository class
#'
#' @param path (\link[base]{character})\cr
#' Path to R package project
#'
#' @return `invisible(self)`
initialize = function(path) {
private$path <- normalizePath(path)
private$name <- basename(private$path)
private$git <- git2r::in_repository(private$path)
private$description <- desc::description$new(private$path)
private$functionUse <- NULL
private$gitIgnore <- private$setupGitIgnore()
private$validate()
private$fetchRFiles()
private$fetchCppFiles()
private$fetchJavaFiles()
private$fetchSqlFiles()
return(invisible(self))
},
#' @description
#' Get method for name.
#'
#' @return (\link[base]{character})\cr
#' Repository name
getName = function() {
return(private$name)
},
#' @description
#' Get method fro path
#'
#' @return (\link[base]{character})\cr
#' Path to Repository folder
getPath = function() {
return(private$path)
},
#' @description
#' Get method to get a list of \link[PaRe]{File} objects.
#'
#' @return (\link[base]{list})\cr
#' List of \link[PaRe]{File} objects.
getFiles = function() {
files <- list(
R = private$rFiles,
cpp = private$cppFiles,
o = private$oFiles,
h = private$hFiles,
java = private$javaFiles,
sql = private$sqlFiles
)
return(files)
},
#' @description
#' Get method to get only R-files.
#'
#' @return (\link[base]{list})\cr
#' List of \link[PaRe]{File} objects.
getRFiles = function() {
return(private$rFiles)
},
#' @description
#' Get method to get the description of the package. See: \link[desc]{description}.
#'
#' @return (\link[desc]{description})\cr
#' Description object.
getDescription = function() {
return(private$description)
},
#' @description
#' Get method for functionUse, will check if functionUse has already been
#' fetched or not.
#'
#' @return (\link[base]{data.frame})\cr
#' See \link[PaRe]{getFunctionUse}.
getFunctionUse = function() {
if (is.null(private$functionUse)) {
private$functionUse <- getFunctionUse(self, verbose = TRUE)
}
return(private$functionUse)
},
#' @description
#' Method to run 'git checkout <branch/commit hash>'
#'
#' @param branch (\link[base]{character})\cr
#' Name of branch or a hash referencing a specific commit.
#' @param ...
#' Further parameters for \link[git2r]{checkout}.
#'
#' @return `invisible(self)`
gitCheckout = function(branch, ...) {
tryCatch(
{
git2r::checkout(object = private$path, branch = branch, ...)
message(glue::glue("Switched to: {branch}"))
message("Re-initializing")
self$initialize(path = private$path)
},
error = function(e) {
message(glue::glue("Availible branches: {paste(names(git2r::branches(private$path)), collapse = ', ')}"))
stop(glue::glue("Branches: '{branch}' not found"))
}
)
return(invisible(self))
},
#' @description
#' Method to run 'git pull'
#'
#' @param ...
#' Further parameters for \link[git2r]{pull}.
#'
#' @return `invisible(self)`
gitPull = function(...) {
message("Pulling latest")
git2r::pull(repo = private$path, ...)
message("Re-initializing")
self$initialize(path = private$path)
return(invisible(self))
},
#' @description
#' Method to fetch data generated by 'git blame'.
#'
#' @return (\link[dplyr]{tibble})
#' | column | data type |
#' | ---------- | ---------------------- |
#' | repository | \link[base]{character} |
#' | author | \link[base]{character} |
#' | file | \link[base]{character} |
#' | date | \link[base]{character} |
#' | lines | \link[base]{integer} |
gitBlame = function() {
files <- unlist(self$getFiles())
dplyr::bind_rows(lapply(files, function(file) {
file$getBlameTable()
}))
}
),
# Private ----
private = list(
name = "name",
path = "",
rFiles = NULL,
cppFiles = NULL,
oFiles = NULL,
hFiles = NULL,
sqlFiles = NULL,
javaFiles = NULL,
git = NULL,
description = NULL,
functionUse = NULL,
gitIgnore = c(),
validate = function() {
errorMessages <- checkmate::makeAssertCollection()
# .rproj file
rproj <- list.files(file.path(private$path), pattern = "*.Rproj", full.names = TRUE)
if (length(rproj) == 0) {
rproj <- ".Rproj"
}
checkmate::assertFileExists(rproj, add = errorMessages)
checkmate::reportAssertions(collection = errorMessages)
status <- git2r::status(repo = private$path)
if (length(status$staged) > 0) {
warning(glue::glue("Staged chagned not committed, unexpected behaviour expected."))
}
return(invisible(self))
},
setupGitIgnore = function() {
ignorePath <- file.path(private$path, ".gitignore")
if (file.exists(ignorePath)) {
gitIgnore <- readLines(ignorePath, warn = FALSE)
gitIgnore <- gitIgnore[!startsWith(x = gitIgnore, prefix = "#")]
gitIgnore <- gitIgnore[!gitIgnore == ""]
gitIgnore <- stringr::str_replace_all(string = gitIgnore, pattern = "\\*", replacement = ".")
private$gitIgnore <- gitIgnore
}
},
filterIgnored = function(paths) {
for (pat in private$gitIgnore) {
paths <- paths[!grepl(pattern = pat, x = paths)]
}
return(paths)
},
fetchRFiles = function() {
paths <- list.files(file.path(private$path, "R"), recursive = TRUE) %>%
private$filterIgnored()
private$rFiles <- unlist(lapply(paths, function(path) {
File$new(repoPath = private$path, filePath = file.path("R", path))
}))
return(invisible(self))
},
fetchCppFiles = function() {
paths <- list.files(
path = private$path,
pattern = "\\.(cpp|O|h)$",
recursive = TRUE
) %>%
private$filterIgnored()
cpp <- paths[endsWith(paths, ".cpp")]
o <- paths[endsWith(paths, ".o")]
h <- paths[endsWith(paths, ".h")]
private$cppFiles <- lapply(cpp, function(path) {
File$new(repoPath = private$path, filePath = path)
})
private$oFiles <- lapply(o, function(path) {
File$new(repoPath = private$path, filePath = path)
})
private$hFiles <- lapply(h, function(path) {
File$new(repoPath = private$path, filePath = path)
})
},
fetchJavaFiles = function() {
paths <- list.files(path = private$path, pattern = "\\.java$", recursive = TRUE)
paths <- paths[endsWith(paths, ".java")] %>%
private$filterIgnored()
private$javaFiles <- lapply(paths, function(path) {
File$new(repoPath = private$path, filePath = path)
})
},
fetchSqlFiles = function() {
paths <- list.files(
path = private$path,
pattern = "\\.sql$",
recursive = TRUE
) %>%
private$filterIgnored()
private$sqlFiles <- lapply(paths, function(path) {
File$new(repoPath = private$path, filePath = path)
})
}
)
)