# Huge thanks to Alacritty, as their configuration served as a starting point for this one!
# See: https://github.com/alacritty/alacritty

name: Release

on:
  push:
    branches:
      - master
      - dev

env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  CARGO_TERM_COLOR: always

jobs:
  extract-version:
    name: extract-version
    runs-on: ubuntu-latest
    outputs:
      espanso_version: ${{ steps.version.outputs.version }}
    steps:
      - uses: actions/checkout@v2
      - name: "Extract version"
        id: "version"
        run: |
          ESPANSO_VERSION=$(cat espanso/Cargo.toml | grep version | head -1 | awk -F '"' '{ print $2 }')
          echo version: $ESPANSO_VERSION
          echo "::set-output name=version::v$ESPANSO_VERSION"

  create-release:
    name: create-release
    needs: ["extract-version"]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Create new release (only on master)
        if: ${{ github.ref == 'refs/heads/master' }}
        run: |
          COMMIT_HASH=$(git rev-list --max-count=1 HEAD)
          echo "Creating release: ${{ needs.extract-version.outputs.espanso_version }}"
          echo "for hash: $COMMIT_HASH"
          gh release create ${{ needs.extract-version.outputs.espanso_version }} \
            -t ${{ needs.extract-version.outputs.espanso_version }} \
            --notes "Automatically released by CI" \
            --prerelease \
            --target $COMMIT_HASH

  windows:
    needs: ["extract-version", "create-release"]
    runs-on: windows-latest

    defaults:
      run:
        shell: bash

    steps:
      - uses: actions/checkout@v2
      - name: Print target version
        run: |
          echo Using version ${{ needs.extract-version.outputs.espanso_version }}
      - name: Install cargo-make
        run: |
          cargo install --force cargo-make --version 0.34.0
      - name: Test
        run: cargo make test-binary --profile release
      - name: Build
        run: cargo make build-windows-all --profile release
      - name: Create portable mode archive
        shell: powershell
        run: |
          Rename-Item target/windows/portable espanso-portable
          Compress-Archive target/windows/espanso-portable target/windows/Espanso-Win-Portable-x86_64.zip
      - name: Calculate hashes
        shell: powershell
        run: |
          Get-FileHash target/windows/Espanso-Win-Portable-x86_64.zip -Algorithm SHA256 | select-object -ExpandProperty Hash > target/windows/Espanso-Win-Portable-x86_64.zip.sha256.txt
          Get-FileHash target/windows/installer/Espanso-Win-Installer-x86_64.exe -Algorithm SHA256 | select-object -ExpandProperty Hash > target/windows/installer/Espanso-Win-Installer-x86_64.exe.sha256.txt
      - uses: actions/upload-artifact@v2
        name: "Upload artifacts"
        with:
          name: Windows Artifacts
          path: |
            target/windows/installer/Espanso-Win-Installer-x86_64.exe
            target/windows/Espanso-Win-Portable-x86_64.zip
            target/windows/installer/Espanso-Win-Installer-x86_64.exe.sha256.txt
            target/windows/Espanso-Win-Portable-x86_64.zip.sha256.txt
      - name: Upload artifacts to Github Releases (if master)
        if: ${{ github.ref == 'refs/heads/master' }}
        run: |
          gh release upload ${{ needs.extract-version.outputs.espanso_version }} target/windows/installer/Espanso-Win-Installer-x86_64.exe target/windows/Espanso-Win-Portable-x86_64.zip target/windows/installer/Espanso-Win-Installer-x86_64.exe.sha256.txt target/windows/Espanso-Win-Portable-x86_64.zip.sha256.txt
  
  linux-x11:
    needs: ["extract-version", "create-release"]
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Print target version
        run: |
          echo Using version ${{ needs.extract-version.outputs.espanso_version }}
      - name: Build docker image
        run: |
          sudo docker build -t espanso-ubuntu . -f .github/scripts/ubuntu/Dockerfile
      - name: Build AppImage
        run: |
          sudo docker run --rm -v "$(pwd):/shared" espanso-ubuntu espanso/.github/scripts/ubuntu/build_appimage.sh
      - uses: actions/upload-artifact@v2
        name: "Upload artifacts"
        with:
          name: Linux X11 Artifacts
          path: |
            Espanso-X11.AppImage
            Espanso-X11.AppImage.sha256.txt
      - name: Upload artifacts to Github Releases (if master)
        if: ${{ github.ref == 'refs/heads/master' }}
        run: |
          gh release upload ${{ needs.extract-version.outputs.espanso_version }} Espanso-X11.AppImage Espanso-X11.AppImage.sha256.txt

  macos-intel:
    needs: ["extract-version", "create-release"]
    runs-on: macos-11

    steps:
      - uses: actions/checkout@v2
      - name: Print target version
        run: |
          echo Using version ${{ needs.extract-version.outputs.espanso_version }}
      - name: Install cargo-make
        run: |
          cargo install --force cargo-make --version 0.34.0
      - name: Test
        run: cargo make test-binary --profile release
        env:
          MACOSX_DEPLOYMENT_TARGET: "10.13"
      - name: Build
        run: cargo make create-bundle --profile release
        env:
          MACOSX_DEPLOYMENT_TARGET: "10.13"
      - name: Create ZIP archive
        run: |
          ditto -c -k --sequesterRsrc --keepParent target/mac/Espanso.app Espanso-Mac-Intel.zip
      - name: Calculate hashes
        run: |
          shasum -a 256 Espanso-Mac-Intel.zip > Espanso-Mac-Intel.zip.sha256.txt
      - uses: actions/upload-artifact@v2
        name: "Upload artifacts"
        with:
          name: Mac Intel Artifacts
          path: |
            Espanso-Mac-Intel.zip
            Espanso-Mac-Intel.zip.sha256.txt
      - name: Upload artifacts to Github Releases (if master)
        if: ${{ github.ref == 'refs/heads/master' }}
        run: |
          gh release upload ${{ needs.extract-version.outputs.espanso_version }} Espanso-Mac-Intel.zip Espanso-Mac-Intel.zip.sha256.txt
  
  macos-m1:
    needs: ["extract-version", "create-release"]
    runs-on: macos-11

    steps:
      - uses: actions/checkout@v2
      - name: Print target version
        run: |
          echo Using version ${{ needs.extract-version.outputs.espanso_version }}
      - name: Install rust target
        run: rustup update && rustup target add aarch64-apple-darwin
      - name: Install cargo-make
        run: |
          cargo install --force cargo-make --version 0.34.0
      - name: Build
        run: cargo make create-bundle --profile release --env BUILD_ARCH=aarch64-apple-darwin
      - name: Codesign executable
        env: 
          MACOS_CERTIFICATE: ${{ secrets.MACOS_CERTIFICATE }}
          MACOS_CERTIFICATE_PWD: ${{ secrets.MACOS_CERTIFICATE_PWD }}
          MACOS_CI_KEYCHAIN_PWD: ${{ secrets.MACOS_CI_KEYCHAIN_PWD }}
        run: |
          echo $MACOS_CERTIFICATE | base64 --decode > certificate.p12
          security create-keychain -p $MACOS_CI_KEYCHAIN_PWD buildespanso.keychain 
          security default-keychain -s buildespanso.keychain
          security unlock-keychain -p $MACOS_CI_KEYCHAIN_PWD buildespanso.keychain
          security import certificate.p12 -k buildespanso.keychain -P $MACOS_CERTIFICATE_PWD -T /usr/bin/codesign
          security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k $MACOS_CI_KEYCHAIN_PWD buildespanso.keychain
          /usr/bin/codesign --force -s "Espanso CI Self-Signed" target/mac/Espanso.app -v
      - name: Create ZIP archive
        run: |
          ditto -c -k --sequesterRsrc --keepParent target/mac/Espanso.app Espanso-Mac-M1.zip
      - name: Calculate hashes
        run: |
          shasum -a 256 Espanso-Mac-M1.zip > Espanso-Mac-M1.zip.sha256.txt
      - uses: actions/upload-artifact@v2
        name: "Upload artifacts"
        with:
          name: Mac M1 Artifacts
          path: |
            Espanso-Mac-M1.zip
            Espanso-Mac-M1.zip.sha256.txt
      - name: Upload artifacts to Github Releases (if master)
        if: ${{ github.ref == 'refs/heads/master' }}
        run: |
          gh release upload ${{ needs.extract-version.outputs.espanso_version }} Espanso-Mac-M1.zip Espanso-Mac-M1.zip.sha256.txt