/
publish_wiki.R
263 lines (197 loc) · 8.66 KB
/
publish_wiki.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
#' Publish an rmarkdown into wiki repo
#'
#' @title Publish into wiki repo
#' @description Publish an specific rmarkdown or qmd into wiki repository, specifying
#' the path of the wiki repository once that you already clone this repository
#' into your machine. See Details if is your first time publishing in a wiki
#' repository.
#'
#' @details If this is the first time you are going to publish in a wiki, you
#' must follow these steps:
#'
#' 1. The wiki repository must be cloned to a local working folder. To do this
#' first go to the wiki tab of your repository and you will need to manually
#' create the first wiki page. If you don't do this it will tell you that the
#' repository does not exist.
#'
#' 2. Once you have created the wiki home page, you will need to clone the wiki
#' repository just like you would any other repository on your computer (using
#' the link that is made available to you once you create the wiki home page).
#'
#' 3. Once you have cloned the wiki on your computer, what you should indicate
#' in the `publica_wiki` function is the` path_wiki_repo`. This is the full
#' address on your computer where this repository you cloned is located. For
#' example: `/home/client/project/wiki_repo`
#'
#' @param report the path of rmarkdown or qmd file
#' @param path_wiki_repo the complete path of wiki repository.
#' @param automatic_update if you like to do automatically the pull, commit and
#' push set in TRUE, if you like to do manually, set FALSE. Default is TRUE.
#' @param quiet_render_logs if you like to see the render logs of rmarkdown,
#' set in TRUE, in otherwise set FALSE. Default is TRUE.
#' @param new_file_name the new name for the markdown file.
#' @return Publish a md into wiki repository.
#' @examples
#'
#' \dontrun{
#' publish_wiki(report = "eda.Rmd",
#' path_wiki_repo = "/home/cliente/proyecto/wiki_repo")
#' }
#'
#' @export
publish_wiki <- function(report, path_wiki_repo, automatic_update = FALSE, new_file_name = NULL,
quiet_render_logs = FALSE) {
# Get the report extension
extension <- stringr::str_sub(report,start = -3, end = -1)
if (!(extension %in% c("Rmd","qmd"))) {
stop("Supported file types are .Rmd or .qmd")
}
# Repare path_wiki_repo.
if (stringr::str_sub(path_wiki_repo,
start = stringr::str_length(path_wiki_repo)) == "/") {
path_wiki_repo <- stringr::str_sub(
path_wiki_repo,
start = 1,
end = stringr::str_length(path_wiki_repo) - 1)
}
# Make pull if automatic_update is TRUE
if (automatic_update) {
if (nrow(gert::git_status(repo = path_wiki_repo)) > 0) {
gert::git_stash_save(repo = path_wiki_repo)
message("Some of your changes to the wiki repository were put into stash.
If you want to retrieve them you must go to the wiki repository
and run: git stash apply")
}
gert::git_pull(repo = path_wiki_repo)
}
# Render the rmarkdown to github_document output
quarto::quarto_render(report, output_format = "gfm",
quiet = quiet_render_logs)
# Copy md to wiki repository
dot_name <- paste0(".",extension)
base_name <- ifelse(is.null(new_file_name),
stringr::str_remove_all(basename(report), dot_name),
new_file_name)
md <- paste0(base_name, ".md")
path_md <- paste0(stringr::str_remove_all(report, basename(report)), md)
file.copy(path_md, path_wiki_repo, overwrite = TRUE)
file.remove(path_md)
# Copy images to wiki repository
files_folder <- paste0(base_name, "_files")
files_path <- paste0(stringr::str_remove_all(report, basename(report)),
files_folder)
if (dir.exists(files_path)) {
file.copy(files_path, path_wiki_repo, overwrite = TRUE, recursive = TRUE)
unlink(files_path, recursive = TRUE, force = TRUE)
}
# Update wiki repository with new changes
if (automatic_update) {
if (nrow(gert::git_status(repo = path_wiki_repo)) > 0) {
if (md %in% gert::git_status(repo = path_wiki_repo)$file) {
gert::git_add(md, repo = path_wiki_repo)
}
if (any(stringr::str_detect(gert::git_status(repo = path_wiki_repo)$file,
files_folder))) {
gert::git_add(files_folder, repo = path_wiki_repo)
}
if (any(gert::git_status(repo = path_wiki_repo)$staged)) {
gert::git_commit(paste0("Actualiza ", base_name), repo = path_wiki_repo)
gert::git_push(repo = path_wiki_repo)
} else {
message(paste0("There are no new changes or something that can be sent in ",
base_name, ".", extension))
}
} else {
message(paste0("There are no new changes or something that can be sent in ",
base_name, ".", extension))
}
} else {
if (dir.exists(paste0(path_wiki_repo, "/", files_folder))) {
cat(
paste("Go to the repository", path_wiki_repo,
"and execute the following commnads in terminal:"), "\n", "\n",
paste("git pull"), "\n",
paste("git add", md), "\n",
paste("git add", files_folder), "\n",
paste0("git commit -m 'Update ", base_name, "'"), "\n",
"git push \n")
} else {
cat(
paste("Go to the repository", path_wiki_repo,
"and execute the following commnads in terminal:"), "\n", "\n",
paste("git pull"), "\n",
paste("git add", md), "\n",
paste0("git commit -m 'Update ", base_name, "'"), "\n",
"git push \n")
}
}
}
#' Publica un rmarkdown en un wiki repo
#'
#' @title Publica en el wiki repo
#' @description Publique un rmarkdown o qmd específico en el repositorio wiki,
#' especificando la ruta del repositorio wiki una vez que ya haya clonado este
#' repositorio en su máquina. Consulte Detalles si es la primera vez que
#' publica en un repositorio wiki.
#'
#' @details Si es la primera vez que va a publicar en una wiki, se deben seguir
#' estos pasos:
#'
#' 1.El repositorio wiki debe clonarse en una carpeta de trabajo local.
#' Para hacer esto, primero vaya a la pestaña wiki de su repositorio
#' y deberá crear manualmente la primera página wiki. Si no lo hace,
#' le dirá que el repositorio no existe.
#'
#' 2. Una vez que haya creado la página de inicio de la wiki,
#' deberá clonar el repositorio de la wiki
#' como lo haría con cualquier otro repositorio en su computadora
#' (usando el enlace que está disponible para usted una vez que haya creado la
#' página de inicio de la wiki).
#'
#' 3. Una vez que haya clonado la wiki en su computadora, lo que debe indicar
#' en la función `publica_wiki` es la` path_wiki_repo`. Esta es la dirección
#' completa en su computadora donde se encuentra este repositorio que clonó.
#' Por ejemplo: `/home/client/project/wiki_repo`
#'
#' @param reporte la ruta al archivo rmarkdown o qmd
#' @param ruta_repo_wiki la ruta completa al repositorio wiki.
#' @param auto_actualizar Si desea hacer automáticamente el pull, commit y push
#' establecido en TRUE, si desea hacerlo manualmente, establezca FALSE. El
#' valor predeterminado es TRUE.
#' @param silenciar_bitacora Si desea ver la bitacora de renderización
#' establezca TRUE, de lo contrario establezca FALSE. El valor default es TRUE.
#' @param nuevo_nombre el nuevo nombre del archivo markdown.
#' @return Publica un md en un repositorio wiki.
#' @examples
#'
#' \dontrun{
#' publish_wiki(reporte = "eda.Rmd",
#' path_wiki_repo = "/home/cliente/proyecto/wiki_repo")
#' }
#'
#' @export
publica_wiki <- function(reporte = report, ruta_repo_wiki = path_wiki_repo,
auto_actualizar = TRUE, silenciar_bitacora= FALSE,
nuevo_nombre = NULL) {
if (!is.null(nuevo_nombre)) {
# Renombrar el archivo de reporte si se proporciona un nuevo nombre
reporte <- nuevo_nombre
}
## True and False
if (auto_actualizar == TRUE && silenciar_bitacora == FALSE) {
publish_wiki(reporte, ruta_repo_wiki,
automatic_update = TRUE, quiet_render_logs = FALSE)
## False and True
} else if (auto_actualizar == FALSE && silenciar_bitacora == TRUE) {
publish_wiki(reporte, ruta_repo_wiki,
automatic_update = FALSE, quiet_render_logs = TRUE)
## True and True
} else if (auto_actualizar == TRUE && silenciar_bitacora == TRUE) {
publish_wiki(reporte, ruta_repo_wiki,
automatic_update = TRUE, quiet_render_logs = TRUE)
## False and False
} else if (auto_actualizar == FALSE && silenciar_bitacora == FALSE) {
publish_wiki(reporte, ruta_repo_wiki,
automatic_update = FALSE, quiet_render_logs = FALSE)
}
}