Package management
Often times when working on a project there arises a need to use some common reusable piece of code, most of the time such functions are provided by the standard library of the programming language. When it is not feasible for the standard library to provide such functions, these functions can be provided by an external library.
To fetch (download) & maintain different versions of external libraries we need a package manager.
LIGO libraries can be published to npm and using ligo install
command we can fetch these ligo libraries (It internally invokes the esy package manager).
There are 2 aspects to working with external packages
- Using a package published on npm
- Creating and publishing packages to npm
Pre-requites:
#
Using a LIGO package published on npmStart with empty package.json
file
We will need the LIGO compiler binary to compile smart contracts, to get the LIGO compiler follow these instructions.
Next we will use a simple dependency ligo-list-helper
which is published on npm, to get the library use the command
Now we will write a smart contract main.mligo
which will use the ligo-list-herpers
library
and we write some tests for our smart contract in main.test.mligo
To compile the contract to Michelson run the command
This will download the dependencies automatically, and compile the main.mligo
file.
To test the contract using ligo's testing framework run the command
If you working with an existing LIGO project, to install the dependencies, at the root of the project just run
By default dependencies are installed in the .ligo
directory at the root of the project, If you wish to change
the path where dependencies are installed use the --cache-path
flag to specify the path e.g.
#
Upgrading version of a LIGO packageDuring the lifecycle of a project, if you wish to upgrade the version of a LIGO package,
Just update the package version to the desired one in the package.json
.
and run the command
This will fetch the updated version of LIGO package, and the compiler will use the updated version of the package.
#
Using a LIGO package via REPLIf you wish to try out a LIGO package in the REPL environment, Install the LIGO package by following the steps above,
And then fire up the LIGO REPL with the additional flag --project-root
#
Creating and publishing LIGO packages to npmSince we are going to publish the library to npm, we start by creating a npm project by running
Fill in the details prompted by npm init
.
Next we are going to need the LIGO compiler binary, to get the LIGO compiler follow these instructions.
We are going the write the ligo-list-helpers
library that we used earlier.
and some tests for the library
To run the tests run the command
Now the final step is publishing the library to npm. The steps are the same as publishing yet another library on npm