tools/updateIonRangeSlider.R

tmpdir <- tempdir()

# https://github.com/IonDen/ion.rangeSlider
version <- "2.3.1"
# types_version <- version
types_version <- "2.3.0"

zip_src <- sprintf("https://github.com/IonDen/ion.rangeSlider/archive/%s.zip", version)
zip_target <- file.path(tmpdir, "ion.zip")
download.file(zip_src, zip_target)
unzip(zip_target, exdir = dirname(zip_target))
src <- file.path(dirname(zip_target), paste0("ion.rangeSlider-", version))
target <- rprojroot::find_package_root_file("inst", "www", "shared", "ionrangeslider")
unlink(target, recursive = TRUE)
dir.create(target)
# Move over JS files
file.rename(
  file.path(src, "js"),
  file.path(target, "js")
)

# Grab less src files and convert to sass
# Use `npx` to temp install and execute on the entire less folder
system(paste0("npx less2sass ", file.path(src, "less")))

# Copy over only the base (i.e., core) scss that we need for the shiny skin
dir.create(file.path(target, "scss"))
file.copy(
  file.path(src, "less", c("_base.scss", "_mixins.scss")),
  file.path(target, "scss", c("_base.scss", "_mixins.scss"))
)

# less2sass conversion doesn't convert this import correctly
base_css <- file.path(target, "scss", "_base.scss")
writeLines(
  sub("@import (reference)", "@import", readLines(base_css), fixed = TRUE),
  base_css
)

# Apply git patches *before* compiling skin Sass -> CSS (this should add the shiny skin)
patch_dir <- rprojroot::find_package_root_file("tools/ion.rangeSlider-patches")
for (patch in list.files(patch_dir, full.names = TRUE)) {
  tryCatch({
    message(sprintf("Applying %s", basename(patch)))
    withr::with_dir(rprojroot::find_package_root_file(), system(sprintf("git apply %s", patch)))
  },
    error = function(e) {
      quit(save = "no", status = 1)
    }
  )
}


# Now compile Sass -> CSS so that if the default styles are requested, we
# can serve them up without compilation (The distributed CSS includes all
# the skins in the same CSS file, but we want them split up)
library(sass)
withr::with_dir(
  target, {
    dir.create("css")
    sass_partial(
      sass_file("scss/shiny.scss"),
      bslib::bs_theme(version = 3),
      output = "css/ion.rangeSlider.css",
      options = sass_options()
    )
  }
)


# Save version to R file
writeLines(
  c(
    "# Generated by tools/updateIonRangeSlider.R; do not edit by hand",
    sprintf('version_ion_range_slider <- "%s"', version)
  ),
  rprojroot::find_package_root_file("R", "version_ion_range_slider.R")
)




# Finally, run yarn build so the JS patches propogate to the minified files
withr::with_dir(rprojroot::find_package_root_file(), {
  exit_code <- system(paste0("yarn add --dev ion-rangeslider@", version))
  if (exit_code != 0) stop("yarn could not install ion-rangeslider")

  exit_code <- system(paste0("yarn add @types/ion-rangeslider@", types_version))
  if (exit_code != 0) stop("yarn could not install @types/ion-rangeslider")

  system("yarn bundle_external_libs")
})
rstudio/shiny documentation built on June 14, 2024, 4:25 p.m.