Nothing
#' Enable Auto-Updates
#'
#' Configures automatic update checking for your Electron application.
#' GitHub Releases and generic HTTP servers are supported. An S3 provider is
#' planned and will be added in a future release.
#'
#' @param appdir Character path to app directory containing `_shinyelectron.yml`
#' @param provider Character update provider: `"github"` (the default) or
#' `"generic"`. `"s3"` is not yet wired into the build and will be added in a
#' future release.
#' @param owner Character GitHub username or organization (required for github provider)
#' @param repo Character GitHub repository name (required for github provider)
#' @param url Character address of the folder that holds the update files
#' (required for the generic provider). It must start with `http://` or
#' `https://` and hold no backslash or space. `http://` warns unless the
#' host is `localhost` or `127.0.0.1`.
#' @param check_on_startup Logical whether to check for updates when app starts. Default `TRUE`.
#' @param auto_download Logical whether to download updates automatically. Default `FALSE`.
#' @param auto_install Logical whether to install updates automatically on quit. Default `FALSE`.
#' @param verbose Logical whether to show progress messages. Default `TRUE`.
#'
#' @return Invisibly returns the path to the updated config file.
#'
#' @details
#' Auto-updates require:
#' 1. A published application (e.g., to GitHub Releases)
#' 2. Proper code signing for macOS and Windows (recommended)
#' 3. The electron-updater package (automatically included in build)
#'
#' ## Update Providers
#'
#' **GitHub Releases** (recommended for open source):
#' - Automatically detects new releases by comparing semver tags
#' - Requires `owner` and `repo` parameters
#' - Private repos require `GH_TOKEN` environment variable
#'
#' **S3 Bucket** (planned, not yet supported):
#' - For self-hosted updates behind a CDN
#' - Configure bucket, region, and path in `_shinyelectron.yml`
#' - Bucket must allow public reads or use CloudFront signed URLs
#' - Required bucket structure: `/{path}/latest-mac.yml`, `latest-linux.yml`, `latest.yml`
#' - Note: `enable_auto_updates()` currently rejects `"s3"` as a provider;
#' S3 support will be added in a future release.
#'
#' **Generic HTTP Server**:
#' - For any static web server hosting the update files
#' - Requires the `url` parameter: the folder that holds the files, saved as
#' `updates.generic.url` in `_shinyelectron.yml`
#' - Upload the `latest*.yml` files from `destdir/electron-app/dist/`, the
#' installers they name, and any `.blockmap` files beside them, side by
#' side in that folder
#' - Use `https://`; an `http://` address warns unless the host is
#' `localhost` or `127.0.0.1`
#'
#' ## Publishing Updates (GitHub Releases)
#'
#' After enabling auto-updates, follow this workflow to publish updates:
#'
#' 1. **Bump version** in `_shinyelectron.yml` (e.g., `version: "1.1.0"`)
#' 2. **Rebuild** with `export(appdir, destdir, build = TRUE)`
#' 3. **Create a GitHub Release** with a semver tag matching the version:
#' - Tag: `v1.1.0` (the `v` prefix is required)
#' - Upload the built artifacts from `destdir/electron-app/dist/`:
#' - macOS: `.dmg` and `latest-mac.yml`
#' - Windows: `.exe` installer and `latest.yml`
#' - Linux: `.AppImage` and `latest-linux.yml`
#' 4. The app checks for updates on startup (if `check_on_startup = TRUE`)
#' and notifies the user when a new version is available
#'
#' ## Code Signing
#'
#' Signing is what lets an app check who published an update. A signed
#' Windows app installs only an update signed by the same publisher; an
#' unsigned one, like a Linux AppImage, installs whatever the update address
#' serves, so it trusts that server (and, over `http://`, the network)
#' completely. Set `signing: sign: true` in your config and provide
#' credentials via environment variables (see `?validate_signing_config`).
#'
#' @examples
#' # Enable GitHub-based updates on a temporary app
#' app <- file.path(tempdir(), "enable-updates-demo")
#' dir.create(app, showWarnings = FALSE)
#' writeLines("library(shiny)", file.path(app, "app.R"))
#' enable_auto_updates(app, owner = "myusername", repo = "myapp")
#'
#' # Enable with automatic download
#' enable_auto_updates(app, owner = "myorg", repo = "dashboard",
#' auto_download = TRUE)
#'
#' @seealso [init_config()] for creating initial configuration
#' @export
enable_auto_updates <- function(appdir,
provider = "github",
owner = NULL,
repo = NULL,
url = NULL,
check_on_startup = TRUE,
auto_download = FALSE,
auto_install = FALSE,
verbose = TRUE) {
# Validate inputs
validate_directory_exists(appdir, "Application directory")
if (!is_nonempty_string(provider)) {
cli::cli_abort(c(
"{.arg provider} must be {.val github} or {.val generic}.",
"x" = "It is not a single string."
))
}
supported_providers <- c("github", "generic")
if (!provider %in% supported_providers) {
cli::cli_abort(c(
"Provider {.val {provider}} is not yet supported.",
"i" = "The S3 provider is planned but not yet implemented.",
"i" = 'Use {.code provider = "github"} or {.code provider = "generic"} instead.'
))
}
# Each provider takes only its own settings.
if (provider == "github" && !is.null(url)) {
cli::cli_abort(c(
"{.arg url} is used only by the generic provider.",
"i" = 'Use {.code provider = "generic"} to update from that address, or leave {.arg url} out for GitHub Releases.'
))
}
if (provider == "generic" && (!is.null(owner) || !is.null(repo))) {
cli::cli_abort(c(
"{.arg owner} and {.arg repo} belong to the GitHub provider.",
"i" = 'Leave them out with {.code provider = "generic"}, which reads only {.arg url}.'
))
}
# Validate provider-specific requirements
if (provider == "github") {
if (is.null(owner) || is.null(repo)) {
cli::cli_abort(c(
"GitHub provider requires {.arg owner} and {.arg repo}",
"i" = "Example: {.code enable_auto_updates(appdir, owner = 'myuser', repo = 'myapp')}"
))
}
}
if (provider == "generic") {
check_generic_update_url(url, arg = "url")
}
# Read existing config
config_path <- find_config(appdir)
if (is.null(config_path)) {
if (verbose) {
cli::cli_alert_info("No config file found, creating one...")
}
init_config(appdir, verbose = FALSE)
config_path <- find_config(appdir)
}
# Read and update config
config <- yaml::read_yaml(config_path)
# Set updates configuration
config$updates <- list(
enabled = TRUE,
provider = provider,
check_on_startup = check_on_startup,
auto_download = auto_download,
auto_install = auto_install
)
# Add provider-specific settings
if (provider == "github") {
config$updates$github <- list(
owner = owner,
repo = repo,
private = FALSE
)
}
if (provider == "generic") {
config$updates$generic <- list(url = url)
}
# Write updated config
yaml::write_yaml(config, config_path)
if (verbose) {
cli::cli_alert_success("Auto-updates enabled with {.val {provider}} provider")
if (provider == "github") {
cli::cli_alert_info("Updates will be published to: {.url https://github.com/{owner}/{repo}/releases}")
}
if (provider == "generic") {
cli::cli_alert_info("Updates will be read from: {.url {url}}")
}
cli::cli_alert_info("Settings:")
cli::cli_bullets(c(
"*" = "Check on startup: {.val {check_on_startup}}",
"*" = "Auto-download: {.val {auto_download}}",
"*" = "Auto-install on quit: {.val {auto_install}}"
))
}
invisible(config_path)
}
#' Disable Auto-Updates
#'
#' Disables automatic update checking in the configuration file.
#'
#' @param appdir Character path to app directory
#' @param verbose Logical whether to show progress messages. Default `TRUE`.
#'
#' @return Invisibly returns the path to the updated config file.
#'
#' @examples
#' # Disable updates on a temporary app
#' app <- file.path(tempdir(), "disable-updates-demo")
#' dir.create(app, showWarnings = FALSE)
#' writeLines("library(shiny)", file.path(app, "app.R"))
#' enable_auto_updates(app, owner = "myusername", repo = "myapp", verbose = FALSE)
#' disable_auto_updates(app)
#'
#' @export
disable_auto_updates <- function(appdir, verbose = TRUE) {
validate_directory_exists(appdir, "Application directory")
config_path <- find_config(appdir)
if (is.null(config_path)) {
cli::cli_abort("No configuration file found in: {.path {appdir}}")
}
config <- yaml::read_yaml(config_path)
if (is.null(config$updates) || !isTRUE(config$updates$enabled)) {
if (verbose) {
cli::cli_alert_info("Auto-updates are already disabled")
}
return(invisible(config_path))
}
config$updates$enabled <- FALSE
yaml::write_yaml(config, config_path)
if (verbose) {
cli::cli_alert_success("Auto-updates disabled")
}
invisible(config_path)
}
#' Check Auto-Update Status
#'
#' Reports the current auto-update configuration status.
#'
#' @param appdir Character path to app directory
#'
#' @return Invisibly returns the `updates` configuration list, which is always
#' present because `read_config()` deep-merges defaults. Elements include
#' `enabled` (logical, `FALSE` by default when auto-updates have never been
#' enabled), `provider` (character), `check_on_startup`, `auto_download`,
#' `auto_install` (logical), `github` (a list with `owner`, `repo`,
#' `private`), used by the GitHub provider, and `generic` (a list with
#' `url`), used by the generic HTTP provider.
#'
#' @examples
#' # Check update configuration on a temporary app
#' app <- file.path(tempdir(), "check-updates-demo")
#' dir.create(app, showWarnings = FALSE)
#' writeLines("library(shiny)", file.path(app, "app.R"))
#' enable_auto_updates(app, owner = "myusername", repo = "myapp", verbose = FALSE)
#' check_auto_update_status(app)
#'
#' @export
check_auto_update_status <- function(appdir) {
validate_directory_exists(appdir, "Application directory")
config <- read_config(appdir)
cli::cli_h2("Auto-Update Status")
if (!isTRUE(config$updates$enabled)) {
cli::cli_alert_warning("Auto-updates are {.strong disabled}")
cli::cli_alert_info("Enable with GitHub Releases: {.code shinyelectron::enable_auto_updates(appdir, owner = '...', repo = '...')}")
cli::cli_alert_info("Or with a generic HTTP server: {.code shinyelectron::enable_auto_updates(appdir, provider = 'generic', url = '...')}")
return(invisible(config$updates))
}
cli::cli_alert_success("Auto-updates are {.strong enabled}")
provider <- config$updates$provider %||% "github"
cli::cli_alert_info("Provider: {.val {provider}}")
if (provider == "github") {
owner <- config$updates$github$owner
repo <- config$updates$github$repo
if (!is.null(owner) && !is.null(repo)) {
cli::cli_alert_info("Repository: {.url https://github.com/{owner}/{repo}}")
}
}
if (provider == "generic") {
url <- config$updates$generic$url
cli::cli_alert_info("Address: {.url {url}}")
}
cli::cli_alert_info("Settings:")
cli::cli_bullets(c(
"*" = "Check on startup: {.val {config$updates$check_on_startup %||% TRUE}}",
"*" = "Auto-download: {.val {config$updates$auto_download %||% FALSE}}",
"*" = "Auto-install: {.val {config$updates$auto_install %||% FALSE}}"
))
invisible(config$updates)
}
#' Check the address of a generic update server
#'
#' The generic provider reads `latest.yml` and the installers from a folder on
#' a static web server. The address must be an `http://` or `https://` URL.
#' Plain `http://` lets anyone on the network change what the app downloads, so
#' it warns unless the host is `localhost` or `127.0.0.1`. An address with a
#' backslash, whitespace or a control character is refused, because the
#' updater would read it differently from how it looks.
#'
#' [enable_auto_updates()] calls it for its `url` argument, and
#' [check_update_settings()] for `updates.generic.url`.
#'
#' @param url Value of `updates.generic.url`.
#' @param arg Name of the setting, used in messages.
#' @return `url`, invisibly. Aborts with class `shinyelectron_invalid_update_url`
#' when `url` is not a single `http://` or `https://` address, and warns with
#' class `shinyelectron_insecure_update_url` for `http://` to any other host.
#' @keywords internal
check_generic_update_url <- function(url, arg = "updates.generic.url") {
if (!is_http_url(url)) {
cli::cli_abort(c(
"{.field {arg}} must be an {.val http://} or {.val https://} address.",
"x" = if (is_nonempty_string(url)) {
"Found {.val {url}}"
} else {
"It is missing or is not a single string."
},
"i" = "Set it to the folder that holds {.file latest.yml} and the installers."
), class = "shinyelectron_invalid_update_url")
}
if (grepl("[\\\\[:space:][:cntrl:]]", url)) {
cli::cli_abort(c(
"{.field {arg}} must not contain a backslash, a space or a control character.",
"x" = "Found {.val {url}}",
"i" = "A browser-style URL parser reads a backslash as a slash, so the host can differ from the one you see. Write a space as {.code %20}."
), class = "shinyelectron_invalid_update_url")
}
if (grepl("^http://", url, ignore.case = TRUE)) {
authority <- sub("^[^:]+://", "", url)
authority <- sub("[/?#].*$", "", authority)
host <- sub(".*@", "", authority)
host <- tolower(sub(":[0-9]*$", "", host))
if (!host %in% c("localhost", "127.0.0.1")) {
cli::cli_warn(c(
"{.field {arg}} uses {.val http://} to reach {.val {host}}.",
"i" = "Use {.val https://} so nobody on the network can change what the app downloads."
), class = "shinyelectron_insecure_update_url")
}
}
invisible(url)
}
#' Check the update settings a build will use
#'
#' Only while `updates.enabled` is true: the generic provider needs a usable
#' address ([check_generic_update_url()]), and the S3 provider stops, since
#' the build does not support it yet. Called from three places: by
#' [validate_config()], so for every [read_config()]; by [build_electron_app()];
#' and by `build_multi_app()`. The two build functions take a configuration
#' list that may not have come through [read_config()], and check it before
#' any output is deleted. [export()] has already read the configuration, so
#' it muffles the second `http://` warning from its build call.
#'
#' @param config List. The configuration, or `NULL`.
#' @return `config`, invisibly. Aborts with class
#' `shinyelectron_invalid_update_url` or
#' `shinyelectron_unsupported_update_provider`, and warns with class
#' `shinyelectron_insecure_update_url`.
#' @keywords internal
check_update_settings <- function(config) {
updates <- config$updates
if (!isTRUE(updates$enabled)) {
return(invisible(config))
}
if (identical(updates$provider, "generic")) {
check_generic_update_url(updates$generic$url)
} else if (identical(updates$provider, "s3")) {
cli::cli_abort(c(
"The {.val s3} update provider is not supported yet.",
"i" = "Set {.field updates.provider} to {.val github} or {.val generic}, or turn updates off."
), class = "shinyelectron_unsupported_update_provider")
}
invisible(config)
}
#' Run a build call without repeating the `http://` address warning
#'
#' [export()] wraps its call to [build_electron_app()] or `build_multi_app()`
#' in this, since its [read_config()] has already given the warning. Only
#' that warning class is muffled.
#'
#' @param expr The build call.
#' @return The value of `expr`.
#' @keywords internal
without_repeated_update_url_warning <- function(expr) {
withCallingHandlers(
expr,
shinyelectron_insecure_update_url = function(w) invokeRestart("muffleWarning")
)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.