Writing Julia packages
It is quite easy to write packages in Julia, and many developers write small packages quite often.
There are well-developed packages for writing package documentation:
Documenter.jl is like roxygen2 for R. It turns documented code into a (html) manual.
The Literate.jl package is very useful for turning scripts into markdown files, often used for documented examples in a package. Literate.jl can return a markdown, a Jupyter or Quarto notebook, or a scrubbed Julia file without the markdown/comments.
The
generate(“ExamplePackage”)
command in the Package manager REPL initializes a new package.The default package is very bare bone. Better to use a template from PkgTemplates.jl that sets up the author, license, Julia version, and Github actions for testing the code and building documentation automatically when pushed to Github. Here is an example template used to generate a package.
using PkgTemplates = Template(; myTemplate ="mattiasvillani", user=["Mattias Villani"], authors=v"1.11", julia=[ pluginsLicense(; name="MIT"), Git(; manifest=true, ssh=true), GitHubActions(; x86=true), Codecov(), Documenter{GitHubActions}(), Develop(), ], )generate("ExamplePackage", myTemplate)
This generates the following folder structure for the package:
The folder structure contains:
src
is the main folder for the source code, with the package’s main module in the ExamplePackage.jl file.docs
is for the documentation, where the file make.jl contains what is needed to build the documentation. Run it to build the docs.test
is for unit tests.The package also gets its own enviroment as seen by the generated Project.toml and Manifest.toml files.
The LICENSE and README.md files will determine the license and readme page once the package is pushed to Github.
The code also sets up a local git repository (in the hidden directory .git) with a remote address to a github repository (that you have to create on Github) named PackageName.jl. Note that the added .jl, which is standard for repo names on Github.
Use the Revise.jl package so that changes in your package is picked up by Julia. Revise.jl is a no-brainer, everyone uses it. Most of us put a
using Revise
in the startup.jl filethat Julia reads before starting Julia (this file lives in ~/.julia/config/startup.jl on Linux, but you need to create the folder and file first time). Revise is however used automatically when developing with the Julia extension for VS Code.