name: build-release

on:
  workflow_dispatch:
    inputs:
      tag:
        description: "tag: git tag you want create. (sample 1.0.0)"
        required: true
      dry_run:
        description: "dry_run: true will never create relase/nuget."
        required: true
        default: "false"

env:
  GIT_TAG: ${{ github.event.inputs.tag }}
  DRY_RUN: ${{ github.event.inputs.dry_run }}
  DRY_RUN_BRANCH_PREFIX: "test_release"
  DOTNET_SDK_VERISON_3: 3.1.x

jobs:
  update-packagejson:
    runs-on: ubuntu-latest
    env:
      TARGET_FILE: ./src/UniTask/Assets/Plugins/UniTask/package.json
    outputs:
      sha: ${{ steps.commit.outputs.sha }}
    steps:
      - uses: actions/checkout@v2
      - name: before
        run: cat ${{ env.TARGET_FILE}}
      - name: update package.json to version ${{ env.GIT_TAG }}
        run: sed -i -e "s/\(\"version\":\) \"\(.*\)\",/\1 \"${{ env.GIT_TAG }}\",/" ${{ env.TARGET_FILE }}
      - name: after
        run: cat ${{ env.TARGET_FILE}}
      - name: Commit files
        id: commit
        run: |
          git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git commit -m "feat: Update package.json to ${{ env.GIT_TAG }}" -a
          echo "::set-output name=sha::$(git rev-parse HEAD)"
      - name: check sha
        run: echo "SHA ${SHA}"
        env:
          SHA: ${{ steps.commit.outputs.sha }}
      - name: tag
        run: git tag ${{ env.GIT_TAG }}
        if: env.DRY_RUN == 'false'
      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}
          tags: true
        if: env.DRY_RUN == 'false'
      - name: Push changes (dry_run)
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ env.DRY_RUN_BRANCH_PREFIX }}-${{ env.GIT_TAG }}
          tags: false
        if: env.DRY_RUN == 'true'

  build-dotnet:
    needs: [update-packagejson]
    runs-on: ubuntu-latest
    timeout-minutes: 10
    env:
      DOTNET_CLI_TELEMETRY_OPTOUT: 1
      DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
      NUGET_XMLDOC_MODE: skip
    steps:
      - run: echo ${{ needs.update-packagejson.outputs.sha }}
      - uses: actions/checkout@v2
        with:
          ref: ${{ needs.update-packagejson.outputs.sha }}
      - uses: actions/setup-dotnet@v1
        with:
          dotnet-version: "${{ env.DOTNET_SDK_VERSION_3 }}"
      # build and pack
      - run: dotnet build -c Release -p:Version=${{ env.GIT_TAG }}
      - run: dotnet test -c Release --no-build
      - run: dotnet pack ./src/UniTask.NetCore/UniTask.NetCore.csproj -c Release --no-build -p:Version=${{ env.GIT_TAG }} -o ./publish
      # Store artifacts.
      - uses: actions/upload-artifact@v1
        with:
          name: nuget
          path: ./publish/

  build-unity:
    needs: [update-packagejson]
    strategy:
      matrix:
        unity: ["2019.3.9f1"]
        include:
          - unity: 2019.3.9f1
            license: UNITY_LICENSE_2019
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - run: echo ${{ needs.update-packagejson.outputs.sha }}
      - uses: actions/checkout@v2
        with:
          ref: ${{ needs.update-packagejson.outputs.sha }}
      # Execute scripts: Export Package
      # /opt/Unity/Editor/Unity -quit -batchmode -nographics -silent-crashes -logFile -projectPath . -executeMethod PackageExporter.Export
      - name: Export unitypackage
        uses: game-ci/unity-builder@v2.0-alpha-6
        env:
          UNITY_LICENSE: ${{ secrets[matrix.license] }}
        with:
          projectPath: src/UniTask
          unityVersion: ${{ matrix.unity }}
          targetPlatform: StandaloneLinux64
          buildMethod: PackageExporter.Export
          versioning: None

      - name: check all .meta is commited
        run: |
          if git ls-files --others --exclude-standard -t | grep --regexp='[.]meta$'; then
            echo "Detected .meta file generated. Do you forgot commit a .meta file?"
            exit 1
          else
            echo "Great, all .meta files are commited."
          fi
        working-directory: src/UniTask

      # Store artifacts.
      - uses: actions/upload-artifact@v2
        with:
          name: UniTask.${{ env.GIT_TAG }}.unitypackage
          path: ./src/UniTask/UniTask.${{ env.GIT_TAG }}.unitypackage

  create-release:
    if: github.event.inputs.dry_run == 'false'
    needs: [update-packagejson, build-dotnet, build-unity]
    runs-on: ubuntu-latest
    env:
      DOTNET_CLI_TELEMETRY_OPTOUT: 1
      DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
      NUGET_XMLDOC_MODE: skip
    steps:
      # setup dotnet for nuget push
      - uses: actions/setup-dotnet@v1
        with:
          dotnet-version: "${{ env.DOTNET_SDK_VERSION_3 }}"
      # Create Releases
      - uses: actions/create-release@v1
        id: create_release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ env.GIT_TAG }}
          release_name: Ver.${{ env.GIT_TAG }}
          commitish: ${{ needs.update-packagejson.outputs.sha }}
          draft: true
          prerelease: false
      # Download(All) Artifacts to current directory
      - uses: actions/download-artifact@v2
      # Upload to NuGet
      - run: dotnet nuget push "./nuget/*.nupkg" --skip-duplicate -s https://www.nuget.org/api/v2/package -k ${{ secrets.NUGET_KEY }}
      # Upload to Releases(unitypackage)
      - uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./UniTask.${{ env.GIT_TAG }}.unitypackage/UniTask.${{ env.GIT_TAG }}.unitypackage
          asset_name: UniTask.${{ env.GIT_TAG }}.unitypackage
          asset_content_type: application/octet-stream

  cleanup:
    if: github.event.inputs.dry_run == 'true'
    needs: [build-dotnet, build-unity]
    runs-on: ubuntu-latest
    steps:
      - name: Delete branch
        uses: dawidd6/action-delete-branch@v3
        with:
          github_token: ${{ github.token }}
          branches: ${{ env.DRY_RUN_BRANCH_PREFIX }}-${{ env.GIT_TAG }}