Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion R/install.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ install_tinytex = function(
if (src_install) {
install_tinytex_source(repository, ...)
} else {
install_prebuilt(bundle, ..., repo = repository)
install_via_script(bundle, ..., repo = repository)
}
}
user_dir = install(user_dir, version, add_path, extra_packages)
Expand Down Expand Up @@ -328,6 +328,48 @@ macos_path = function(dir = NULL, action = 'add') {
ret
}

install_via_script = function(pkg = '', dir = '', version = 'daily', add_path = TRUE, extra_packages = NULL, repo = 'ctan') {
# Set env vars consumed by the install-bin-*.sh / install-bin-*.ps1 scripts
env_vars = c(TINYTEX_INSTALLER = pkg)
if (version != 'daily') env_vars['TINYTEX_VERSION'] = version

# xfun::normalize_path() expands ~ so we can pass TINYTEX_TEXDIR to the script;
# normalizePath() below (after install) resolves symlinks for the canonical path
target = if (dir == '') default_inst() else xfun::normalize_path(dir)
# Pass the full target path to the script only when using a custom directory
if (dir != '') env_vars['TINYTEX_TEXDIR'] = target

old_vars = xfun::set_envvar(env_vars)
on.exit(xfun::set_envvar(old_vars), add = TRUE)

if (is_windows()) {
script = 'install-bin-windows.ps1'
download_file('https://tinytex.yihui.org/install-bin-windows.ps1', script)
on.exit(unlink(script), add = TRUE)
script_args = c('-NonInteractive', '-File', script, if (!add_path) '--no-path')
res = system2('powershell', script_args)
} else {
script = 'install-bin-unix.sh'
download_file('https://tinytex.yihui.org/install-bin-unix.sh', script)
on.exit(unlink(script), add = TRUE)
res = system2('sh', c(script, if (!add_path) '--no-path'))
}
if (res != 0) stop('Failed to install TinyTeX', call. = FALSE)
if (!dir_exists(target)) stop('Failed to install TinyTeX.')
target = normalizePath(target)

opts = options(tinytex.tlmgr.path = find_tlmgr(target))
on.exit(options(opts), add = TRUE)

r_texmf(.quiet = TRUE)
# don't use the default random ctan mirror when installing on CI servers
if (repo != 'ctan' || tolower(Sys.getenv('CI')) != 'true')
tlmgr_repo(repo, stdout = FALSE, .quiet = TRUE)
tlmgr_install(setdiff(extra_packages, tl_pkgs()))

target
}

install_tinytex_source = function(repo = '', dir, version, add_path, extra_packages) {
if (version != 'daily') stop(
'tinytex::install_tinytex() does not support installing a specific version of ',
Expand Down
35 changes: 30 additions & 5 deletions tools/install-bin-unix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

set -e

# abort installation if TINYTEX_PREVENT_INSTALL=true
[ "$(echo "${TINYTEX_PREVENT_INSTALL}" | tr '[:upper:]' '[:lower:]')" != 'true' ] ||
(echo "The environment variable 'TINYTEX_PREVENT_INSTALL' was set to 'true', so the installation is aborted." && exit 1)

perl -mFile::Find /dev/null ||
(echo "perl is required but not found (https://github.com/rstudio/tinytex/issues/419)" && exit 1)

Expand Down Expand Up @@ -41,9 +45,12 @@ is_musl() {
}

if [ $OSNAME = 'Darwin' ]; then
TEXDIR=${TINYTEX_DIR:-~/Library}/TinyTeX
# default name in the archive; used to compute TEXDIR and rename after extraction
TEXDIR_DEFAULT_BASENAME="TinyTeX"
TEXDIR_DEFAULT_PARENT=${TINYTEX_DIR:-~/Library}
else
TEXDIR=${TINYTEX_DIR:-~}/.TinyTeX
TEXDIR_DEFAULT_BASENAME=".TinyTeX"
TEXDIR_DEFAULT_PARENT=${TINYTEX_DIR:-~}
if [ $OSNAME != 'Linux' ]; then
TINYTEX_INSTALLER="installer-unix"
elif is_musl; then
Expand All @@ -58,7 +65,18 @@ else
fi
fi

rm -rf $TEXDIR
# TINYTEX_TEXDIR allows callers (e.g. the R package) to specify the full
# installation path directly; fall back to the traditional TINYTEX_DIR-based default
TEXDIR=${TINYTEX_TEXDIR:-${TEXDIR_DEFAULT_PARENT}/${TEXDIR_DEFAULT_BASENAME}}

# the path where the archive will be extracted (always the standard basename under the
# same parent as TEXDIR, regardless of whether a custom name was requested)
TEXDIR_EXTRACTED="$(dirname "$TEXDIR")/$TEXDIR_DEFAULT_BASENAME"

rm -rf "$TEXDIR"
# also remove the standard extraction target when a custom path is requested,
# so the rename step below has a clean destination
[ "$TEXDIR" = "$TEXDIR_EXTRACTED" ] || rm -rf "$TEXDIR_EXTRACTED"

# determine the OS/arch suffix and file extension based on the naming scheme
if [ "$USE_NEW_NAMES" = true ] && [ "${TINYTEX_INSTALLER#"TinyTeX"}" != "$TINYTEX_INSTALLER" ]; then
Expand Down Expand Up @@ -101,8 +119,13 @@ if [ "${TINYTEX_INSTALLER#"TinyTeX"}" != "$TINYTEX_INSTALLER" ]; then
else
wget --retry-connrefused --progress=dot:giga -O "${INSTALLER_FILE}" ${TINYTEX_URL}
fi
tar xf "${INSTALLER_FILE}" -C $(dirname $TEXDIR)
if [ -n "$1" ]; then mv "${INSTALLER_FILE}" "$1/"; else rm "${INSTALLER_FILE}"; fi
tar xf "${INSTALLER_FILE}" -C "$(dirname "$TEXDIR")"
# rename to TEXDIR if a custom path (TINYTEX_TEXDIR) was requested and the
# archive extracted to a different name (e.g. .TinyTeX or TinyTeX)
[ "$TEXDIR" = "$TEXDIR_EXTRACTED" ] || mv "$TEXDIR_EXTRACTED" "$TEXDIR"
# the first positional arg may be a directory to move the installer to (used by
# build scripts); ignore it if it looks like a flag (e.g. --no-path)
if [ -n "$1" ] && [ "${1#--}" = "$1" ]; then mv "${INSTALLER_FILE}" "$1/"; else rm "${INSTALLER_FILE}"; fi
else
echo "We do not have a prebuilt TinyTeX package for this operating system ($(uname -s) $(uname -m))."
echo "I will try to install from source for you instead."
Expand All @@ -123,6 +146,8 @@ fi

[ $OSNAME != "Darwin" ] && ./tlmgr option sys_bin $BINDIR
./tlmgr postaction install script xetex # GH issue #313
# do not wrap lines in latex log (https://github.com/rstudio/tinytex/issues/322)
./tlmgr conf texmf max_print_line 10000

NO_PATH=0
for arg in "$@"; do
Expand Down
40 changes: 33 additions & 7 deletions tools/install-bin-windows.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
$ErrorActionPreference = 'Stop'

if ($env:TINYTEX_PREVENT_INSTALL -eq 'true') {
throw "The environment variable 'TINYTEX_PREVENT_INSTALL' was set to 'true', so the installation is aborted."
}

# parse --no-path argument
$AddPath = -not ($args -contains '--no-path')

# switch to a temp directory
cd $env:TEMP
[Environment]::CurrentDirectory = $PWD.Path
Expand All @@ -15,6 +22,10 @@ if (-not $env:TINYTEX_DIR) {
$env:TINYTEX_DIR = if ($env:APPDATA -match '^[!-~]+$') { $env:APPDATA } else { $env:ProgramData }
}

# TINYTEX_TEXDIR allows callers (e.g. the R package) to specify the full installation
# path directly; fall back to the traditional TINYTEX_DIR\TinyTeX default
$TargetDir = if ($env:TINYTEX_TEXDIR) { $env:TINYTEX_TEXDIR } else { "$($env:TINYTEX_DIR)\TinyTeX" }

# new naming scheme: TinyTeX-{N}-windows.exe for daily and versions after v2026.03.02
$UseNewNames = $true
if ($env:TINYTEX_VERSION) {
Expand Down Expand Up @@ -52,7 +63,7 @@ if ($BundleExt -eq 'exe') {
}

# save the downloaded file to the output dir (for build-tinytex-2.ps1)
if ($args[0]) {
if ($args[0] -and $args[0] -ne '--no-path') {
move $DownloadedFile "$($args[0])\$DownloadedFile"
} else {
del $DownloadedFile
Expand All @@ -61,12 +72,27 @@ if ($args[0]) {
# in case it was installed to APPDATA previously
rd $env:APPDATA\TinyTeX -r -fo -ErrorAction SilentlyContinue

rd $env:TINYTEX_DIR\TinyTeX -r -fo -ErrorAction SilentlyContinue
rd $env:TINYTEX_DIR\TinyTeX -r -fo -ErrorAction SilentlyContinue
move TinyTeX $env:TINYTEX_DIR
# remove any existing installation at the target directory
rd $TargetDir -r -fo -ErrorAction SilentlyContinue

# the bundle always extracts to a 'TinyTeX' directory in the current dir (TEMP);
# move it to the parent of TargetDir, then rename if a custom leaf name was requested
$TargetParent = Split-Path $TargetDir -Parent
$TargetLeaf = Split-Path $TargetDir -Leaf
if (-not (Test-Path $TargetParent)) { mkdir $TargetParent -Force | Out-Null }
# remove an existing TinyTeX dir in the target parent that may conflict with the move
rd (Join-Path $TargetParent 'TinyTeX') -r -fo -ErrorAction SilentlyContinue
move TinyTeX $TargetParent
if ($TargetLeaf -ne 'TinyTeX') {
rename-item (Join-Path $TargetParent 'TinyTeX') $TargetLeaf
}

# add tlmgr to PATH
Write-Host 'add tlmgr to PATH'
$tlmgr = "$env:TINYTEX_DIR\TinyTeX\bin\windows\tlmgr.bat"
& $tlmgr path add
$tlmgr = "$TargetDir\bin\windows\tlmgr.bat"
& $tlmgr postaction install script xetex
# do not wrap lines in latex log (https://github.com/rstudio/tinytex/issues/322)
& $tlmgr conf texmf max_print_line 10000
if ($AddPath) {
Write-Host 'add tlmgr to PATH'
& $tlmgr path add
}
Loading