🎓 A1Learner
🔍
0 of 47 lessons completed

Julia Package Structure

Package structure is the fixed layout of a package directory. The layout holds a Project.toml file, a src folder with the module file, and a test folder.

Project.toml names the package

The code writes a Project.toml text with a name, a uuid, and a version. It then reads the text back.

text = "name = \"Demo\"\nuuid = \"12345678-1234-1234-1234-123456789012\"\nversion = \"0.1.0\"\n"
write("Project.toml", text)
print(read("Project.toml", String))

The src folder holds the module file

The code makes the src folder and writes Demo.jl there. It then reads the module text back.

mkpath("src")
write("src/Demo.jl", "module Demo\nexport greet\ngreet() = 42\nend\n")
print(read("src/Demo.jl", String))

Three parts make the full layout

The code writes all three parts of the layout. Three file checks then print true.

mkpath("src")
mkpath("test")
write("Project.toml", "name = \"Demo\"\nversion = \"0.1.0\"\n")
write("src/Demo.jl", "module Demo\nend\n")
write("test/runtests.jl", "println(\"checks run\")\n")
println(isfile("Project.toml"))
println(isfile("src/Demo.jl"))
println(isfile("test/runtests.jl"))

Example

mkpath("src")
mkpath("test")
write("Project.toml", "name = \"Demo\"\nversion = \"0.1.0\"\n")
write("src/Demo.jl", "module Demo\nexport greet\ngreet() = 42\nend\n")
write("test/runtests.jl", "println(\"checks run\")\n")
print(read("Project.toml", String))
println(isfile("src/Demo.jl"))
println(isfile("test/runtests.jl"))

Key points

  • Project.toml: the file that names the package and states its version.
  • Name: the package name written in the Project.toml file.
  • Version: a version number such as 0.1.0 written in the Project.toml file.
  • Src folder: the folder that holds the module file.
  • Module file: a file such as src/Demo.jl that holds the module code.
  • Test folder: the folder that holds the check files for the package.

In this example, the code makes the src and test folders and writes all three files. It reads the Project.toml text back and checks that the module file and the check file exist.

Note

Project.toml is plain text, so code can write it and read it back.

Try it Yourself

Edit the code, then press Run.

Running is simulated · JuliaHub

Exercise

Which file names the package and states its version?