golem icon indicating copy to clipboard operation
golem copied to clipboard

Add replace arg to use_external

Open ColinFay opened this issue 3 years ago • 1 comments

We have chosen to prevent overwritting the external files when they exist, but we need to allow to force it in command line.

ColinFay avatar Jan 24 '22 10:01 ColinFay

Maybe I am mistaken but there are four use_external_* functions in R/use_files.R with a threefold/almost four-fold repetition.

This may not be relevant for your issue but it seems good to reduce duplication and one could as well go ahead and standardize the behaviour in a separate/encapsulated function call before adding more clutter with an additional argument replace. I am pretty sure there will be no breaking changes as it is really a repetition...

Here is what I suggest (and happy to do this of course):

  1. use_external_js_file and use_external_css_file are doing the same except for having a different ending ("js" vs "css") and printing behaviour
  2. use_external_html_template is doing almost the same as those above the with difference that
    • in the end, cat_download(where) is called separately instead of making the call inside file_created_dance(..., catfun = cat_downloaded) (which the others do); I do not think this is necessary, it seems like an artefact
    • in the beginning it is not checking for missing() "name" argument as there is a default provided, but adding the missing part would never harm
    • in the middle there is a missing if(file.ext() check (why? it seems to be easy to provide a default one)
  3. The fourth function use_external_file() is a bit different:
    • the first part makes the name check before the missing which seems like a hidden bug (at least compared to the other use_external funcs) , and defines the on-exit behavoiur not at the very beginning for no reasons:
      check_name_length(name)
    
      if (missing(name)) {
        name <- basename(url)
      }
      old <- setwd(fs_path_abs(pkg))
      on.exit(setwd(old))
    
    • it skips the creation of new_file which may be expected behaviour; but that could as well be handled with an optional argument of an encapsulated function, or simply performed for consistency reasons

Now that is a threefold (see (1) and (2)), almost four-fold repetition if you include (3).

What do yo uthink ?

ilyaZar avatar Mar 31 '23 07:03 ilyaZar