first commit

This commit is contained in:
2025-12-11 06:26:12 -08:00
commit 4662fe2d3b
2327 changed files with 114173 additions and 0 deletions

View File

@@ -0,0 +1,116 @@
---
title: Single-file scripts that download their dependencies · DBohdan.com
updated: 2023-01-15 15:15:30Z
created: 2023-01-15 15:15:30Z
source: 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](https://docs.python.org/3/tutorial/venv.html) 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](https://dbohdan.com/contact).
## Contents
- [Anything with a Nix package](#anything-with-a-nix-package)
- [D](#d)
- [Groovy](#groovy)
- [JavaScript (Deno)](#javascript-deno)
- [Kotlin (kscript)](#kotlin-kscript)
- [Racket (Scripty)](#racket-scripty)
- [Scala (Ammonite)](#scala-ammonite)
## Anything with a Nix package
The Nix package manager can [act as a `#!` interpreter](https://nixos.org/manual/nix/stable/command-ref/nix-shell.html#use-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](https://dub.pm/advanced_usage).
```
<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](http://docs.groovy-lang.org/latest/html/documentation/grape.html).
```
#! /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](https://deno.land/) 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”](https://wiki.tcl-lang.org/page/exec+magic). Here the magic is modified from a [comment](https://github.com/denoland/deno/issues/929#issuecomment-429004626) 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)](https://coreutils.gnu.narkive.com/e0afmL4P/env-add-s-option-split-string-for-shebang-lines-in-scripts) and on [FreeBSD](https://www.freebsd.org/cgi/man.cgi?env) 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](https://github.com/holgerbrandl/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](https://docs.racket-lang.org/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](https://ammonite.io/#IvyDependencies).
```
<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](https://dbohdan.com/tags#list), [programming](https://dbohdan.com/tags#programming).

View File

@@ -0,0 +1,116 @@
---
title: Single-file scripts that download their dependencies · DBohdan.com
updated: 2023-01-15 15:15:30Z
created: 2023-01-15 15:15:30Z
source: 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](https://docs.python.org/3/tutorial/venv.html) 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](https://dbohdan.com/contact).
## Contents
- [Anything with a Nix package](#anything-with-a-nix-package)
- [D](#d)
- [Groovy](#groovy)
- [JavaScript (Deno)](#javascript-deno)
- [Kotlin (kscript)](#kotlin-kscript)
- [Racket (Scripty)](#racket-scripty)
- [Scala (Ammonite)](#scala-ammonite)
## Anything with a Nix package
The Nix package manager can [act as a `#!` interpreter](https://nixos.org/manual/nix/stable/command-ref/nix-shell.html#use-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](https://dub.pm/advanced_usage).
```
<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](http://docs.groovy-lang.org/latest/html/documentation/grape.html).
```
#! /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](https://deno.land/) 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”](https://wiki.tcl-lang.org/page/exec+magic). Here the magic is modified from a [comment](https://github.com/denoland/deno/issues/929#issuecomment-429004626) 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)](https://coreutils.gnu.narkive.com/e0afmL4P/env-add-s-option-split-string-for-shebang-lines-in-scripts) and on [FreeBSD](https://www.freebsd.org/cgi/man.cgi?env) 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](https://github.com/holgerbrandl/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](https://docs.racket-lang.org/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](https://ammonite.io/#IvyDependencies).
```
<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](https://dbohdan.com/tags#list), [programming](https://dbohdan.com/tags#programming).

View File

@@ -0,0 +1,116 @@
---
title: Single-file scripts that download their dependencies · DBohdan.com
updated: 2023-01-15 15:15:30Z
created: 2023-01-15 15:15:30Z
source: 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](https://docs.python.org/3/tutorial/venv.html) 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](https://dbohdan.com/contact).
## Contents
- [Anything with a Nix package](#anything-with-a-nix-package)
- [D](#d)
- [Groovy](#groovy)
- [JavaScript (Deno)](#javascript-deno)
- [Kotlin (kscript)](#kotlin-kscript)
- [Racket (Scripty)](#racket-scripty)
- [Scala (Ammonite)](#scala-ammonite)
## Anything with a Nix package
The Nix package manager can [act as a `#!` interpreter](https://nixos.org/manual/nix/stable/command-ref/nix-shell.html#use-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](https://dub.pm/advanced_usage).
```
<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](http://docs.groovy-lang.org/latest/html/documentation/grape.html).
```
#! /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](https://deno.land/) 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”](https://wiki.tcl-lang.org/page/exec+magic). Here the magic is modified from a [comment](https://github.com/denoland/deno/issues/929#issuecomment-429004626) 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)](https://coreutils.gnu.narkive.com/e0afmL4P/env-add-s-option-split-string-for-shebang-lines-in-scripts) and on [FreeBSD](https://www.freebsd.org/cgi/man.cgi?env) 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](https://github.com/holgerbrandl/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](https://docs.racket-lang.org/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](https://ammonite.io/#IvyDependencies).
```
<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](https://dbohdan.com/tags#list), [programming](https://dbohdan.com/tags#programming).

View File

@@ -0,0 +1,116 @@
---
title: Single-file scripts that download their dependencies · DBohdan.com
updated: 2023-01-15 15:15:30Z
created: 2023-01-15 15:15:30Z
source: 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](https://docs.python.org/3/tutorial/venv.html) 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](https://dbohdan.com/contact).
## Contents
- [Anything with a Nix package](#anything-with-a-nix-package)
- [D](#d)
- [Groovy](#groovy)
- [JavaScript (Deno)](#javascript-deno)
- [Kotlin (kscript)](#kotlin-kscript)
- [Racket (Scripty)](#racket-scripty)
- [Scala (Ammonite)](#scala-ammonite)
## Anything with a Nix package
The Nix package manager can [act as a `#!` interpreter](https://nixos.org/manual/nix/stable/command-ref/nix-shell.html#use-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](https://dub.pm/advanced_usage).
```
<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](http://docs.groovy-lang.org/latest/html/documentation/grape.html).
```
#! /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](https://deno.land/) 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”](https://wiki.tcl-lang.org/page/exec+magic). Here the magic is modified from a [comment](https://github.com/denoland/deno/issues/929#issuecomment-429004626) 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)](https://coreutils.gnu.narkive.com/e0afmL4P/env-add-s-option-split-string-for-shebang-lines-in-scripts) and on [FreeBSD](https://www.freebsd.org/cgi/man.cgi?env) 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](https://github.com/holgerbrandl/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](https://docs.racket-lang.org/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](https://ammonite.io/#IvyDependencies).
```
<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](https://dbohdan.com/tags#list), [programming](https://dbohdan.com/tags#programming).

View File

@@ -0,0 +1,116 @@
---
title: Single-file scripts that download their dependencies · DBohdan.com
updated: 2023-01-15 15:15:30Z
created: 2023-01-15 15:15:30Z
source: 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](https://docs.python.org/3/tutorial/venv.html) 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](https://dbohdan.com/contact).
## Contents
- [Anything with a Nix package](#anything-with-a-nix-package)
- [D](#d)
- [Groovy](#groovy)
- [JavaScript (Deno)](#javascript-deno)
- [Kotlin (kscript)](#kotlin-kscript)
- [Racket (Scripty)](#racket-scripty)
- [Scala (Ammonite)](#scala-ammonite)
## Anything with a Nix package
The Nix package manager can [act as a `#!` interpreter](https://nixos.org/manual/nix/stable/command-ref/nix-shell.html#use-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](https://dub.pm/advanced_usage).
```
<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](http://docs.groovy-lang.org/latest/html/documentation/grape.html).
```
#! /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](https://deno.land/) 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”](https://wiki.tcl-lang.org/page/exec+magic). Here the magic is modified from a [comment](https://github.com/denoland/deno/issues/929#issuecomment-429004626) 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)](https://coreutils.gnu.narkive.com/e0afmL4P/env-add-s-option-split-string-for-shebang-lines-in-scripts) and on [FreeBSD](https://www.freebsd.org/cgi/man.cgi?env) 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](https://github.com/holgerbrandl/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](https://docs.racket-lang.org/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](https://ammonite.io/#IvyDependencies).
```
<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](https://dbohdan.com/tags#list), [programming](https://dbohdan.com/tags#programming).

View File

@@ -0,0 +1,116 @@
---
title: Single-file scripts that download their dependencies · DBohdan.com
updated: 2023-01-15 15:15:30Z
created: 2023-01-15 15:15:30Z
source: 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](https://docs.python.org/3/tutorial/venv.html) 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](https://dbohdan.com/contact).
## Contents
- [Anything with a Nix package](#anything-with-a-nix-package)
- [D](#d)
- [Groovy](#groovy)
- [JavaScript (Deno)](#javascript-deno)
- [Kotlin (kscript)](#kotlin-kscript)
- [Racket (Scripty)](#racket-scripty)
- [Scala (Ammonite)](#scala-ammonite)
## Anything with a Nix package
The Nix package manager can [act as a `#!` interpreter](https://nixos.org/manual/nix/stable/command-ref/nix-shell.html#use-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](https://dub.pm/advanced_usage).
```
<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](http://docs.groovy-lang.org/latest/html/documentation/grape.html).
```
#! /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](https://deno.land/) 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”](https://wiki.tcl-lang.org/page/exec+magic). Here the magic is modified from a [comment](https://github.com/denoland/deno/issues/929#issuecomment-429004626) 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)](https://coreutils.gnu.narkive.com/e0afmL4P/env-add-s-option-split-string-for-shebang-lines-in-scripts) and on [FreeBSD](https://www.freebsd.org/cgi/man.cgi?env) 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](https://github.com/holgerbrandl/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](https://docs.racket-lang.org/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](https://ammonite.io/#IvyDependencies).
```
<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](https://dbohdan.com/tags#list), [programming](https://dbohdan.com/tags#programming).