diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index d7065e5..dea3b1f 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -21,26 +21,21 @@ jobs: go-version: '1.20' - uses: actions/checkout@v4 + + - name: Download libraries + run: go generate ./... - - name: Download and extract lbug library + - name: Set library environment variables run: | if [ "$RUNNER_OS" == "Linux" ]; then if [ "$RUNNER_ARCH" == "X64" ]; then - asset="liblbug-linux-x86_64.tar.gz" + os="linux-amd64" elif [ "$RUNNER_ARCH" == "ARM64" ]; then - asset="liblbug-linux-aarch64.tar.gz" + os="linux-arm64" fi - os="linux" - libname="liblbug.so" elif [ "$RUNNER_OS" == "macOS" ]; then - asset="liblbug-osx-universal.tar.gz" os="osx" - libname="liblbug.dylib" fi - curl -L -o lbug.tar.gz https://github.com/LadybugDB/ladybug/releases/latest/download/$asset - tar -xzf lbug.tar.gz - mkdir -p lib/dynamic/$os - mv $libname lib/dynamic/$os/ echo "LIBRARY_PATH=$(pwd)/lib/dynamic/$os:$LIBRARY_PATH" >> $GITHUB_ENV if [ "$RUNNER_OS" == "Linux" ]; then echo "LD_LIBRARY_PATH=$(pwd)/lib/dynamic/$os:$LD_LIBRARY_PATH" >> $GITHUB_ENV @@ -74,22 +69,18 @@ jobs: update: true msystem: UCRT64 install: >- - curl git mingw-w64-ucrt-x86_64-go mingw-w64-ucrt-x86_64-gcc unzip - uses: actions/checkout@v4 - - - name: Download and extract lbug library + + - name: Download libraries + run: go generate ./... + + - name: Set library environment variables run: | - asset="liblbug-windows-x86_64.zip" - curl -L -o lbug.zip https://github.com/LadybugDB/ladybug/releases/latest/download/$asset - unzip -o lbug.zip - mkdir -p lib/dynamic/windows - mv lbug_shared.dll lib/dynamic/windows/ - mv lbug_shared.lib lib/dynamic/windows/ echo "LIBRARY_PATH=$(pwd)/lib/dynamic/windows:$LIBRARY_PATH" >> $GITHUB_ENV - name: Build @@ -105,3 +96,4 @@ jobs: export PATH="$(pwd)/lib/dynamic/windows:$PATH" cd example go run main.go + diff --git a/.gitignore b/.gitignore index e65ea3d..cb88edc 100644 --- a/.gitignore +++ b/.gitignore @@ -68,3 +68,7 @@ Temporary Items .ionide example_db + +# Libraries and Headers +lib/dynamic + diff --git a/README.md b/README.md index c60c046..2585e6a 100644 --- a/README.md +++ b/README.md @@ -7,10 +7,23 @@ Official Go language binding for [LadybugDB](https://github.com/LadybugDB/ladybug). Ladybug is an embeddable property graph database management system built for query speed and scalability. For more information, please visit the [Ladybug GitHub repository](https://github.com/LadybugDB/ladybug) or the [LadybugDB website](https://ladybugdb.com). ## Installation +To use `go-ladybug` in your project, you need to add a `go:generate` directive to download the required dynamic libraries. -```bash -go get github.com/LadybugDB/go-ladybug -``` +1. Add `go-ladybug` to your project: + ```bash + go get github.com/LadybugDB/go-ladybug + ``` + +2. Add a `go:generate` directive in your project (e.g., in `main.go` or `tools.go`): + ```go + //go:generate sh -c "cd $(go list -f '{{.Dir}}' -m github.com/LadybugDB/go-ladybug) && go generate ./..." + ``` + +3. Run `go generate` and build your project: + ```bash + go generate ./... + go build + ``` ## Get started An example project is available in the [example](example) directory. diff --git a/cgo_shared.go b/cgo_shared.go index f3af921..91e73fd 100644 --- a/cgo_shared.go +++ b/cgo_shared.go @@ -3,7 +3,7 @@ package lbug //go:generate sh download_lbug.sh /* -#cgo darwin LDFLAGS: -lc++ -L${SRCDIR}/lib/dynamic/darwin -llbug -Wl,-rpath,${SRCDIR}/lib/dynamic/darwin +#cgo darwin LDFLAGS: -lc++ -L${SRCDIR}/lib/dynamic/osx -llbug -Wl,-rpath,${SRCDIR}/lib/dynamic/osx #cgo linux,amd64 LDFLAGS: -L${SRCDIR}/lib/dynamic/linux-amd64 -llbug -Wl,-rpath,${SRCDIR}/lib/dynamic/linux-amd64 #cgo linux,arm64 LDFLAGS: -L${SRCDIR}/lib/dynamic/linux-arm64 -llbug -Wl,-rpath,${SRCDIR}/lib/dynamic/linux-arm64 #cgo windows LDFLAGS: -L${SRCDIR}/lib/dynamic/windows -llbug_shared diff --git a/download_lbug.sh b/download_lbug.sh index 5f2e76f..0c72493 100755 --- a/download_lbug.sh +++ b/download_lbug.sh @@ -2,13 +2,110 @@ set -e +# Function to download and extract a specific library +download_library() { + local asset=$1 + local target_dir=$2 + local lib_pattern=$3 + local os_type=$4 + + echo "Downloading asset: $asset" + + # Create temp directory + local temp_dir=$(mktemp -d) + cd "$temp_dir" + + # Download the asset + local download_url="https://github.com/LadybugDB/ladybug/releases/latest/download/$asset" + echo " Downloading from: $download_url" + + if command -v curl >/dev/null 2>&1; then + curl -L -o "$asset" "$download_url" + elif command -v wget >/dev/null 2>&1; then + wget -O "$asset" "$download_url" + else + echo "ERROR: Neither curl nor wget is available" + exit 1 + fi + + # Extract the asset + case "$asset" in + *.tar.gz) + tar -xzf "$asset" + ;; + *) + unzip -q "$asset" + ;; + esac + + # Find and copy library file + local lib_file=$(find . -name "$lib_pattern" | head -1) + if [ -n "$lib_file" ]; then + # Create target directory + mkdir -p "$OLDPWD/$target_dir" + + cp "$lib_file" "$OLDPWD/$target_dir/" + echo "Copied $lib_pattern to $target_dir" + + # For Windows, also look for .lib if it exists + if [ "$os_type" = "windows" ]; then + local lib_import=$(find . -name "lbug_shared.lib" -o -name "lbug.lib" | head -1) + if [ -n "$lib_import" ]; then + cp "$lib_import" "$OLDPWD/$target_dir/" + echo "Copied $(basename "$lib_import") to $target_dir" + fi + fi + else + echo "ERROR: Library file ($lib_pattern) not found in the extracted files" + cd "$OLDPWD" + rm -rf "$temp_dir" + exit 1 + fi + + # Cleanup + cd "$OLDPWD" + rm -rf "$temp_dir" +} + +# Check if we should download all libraries +if [ -n "$DOWNLOAD_ALL_LIBS" ]; then + echo "Downloading all libraries for all platforms..." + + # Create temp directory for header file + temp_dir=$(mktemp -d) + cd "$temp_dir" + + # Download one package to get lbug.h (use osx-universal as it's reliable) + curl -L -o "liblbug-osx-universal.tar.gz" "https://github.com/LadybugDB/ladybug/releases/latest/download/liblbug-osx-universal.tar.gz" + tar -xzf "liblbug-osx-universal.tar.gz" + lbug_file=$(find . -name "lbug.h" | head -1) + if [ -n "$lbug_file" ]; then + cp "$lbug_file" "$OLDPWD" + echo "Copied lbug.h to project root" + else + echo "ERROR: lbug.h not found" + exit 1 + fi + cd "$OLDPWD" + rm -rf "$temp_dir" + + # Download all platform libraries + download_library "liblbug-linux-x86_64.tar.gz" "lib/dynamic/linux-amd64" "liblbug.so" "linux" + download_library "liblbug-linux-aarch64.tar.gz" "lib/dynamic/linux-arm64" "liblbug.so" "linux" + download_library "liblbug-osx-universal.tar.gz" "lib/dynamic/osx" "liblbug.dylib" "osx" + download_library "liblbug-windows-x86_64.zip" "lib/dynamic/windows" "lbug_shared.dll" "windows" + + echo "All libraries downloaded successfully!" + exit 0 +fi + # Detect OS os=$(uname -s) case $os in Linux) os="linux" ;; Darwin) os="osx" ;; MINGW*|CYGWIN*) os="windows" ;; - *) echo "❌ Unsupported OS: $os"; exit 1 ;; + *) echo "ERROR: Unsupported OS: $os"; exit 1 ;; esac # Detect Architecture @@ -16,33 +113,62 @@ arch=$(uname -m) case $arch in x86_64) arch="x86_64" ;; aarch64|arm64) arch="aarch64" ;; - *) echo "❌ Unsupported architecture: $arch"; exit 1 ;; + *) echo "ERROR: Unsupported architecture: $arch"; exit 1 ;; esac -# Determine asset name + +# Map to Go conventions for variable usage, but custom path construction +if [ "$os" = "osx" ]; then + go_os="darwin" +else + go_os="$os" +fi + +if [ "$arch" = "x86_64" ]; then + go_arch="amd64" +elif [ "$arch" = "aarch64" ]; then + go_arch="arm64" +else + go_arch="$arch" +fi + +# Construct target directory based on cgo_shared.go expectations +if [ "$go_os" = "linux" ]; then + platform="linux-${go_arch}" +elif [ "$go_os" = "darwin" ]; then + platform="osx" +elif [ "$go_os" = "windows" ]; then + platform="windows" +else + platform="${go_os}_${go_arch}" +fi + +target_dir="lib/dynamic/$platform" +echo "Target Directory: $target_dir" + +# Determine asset name and library pattern if [ "$os" = "osx" ]; then asset="liblbug-osx-universal.tar.gz" - ext="tar.gz" + lib_pattern="liblbug.dylib" elif [ "$os" = "windows" ]; then if [ "$arch" != "x86_64" ]; then - echo "❌ Windows only supports x86_64 architecture" + echo "ERROR: Windows only supports x86_64 architecture" exit 1 fi asset="liblbug-windows-x86_64.zip" - ext="zip" + lib_pattern="lbug_shared.dll" else asset="liblbug-linux-${arch}.tar.gz" - ext="tar.gz" + lib_pattern="liblbug.so" fi -echo "🔍 Detected OS: $os, Architecture: $arch" -echo "📦 Downloading asset: $asset" +echo "Detected OS: $os, Architecture: $arch" -# Create temp directory +# Create temp directory for header file temp_dir=$(mktemp -d) cd "$temp_dir" -# Download the asset +# Download the asset to get lbug.h download_url="https://github.com/LadybugDB/ladybug/releases/latest/download/$asset" echo " Downloading from: $download_url" @@ -51,29 +177,35 @@ if command -v curl >/dev/null 2>&1; then elif command -v wget >/dev/null 2>&1; then wget -O "$asset" "$download_url" else - echo "❌ Neither curl nor wget is available" + echo "ERROR: Neither curl nor wget is available" exit 1 fi -# Extract the asset -if [ "$ext" = "tar.gz" ]; then - tar -xzf "$asset" -else - unzip "$asset" -fi +# Extract to get lbug.h + case "$asset" in + *.tar.gz) + tar -xzf "$asset" + ;; + *) + unzip -q "$asset" + ;; + esac # Find and copy lbug.h lbug_file=$(find . -name "lbug.h" | head -1) if [ -n "$lbug_file" ]; then cp "$lbug_file" "$OLDPWD" - echo "✅ Copied lbug.h to project root" + echo "Copied lbug.h to project root" else - echo "❌ lbug.h not found in the extracted files" + echo "ERROR: lbug.h not found in the extracted files" exit 1 fi -# Cleanup +# Cleanup header temp directory cd "$OLDPWD" rm -rf "$temp_dir" -echo "🎉 Done!" \ No newline at end of file +# Download the platform-specific library +download_library "$asset" "$target_dir" "$lib_pattern" "$os" + +echo "Done!" \ No newline at end of file diff --git a/go.mod b/go.mod index 4f52451..5a11b9d 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,9 @@ go 1.20 require github.com/google/uuid v1.6.0 -require github.com/shopspring/decimal v1.4.0 -require github.com/stretchr/testify v1.9.0 +require github.com/shopspring/decimal v1.4.0 + +require github.com/stretchr/testify v1.9.0 require ( github.com/davecgh/go-spew v1.1.1 // indirect