Scripts that download their dependencies

An ideal distributable script is fully contained in a single file. It runs on any compatible operating system with an appropriate language runtime. It is plain text, and you can copy and paste it. It does not require mucking about with a package manager, or several, to run. It does not conflict with other scripts' packages or require managing a project environment to avoid such conflicts.

The classic way to get around all of these issues with scripts is to limit yourself to using the scripting language's standard library. However, programmers writing scripts don't want to; they want to use libraries that do not come with the language by default. Some scripting languages, runtimes, and environments resolve this conflict by offering a means to download and cache a script's dependencies with just declarations in the script itself. This page lists such languages, runtimes, and environments. If you know more, drop me a line.

Contents

Anything with a Nix package

The Nix package manager can act as a #! interpreter and start another program with a list of dependencies available.

#! /usr/bin/env nix-shell
#! nix-shell -i python3 -p python3
print("Hello, world!".rjust(20, "-"))

Groovy

Groovy comes with an embedded JAR dependency manager.

#! /usr/bin/env groovy
@Grab(group='org.apache.commons', module='commons-lang3', version='3.12.0')
import org.apache.commons.lang3.StringUtils
println StringUtils.leftPad('Hello, world!', 20, '-')

JavaScript (Deno)

Deno downloads dependencies like a browser. Recent versions require "exec magic" to pass the run argument to /usr/bin/env. Here it is modified from a comment by RafaƂ Pocztarski. JSPM makes it possible to import npm packages meant for Node.js in the browser and thus Deno.

#! /bin/sh
":" //#; exec /usr/bin/env deno run "$0" "$@"
import leftPad from "https://dev.jspm.io/left-pad";
console.log(leftPad("Hello, world!", 20, "-"));

Kotlin (kscript)

kscript is an unofficial scripting tool for Kotlin that understands several comment-based directives.

#! /usr/bin/env kscript
//DEPS org.apache.commons:commons-lang3:3.12.0
import org.apache.commons.lang3.StringUtils
println(StringUtils.leftPad("Hello, world!", 20, "-"))

Scala (Ammonite)

The scripting environment in Ammonite lets you import Ivy dependencies.

#! /usr/bin/env amm
import $ivy.`org.apache.commons:commons-lang3:3.12.0`,
  org.apache.commons.lang3.StringUtils
println(StringUtils.leftPad("Hello, world!", 20, "-"))

Tags: list, programming.