Ballerina Hello World: The IntelliJ Style!
Disclaimer - If you are someone who accidentally stumbled upon this article and still wondering, you are just about to witness one of the most interesting programming languages which recently came into play! :D
The Ballerina Lang: What is it??
Ballerina is a new general-purpose and open source programming language specialized in microservices, easy integration and serverless computing. More importantly, every ballerina program is also represented as a sequence diagram that illustrates distributed and concurrent interactions automatically. Building this language platform and its ecosystem has been a massive effort of nearly 3 years with over 250 contributors.
If you are interested to know more, you can find almost everything in our Ballerina Official Website.
So, as the first blog of this series (which I’m about to share some of my interesting stories and experiences of being a developer of a new programming language tooling), let’s have a quick tour where you’ll end up writing your first ballerina hello world program. Cool right?!
Before getting started…
We’ll be using Ballerina 1.0 distribution and Ballerina IntelliJ plugin 1.0 release for this tutorial and therefore you need to have IntelliJ IDEA installed. Also please make sure your IntelliJ IDEA version is 2018.2 or above as it’s the minimum compatible version for the ballerina 1.0 plugin.
Now it’s time to dance with Ballerina!
Installing ballerina distribution
Based on your operating system, you can download your preferred Ballerina installer from the Ballerina download page.
- IMPORTANT — After installing the ballerina distribution, please make sure that the ballerina has been successfully installed in your system by executing the
ballerina
command using the terminal.
Installing the Ballerina IntelliJ plugin
- Open IntelliJ IDEA and navigate to IntelliJ IDEA ->Preferences -> Plugins. (If you are using Ubuntu/Windows, try File -> Settings -> Plugins.)
- Search “Ballerina” using the search bar available in the plugins window and the Ballerina plugin will appear in the search results. Click Install to proceed.
- IMPORTANT — After installing the plugin, please make sure to proceed with the Restart IDE action to apply the plugin changes properly.
Creating your first Ballerina Project in IntelliJ
- Navigate to File -> New -> Project, select Ballerina as the type of the project and click Next to proceed with the ballerina project creation wizard.
2. Select a Ballerina SDK for the project, and click Next.
Tip: If you do not have an already-configured Ballerina SDK to select, click Configure, select the location of the Ballerina distribution and then click Next to continue with the project creation. However, if you don’t configure an SDK, the plugin is still able to auto-detect the Ballerina Home for you!
3. Enter a name for the project, a location to save it, and click Finish.
Creating and running the Ballerina HelloWorld Program
Note - Ballerina programmers can either structure their code into a single source code file or as modules under ballerina project source directory. you can read more How to Structure Ballerina Code section to know how you can structure your code into ballerina modules and we will be using the single file method for this guide in the next steps.
1. Now let’s add a new file to our new project, by right-clicking on the project root directory in the Project tool window, as shown below. (Please note that you can either create an empty ballerina file or a file template a with ballerina main function/service. For this guide we are going to use the ballerina main function template.)
IMPORTANT- After opening the created file, make sure that the green status icon is appeared in the bottom right corner of the IDE as shown above. (This status icon indicates the connection status with the ballerina language server, which serves most of the plugin features).
2. You can simply run this hello world program by clicking the run icon available inline with the main function and selecting the corresponding run command as shown below. (It will execute the main function and the program output will be shown in the Run window.)
So you have just successfully created and executed your first Ballerina hello world program using IntelliJ IDEA! Yay!!
But wait, You haven’t seen the most amazing feature yet ;)
Ballerina Sequence Diagrams
The underlying language semantics of Ballerina was designed by modelling how independent parties communicate via structured interactions. Subsequently, every Ballerina program can be displayed as a sequence diagram of its flow including endpoints as well as synchronous and asynchronous calls.
To view the sequence diagram of a Ballerina file, click the “split view” icon in the top right corner of the IDE window as shown in the below example. You can click on any part of the sequence diagram to see the Ballerina source code segment that is related to the diagram component and you can even edit your Ballerina source code using the “edit” option in the diagram.
Thanks everyone for your time and I hope you are now familiar with the basics of the language and the IntelliJ tooling support as well. See you soon with my next set of articles :) Happy coding!