name: release-dart

on:
  push:
    branches:
      - ci-pub-dart
    tags:
      - 'dart-v[0-9]+.[0-9]+.[0-9]+*' # tag-pattern on pub.dev: 'v{{version}}'

  workflow_dispatch:

concurrency:
  group: release-dart-${{ github.ref }}
  cancel-in-progress: true

jobs:
  sherpa_onnx_linux:
    # if: false
    permissions:
      id-token: write # Required for authentication using OIDC
    name: sherpa_onnx_linux
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Fix version
        shell: bash
        run: |
          SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt  | cut -d " " -f 2  | cut -d '"' -f 2)

          src_dir=$PWD/flutter/sherpa_onnx_linux
          pushd $src_dir
          v="version: $SHERPA_ONNX_VERSION"
          echo "v: $v"
          sed -i.bak s"/^version: .*/$v/" ./pubspec.yaml
          rm *.bak
          git status
          git diff

      - name: Copy extra files
        shell: bash
        run: |
          dst=flutter/sherpa_onnx_linux

          mkdir $dst/example

          cp -v flutter/sherpa_onnx/example/* $dst/example
          cp -v LICENSE $dst/
          cp -v CHANGELOG.md $dst/

          git status

      - name: Build sherpa-onnx
        uses: addnab/docker-run-action@v3
        with:
            image: quay.io/pypa/manylinux2014_x86_64
            options: |
              --volume ${{ github.workspace }}/:/home/runner/work/sherpa-onnx/sherpa-onnx
            shell: bash
            run: |
              uname -a
              gcc --version
              cmake --version
              cat /etc/*release
              id
              pwd

              cd /home/runner/work/sherpa-onnx/sherpa-onnx

              mkdir build
              cd build

              cmake \
                -D SHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
                -D SHERPA_ONNX_ENABLE_TTS=ON \
                -D CMAKE_BUILD_TYPE=Release \
                -D BUILD_SHARED_LIBS=ON \
                -D CMAKE_INSTALL_PREFIX=./install \
                -D SHERPA_ONNX_ENABLE_JNI=OFF \
                -D SHERPA_ONNX_ENABLE_BINARY=OFF \
                ..

              make -j2
              make install

              ls -lh ./install/lib

      - name: Copy pre-built libs
        shell: bash
        run: |
          cp -v build/install/lib/lib*.so* flutter/sherpa_onnx_linux/linux/

          mv -v flutter/sherpa_onnx_linux /tmp/to_be_published

          ls -lh /tmp/to_be_published/linux

      - name: Setup Flutter SDK
        uses: flutter-actions/setup-flutter@v3
        with:
          channel: stable
          version: latest

      - uses: dart-lang/setup-dart@v1

      - name: Release
        shell: bash
        run: |
          cd /tmp/to_be_published
          flutter pub get
          flutter pub publish --dry-run
          flutter pub publish --force

  sherpa_onnx_macos:
    # if: false
    permissions:
      id-token: write # Required for authentication using OIDC
    name: sherpa_onnx_macos
    runs-on: macos-latest

    steps:
      - uses: actions/checkout@v4

      - name: ccache
        uses: hendrikmuhs/ccache-action@v1.2
        with:
          key: ${{ matrix.os }}-flutter-release-package

      - name: Fix version
        shell: bash
        run: |
          SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt  | cut -d " " -f 2  | cut -d '"' -f 2)

          src_dir=$PWD/flutter/sherpa_onnx_macos
          pushd $src_dir
          v="version: $SHERPA_ONNX_VERSION"
          echo "v: $v"
          sed -i.bak s"/^version: .*/$v/" ./pubspec.yaml
          rm *.bak
          git status
          git diff

      - name: Copy extra files
        shell: bash
        run: |
          dst=flutter/sherpa_onnx_macos

          mkdir $dst/example

          cp -v flutter/sherpa_onnx/example/* $dst/example
          cp -v LICENSE $dst/
          cp -v CHANGELOG.md $dst/

          git status

      - name: Configure CMake
        shell: bash
        run: |
          export CMAKE_CXX_COMPILER_LAUNCHER=ccache
          export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
          cmake --version

          mkdir build
          cd build
          cmake \
            -D SHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
            -D SHERPA_ONNX_ENABLE_TTS=ON \
            -D CMAKE_BUILD_TYPE=Release \
            -D BUILD_SHARED_LIBS=ON \
            -D CMAKE_INSTALL_PREFIX=./install \
            -D SHERPA_ONNX_ENABLE_JNI=OFF \
            -D SHERPA_ONNX_ENABLE_BINARY=OFF \
            -D CMAKE_OSX_ARCHITECTURES="x86_64;arm64" \
            ..

      - name: Build sherpa-onnx
        shell: bash
        run: |
          export CMAKE_CXX_COMPILER_LAUNCHER=ccache
          export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
          cmake --version

          cd build
          make -j2 install

          ls -lh install/lib/libsherpa-onnx-c-api.dylib
          file install/lib/libsherpa-onnx-c-api.dylib
          rm -v install/lib/libonnxruntime.dylib

      - name: Copy pre-built libs
        shell: bash
        run: |
          cp -v build/install/lib/lib*.dylib* flutter/sherpa_onnx_macos/macos/

          mv -v flutter/sherpa_onnx_macos /tmp/to_be_published

          ls -lh /tmp/to_be_published/macos

      - name: Setup Flutter SDK
        uses: flutter-actions/setup-flutter@v3
        with:
          channel: stable
          version: latest

      - uses: dart-lang/setup-dart@v1

      - name: Release
        shell: bash
        run: |
          cd /tmp/to_be_published
          du -h -d1 .
          flutter pub get
          flutter pub publish --dry-run
          flutter pub publish --force

  sherpa_onnx_windows:
    # if: false
    permissions:
      id-token: write # Required for authentication using OIDC
    name: sherpa_onnx_windows
    runs-on: windows-2019

    steps:
      - uses: actions/checkout@v4

      - name: Fix version
        shell: bash
        run: |
          SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt  | cut -d " " -f 2  | cut -d '"' -f 2)

          src_dir=$PWD/flutter/sherpa_onnx_windows
          pushd $src_dir
          v="version: $SHERPA_ONNX_VERSION"
          echo "v: $v"
          sed -i.bak s"/^version: .*/$v/" ./pubspec.yaml
          rm *.bak
          git status
          git diff

      - name: Copy extra files
        shell: bash
        run: |
          dst=flutter/sherpa_onnx_windows

          mkdir $dst/example

          cp -v flutter/sherpa_onnx/example/* $dst/example
          cp -v LICENSE $dst/
          cp -v CHANGELOG.md $dst/

          git status

      - name: Configure CMake
        shell: bash
        run: |
          mkdir build
          cd build
          cmake \
            -D SHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
            -D SHERPA_ONNX_ENABLE_TTS=ON \
            -D CMAKE_BUILD_TYPE=Release \
            -D BUILD_SHARED_LIBS=ON \
            -D CMAKE_INSTALL_PREFIX=./install \
            -D SHERPA_ONNX_ENABLE_JNI=OFF \
            -D SHERPA_ONNX_ENABLE_BINARY=OFF \
            ..

      - name: Build sherpa-onnx
        shell: bash
        run: |
          cd build
          cmake --build . --target install --config Release -- -m:2

          ls -lh install/lib/*.dll

      - name: Copy pre-built libs
        shell: bash
        run: |
          cp -v build/install/lib/*.dll flutter/sherpa_onnx_windows/windows/
          mv -v flutter/sherpa_onnx_windows /tmp/to_be_published

          ls -lh /tmp/to_be_published/windows

      - name: Setup Flutter SDK
        uses: flutter-actions/setup-flutter@v3
        with:
          channel: stable
          version: latest

      - uses: dart-lang/setup-dart@v1

      - name: Release
        shell: bash
        run: |
          cd /tmp/to_be_published
          flutter pub get
          flutter pub publish --dry-run
          flutter pub publish --force

  sherpa_onnx_android:
    # if: false
    permissions:
      id-token: write # Required for authentication using OIDC
    name: sherpa_onnx_android
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: ccache
        uses: hendrikmuhs/ccache-action@v1.2
        with:
          key: ${{ matrix.os }}-flutter-release-package-android

      - name: Fix version
        shell: bash
        run: |
          SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt  | cut -d " " -f 2  | cut -d '"' -f 2)

          src_dir=$PWD/flutter/sherpa_onnx_android
          pushd $src_dir
          v="version: $SHERPA_ONNX_VERSION"
          echo "v: $v"
          sed -i.bak s"/^version: .*/$v/" ./pubspec.yaml
          rm *.bak
          git status
          git diff

      - name: Copy extra files
        shell: bash
        run: |
          dst=flutter/sherpa_onnx_android

          mkdir $dst/example

          cp -v flutter/sherpa_onnx/example/* $dst/example
          cp -v LICENSE $dst/
          cp -v CHANGELOG.md $dst/

          git status

      - name: Build android-arm64-v8a
        shell: bash
        run: |
          export CMAKE_CXX_COMPILER_LAUNCHER=ccache
          export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
          cmake --version

          export SHERPA_ONNX_ENABLE_C_API=ON
          export SHERPA_ONNX_ENABLE_JNI=OFF
          export SHERPA_ONNX_ENABLE_BINARY=OFF

          ./build-android-arm64-v8a.sh

      - name: Build android-armv7-eabi
        shell: bash
        run: |
          export CMAKE_CXX_COMPILER_LAUNCHER=ccache
          export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
          cmake --version

          export SHERPA_ONNX_ENABLE_C_API=ON
          export SHERPA_ONNX_ENABLE_JNI=OFF
          export SHERPA_ONNX_ENABLE_BINARY=OFF

          ./build-android-armv7-eabi.sh

      - name: Build android-x86
        shell: bash
        run: |
          export CMAKE_CXX_COMPILER_LAUNCHER=ccache
          export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
          cmake --version

          export SHERPA_ONNX_ENABLE_C_API=ON
          export SHERPA_ONNX_ENABLE_JNI=OFF
          export SHERPA_ONNX_ENABLE_BINARY=OFF

          ./build-android-x86.sh

      - name: Build android-x86-64
        shell: bash
        run: |
          export CMAKE_CXX_COMPILER_LAUNCHER=ccache
          export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
          cmake --version

          export SHERPA_ONNX_ENABLE_C_API=ON
          export SHERPA_ONNX_ENABLE_JNI=OFF
          export SHERPA_ONNX_ENABLE_BINARY=OFF

          ./build-android-x86-64.sh

      - name: Copy pre-built libs
        shell: bash
        run: |
          echo "----arm64-v8a----"
          cp -v build-android-arm64-v8a/install/lib/lib*.so flutter/sherpa_onnx_android/android/src/main/jniLibs/arm64-v8a/

          echo "----armv7-eabi----"
          cp -v build-android-armv7-eabi/install/lib/lib*.so flutter/sherpa_onnx_android/android/src/main/jniLibs/armeabi-v7a

          echo "----x86----"
          cp -v build-android-x86/install/lib/lib*.so flutter/sherpa_onnx_android/android/src/main/jniLibs/x86

          echo "----x86_64----"
          cp -v build-android-x86-64/install/lib/lib*.so flutter/sherpa_onnx_android/android/src/main/jniLibs/x86_64

          mv -v flutter/sherpa_onnx_android /tmp/to_be_published

          ls -lh /tmp/to_be_published

      - name: Setup Flutter SDK
        uses: flutter-actions/setup-flutter@v3
        with:
          channel: stable
          version: latest

      - uses: dart-lang/setup-dart@v1

      - name: Release
        shell: bash
        run: |
          cd /tmp/to_be_published
          du -h -d1 .

          flutter pub get
          flutter pub publish --dry-run
          flutter pub publish --force

  sherpa_onnx_ios:
    # if: false
    permissions:
      id-token: write # Required for authentication using OIDC
    name: sherpa_onnx_ios
    runs-on: macos-latest

    steps:
      - uses: actions/checkout@v4

      - name: ccache
        uses: hendrikmuhs/ccache-action@v1.2
        with:
          key: ${{ matrix.os }}-flutter-release-package-ios

      - name: Fix version
        shell: bash
        run: |
          SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt  | cut -d " " -f 2  | cut -d '"' -f 2)

          src_dir=$PWD/flutter/sherpa_onnx_ios
          pushd $src_dir
          v="version: $SHERPA_ONNX_VERSION"
          echo "v: $v"
          sed -i.bak s"/^version: .*/$v/" ./pubspec.yaml
          rm *.bak
          git status
          git diff

      - name: Copy extra files
        shell: bash
        run: |
          dst=flutter/sherpa_onnx_ios

          mkdir $dst/example

          cp -v flutter/sherpa_onnx/example/* $dst/example
          cp -v LICENSE $dst/
          cp -v CHANGELOG.md $dst/

          git status

      - name: Build ios
        shell: bash
        run: |
          export CMAKE_CXX_COMPILER_LAUNCHER=ccache
          export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
          cmake --version
          ./build-ios-shared.sh

      - name: Copy pre-built libs
        shell: bash
        run: |
          echo "----ios-arm64----"
          cp -v build-ios-shared/ios-arm64/libsherpa-onnx-c-api.dylib flutter/sherpa_onnx_ios/ios/
          cp -v build-ios-shared/ios-onnxruntime/onnxruntime.xcframework/ios-arm64/onnxruntime.a flutter/sherpa_onnx_ios/ios/libonnxruntime.a

          ls -lh flutter/sherpa_onnx_ios/ios/libonnxruntime.a

          mv -v flutter/sherpa_onnx_ios /tmp/to_be_published

          ls -lh /tmp/to_be_published

      - name: Setup Flutter SDK
        uses: flutter-actions/setup-flutter@v3
        with:
          channel: stable
          version: latest

      - uses: dart-lang/setup-dart@v1

      - name: Release
        shell: bash
        run: |
          cd /tmp/to_be_published
          du -h -d1 .

          flutter pub get
          flutter pub publish --dry-run
          flutter pub publish --force

  sherpa_onnx:
    needs: [sherpa_onnx_linux, sherpa_onnx_macos, sherpa_onnx_windows, sherpa_onnx_android, sherpa_onnx_ios]
    # if: false
    permissions:
      id-token: write # Required for authentication using OIDC
    name: sherpa_onnx
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Setup Flutter SDK
        uses: flutter-actions/setup-flutter@v3
        with:
          channel: stable
          version: latest

      - uses: dart-lang/setup-dart@v1

      - name: Fix version
        shell: bash
        run: |
          SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt  | cut -d " " -f 2  | cut -d '"' -f 2)

          src_dir=$PWD/flutter/sherpa_onnx
          pushd $src_dir
          v="version: $SHERPA_ONNX_VERSION"
          echo "v: $v"
          sed -i.bak s"/^version: .*/$v/" ./pubspec.yaml
          rm *.bak
          git status
          git diff

      - name: Copy extra files
        shell: bash
        run: |
          dst=flutter/sherpa_onnx

          cp -v LICENSE $dst/
          cp -v CHANGELOG.md $dst/
          cp -v README.md $dst/

          git status

          mv -v flutter/sherpa_onnx /tmp/to_be_published

          ls -lh /tmp/to_be_published

      - name: Release
        shell: bash
        run: |
          cd /tmp/to_be_published
          du -h -d1 .

          flutter pub get
          flutter pub publish --dry-run
          flutter pub publish --force
