5

How to Use Ballerina Local Repository

 2 years ago
source link: https://dzone.com/articles/how-to-use-ballerina-local-repository
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to Use Ballerina Local Repository

Learn how to use Ballerina programming language local repository for overriding dependencies with an example from a pramodya/greetings package.

Join the DZone community and get the full member experience.

Join For Free

The need for overriding dependencies in a programming language is a fundamental need for many scenarios. Here we are discussing how to override dependencies in Ballerina. If you are new to Ballerina, I recommend you to play with it before reading this article. You can find everything you need in the Learn section of Language’s website.

Let’s start! Think you are working on a library that is used in a larger critical application, and you want to test a bug fix for this library inside of the larger application. Then it’s crucial to test it before publishing this library to a public package repository like Ballerina central. This is where the Local Repository comes into the picture. Also, you can use it in the following scenarios:

  • You are going to publish a new library version to the central repository. but you want to do some integration testing across the entire library to confirm it works.
  • You want to try out a library that has not released bug fixes and new features in their git repository.
  • You have done a bug fix to your library, and you want to use it immediately in your application that is using the above library.

This blog explains how you can use Ballerina Local Repository to override dependencies in a Ballerina package. Here we discuss how to test a bugfix of a Ballerina package easily using Ballerina local repository.

You have found a bug in an existing ballerina package and you are thinking to fix it. However, before you publish into the Ballerina central, you want to test the package locally using another ballerina package that uses this package.

We are using pramodya/greetings package to demonstrate this scenario. You can find it in the ballerina central using this link

Create an Application Package That Uses Your Library

First, let’s create a ballerina package called my-package which consumes this library package.

$ bal new my-package

This will create a new ballerina package. Inside the package, you will find main.bal file. Let’s edit that file with the following content.

import ballerina/io;
import pramodya/greetings;

// Prints `Hello World`.

public function main() {
    io:println(greetings:sayHello("Doe"));
}

Now let’s run this package with the command bal run. Then you will see  greetings package will get pulled from central to your machine, After that, your program will be compiled and executed with the following output.

Hello, Doe

Add the Bugfix to the Library

First, let’s clone the repository of pramodya/greetings package to your local machine.

$ git clone https://github.com/pramodya1994/greetings.git

Then you can open this repository using VS Code. Please make sure you have installed the latest Ballerina plugin. Let's say you want to make some changes to the  sayHello function as a bugfix and also you want to introduce a new function sayBye. So we are going to change the lib.bal file with the following content.

# Prints `Hello` with input string name.
#
# + name - the input sting name
# + return - "Hello " with the input string name

public function sayHello(string name) returns string {
    if !(name is "") {
        // We are adding "!" in the end and remove "," after hello as a bug fix.
        return "Hello " + name + "!";
    }
    return "Hello, World!";
}

# Prints `Bye` with input string name.
#
# + name - the input sting name
# + return - "Bye" with the input string name

public function sayBye(string name) returns string {
    if !(name is "") {
        return "Bye " + name + "!";
    }
    return "Bye, World!";
}

Push the Package to the Local Repository

The local repository is a custom repository in the local file system. You can push a package to the repository by providing the --repository=local option with the bal push command below.

First, let’s create the bala with the following command.

$ bal build -c

You will see the following in the command output.

Creating bala
        target/bala/pramodya-greetings-any-0.1.0.bala

Now let’s push the package to the local repository with the following command.

$ bal push --repository=local

You will see the following log after successfully pushing pramodya/greetings library to the local repository.

Successfully pushed target/bala/pramodya-greetings-any-0.1.0.bala to 'local' repository.

Use Local Library in the My-Package Application Package

Now you should switch back to the my-package application you have created to use the greetings local package which we have pushed to the local repository a few seconds ago.

Open the Ballerina.toml file of the my-package application, and specify the repository for the greetings dependency by adding the following content to the end of the file. 

[[dependency]]
org = "pramodya"
name = "greetings"
version = "0.1.0"
repository = "local"

This is the syntax you can use to force resolve a package from the local repository. Now you can run your application again to see it is reflecting changes from the local package.

$ bal run

You will see the output of the program has changed with the following. You can see , in the middle has removed and ! has added to the end.

Hello Doe!

Now let's try t use the newly added sayBye function in our application project. Add the following line as the second line of the main method of the main.bal.

io:println(greetings:sayBye("Doe"));

Let’s run our package again. You will see the following output in the console. There you can see output from the newly added sayBye function.

Hello Doe!
Bye Doe!

Hope you found this useful! We will be looking into more use cases of overriding dependencies in the future. 


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK