name: Build & Publish
on: [workflow_dispatch]

jobs:
  build:
    name: release
    runs-on: ubuntu-latest
    permissions:
        actions: read
        contents: write
        deployments: read
        packages: none
    steps:
    - name: Checkout
      uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
    - name: Use Node.js
      uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6.1.0
      with:
        node-version: 20
    # Run install dependencies
    - name: Install dependencies
      run: npm install
    # Run tests
    - name: Build
      run: npm run compile
    - name: Get current package version
      id: package_version
      uses: martinbeentjes/npm-get-version-action@3cf273023a0dda27efcd3164bdfb51908dd46a5b # v1.3.1
    - name: Check version is mentioned in Changelog
      uses: mindsers/changelog-reader-action@32aa5b4c155d76c94e4ec883a223c947b2f02656 # v2.2.3
      id: changelog_reader
      with:
        version: ${{ steps.package_version.outputs.current-version }}
        path: 'CHANGELOG.md'
    - name: Create a Release
      id: create_release
      uses: actions/create-release@0cb9c9b65d5d1901c1f53e5e66eaf4afd303e70e # v1.1.4
      env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name : ${{ steps.package_version.outputs.current-version}}
        release_name: ${{ steps.package_version.outputs.current-version}}
        body: Publish ${{ steps.changelog_reader.outputs.changes }}
    - name: Create vsix and publish to marketplace
      id: create_vsix
      uses: HaaLeo/publish-vscode-extension@v0
      with:
        pat: ${{ secrets.VS_MARKETPLACE_TOKEN }}
        registryUrl: https://marketplace.visualstudio.com
    - name: Attach vsix to release
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: ${{ steps.create_vsix.outputs.vsixPath}}
        asset_name: ${{ steps.create_vsix.outputs.vsixPath}}
        asset_content_type: application/vsix


