Skip to content

Commit

Permalink
Merge pull request #51 from ballerina-platform/kaneel-dev
Browse files Browse the repository at this point in the history
Add examples
  • Loading branch information
kaneeldias authored Nov 30, 2023
2 parents c3b3ef6 + 03f01ce commit 62a2624
Show file tree
Hide file tree
Showing 14 changed files with 900 additions and 147 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,9 @@ hs_err_pid*
# Ignore Gradle build output directory
build

# Ballerina configuartion file
Config.toml
/ballerina/docker.env

# Environment files
*.env
1 change: 0 additions & 1 deletion ballerina/tests/sms-sandbox-tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ function listSMSSandboxPhoneNumbersTest() returns error? {
test:assertTrue(phoneNumbers.length() > 0);
_ = from SMSSandboxPhoneNumber phoneNumber in phoneNumbers
do {
test:assertTrue(phoneNumber.phoneNumber.startsWith("+9477"));
test:assertTrue(phoneNumber.status == PENDING || phoneNumber.status == VERIFIED);
};
}
Expand Down
34 changes: 0 additions & 34 deletions examples/CreateTopic.bal

This file was deleted.

30 changes: 0 additions & 30 deletions examples/DeleteTopic.bal

This file was deleted.

30 changes: 0 additions & 30 deletions examples/Publish.bal

This file was deleted.

52 changes: 35 additions & 17 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,52 @@
# Examples

This directory contains a collection of sample code examples written in Ballerina. These examples demonstrate various
use cases of the module. You can follow the instructions below to build and run these examples.
The `aws.sns` connector provides practical examples illustrating usage in various scenarios. Explore these [examples](https://github.com/ballerina-platform/module-ballerinax-aws.sns/tree/master/examples), covering use cases like creating an alert service.

## Running an Example
1. [Weather alert service](https://github.com/ballerina-platform/module-ballerinax-aws.sns/tree/master/examples/weather-alert/main.bal)
Send weather alerts for multiple cities. Users can subscribe to different cities to receive alerts for their city only.

## Prerequisites

Execute the following commands to build an example from the source.
1. Follow the [instructions](https://github.com/ballerina-platform/module-ballerinax-aws.sns#set-up-aws-sns-api) to set up the AWS SNS API.

* To build an example
2. For each example, create a `Config.toml` file with your access key ID, secret access key, and region. Here's an example of how your Config.toml` file should look:

`bal build <example-name>`
```toml
accessKeyId = "<your-access-key-id>"
secretAccessKey = "<your-secret-access-key"
region = "<aws-instance-region>"
```

## Running an Example

* To run an example
Execute the following commands to build an example from the source:

`bal run <example-name>`
* To build an example:

## Building the Examples with the Local Module
```bash
bal build
```

**Warning**: Because of the absence of support for reading local repositories for single Ballerina files, the bala of
the module is manually written to the central repository as a workaround. Consequently, the bash script may modify your
local Ballerina repositories.
* To run an example:

```bash
bal run
```

## Building the Examples with the Local Module

Execute the following commands to build all the examples against the changes you have made to the module locally.
**Warning**: Due to the absence of support for reading local repositories for single Ballerina files, the Bala of the module is manually written to the central repository as a workaround. Consequently, the bash script may modify your local Ballerina repositories.

* To build all the examples
Execute the following commands to build all the examples against the changes you have made to the module locally:

`./build.sh build`
* To build all the examples:

```bash
./build.sh build
```

* To run all the examples
* To run all the examples:

`./build.sh run`
```bash
./build.sh run
```
31 changes: 0 additions & 31 deletions examples/Subscribe.bal

This file was deleted.

24 changes: 20 additions & 4 deletions examples/build.sh
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

BAL_EXAMPLES_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BAL_CENTRAL_DIR="$HOME/.ballerina/repositories/central.ballerina.io/"
BAL_CENTRAL_DIR="$HOME/.ballerina/repositories/central.ballerina.io"
BAL_HOME_DIR="$BAL_EXAMPLES_DIR/../ballerina"

set -e
Expand All @@ -28,20 +28,36 @@ cd "$BAL_HOME_DIR" &&
bal push --repository=local

# Remove the cache directories in the repositories
cacheDirs=($(ls -d "$BAL_CENTRAL_DIR"/cache-* 2>/dev/null))
cacheDirs=$(ls -d $BAL_CENTRAL_DIR/cache-* 2>/dev/null) || true
for dir in "${cacheDirs[@]}"; do
[ -d "$dir" ] && rm -r "$dir"
done
echo "Successfully cleaned the cache directories"

# Create the package directory in the central repository, this will not be present if no modules are pulled
mkdir -p "$BAL_CENTRAL_DIR/bala/ballerinax/$BAL_PACKAGE_NAME"

# Update the central repository
BAL_DESTINATION_DIR="$HOME/.ballerina/repositories/central.ballerina.io/bala/ballerinax/$BAL_PACKAGE_NAME"
BAL_SOURCE_DIR="$HOME/.ballerina/repositories/local/bala/ballerinax/$BAL_PACKAGE_NAME"
[ -d "$BAL_DESTINATION_DIR" ] && rm -r "$BAL_DESTINATION_DIR"
[ -d "$BAL_SOURCE_DIR" ] && cp -r "$BAL_SOURCE_DIR" "$BAL_DESTINATION_DIR"
echo "Successfully updated the local central repositories"

echo "$BAL_DESTINATION_DIR"
echo "$BAL_SOURCE_DIR"

# Loop through examples in the examples directory
find "$BAL_EXAMPLES_DIR" -type f -name "*.bal" | while read -r BAL_EXAMPLE_FILE; do
bal "$BAL_CMD" --offline "$BAL_EXAMPLE_FILE"
cd "$BAL_EXAMPLES_DIR"
for dir in $(find "$BAL_EXAMPLES_DIR" -type d -maxdepth 1 -mindepth 1); do
# Skip the build directory
if [[ "$dir" == *build ]]; then
continue
fi
(cd "$dir" && bal "$BAL_CMD" --offline && cd ..);
done

# Remove generated JAR files
find "$BAL_HOME_DIR" -maxdepth 1 -type f -name "*.jar" | while read -r JAR_FILE; do
rm "$JAR_FILE"
done
11 changes: 11 additions & 0 deletions examples/football-scores/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
org = "wso2"
name = "football_scores"
version = "0.1.0"
distribution = "2201.8.2"

[[dependency]]
org = "ballerinax"
name = "aws.sns"
version = "3.0.0"
repository = "local"
Loading

0 comments on commit 62a2624

Please sign in to comment.