@@ -42,21 +42,6 @@ find_bin <- function(prog = "diyabc") {
4242 return (bin_file )
4343}
4444
45- # ' Clean binary directory
46- # ' @keywords internal
47- # ' @author Ghislain Durif
48- clean_bin_dir <- function () {
49- # bin directory
50- path <- bin_dir()
51- # existing binary file
52- existing_bin_files <- list.files(path )
53- existing_bin_files <- existing_bin_files [str_detect(existing_bin_files ,
54- " diyabc|abcranger|dll" )]
55- # delete diyabc/abcranger files
56- if (length(existing_bin_files ) > 0 ) {
57- fs :: file_delete(file.path(path , existing_bin_files ))
58- }
59- }
6045
6146# ' Find which OS is running
6247# ' @keywords internal
@@ -85,6 +70,8 @@ get_os <- function() {
8570# ' @author Ghislain Durif
8671# ' @param prog string, name of the program to download, eligible name are
8772# ' `"diyabc-RF"` and `"abcranger"`.
73+ # ' @return integer value, `0` if download succeeded, `1` if download failed,
74+ # ' `-1` if latest version is already here.
8875# ' @importFrom fs file_chmod
8976# ' @importFrom jsonlite fromJSON
9077# ' @export
@@ -107,6 +94,7 @@ dl_latest_bin <- function(prog = "diyabc") {
10794 " releases/latest" ,
10895 sep = " /"
10996 )
97+
11098 # check latest release info
11199 release_info <- fromJSON(release_url )
112100
@@ -115,94 +103,91 @@ dl_latest_bin <- function(prog = "diyabc") {
115103
116104 # check if file in release
117105 if (nrow(release ) < 1 ) {
118- stop(str_c(" Issue with available files at " , release_url , " . " ,
119- " Contact DIYABC-RF support." ,
120- sep = " " ))
106+ stop(str_c(
107+ " Issue with files available at" , release_url , " ." ,
108+ " Please contact DIYABC-RF support." ,
109+ sep = " "
110+ ))
111+ }
112+
113+ # select release for current OS
114+ release <- subset(release , str_detect(release $ name , os_id ))
115+
116+ # check if release available for current OS
117+ if (nrow(release ) != 1 ) {
118+ stop(str_c(
119+ prog , " binary file is not available for" , os_id , " OS at" ,
120+ release_url , " ." ,
121+ " Please contact DIYABC-RF support." ,
122+ sep = " "
123+ ))
121124 }
122125
123126 # already existing binary file
124127 existing_bin_files <- list.files(path )
125128
126129 # download release
127- out <- lapply(
128- split(release , seq(nrow(release ))),
129- function (single_file ) {
130- # output
131- # check: 0 if dl is ok or latest version already here,
132- # 1 if dl failed
133- # -1 if no binary files was available
134- check <- 1
135- bin_name <- single_file $ name
136- bin_url <- single_file $ browser_download_url
137-
138- # abcranger/diyabc binary files
139- if (str_detect(bin_name , str_c(prog_name(prog ), " -" ,
140- os_id , sep = " " ))) {
141- if (! bin_name %in% existing_bin_files ) {
142- # avoid blacklisting
143- Sys.sleep(2 )
144- # dl
145- check <- download.file(
146- bin_url ,
147- destfile = file.path(path , bin_name ),
148- mode = " wb"
149- )
150- } else {
151- check <- 0
152- warning(str_c(
153- " The latest release" , bin_name ,
154- " was already downloaded." , sep = " "
155- ))
156- }
157- } else {
158- check <- - 1
159- }
160- return (check )
161- }
162- )
130+ check <- 1
131+ bin_name <- release $ name
132+ bin_url <- release $ browser_download_url
163133
164- # binary files not available at all among files in latest release
165- if (all(out == - 1 )) {
166- warning(str_c(" No binary file available for " , prog , " on " ,
167- os_id , " . " ,
168- " Contact DIYABC-RF support." ,
169- sep = " " ))
170- }
171-
172- # no download success among files in latest release
173- if (! any(out == 0 )) {
174- stop(" Issue with download" )
134+ # check if bin file already available locally
135+ if (! bin_name %in% existing_bin_files ) {
136+ # avoid blacklisting
137+ Sys.sleep(2 )
138+ # dl
139+ check <- download.file(
140+ bin_url ,
141+ destfile = file.path(path , bin_name ),
142+ mode = " wb"
143+ )
144+ } else {
145+ check <- - 1
146+ warning(str_c(
147+ " The latest release" , bin_name ,
148+ " was already downloaded." , sep = " "
149+ ))
175150 }
176151
177152 # zip extraction for diyabc on Windows
178153 zip_files <- list.files(path , pattern = " \\ .zip$" )
179154 if (length(zip_files ) > 0 ) {
180155 latest_zip <- which.max(file.info(file.path(path , zip_files ))$ mtime )
181- tmp <- utils :: unzip(file.path(path , zip_files [latest_zip ]), exdir = path )
156+ tmp <- utils :: unzip(
157+ file.path(path , zip_files [latest_zip ]),
158+ exdir = path
159+ )
182160 if (length(tmp ) == 0 ) {
183- stop(str_c(" Issue when unzipping " , zip_files [latest_zip ]))
161+ stop(str_c(
162+ " Issue when unzipping" , zip_files [latest_zip ], sep = " "
163+ ))
184164 }
185165 fs :: file_delete(file.path(path , zip_files ))
186166 }
187167
188168 # set up rights
189169 bin_files <- list.files(path , pattern = prog )
190170 fs :: file_chmod(file.path(path , bin_files ), " a+rx" )
171+
172+ # output
173+ return (check )
191174}
192175
193176# ' Download all latest diyabcGUI related binary files if missing
194177# ' @keywords internal
195178# ' @author Ghislain Durif
196179# ' @export
197180dl_all_latest_bin <- function () {
198- dl_latest_bin(" diyabc" )
199- dl_latest_bin(" abcranger" )
181+ check_diyabc <- dl_latest_bin(" diyabc" )
182+ check_abcranger <- dl_latest_bin(" abcranger" )
183+ return (lst(check_diyabc , check_abcranger ))
200184}
201185
202186# ' Custom print
203187# ' @keywords internal
204188# ' @author Ghislain Durif
205189pprint <- function (... ) {
190+ print(str_c(" --- content of " ,deparse(substitute(... ))))
206191 # message(as.character(...))
207192 print(... )
208193}
@@ -222,7 +207,7 @@ reset_sink <- function() {
222207# ' @author Ghislain Durif
223208logging <- function (... ) {
224209 if (getOption(" diyabcGUI" )$ verbose )
225- pprint (str_c(... , sep = " " , collapse = " " ))
210+ print (str_c(... , sep = " " , collapse = " " ))
226211}
227212
228213# ' Enable logging verbosity
0 commit comments