name: Create a Release

on:
  workflow_dispatch:
    inputs:
      major:
        description: "Major"
        type: boolean
        default: false
      minor:
        description: "Minor"
        type: boolean
        default: false

jobs:
  build-skia:
    strategy:
      matrix:
        arch: ["x86", "x64", "arm", "arm64", "iossim_arm64"]

    runs-on: macos-12
    permissions:
      id-token: write
      contents: read
    steps:
      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-region: us-west-2
          role-to-assume: ${{ secrets.ACTIONS_ROLE }}
      - name: Checkout Code
        uses: actions/checkout@v3
        with:
          submodules: true
          token: ${{ secrets.PAT_GITHUB }}

      - name: Update Java
        uses: actions/setup-java@v2
        with:
          distribution: "zulu"
          java-version: "16"

      - name: Installing pre-requisites
        run: |
          set -x
          # Install some dependencies & premake5
          brew install ninja
          curl https://github.com/premake/premake-core/releases/download/v5.0.0-beta1/premake-5.0.0-beta1-macosx.tar.gz -L -o premake_macosx.tar.gz
          tar -xvf premake_macosx.tar.gz 2>/dev/null
          rm premake_macosx.tar.gz
          sudo chmod a+x premake5
          mkdir bin
          cp premake5 bin/premake5
          sudo mv premake5 /usr/local/bin
      - name: Build skia files
        run: ./scripts/build.skia.sh -a ${{ matrix.arch }}

  determine_version:
    name: Determine the next build version
    needs: build-skia
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.echo_version.outputs.version }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Install dependencies
        run: npm ci
        working-directory: ./.github/release
      - if: ${{ inputs.major == true }}
        id: determine_version_major
        name: Get Version - MAJOR
        run: npm run release -- major --ci --release-version | tail -n 1 > RELEASE_VERSION
        working-directory: ./.github/release
        env:
          GITHUB_TOKEN: ${{ secrets.PAT_GITHUB }}
      - if: ${{inputs.major == false && inputs.minor == true}}
        id: determine_version_minor
        name: Get Version - MINOR
        run: npm run release -- minor --ci --release-version | tail -n 1 > RELEASE_VERSION
        working-directory: ./.github/release
        env:
          GITHUB_TOKEN: ${{ secrets.PAT_GITHUB }}
      - if: ${{ inputs.major == false && inputs.minor == false }}
        id: determine_version_patch
        name: Get Version - PATCH
        run: npm run release -- --ci --release-version | tail -n 1 > RELEASE_VERSION
        working-directory: ./.github/release
        env:
          GITHUB_TOKEN: ${{ secrets.PAT_GITHUB }}
      - id: echo_version
        run: echo "::set-output name=version::$(cat ./.github/release/RELEASE_VERSION)"

  build_framework:
    name: Create RiveRuntime.xcframework
    runs-on: [self-hosted, macOS, ARM64]
    needs: [determine_version]
    outputs:
      checksum: ${{steps.get_checksum.outputs.checksum}}
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3
        with:
          submodules: true
          token: ${{ secrets.PAT_GITHUB }}

      - name: Get Renderer ref
        id: renderer-ref
        run: echo "REF_VALUE=$(cat .rive_renderer)" >> $GITHUB_OUTPUT

      - name: Checkout Rive Renderer Repo
        uses: actions/checkout@v3
        with:
          submodules: true
          repository: ${{ secrets.RIVE_RENDERER_REPO }}
          token: ${{ secrets.PAT_GITHUB }}
          path: ./submodules/pls
          ref: ${{ steps.renderer-ref.outputs.REF_VALUE }}

      - name: Installing pre-requisites
        run: |
          set -x
          # Install some dependencies & premake5
          brew install ninja
          curl https://github.com/premake/premake-core/releases/download/v5.0.0-beta1/premake-5.0.0-beta1-macosx.tar.gz -L -o premake_macosx.tar.gz
          tar -xvf premake_macosx.tar.gz 2>/dev/null
          rm premake_macosx.tar.gz
          mkdir bin
          sudo chmod a+x premake5
          cp premake5 bin/premake5
          sudo mv premake5 /usr/local/bin
          pip3 install ply
      - name: Build everything (using the cache, we should make an archive of course)
        run: ./scripts/build.sh all
      - name: Update Marketing versions
        run: agvtool new-marketing-version ${{ needs.determine_version.outputs.version }}
      - name: Upload versionFiles
        uses: actions/upload-artifact@v3
        with:
          name: version-files
          path: |
            RiveRuntime.xcodeproj/project.pbxproj
            Source/Info.plist
            Tests/Info.plist
      - id: build_frameworks
        name: Build frameworks
        run: sh ./scripts/build_framework.sh -c Release
      - id: zip
        name: Zip the framework
        # --symlinks is critical here to retain the structure of a macos framework.
        run: zip --symlinks -r RiveRuntime.xcframework.zip RiveRuntime.xcframework
        working-directory: ./archive
      - id: get_checksum
        name: Add the checksum of the zip file into our environment
        run: echo "::set-output name=checksum::$(swift package compute-checksum archive/RiveRuntime.xcframework.zip)"
      - name: Upload xcFramework
        uses: actions/upload-artifact@v3
        with:
          name: RiveRuntime.xcframework.zip
          path: archive/RiveRuntime.xcframework.zip
      - name: Upload versionFiles
        uses: actions/upload-artifact@v3
        with:
          name: version-files
          path: |
            Source/Info.plist
            Tests/Info.plist

  do_release:
    name: Do the actual release
    runs-on: ubuntu-latest
    needs: [determine_version, build_framework]
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Install dependencies
        run: npm ci
        working-directory: ./.github/release

      - id: read_package_template
        uses: pCYSl5EDgo/cat@master
        with:
          path: .github/workflows/Package.swift.template
      - name: Create Package.swift
        run: |
          cat > Package.swift <<-EOF
          ${{ steps.read_package_template.outputs.text }}
          EOF
        env:
          CHECKSUM: ${{ needs.build_framework.outputs.checksum }}
          RELEASE_VERSION: ${{ needs.determine_version.outputs.version }}

      - id: read_podspec_template
        uses: pCYSl5EDgo/cat@master
        with:
          path: .github/workflows/RiveRuntime.podspec.template
      - name: Create RiveRuntime.podspec
        run: |
          cat > RiveRuntime.podspec <<-EOF
          ${{ steps.read_podspec_template.outputs.text }}
          EOF
        env:
          RELEASE_VERSION: ${{ needs.determine_version.outputs.version }}
      - name: Upload podspec file
        uses: actions/upload-artifact@v2
        with:
          name: RiveRuntime.podspec
          path: RiveRuntime.podspec

      - name: Git config
        run: |
          git config --local user.email 'hello@rive.app'
          git config --local user.name ${{ github.actor }}
      - name: Download versionFiles
        uses: actions/download-artifact@v3
        with:
          name: version-files
      - name: Add & commit version files (might want to look into doing this in release-it)
        run: git add RiveRuntime.podspec Package.swift Source/Info.plist Tests/Info.plist && git commit -m "Updating version files"
      - name: Download framework artifact
        uses: actions/download-artifact@v2
        with:
          name: RiveRuntime.xcframework.zip
          path: archive/
      - if: ${{ inputs.major == true }}
        name: Major Release - Bump version number, update changelog, push and tag
        run: npm run release -- major --ci
        working-directory: ./.github/release
        env:
          GITHUB_TOKEN: ${{ secrets.PAT_GITHUB }}
      - if: ${{inputs.major == false && inputs.minor == true}}
        name: Minor release - Bump version number, update changelog, push and tag
        run: npm run release -- minor --ci
        working-directory: ./.github/release
        env:
          GITHUB_TOKEN: ${{ secrets.PAT_GITHUB }}
      - if: ${{inputs.major == false && inputs.minor == false}}
        name: Build release - Bump version number, update changelog, push and tag
        run: npm run release -- --ci
        working-directory: ./.github/release
        env:
          GITHUB_TOKEN: ${{ secrets.PAT_GITHUB }}

  publish_cocoapods:
    name: Publish framework to cocoapods
    runs-on: macos-12
    needs: [determine_version, build_framework, do_release]
    steps:
      - name: Download framework artifact
        uses: actions/download-artifact@v3
        with:
          name: RiveRuntime.xcframework.zip
      - name: Download podspec file
        uses: actions/download-artifact@v3
        with:
          name: RiveRuntime.podspec
      - name: Extract archive to pass cocoapods validation
        run: unzip RiveRuntime.xcframework.zip
      - name: Publish pod to the CocoaPods
        uses: michaelhenry/deploy-to-cocoapods-github-action@1.0.10
        env:
          COCOAPODS_TRUNK_TOKEN: ${{ secrets.COCOAPODS_TRUNK_TOKEN }}
