name: Build Binaries

on:
  # Runs on pushes targeting the default branch
  push:

jobs:
  # Build job
  build:
    name: Build & Test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          github-server-url: https://gitea.sheep-ghoul.ts.net

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable

      - name: Cargo Build
        uses: actions-rs/cargo@v1
        with:
          command: build

      - name: Cargo Test
        uses: actions-rs/cargo@v1
        with:
          command: test

  buildPy:
    name: Build Python
    runs-on: ubuntu-latest
    needs: [ build ] # for ignoring bad builds
    steps:
      - uses: actions/checkout@v4 # get source
        with:
          github-server-url: https://gitea.sheep-ghoul.ts.net

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable

      - name: Install Python 3
        uses: actions/setup-python@v4
        with:
          python-version: ${{ env.python-version }}

      - name: Install Maturin
        run: sudo apt update && sudo apt install -y python3-maturin

      - name: Build Python
        working-directory: ./pyfinlib
        run: maturin build

  buildNET:
    name: Build .NET
    runs-on: ubuntu-latest
    needs: [ build ] # for ignoring bad builds
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          github-server-url: https://gitea.sheep-ghoul.ts.net

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable

      - name: Cargo Build
        uses: actions-rs/cargo@v1
        with:
          command: build

      - name: Setup .NET Core SDK 9.0.x
        uses: actions/setup-dotnet@v3.0.3
        with:
          dotnet-version: 9.0.x

      - name: Install Dependencies
        working-directory: ./FinLib.NET
        run: dotnet restore

      - name: Build
        working-directory: ./FinLib.NET
        run: dotnet build --configuration Debug --no-restore

      - name: Test
        working-directory: ./FinLib.NET
        run: dotnet test --no-restore

  publishNET:
    name: Publish .NET
    runs-on: ubuntu-latest
    needs: [ buildNET ] # for ignoring bad builds
    if: github.event_name == 'push' && github.ref == 'refs/heads/master'
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          github-server-url: https://gitea.sheep-ghoul.ts.net

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable

      - name: Cargo Build
        uses: actions-rs/cargo@v1
        with:
          command: build

      - name: Setup .NET Core SDK 9.0.x
        uses: actions/setup-dotnet@v3.0.3
        with:
          dotnet-version: 9.0.x

      - name: Install Dependencies
        working-directory: ./FinLib.NET
        run: dotnet restore

      - name: Pack
        working-directory: ./FinLib.NET/FinLib
        run: dotnet pack -p:PackageVersion=$(pushd ../../finlib > /dev/null && cargo pkgid | awk -F '[,#]' '{print $2}' && popd > /dev/null)

      - name: Push
        working-directory: ./FinLib.NET/FinLib
        run: dotnet nuget push ./bin/Release/FinLib.NET.$(pushd ../../finlib > /dev/null && cargo pkgid | awk -F '[,#]' '{print $2}' && popd > /dev/null).nupkg --api-key ${{ secrets.DOCKERHUB_TOKEN }} --source https://gitea.sheep-ghoul.ts.net/api/packages/sarsoo/nuget/index.json

  buildWASM:
    name: Build WASM
    runs-on: ubuntu-latest
    needs: [ build ] # for ignoring bad builds
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          github-server-url: https://gitea.sheep-ghoul.ts.net

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable

      - name: Install wasm-pack
        run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

      - name: Build Rust for WASM
        working-directory: ./finlib-wasm
        run: wasm-pack build

  publishNPM:
    name: Publish NPM
    runs-on: ubuntu-latest
    needs: [ buildWASM ] # for ignoring bad builds
    if: github.event_name == 'push' && github.ref == 'refs/heads/master'
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          github-server-url: https://gitea.sheep-ghoul.ts.net

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable

      - name: Install wasm-pack
        run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

      - name: Build Rust for WASM
        working-directory: ./finlib-wasm
        run: wasm-pack build --release

      - name: Change Package Name
        working-directory: ./finlib-wasm
        run: ./rename-pkg.sh

      - name: Install Node
        uses: actions/setup-node@v2
        with:
          node-version: 22
          registry-url: 'https://gitea.sheep-ghoul.ts.net/api/packages/sarsoo/npm/'

      - name: Publish
        working-directory: ./finlib-wasm/pkg
        run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

  publishRustLib:
    runs-on: ubuntu-latest
    name: Publish Rust Library
    needs: [ build ] # for ignoring bad builds
    if: github.event_name == 'push' && github.ref == 'refs/heads/master'
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          github-server-url: https://gitea.sheep-ghoul.ts.net

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable

      - name: Add SarGit Repo
        run: |
          cat <<EOF > ~/.cargo/config.toml
          [registry]
          default = "sargit"

          [registries.sargit]
          index = "sparse+https://git.sarsoo.xyz/api/packages/${{ secrets.DOCKERHUB_USERNAME }}/cargo/"

          [net]
          git-fetch-with-cli = true
          EOF

      - name: Add SarGit Credentials
        run: |
          cat <<EOF > ~/.cargo/credentials.toml
          [registries.sargit]
          token = "Bearer ${{ secrets.DOCKERHUB_TOKEN }}"
          EOF

      - name: Cargo Publish
        uses: actions-rs/cargo@v1
        with:
          command: publish
          args: --package finlib --registry sargit

  publishPy:
    runs-on: ubuntu-latest
    name: Publish Python Library
    needs: [ buildPy ] # for ignoring bad builds
    if: github.event_name == 'push' && github.ref == 'refs/heads/master'
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          github-server-url: https://gitea.sheep-ghoul.ts.net

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable

      - name: Install Python 3
        uses: actions/setup-python@v4
        with:
          python-version: ${{ env.python-version }}

      - name: Install Maturin
        working-directory: ./pyfinlib
        run: python3 -m venv .venv && source .venv/bin/activate && pip3 install -r requirements.txt

      - name: Publish
        working-directory: ./pyfinlib
        run: source .venv/bin/activate && maturin publish --repository-url https://gitea.sheep-ghoul.ts.net/api/packages/sarsoo/pypi
        env:
          MATURIN_PYPI_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}