This week, I worked on Julia packaging. Since I knew close to nothing at first, I created a bash script for generating a package architecture at the command line – that ChatGPT definitely helped me craft, but in case you want to do the same, you’ll have to ask precise questions and check its answers.

There are actually other ways to do that, I found out. For example by using Julia function generate.

I think I’ll continue to use and improve the script anyway, because I know exactly what it does at each step. Here it is (can also be found here), use Calculator as package name when asked:


# make it an executable 
# chmod +x
# then run
# ./

# Prompt user to enter the package name
read -p "Enter package name: " package_name

# Replace spaces with underscores in the package name
package_name="${package_name// /_}"

# Create a folder for the package (folder name will be 'Calculator.jl')
mkdir "${package_name}.jl"
cd "${package_name}.jl" || exit

# Create the src and test directories
mkdir src
mkdir test

# Create necessary files within the src directory
touch src/"${package_name}.jl"
touch src/add.jl
touch src/subtract.jl

# Create necessary files within the test directory
touch test/runtests.jl

# Create Project.toml and populate it with initial information
cat <<EOF > Project.toml
name = "$package_name"
uuid = "$(uuidgen)"
authors = ["Your Name <>"]
version = "0.1.0"

# Dependencies, if any, will be listed here

julia = "1.6"  # Minimum Julia version required by the package

# Populate files with initial code

# src/Calculator.jl
cat <<EOF > src/"${package_name}.jl"
module $package_name


end # module

# src/add.jl
cat <<EOF > src/add.jl
function add(a, b)
    return a + b

# src/subtract.jl
cat <<EOF > src/subtract.jl
function subtract(a, b)
    return a - b

# test/runtests.jl
cat <<EOF > test/runtests.jl
using Test
using $package_name

# Test cases for add and subtract functions
@test add(2, 3) == 5
@test subtract(5, 2) == 3

# Additional test cases can be added here

# To run tests, execute this file or include it in a test suite

# Optionally, create a file

# Output success message
echo "$package_name package structure, Project.toml, and initial code created successfully."

Using the package in Julia console:

(@v1.8) pkg> add
     Cloning git-repo ``
    Updating git-repo ``
    Updating registry at `~/.julia/registries/General.toml`
   Resolving package versions...
    Updating `~/.julia/environments/v1.8/Project.toml`
  [05fd6518] + Calculator2 v0.1.0 ``
    Updating `~/.julia/environments/v1.8/Manifest.toml`
  [05fd6518] + Calculator2 v0.1.0 ``
Precompiling project...
  1 dependency successfully precompiled in 2 seconds
julia> import Calculator2 as calc
julia> calc.add(1, 2)