on:
  schedule:
    # Run every Wednesday
    - cron: 0 0 * * WED
  workflow_dispatch:
    inputs:
      create-release:
        required: false
        type: boolean
        description: If true, this run will tag and create a GitHub Release. Defaults to false.
      extension:
        required: false
        type: string
        description: The specific extension to publish

name: Release

jobs:
  setup-variables:
    name: Setup variables
    runs-on: ubuntu-latest
    outputs:
      tag: ${{ steps.get-tag.outputs.tag }}
      version: ${{ steps.get-tag.outputs.version }}
      matrix: ${{ steps.get-matrix.outputs.matrix }}
      release_upload_url: ${{ steps.create-release.outputs.upload_url }}
    steps:
      - name: Print inputs
        shell: pwsh
        run: |
          "Inputs"
          "create-release: ${{ github.event.inputs.create-release }}"
          "extension: ${{ github.event.inputs.extension }}"
  
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Get tag
        id: get-tag
        run: |
          $version = (Get-Content ./i18n/vscode-language-pack-cs/package.json -Raw | ConvertFrom-Json).version
          Write-Host "tag: release/$version"
          Write-Host "::set-output name=tag::release/$version"
          Write-Host "::set-output name=version::$version"
        shell: pwsh

      - name: Commit tagger
        if: ${{ github.event.inputs.create-release != 'false' }}
        uses: tvdias/github-tagger@v0.0.2
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          tag: ${{ steps.get-tag.outputs.tag }}
          
      - name: Create Release
        if: ${{ github.event.inputs.create-release != 'false' }}
        id: create-release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ steps.get-tag.outputs.tag }}
          release_name: Release ${{ steps.get-tag.outputs.version }}
          draft: false
          prerelease: false
          
      - name: Get full matrix
        id: get-matrix
        run: |
          $specifiedExtension = '${{ github.event.inputs.extension }}'
          $obj = if ($specifiedExtension) {
            if (!(Test-Path "i18n/$specifiedExtension")) {
              throw "specified extension not found"
            }
            @{
              languagePack = @($specifiedExtension)
            }
          } else {
            @{
              languagePack = Get-ChildItem i18n | ForEach-Object Name
            }
          }
          Write-Host "::set-output name=matrix::$($obj | ConvertTo-Json -Depth 100 -Compress)"
        shell: pwsh

  build-publish:
    name: 'Build & Publish'
    needs: setup-variables
    environment: deploy
    runs-on: ubuntu-latest
    strategy:
      matrix: ${{fromJSON(needs.setup-variables.outputs.matrix)}}
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          ref: ${{ needs.setup-variables.outputs.tag }}

      - name: Generate Name
        run: node -e "console.log('PACKAGE_NAME=' + require('./i18n/${{ matrix.languagePack }}/package.json').name + '-v' + require('./i18n/${{ matrix.languagePack }}/package.json').version)" >> $GITHUB_ENV

      - name: Install
        run: cd ./i18n/${{ matrix.languagePack }} && npm i

      - name: Build Extension
        run: cd ./i18n/${{ matrix.languagePack }} && npx vsce package -o ${{ env.PACKAGE_NAME }}.vsix

      - name: Publish Test Artifact
        uses: actions/upload-artifact@v2
        with:
          name: ${{ env.PACKAGE_NAME }}.vsix
          path: ./i18n/${{ matrix.languagePack }}/${{ env.PACKAGE_NAME }}.vsix
          
      - name: Verify access to publisher
        run: |
          RETRY_NUM=3
          RETRY_EVERY=10

          NUM=$RETRY_NUM
          until npx vsce verify-pat -p ${{ secrets.MARKETPLACE_PAT }} MS-CEINTL
          do
            1>&2 echo failure ... retrying $NUM more times
            sleep $RETRY_EVERY
            ((NUM--))

            if [ $NUM -eq 0 ]
            then
              1>&2 echo command was not successful after $RETRY_NUM tries
              exit 1
            fi
          done

          echo success!

      - name: Publish Extension
        run: npx vsce publish --packagePath ./i18n/${{ matrix.languagePack }}/${{ env.PACKAGE_NAME }}.vsix -p ${{ secrets.MARKETPLACE_PAT }}
          
      - name: Upload assets to a Release
        uses: AButler/upload-release-assets@v2.0
        with:
          files: ./i18n/${{ matrix.languagePack }}/${{ env.PACKAGE_NAME }}.vsix
          release-tag: ${{ needs.setup-variables.outputs.tag }}
          repo-token: ${{ secrets.GITHUB_TOKEN }}
  
  update-version:
    name: Update version
    needs: build-publish
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Rev patch versions on all
        if: ${{ github.event.inputs.extension == '' }}
        run: |
          Get-ChildItem i18n | ForEach-Object {
              cd $_.FullName
              npm version patch
          }
        shell: pwsh

      - name: Expose version for all
        if: ${{ github.event.inputs.extension == '' }}
        run: node -e "console.log('PACKAGE_VERSION=' + require('./i18n/vscode-language-pack-cs/package.json').version)" >> $GITHUB_ENV

      - name: Rev patch versions on specified extension
        if: ${{ github.event.inputs.extension != '' }}
        run: |
          Get-ChildItem i18n | Where-Object Name -eq '${{ github.event.inputs.extension }}' | ForEach-Object {
              cd $_.FullName
              npm version patch
          }
        shell: pwsh

      - name: Expose version for specified extension
        if: ${{ github.event.inputs.extension != '' }}
        run: node -e "console.log('PACKAGE_VERSION=' + require('./i18n/${{ github.event.inputs.extension }}/package.json').version)" >> $GITHUB_ENV

      - name: Create Pull Request on vscode-loc
        id: cpr
        uses: peter-evans/create-pull-request@v3
        with:
          token: ${{ secrets.REPO_PAT }}
          title: ${{ github.event.inputs.extension }}Rev patch versions to ${{ env.PACKAGE_VERSION }}
          body: Automated changes
          branch: ${{ github.event.inputs.extension }}rev-patch-versions-to-${{ env.PACKAGE_VERSION }}
          commit-message: rev patch versions to ${{ env.PACKAGE_VERSION }}
          delete-branch: true

      - name: Enable Pull Request Automerge
        if: steps.cpr.outputs.pull-request-operation == 'created'
        uses: peter-evans/enable-pull-request-automerge@v1
        with:
          token: ${{ secrets.REPO_PAT }}
          pull-request-number: ${{ steps.cpr.outputs.pull-request-number }}
          merge-method: squash
