From 52e3bc1d514edc2b8c7ea7eef868442c1197754c Mon Sep 17 00:00:00 2001 From: Daniel Nakov Date: Sat, 30 Aug 2025 14:56:39 -0400 Subject: [PATCH] add a release workflow for the dylib; create install.sh; move the C header to go/ for now, cgo doesnt support looking in parent dirs (#105) It needs proper packaging like brew and stuff, but we can at least have an install.sh script for now --- .github/workflows/release.yml | 91 ++++++++++++++ README.md | 8 ++ install.sh | 150 ++++++++++++++++++++++++ opentui.pc.in | 9 ++ packages/go/.gitignore | 0 packages/go/README.md | 23 +++- packages/go/opentui.go | 11 +- packages/{core/src/zig => go}/opentui.h | 0 8 files changed, 280 insertions(+), 12 deletions(-) create mode 100644 .github/workflows/release.yml create mode 100755 install.sh create mode 100644 opentui.pc.in create mode 100644 packages/go/.gitignore rename packages/{core/src/zig => go}/opentui.h (100%) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 000000000..0f64f7b7f --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,91 @@ +name: Build and Release + +on: + push: + tags: + - "v*" + workflow_dispatch: + +jobs: + build-zig: + strategy: + matrix: + include: + - os: ubuntu-latest + target: x86_64-linux + - os: macos-13 + target: x86_64-macos + - os: macos-latest + target: aarch64-macos + - os: windows-latest + target: x86_64-windows + + runs-on: ${{ matrix.os }} + + steps: + - uses: actions/checkout@v4 + + - name: Setup Zig + uses: goto-bus-stop/setup-zig@v2 + with: + version: 0.14.1 + + - name: Build Zig library + run: | + cd packages/core/src/zig + zig build -Doptimize=ReleaseFast + + - name: Upload library artifact + uses: actions/upload-artifact@v4 + with: + name: library-${{ matrix.target }} + path: packages/core/src/zig/lib/ + + release: + needs: build-zig + runs-on: ubuntu-latest + if: startsWith(github.ref, 'refs/tags/') + + steps: + - uses: actions/checkout@v4 + + - name: Download all artifacts + uses: actions/download-artifact@v4 + with: + path: artifacts + + - name: Prepare release assets + run: | + mkdir -p release-assets + + # Copy header file and install script + cp packages/go/opentui.h release-assets/ + cp install.sh release-assets/ + cp opentui.pc.in release-assets/ + + # Copy libraries with proper naming + for target in x86_64-linux x86_64-macos aarch64-macos x86_64-windows; do + if [ -d "artifacts/library-$target" ]; then + case $target in + *-linux) + find artifacts/library-$target -name "*.so" -exec cp {} release-assets/libopentui-$target.so \; + ;; + *-macos) + find artifacts/library-$target -name "*.dylib" -exec cp {} release-assets/libopentui-$target.dylib \; + ;; + *-windows) + find artifacts/library-$target -name "*.dll" -exec cp {} release-assets/opentui-$target.dll \; + ;; + esac + fi + done + + ls -la release-assets/ + + - name: Create Release + uses: softprops/action-gh-release@v1 + with: + files: release-assets/* + generate_release_notes: true + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/README.md b/README.md index 6df975940..b71aad7e9 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,14 @@ bun install @opentui/core ### Go +First install OpenTUI system-wide: + +```bash +curl -L https://github.com/sst/opentui/releases/latest/download/install.sh | sh +``` + +Then use in your Go projects: + ```bash go get github.com/sst/opentui/packages/go ``` diff --git a/install.sh b/install.sh new file mode 100755 index 000000000..4bda8a7ad --- /dev/null +++ b/install.sh @@ -0,0 +1,150 @@ +#!/bin/bash +set -e + +# OpenTUI System Installation Script +# Installs OpenTUI headers and libraries system-wide + +REPO="sst/opentui" +RELEASE_URL="https://github.com/$REPO/releases/latest/download" + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +echo -e "${GREEN}OpenTUI System Installer${NC}" +echo "Installing OpenTUI headers and libraries..." + +# Detect platform +OS=$(uname -s | tr '[:upper:]' '[:lower:]') +ARCH=$(uname -m) + +case "$ARCH" in + x86_64|amd64) ARCH="x86_64" ;; + arm64|aarch64) ARCH="aarch64" ;; + *) echo -e "${RED}Error: Unsupported architecture: $ARCH${NC}"; exit 1 ;; +esac + +case "$OS" in + darwin) OS="macos" ;; + linux) OS="linux" ;; + mingw*|cygwin*|msys*) OS="windows" ;; + *) echo -e "${RED}Error: Unsupported OS: $OS${NC}"; exit 1 ;; +esac + +PLATFORM="${ARCH}-${OS}" +echo "Detected platform: $PLATFORM" + +# Determine installation paths +if [[ "$OS" == "macos" ]]; then + INCLUDE_DIR="/opt/homebrew/include" + LIB_DIR="/opt/homebrew/lib" + if [[ ! -d "$INCLUDE_DIR" ]]; then + INCLUDE_DIR="/usr/local/include" + LIB_DIR="/usr/local/lib" + fi +else + INCLUDE_DIR="/usr/local/include" + LIB_DIR="/usr/local/lib" +fi + +echo "Installation paths:" +echo " Headers: $INCLUDE_DIR" +echo " Libraries: $LIB_DIR" + +# Check for sudo if needed +if [[ ! -w "$INCLUDE_DIR" ]] || [[ ! -w "$LIB_DIR" ]]; then + if [[ $EUID -ne 0 ]]; then + echo -e "${YELLOW}Administrator privileges required for system installation${NC}" + echo "Please run with sudo or as administrator" + exit 1 + fi +fi + +# Create temporary directory +TEMP_DIR=$(mktemp -d) +trap "rm -rf $TEMP_DIR" EXIT + +echo "Downloading assets..." + +# Download header file +echo " - opentui.h" +curl -L -o "$TEMP_DIR/opentui.h" "$RELEASE_URL/opentui.h" || { + echo -e "${RED}Error: Failed to download header file${NC}" + exit 1 +} + +# Download library for current platform +case "$OS" in + macos) + LIB_FILE="libopentui.dylib" + ASSET_NAME="libopentui-$PLATFORM.dylib" + ;; + linux) + LIB_FILE="libopentui.so" + ASSET_NAME="libopentui-$PLATFORM.so" + ;; + windows) + LIB_FILE="opentui.dll" + ASSET_NAME="opentui-$PLATFORM.dll" + ;; +esac + +echo " - $ASSET_NAME" +curl -L -o "$TEMP_DIR/$LIB_FILE" "$RELEASE_URL/$ASSET_NAME" || { + echo -e "${RED}Error: Failed to download library for $PLATFORM${NC}" + exit 1 +} + +# Install files +echo "Installing files..." + +echo " - Installing header to $INCLUDE_DIR/opentui.h" +cp "$TEMP_DIR/opentui.h" "$INCLUDE_DIR/opentui.h" || { + echo -e "${RED}Error: Failed to install header file${NC}" + exit 1 +} + +echo " - Installing library to $LIB_DIR/$LIB_FILE" +cp "$TEMP_DIR/$LIB_FILE" "$LIB_DIR/$LIB_FILE" || { + echo -e "${RED}Error: Failed to install library file${NC}" + exit 1 +} + +# Set permissions +chmod 644 "$INCLUDE_DIR/opentui.h" +chmod 755 "$LIB_DIR/$LIB_FILE" + +# Install pkg-config file +PKG_CONFIG_DIR="/usr/local/lib/pkgconfig" +if [[ "$OS" == "macos" ]] && [[ -d "/opt/homebrew/lib/pkgconfig" ]]; then + PKG_CONFIG_DIR="/opt/homebrew/lib/pkgconfig" +fi + +echo " - Installing pkg-config file to $PKG_CONFIG_DIR/opentui.pc" +mkdir -p "$PKG_CONFIG_DIR" + +# Download and install pkg-config template +curl -L -o "$TEMP_DIR/opentui.pc.in" "$RELEASE_URL/opentui.pc.in" || { + echo -e "${YELLOW}Warning: Could not download pkg-config template${NC}" +} + +if [[ -f "$TEMP_DIR/opentui.pc.in" ]]; then + sed "s|@PREFIX@|${INCLUDE_DIR%/include}|g; s|@VERSION@|latest|g" "$TEMP_DIR/opentui.pc.in" > "$PKG_CONFIG_DIR/opentui.pc" + chmod 644 "$PKG_CONFIG_DIR/opentui.pc" +fi + +# Update library cache on Linux +if [[ "$OS" == "linux" ]]; then + echo "Updating library cache..." + ldconfig 2>/dev/null || true +fi + +echo -e "${GREEN}✓ OpenTUI installed successfully!${NC}" +echo "" +echo "You can now use OpenTUI in your Go projects:" +echo " go get github.com/dnakov/opentui/packages/go" +echo "" +echo "To uninstall:" +echo " sudo rm $INCLUDE_DIR/opentui.h $LIB_DIR/$LIB_FILE" \ No newline at end of file diff --git a/opentui.pc.in b/opentui.pc.in new file mode 100644 index 000000000..96cde3930 --- /dev/null +++ b/opentui.pc.in @@ -0,0 +1,9 @@ +prefix=@PREFIX@ +libdir=${prefix}/lib +includedir=${prefix}/include + +Name: OpenTUI +Description: High-performance terminal user interface library +Version: @VERSION@ +Libs: -L${libdir} -lopentui -Wl,-rpath,${libdir} +Cflags: -I${includedir} \ No newline at end of file diff --git a/packages/go/.gitignore b/packages/go/.gitignore new file mode 100644 index 000000000..e69de29bb diff --git a/packages/go/README.md b/packages/go/README.md index 5c5abe3e9..5252150d6 100644 --- a/packages/go/README.md +++ b/packages/go/README.md @@ -12,6 +12,20 @@ Go bindings for [OpenTUI](https://github.com/sst/opentui), a high-performance te ## Installation +### 1. Install OpenTUI System Dependencies + +First, install OpenTUI headers and libraries system-wide: + +```bash +curl -L https://github.com/sst/opentui/releases/latest/download/install.sh | sh +``` + +This downloads the latest compiled libraries and headers for your platform and installs them to standard system locations. + +### 2. Install Go Package + +Then use in your Go projects: + ```bash go get github.com/sst/opentui/packages/go ``` @@ -20,7 +34,7 @@ go get github.com/sst/opentui/packages/go - Go 1.21 or later - CGO enabled -- C compiler (gcc, clang, or MSVC) +- pkg-config (usually pre-installed on most systems) ## Quick Start @@ -222,8 +236,11 @@ To build with a custom OpenTUI library: cd ../../core/src/zig zig build -Doptimize=ReleaseFast -# Copy library to appropriate system location or set CGO_LDFLAGS -# Then test Go bindings +# Install locally instead of using releases +sudo cp lib/$(uname -m)-$(uname -s | tr '[:upper:]' '[:lower:]')/libopentui.* /usr/local/lib/ +sudo cp ../../go/opentui.h /usr/local/include/ + +# Test Go bindings cd ../../go go test ``` diff --git a/packages/go/opentui.go b/packages/go/opentui.go index 3fce9da07..ad7382afe 100644 --- a/packages/go/opentui.go +++ b/packages/go/opentui.go @@ -1,15 +1,8 @@ package opentui /* -#cgo CFLAGS: -I${SRCDIR}/../core/src/zig -#cgo darwin,arm64 LDFLAGS: -L${SRCDIR}/../core/src/zig/lib/aarch64-macos -lopentui -Wl,-rpath,${SRCDIR}/../core/src/zig/lib/aarch64-macos -#cgo darwin,amd64 LDFLAGS: -L${SRCDIR}/../core/src/zig/lib/x86_64-macos -lopentui -Wl,-rpath,${SRCDIR}/../core/src/zig/lib/x86_64-macos -#cgo linux,amd64 LDFLAGS: -L${SRCDIR}/../core/src/zig/lib/x86_64-linux -lopentui -Wl,-rpath,${SRCDIR}/../core/src/zig/lib/x86_64-linux -#cgo linux,arm64 LDFLAGS: -L${SRCDIR}/../core/src/zig/lib/aarch64-linux -lopentui -Wl,-rpath,${SRCDIR}/../core/src/zig/lib/aarch64-linux -#cgo windows,amd64 LDFLAGS: -L${SRCDIR}/../core/src/zig/lib/x86_64-windows -lopentui -#cgo windows,arm64 LDFLAGS: -L${SRCDIR}/../core/src/zig/lib/aarch64-windows -lopentui - -#include "opentui.h" +#cgo pkg-config: opentui +#include #include */ import "C" diff --git a/packages/core/src/zig/opentui.h b/packages/go/opentui.h similarity index 100% rename from packages/core/src/zig/opentui.h rename to packages/go/opentui.h