name: "publish"
on:
  push:
    branches:
      - release

jobs:
  create-release:
    runs-on: ubuntu-latest
    outputs:
      RELEASE_UPLOAD_ID: ${{ steps.create_release.outputs.id }}
      RELEASE_UPLOAD_URL: ${{ steps.create_release.outputs.upload_url }}

    steps:
      - uses: actions/checkout@v2
      - name: setup node
        uses: actions/setup-node@v1
        with:
          node-version: 12
      - name: get version
        run: echo ::set-env name=PACKAGE_VERSION::$(node -p "require('./package.json').version")
      - name: create release
        id: create_release
        uses: actions/create-release@v1.1.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: v${{ env.PACKAGE_VERSION }}
          release_name: "N-Link v${{ env.PACKAGE_VERSION }}"
          body: "See the assets to download this version and install."
          draft: true
          prerelease: false
  publish-tauri:
    needs: create-release
    strategy:
      fail-fast: false
      matrix:
        platform: [ macos-latest, ubuntu-18.04, windows-latest ]

    runs-on: ${{ matrix.platform }}
    steps:
      - uses: actions/checkout@v2
      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"
      - uses: actions/cache@v2
        id: yarn-cache
        with:
          path: |
            ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ matrix.platform }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ matrix.platform }}-yarn-
      - uses: actions/cache@v2
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            ~/.cargo/bin
            desktop/src-tauri/target
          key: ${{ matrix.platform }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ matrix.platform }}-cargo-
      - name: setup node
        uses: actions/setup-node@v1
        with:
          node-version: 12
      - name: install Rust stable
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
      - name: install tauri bundler
        if: runner.os != 'Windows'
        run: cargo install tauri-bundler || [ $? -eq 101 ]
      - name: install tauri bundler
        if: runner.os == 'Windows'
        run: cargo install tauri-bundler; exit ($LASTEXITCODE -ne 101 -and $LASTEXITCODE -ne 0)
        shell: powershell -command "& '{0}'"
      - name: install webkit2gtk (ubuntu only)
        if: matrix.platform == 'ubuntu-18.04'
        run: |
          sudo apt-get update
          sudo apt-get install -y webkit2gtk-4.0 squashfs-tools
      - name: install app dependencies
        run: yarn
      - uses: tauri-apps/tauri-action@v0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          npmScript: "tauri:build"
          projectPath: desktop/
          releaseId: ${{ needs.create-release.outputs.RELEASE_UPLOAD_ID }}
      - name: Upload Release
        if: runner.os == 'Linux'
        uses: actions/github-script@v2
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            console.log('environment', process.versions);

            const fs = require('fs').promises;

            const {repo: {owner, repo}, sha} = context;
            console.log({owner, repo, sha});
            const {name, version} = require(`${process.env.GITHUB_WORKSPACE}/package.json`);
            const arch = process.arch === 'x64' ? 'amd64' : process.arch;

            await github.repos.uploadReleaseAsset({
              owner, repo,
              release_id: "${{ needs.create-release.outputs.RELEASE_UPLOAD_ID }}",
              name: `${name}_${version}_${arch}.AppImage`,
              data: await fs.readFile(`${process.env.GITHUB_WORKSPACE}/desktop/src-tauri/target/release/bundle/appimage/n-link.AppImage`)
            });
  deploy-web:
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v2
      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"
      - uses: actions/cache@v2
        id: yarn-cache
        with:
          path: |
            ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ matrix.platform }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ubuntu-18.04-yarn-
      - name: setup node
        uses: actions/setup-node@v1
        with:
          node-version: 12
      - name: install app dependencies
        run: yarn
      - name: Build
        run: |
          yarn workspace web run generate
      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./web/dist
          publish_branch: gh-pages
          cname: n-link.lights0123.com
