# Copyright (C) 2019 Intel Corporation.  All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
name: build wamr-ide vscode extension

on:
  workflow_call:
    inputs:
      upload_url:
        description: upload binary assets to the URL of release
        type: string
        required: true
      ver_num:
        description: a semantic version number.
        type: string
        required: true

jobs:
  build:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4

      - name: Use Node.js 16.x
        uses: actions/setup-node@v3
        with:
          node-version: 16.x

      - name: set vscode extension to correct version
        run: |
          npm install -g json
          json -I -f package.json -e "this.version=\"${{ inputs.ver_num }}\""
        working-directory: test-tools/wamr-ide/VSCode-Extension

      - name: generate wamr ide vscode extension
        run: |
          npm install -g vsce
          rm -rf node_modules
          npm install
          vsce package
        working-directory: test-tools/wamr-ide/VSCode-Extension

      - name: publish wamr ide vscode extension to the vsce marketplace
        if: ${{ github.repository == 'bytecodealliance/wasm-micro-runtime' }}
        run: |
          vsce publish -p ${{ secrets.TOKEN }}
        working-directory: test-tools/wamr-ide/VSCode-Extension

      - name: compress the vscode extension
        run: |
          mv wamride-*.vsix wamr-ide.vsix
          tar czf wamr-ide-${{ inputs.ver_num }}.tar.gz wamr-ide.vsix
          zip wamr-ide-${{ inputs.ver_num }}.zip wamr-ide.vsix
        working-directory: test-tools/wamr-ide/VSCode-Extension

      - name: upload release tar.gz
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ inputs.upload_url }}
          asset_path: test-tools/wamr-ide/VSCode-Extension/wamr-ide-${{ inputs.ver_num }}.tar.gz
          asset_name: wamr-ide-${{ inputs.ver_num }}.tar.gz
          asset_content_type: application/x-gzip

      - name: upload release zip
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ inputs.upload_url }}
          asset_path: test-tools/wamr-ide/VSCode-Extension/wamr-ide-${{ inputs.ver_num }}.zip
          asset_name: wamr-ide-${{ inputs.ver_num }}.zip
          asset_content_type: application/zip
