Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Update scripts to fetch schemas from bom core #336

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .gitmodules

This file was deleted.

11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.PHONY: help update_schemas setup

help:
@echo "update_schemas - Update the schemas"
@echo "setup - Generate dart code from the schemas"

update_schemas:
./update_schemas.sh

setup:
./setup.sh
31 changes: 7 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ $ cd flutter-deriv-api
$ flutter pub get
```

### Setup
```bash
$ make update_schemas
$ make setup
$ dart run build_runner build --delete-conflicting-outputs
```

### Use this package as a library

Add this to your package's `pubspec.yaml` file:
Expand All @@ -121,30 +128,6 @@ dependencies:
ref: master
```

### Use this package as a submodule

```
$ git submodule add https://github.com/regentmarkets/flutter-deriv-api.git
```

Add this to your package's `pubspec.yaml` file:

```
dependencies:
...
flutter_deriv_api:
path: ./flutter-deriv-api/
```

### Initialize and update submodule

```
$ git submodule init
$ git submodule update --remote
$ ./setup.sh
$ dart run build_runner build --delete-conflicting-outputs
```

### Run the tests

```
Expand Down
41 changes: 34 additions & 7 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
#!/bin/bash

# symlink files from submodule to lib/basic_api/generated
ls binary-websocket-api/config/v3/**/{receive,send}.json | perl -lpe'my $base = s{^binary-websocket-api/config/v3/}{}r; my $target = "lib/basic_api/generated/" . ($base =~ s{/}{_}r); symlink "../../../$_" => $target or die "no luck with symlink on $_ - $!" unless -r $target'
# Exit immediately if a command exits with a non-zero status
set -e

# copy manually added json files to lib/basic_api/generated if not already there
# cp -n lib/basic_api/manually/*.json lib/basic_api/generated
# Define the source and target directories
SOURCE_DIR="schemas"
TARGET_DIR="lib/basic_api/generated"

# generates lib/basic_api/generated/api.dart
ls lib/basic_api/generated | egrep '\.json$' | perl -lne'print qq{export "$_.dart";}' > lib/basic_api/generated/api.dart
perl -pi -e 's/.json//g' lib/basic_api/generated/api.dart
# Remove the target directory if it exists
if [ -d "$TARGET_DIR" ]; then
echo "Removing existing directory: $TARGET_DIR"
rm -rf "$TARGET_DIR"
fi

# Ensure the target directory exists
mkdir -p "$TARGET_DIR"

# Create symlinks for the JSON files
find "$SOURCE_DIR" -type f \( -name 'receive.json' -o -name 'send.json' \) | while read -r file; do
base=$(echo "$file" | sed "s|^$SOURCE_DIR/||")
target="$TARGET_DIR/$(echo "$base" | tr '/' '_')"
if [ ! -e "$target" ]; then
ln -s "../../../$file" "$target"
echo "Symlink created: $target -> $file"
else
echo "Symlink already exists: $target"
fi
done

# Uncomment the following line if you want to copy manually added JSON files to the target directory if not already there
# cp -n lib/basic_api/manually/*.json "$TARGET_DIR"

# Generate lib/basic_api/generated/api.dart
ls "$TARGET_DIR" | grep '\.json$' | awk '{print "export \"" $0 ".dart\";"}' > "$TARGET_DIR/api.dart"
perl -pi -e 's/.json//g' "$TARGET_DIR/api.dart"

echo "api.dart generated at $TARGET_DIR/api.dart"
38 changes: 38 additions & 0 deletions update_schemas.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

# Exit immediately if a command exits with a non-zero status
set -e

# Define the path for the submodule within the main repository
SUBMODULE_PATH="binary-websocket-api"
TARGET_PATH="schemas"

# Create a temporary directory
TEMP_DIR=$(mktemp -d)

# Function to clean up the temporary directory on exit
cleanup() {
rm -rf "$TEMP_DIR"
}
trap cleanup EXIT

# Clone the bom-core repository into the temporary directory
git clone --depth 1 [email protected]:regentmarkets/bom-core.git "$TEMP_DIR/bom-core"

# Check if the binary-websocket-api directory exists in the cloned repository
if [ ! -d "$TEMP_DIR/bom-core/$SUBMODULE_PATH" ]; then
echo "Directory $SUBMODULE_PATH does not exist in the bom-core repository. Aborting."
exit 1
fi

# Remove existing submodule directory if necessary
if [ -d "$TARGET_PATH" ]; then
echo "Removing existing directory: $TARGET_PATH"
rm -rf "$TARGET_PATH"
fi

# Copy the binary-websocket-api directory from the temporary directory to the current directory
cp -r "$TEMP_DIR/bom-core/$SUBMODULE_PATH/config/v3/" "$TARGET_PATH"


echo "Submodule 'binary-websocket-api' successfully copied into the main repository."
Loading