Making a package¶
Now that our code is getting more complex, it is time to make it into a package.
We start by making module UtilityFunctions
into a package, so we can reuse it more easily.
Since we plan to put this up on github
(for you to download), the package name must be unique (in my github repo, at least). So we will name all of the packages that we create for this class with an 890
suffix.
Generic instructions for creating packages are here.
It is helpful to have a github
account and put your packages there. But you can skip this step for the purposes of this class.
Disclaimer: I have not tried to create a package without github
info!
Generating the package¶
The most common approach is PkgTemplates
. It works like this:
In your base environment: pkg> add PkgTemplates
. This is now always available.
Now let's generate a UtilityFunctions890
package.
cd
to the directory where all of yourEcon890
code lives.- Make a template.
- Create the package.
- Add the source code and tests.
- Add dependencies.
- Run tests.
Details for my case:
julia> econDir = "/Users/lutz/Documents/julia/Econ890";
julia> cd(econDir);
julia> using PkgTemplates
julia> gitIgnore = ["*.jl.cov", "*.jl.*.cov", "*.jl.mem", "/deps/deps.jl", "/docs/build"];
julia> t = Template(; dir = econDir,
plugins = [
Documenter{GitHubActions},
Git(; jl = false, ignore = gitIgnore),
!CompatHelper,
!AppVeyor,
!TravisCI,
!TagBot
]);
julia> t("UtilityFunctions890")
[ Info: Running prehooks
[ Info: Running hooks
Activating environment at `~/Documents/julia/Econ890/UtilityFunctions/Project.toml`
Updating registry at `~/.julia/registries/General`
Updating registry at `~/.julia/registries/registryLH`
Updating git-repo `https://github.com/hendri54/registryLH`
No Changes to `~/Documents/julia/Econ890/UtilityFunctions/Project.toml`
No Changes to `~/Documents/julia/Econ890/UtilityFunctions/Manifest.toml`
Precompiling project...
1 dependency successfully precompiled in 1 seconds
Activating environment at `~/.julia/environments/v1.6/Project.toml`
[ Info: Running posthooks
[ Info: New package is at /Users/lutz/Documents/julia/Econ890/UtilityFunctions
The package has now been created.
Check that you have a new folder UtilityFunctions890
with sub-folders src
and test
.
After creating the package, copy the contents of utility.jl
into UtilityFunctions890.jl
.
Copy the tests into test/runtests.jl
.
We now need to modify those files a little to reflect the fact that the module UtilityFunctions890
is now a package. In this case, all we need to do is remove the lines
# This is one reason why packages work better.
include("utility.jl");
using .UtilityFunctions
in the runtests.jl
file.
Add dependencies (e.g., Pkg.add("Random")
) until ] test
works:
julia> Pkg.activate("UtilityFunctions890")
Activating environment at `~/Documents/julia/Econ890/UtilityFunctions890/Project.toml`
julia> Pkg.add("Random")
julia> using UtiltyFunctions890
Now run the tests: pkg> test
. And Bob is your uncle.