6. Apps
The app support in Pkg is currently considered experimental and some functionality and API may change.
Some inconveniences that can be encountered are:
You need to manually make
~/.julia/binavailable on the PATH environment.The path to the julia executable used is the same as the one used to install the app. If this julia installation gets removed, you might need to reinstall the app.
Apps are Julia packages that are intended to be run as "standalone programs" (by e.g. typing the name of the app in the terminal possibly together with some arguments or flags/options). This is in contrast to most Julia packages that are used as "libraries" and are loaded by other files or in the Julia REPL.
Creating a Julia app
A Julia app is structured similar to a standard Julia library with the following additions:
A
@mainentry point in the package module (see the Julia help on@mainfor details)An
[apps]section in theProject.tomlfile listing the executable names that the package provides.
A very simple example of an app that prints the reversed input arguments would be:
# src/MyReverseApp.jl
module MyReverseApp
function @main(ARGS)
for arg in ARGS
print(stdout, reverse(arg), " ")
end
return
end
end # module# Project.toml
# standard fields here
[apps]
reverse = {}The empty table {} is to allow for giving metadata about the app.
In TOML, reverse = {} under [apps] and a [apps.reverse] table are
equivalent. Pkg may rewrite between these two forms when it writes out the
project file.
After installing this app one could run:
$ reverse some input string
emos tupni gnirtsdirectly in the terminal.
Multiple Apps per Package
A single package can define multiple apps by using submodules. Each app can have its own entry point in a different submodule of the package.
# src/MyMultiApp.jl
module MyMultiApp
function @main(ARGS)
println("Main app: ", join(ARGS, " "))
end
include("CLI.jl")
end # module# src/CLI.jl
module CLI
function @main(ARGS)
println("CLI submodule: ", join(ARGS, " "))
end
end # module CLI# Project.toml
# standard fields here
[apps]
main-app = {}
cli-app = { submodule = "CLI" }This will create two executables:
main-appthat runsjulia -m MyMultiAppcli-appthat runsjulia -m MyMultiApp.CLI
Configuring Julia Flags
Apps can specify default Julia command-line flags that will be passed to the Julia process when the app is run. This is useful for configuring performance settings, threading, or other Julia options specific to your application.
Default Julia Flags
You can specify default Julia flags in the Project.toml file using the julia_flags field:
# Project.toml
[apps]
myapp = { julia_flags = ["--threads=4", "--optimize=2"] }
performance-app = { julia_flags = ["--threads=auto", "--startup-file=yes", "--depwarn=no"] }
debug-app = { submodule = "Debug", julia_flags = ["--check-bounds=yes", "--optimize=0"] }With this configuration:
myappwill run with 4 threads and optimization level 2performance-appwill run with automatic thread detection, startup file enabled, and deprecation warnings disableddebug-appwill run with bounds checking enabled and no optimization
Runtime Julia Flags
You can override or add to the default Julia flags at runtime using the -- separator. Everything before -- will be passed as flags to Julia, and everything after -- will be passed as arguments to your app:
# Uses default flags from Project.toml
myapp input.txt output.txt
# Override thread count, keep other defaults
myapp --threads=8 -- input.txt output.txt
# Add additional flags
myapp --threads=2 --optimize=3 --check-bounds=yes -- input.txt output.txt
# Only Julia flags, no app arguments
myapp --threads=1 --The final Julia command will combine:
Fixed flags (like
--startup-file=noand-m ModuleName)Default flags from
julia_flagsin Project.tomlRuntime flags specified before
--App arguments specified after
--
Overriding the Julia Executable
By default, apps run with the same Julia executable that was used to install them. You can override this globally using the JULIA_APPS_JULIA_CMD environment variable:
# Use a different Julia version for all apps
export JULIA_APPS_JULIA_CMD=/path/to/different/julia
myapp input.txt
# On Windows
set JULIA_APPS_JULIA_CMD=C:\path\to\different\julia.exe
myapp input.txtInstalling Julia apps
The installation of Julia apps is similar to installing Julia libraries but instead of using e.g. Pkg.add or pkg> add one uses Pkg.Apps.add or pkg> app add.
Apps can be installed from a registry, a git URL, or a local path:
pkg> app add Runic # from a registry
pkg> app add Runic@1.5 # specific version
pkg> app add https://github.com/fredrikekre/Runic.jl
pkg> app add path/to/Package # local path (must be a git repository)To install an app from a local package such that changes to the package are immediately reflected in the app, use Pkg.Apps.develop or pkg> app develop:
pkg> app develop path/to/PackageInstalled apps are updated with pkg> app update [name] (all apps if no name is given), listed with pkg> app status, and removed with pkg> app rm name.
Apps API Reference
Pkg.Apps.add — Function
Pkg.Apps.add(pkg::Union{String, PackageSpec})
Pkg.Apps.add(; name, uuid, version, url, rev, path, subdir)Install the apps of package pkg. The package is installed into an isolated
environment and shims for its apps are created in ~/.julia/bin (which should
be added to PATH). Apps can be added from a registry, a git URL, or a local
path (which needs to be a git repository).
Examples
Pkg.Apps.add("Runic")
Pkg.Apps.add(name = "Runic", version = "1.5")
Pkg.Apps.add(url = "https://github.com/fredrikekre/Runic.jl", rev = "master")
Pkg.Apps.add(path = "path/to/Package")Pkg.Apps.develop — Function
Pkg.Apps.develop(pkg::Union{String, PackageSpec})
Pkg.Apps.develop(; name, uuid, version, url, rev, path, subdir)Like Pkg.Apps.add but the apps run directly from the developed
project so changes made to it are immediately reflected in the apps.
Examples
Pkg.Apps.develop("Runic")
Pkg.Apps.develop(path = "path/to/Package")Pkg.Apps.update — Function
Pkg.Apps.update(name::Union{Nothing, String} = nothing)Update the package providing the app name (or the package named name) to
the latest compatible version, or all installed apps if no name is given.
For developed packages the dependencies of the project are updated instead.
Pkg.Apps.status — Function
Pkg.Apps.status(name::Union{Nothing, String} = nothing)Show the installed apps, the version of their package, and the julia command
used to run them. If name is given, limit the output to that app or package.
Pkg.Apps.rm — Function
Pkg.Apps.rm(name::String)Remove the app name, or all apps of the package named name. This removes
the shims from ~/.julia/bin and, when the last app of a package is removed,
its app environment.