Files
LogSeqDB/pages/Scripts/Single-file scripts that download their dependenci.sync-conflict-20250817-085613-UULL5XD.md
2025-12-11 06:26:12 -08:00

5.2 KiB
Raw Blame History

title, updated, created, source
title updated created source
Single-file scripts that download their dependencies · DBohdan.com 2023-01-15 15:15:30Z 2023-01-15 15:15:30Z https://dbohdan.com/scripts-with-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 languages standard library. However, programmers writing scripts dont 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 scripts 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

  • D

  • Groovy

  • JavaScript (Deno)

  • Kotlin (kscript)

  • Racket (Scripty)

  • Scala (Ammonite)

  • Anything with a Nix package

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

    <a id="cb1-1"></a>#! /usr/bin/env nix-shell
    <a id="cb1-2"></a>#! nix-shell -i python3 -p python3
    <a id="cb1-3"></a>print("Hello, world!".rjust(20, "-"))
    
  • D

    Ds official package manager DUB supports single-file packages.

    <a id="cb2-1"></a>#! /usr/bin/env dub
    <a id="cb2-2"></a>/+ dub.sdl:
    <a id="cb2-3"></a>name "foo"
    <a id="cb2-4"></a>+/
    <a id="cb2-5"></a>import std.range : padLeft;
    <a id="cb2-6"></a>import std.stdio : writeln;
    <a id="cb2-7"></a>void main() {
     <a id="cb2-8"></a>writeln(padLeft("Hello, world!", '-', 20));
    <a id="cb2-9"></a>}
    
  • 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. Deno 1.28 and later can also import from NPM packages. Current versions of Deno require you to pass a run argument to deno. One way to accomplish this from a script is with a form of “exec magic”. Here the magic is modified from a comment by Rafał Pocztarski.

    <a id="cb4-1"></a>#! /bin/sh
    <a id="cb4-2"></a>":" //#; exec /usr/bin/env deno run "$0" "$@"
    <a id="cb4-3"></a>import leftPad from "npm:left-pad";
    <a id="cb4-4"></a>console.log(leftPad("Hello, world!", 20, "-"));
    

    On Linux systems with recent GNU env(1) and on FreeBSD you can replace the magic with env -S.

    <a id="cb5-1"></a>#! /usr/bin/env -S deno run
    <a id="cb5-2"></a>import leftPad from "npm:left-pad";
    <a id="cb5-3"></a>console.log(leftPad("Hello, world!", 20, "-"));
    
  • Kotlin (kscript)

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

    <a id="cb6-1"></a>#! /usr/bin/env kscript
    <a id="cb6-2"></a>//DEPS org.apache.commons:commons-lang3:3.12.0
    <a id="cb6-3"></a>import org.apache.commons.lang3.StringUtils
    <a id="cb6-4"></a>println(StringUtils.leftPad("Hello, world!", 20, "-"))
    
  • Racket (Scripty)

    Scripty interactively prompts you to install the missing dependencies for a script in any Racket language.

    #! /usr/bin/env racket
    #lang scripty
    #:dependencies '("base" "typed-racket-lib" "left-pad")
    ------------------------------------------
    #lang typed/racket/base
    (require left-pad/typed)
    (displayln (left-pad "Hello, world!" 20 "-"))
    
  • Scala (Ammonite)

    The scripting environment in Ammonite lets you import Ivy dependencies.

    <a id="cb8-1"></a>#! /usr/bin/env amm
    <a id="cb8-2"></a>import $ivy.`org.apache.commons:commons-lang3:3.12.0`,
     <a id="cb8-3"></a>org.apache.commons.lang3.StringUtils
    <a id="cb8-4"></a>println(StringUtils.leftPad("Hello, world!", 20, "-"))
    

    Tags: list, programming.