name: Release

on:
  push:
    tags:
      - 'v*'

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Build macOS ARM64
    runs-on: macos-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: aarch64-apple-darwin

      - name: Install build dependencies
        run: brew install cmake

      - name: Build
        run: cargo build --release --target aarch64-apple-darwin
        env:
          # 标记安装来源为 github，用于 j update 命令识别
          INSTALL_SOURCE: github

      - name: Build j-indicator
        run: swiftc helpers/indicator.swift -o target/aarch64-apple-darwin/release/j-indicator -O

      - name: Build j-ax
        run: swiftc helpers/ax.swift -o target/aarch64-apple-darwin/release/j-ax -O -framework Cocoa -framework ApplicationServices

      - name: Package
        run: |
          cd target/aarch64-apple-darwin/release
          strip j
          tar -czvf j-darwin-arm64.tar.gz j j-indicator j-ax
          shasum -a 256 j-darwin-arm64.tar.gz > j-darwin-arm64.tar.gz.sha256

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: j-darwin-arm64
          path: |
            target/aarch64-apple-darwin/release/j-darwin-arm64.tar.gz
            target/aarch64-apple-darwin/release/j-darwin-arm64.tar.gz.sha256

  release:
    name: Create Release
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Download artifact
        uses: actions/download-artifact@v4
        with:
          name: j-darwin-arm64
          path: artifacts

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          files: artifacts/*
