name: CutBox Test/Build CI

on:
  push:
    branches: [ "master" ]
  workflow_dispatch:

jobs:
  build:
    name: CutBox Build
    runs-on: macos-12
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          submodules: true
          fetch-depth: 0
          ref: master

      - name: Python 3.11
        uses: actions/setup-python@v4
        with:
          python-version: '3.11'

      - name: Go-lang 1.19
        uses: gacts/setup-go-with-cache@v1
        with:
          go-version: 1.19

      - name: Install  Golang deps
        run: |
          go install github.com/santhosh-tekuri/jsonschema/cmd/jv@latest
          go install github.com/ericchiang/pup@latest

      - name: CI diagnostics
        run: |
          echo "============================================================"
          for b in ci_is_production ci_get_release_info ci_update_info_plist ci_update_cutbox_homebrew setup test build
          do
            if [[ -x bin/$b ]]; then
              echo "[✓] bin/$b executable."
            else
              echo "[✗] bin/$b - missing or non-executable" && exit 1
            fi
          done

      - name: Production Release when commit message has tag [release]
        run: |
          bin/ci_is_production

      - name: Setup
        run: |
          make setup

      # Temporary debugging for tests
      # - name: Setup tmate session
      #   uses: mxschmitt/action-tmate@v3
      #   timeout-minutes: 15
      #   with:
      #     limit-access-to-actor: true

      - name: Run Tests
        run: |
          make test_ci

      - name: Upload CutBox Test Coverage to Codacy
        run: |
          if bin/codacy_upload_coverage; then
            echo "### Code coverage uploaded to Codacy [✓]" >> "$GITHUB_STEP_SUMMARY"
          else
            echo "### Failed uploaded to Codacy [✗]" >> "$GITHUB_STEP_SUMMARY"
            exit 1
          fi

      - name: Prepare CutBox Test Coverage Docs for Pages
        run: |
          if bin/pages_prepare_coverage_report ; then
            echo "### Code coverage prepared for to cutbox.github.io/CutBox/App/ [✓]" >> "$GITHUB_STEP_SUMMARY"
          else
            echo "### Failed prepare coverage for cutbox.github.io/CutBox/App/ [✗]" >> "$GITHUB_STEP_SUMMARY"
            exit 1
          fi

      - name: Check Localized.strings
        run: |
          echo "# Check Localized.strings" >> "$GITHUB_STEP_SUMMARY"
          echo "## Linting" >> "$GITHUB_STEP_SUMMARY"
          bin/lint_localized_strings

          echo "## Languages Correlate" >> "$GITHUB_STEP_SUMMARY"

          find CutBox/Localization \
             -type d \
             -name "*lproj" \
             -not -name 'en.lproj' \
             -exec bin/check_localization "{}"/Localizable.strings \;

          echo "## Strings Are Localized?" >> "$GITHUB_STEP_SUMMARY"
          bin/check_l7n_source

      - name: Theme validation
        run: |
          make test_themes

      - name: CutBox Build
        run: |
          make build

      - name: Command line test
        run: |
          bin/command_line_test
          make -C cutbox_command coverage_report

      - name: Refry llvm-cov html reports
        run: |
           bin/fix_llvm_html_pathnames docs/CLI 'cutbox_command'
           bin/llvm_cov_refryer --docs-root="docs/App" --source-dir="Source"

      - name: Append addBlobs.js to coverage index pages
        run: |
          for a in App CLI; do
            echo '<script type="text/javascript" src="../js/addBlobs.js"></script>' >> docs/$a/index.html
          done

      - name: Commit Coverage Reports
        run: |
          git add docs
          if git diff --cached --quiet; then
            echo "No changes to coverage reports"
          else
            git commit -m "App & CLI coverage reports - $(TZ=UTC date +%Y-%m-%dT%H:%M:%SZ) [codacy skip]"
          fi

      - name: Generate coverage graphs
        run: |
          bin/get_coverage_numbers docs/App/index.html \
            > docs/App/cov_progress.json

          bin/render_cov_graph \
            --output docs/App/cov.svg \
            --title "CutBox.app test coverage" \
            < docs/App/cov_progress.json

          bin/get_coverage_numbers docs/CLI/index.html \
            > docs/CLI/cov_progress.json

          bin/render_cov_graph \
            --output docs/CLI/cov.svg \
            --title "CutBox CLI test coverage" \
            < docs/CLI/cov_progress.json

      - name: Commit coverage graphs
        run: |
          git add docs
          if git diff --cached --quiet; then
            echo "No changes to coverage grapghs"
          else
            git commit -m "App & CLI coverage graphs - $(TZ=UTC date +%Y-%m-%dT%H:%M:%SZ) [codacy skip]"
          fi

      - name: Push runner commits
        run: |
          git pull --rebase --autostash
          git push origin master

      - name: CLI Build
        run: |
          make -C cutbox_command dmg_bin

      - name: Make DMG
        run: |
          make dmg

      - name: Get Release Info
        run: |
          make ci_get_release_info

      - name: Assemble release notes
        run: |
          mkdir tmp/
          bin/get_release_notes | tee tmp/release-notes.md

      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        with:
          tag_name: ${{ env.RELEASE }}
          release_name: ${{ env.RELEASE }}
          draft: ${{ env.PRODUCTION_RUN != 1}}
          prerelease: false
          body_path: tmp/release-notes.md

      - name: Upload CutBox.dmg release asset
        uses: actions/upload-release-asset@v1
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: CutBox.dmg
          asset_name: CutBox-${{env.RELEASE}}.dmg
          asset_content_type: application/octet-stream      

      - name: Update Info.plist (commit/push)
        run: |
          if [[ "$PRODUCTION_RUN" ==  "1" ]]; then
            bin/ci_update_info_plist
          fi

      - name: Notify brew tap cutbox
        run: |
          if [[ "$PRODUCTION_RUN" ==  "1" ]]; then
            curl \
               -s \
               -XPOST \
               -H "Accept: application/vnd.github+json" \
               -H "Authorization: Bearer ${{ secrets.CUTBOX_PAT }}" \
               https://api.github.com/repos/cutbox/homebrew-cutbox/actions/workflows/35793720/dispatches \
               --data '{"ref":"main","inputs":{"release":"${{ env.RELEASE }}"}}'
          fi

      - name: Notify cutbox.github.io
        run: |
          if [[ "$PRODUCTION_RUN" ==  "1" ]]; then
            curl \
               -s \
               -XPOST \
               -H "Accept: application/vnd.github+json" \
               -H "Authorization: Bearer ${{ secrets.CUTBOX_PAT }}" \
               https://api.github.com/repos/cutbox/cutbox.github.io/actions/workflows/35834026/dispatches \
               --data '{"ref":"master","inputs":{"release":"${{ env.RELEASE }}"}}'
          fi

      - name: Release Notes to Step Summary
        run: |
          {
            echo "- - -"
            cat tmp/release-notes.md
            echo ""
            echo "### Link to [${{env.RELEASE}}](${{ steps.create_release.outputs.html_url }})"
            echo "- - -"
            echo "### [Codacy Dashboard...](https://app.codacy.com/gh/cutbox/CutBox/dashboard)"
            echo "### [Code Coverage reports...](https://cutbox.github.io/CutBox/)"
            echo "- - -"
          } >> "$GITHUB_STEP_SUMMARY"
