name: Release Packages

on:
  push:
    tags:
      - 'v*'

  workflow_call:
    inputs:
      version:
        required: true
        description: Version
        type: string

  workflow_dispatch:
    inputs:
      version:
        description: Version
        required: true

permissions:
  id-token: write # Required for OIDC
  contents: write

jobs:
  pre-release:
    if: github.repository == 'lucide-icons/lucide' && contains('["ericfennis", "karsa-mistmere", "jguddas"]', github.actor)
    runs-on: ubuntu-latest
    outputs:
      VERSION: ${{ steps.get_version.outputs.VERSION }}

    steps:
      - name: Get the version
        id: get_version
        run: |
          echo $VERSION_REF
          echo "VERSION=${VERSION_REF/refs\/tags\/\v}" >> $GITHUB_OUTPUT
        env:
          VERSION_REF: ${{ inputs.version || github.event.inputs.version || github.ref }}

  release:
    if: github.repository == 'lucide-icons/lucide'
    runs-on: ubuntu-latest
    needs: pre-release
    permissions:
      id-token: write # Required for OIDC
      contents: read
    strategy:
      fail-fast: false
      matrix:
        package:
          [
            'lucide',
            'lucide-react',
            'lucide-react-native',
            'lucide-vue-next',
            'lucide-angular',
            'lucide-preact',
            'lucide-solid',
            'lucide-svelte',
            '@lucide/astro',
            '@lucide/svelte',
          ]
    steps:
      - uses: actions/checkout@v6
      - uses: pnpm/action-setup@v4
      - uses: actions/setup-node@v6
        with:
          cache: 'pnpm'
          node-version-file: 'package.json'

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Set new version
        run: pnpm --filter ${{ matrix.package }} version --new-version ${{ needs.pre-release.outputs.VERSION }} --no-git-tag-version

      - name: Build
        run: pnpm --filter ${{ matrix.package }} build

      - name: Test
        run: pnpm --filter ${{ matrix.package }} test

      - name: Publish
        run: pnpm --filter ${{ matrix.package }} publish --access public --no-git-checks --ignore-scripts
        env:
          NPM_CONFIG_PROVENANCE: true

  lucide-static:
    if: github.repository == 'lucide-icons/lucide'
    runs-on: ubuntu-latest
    needs: [pre-release, lucide-font]
    permissions:
      id-token: write # Required for OIDC
      contents: read

    steps:
      - uses: actions/checkout@v6
      - uses: actions/download-artifact@v4
      - uses: pnpm/action-setup@v4
      - uses: actions/setup-node@v6
        with:
          cache: 'pnpm'
          node-version-file: 'package.json'

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Set new version
        run: pnpm --filter lucide-static version --new-version ${{ needs.pre-release.outputs.VERSION }} --no-git-tag-version

      - name: Move Font
        run: cp -r lucide-font packages/lucide-static/font

      - name: Build
        run: pnpm --filter lucide-static build

      - name: Publish
        run: pnpm --filter lucide-static publish --no-git-checks
        env:
          NPM_CONFIG_PROVENANCE: true

  lucide-font:
    if: github.repository == 'lucide-icons/lucide'
    runs-on: ubuntu-latest
    needs: pre-release
    steps:
      - uses: actions/checkout@v6
      - uses: pnpm/action-setup@v4
      - uses: actions/setup-node@v6
        with:
          cache: 'pnpm'
          node-version-file: 'package.json'

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Outline svg Icons
        run: pnpm build:outline-icons

      - name: Create font in ./lucide-font
        run: pnpm build:font

      - name: 'Upload to Artifacts'
        uses: actions/upload-artifact@v4
        with:
          name: lucide-font
          path: lucide-font

  post-release:
    if: github.repository == 'lucide-icons/lucide'
    runs-on: ubuntu-latest
    needs: [pre-release, lucide-font]
    permissions:
      id-token: write # Required for OIDC
      contents: write
    steps:
      - uses: actions/checkout@v6
      - uses: actions/download-artifact@v4
      - name: Zip font and icons
        run: |
          zip -r lucide-font-${{ needs.pre-release.outputs.VERSION }}.zip lucide-font
          zip -r lucide-icons-${{ needs.pre-release.outputs.VERSION }}.zip icons

      - name: Release zip and fonts
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ needs.pre-release.outputs.VERSION }}
          files: |
            lucide-font-${{ needs.pre-release.outputs.VERSION }}.zip
            lucide-icons-${{ needs.pre-release.outputs.VERSION }}.zip
