name: LANCommander Release

on:
  push:
    tags:
      - 'v*'
jobs:
  build:
    runs-on: windows-latest

    steps:
    - uses: frabert/replace-string-action@v2
      name: Trim Tag Ref
      id: trim_tag_ref
      with:
        string: '${{ github.ref }}'
        pattern: 'refs/tags/v'
        replace-with: ''
    # Server
    - uses: actions/checkout@v3
    - name: Setup .NET
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: 6.0.x
    - name: Restore dependencies
      run: dotnet restore
    - name: Setup Node.js environment
      uses: actions/setup-node@v3.8.1
    - run: cd LANCommander/wwwroot/scripts; npm install
    - name: Build
      run: dotnet build "./LANCommander/LANCommander.csproj" --no-restore /p:Version="${{ steps.trim_tag_ref.outputs.replaced }}"
    - name: Publish
      run: dotnet publish "./LANCommander/LANCommander.csproj" -c Release -o _Build --self-contained --os win -p:PublishSingleFile=true
    - name: Sign Windows Binary
      uses: nadeemjazmawe/Sign-action-signtool.exe@v0.1
      with:
        certificate: "${{ secrets.CERTIFICATE }}"
        cert-password: "${{ secrets.CERTIFICATE_PASSWORD }}"
        filepath: "./_Build/LANCommander.exe"
    - name: Upload Artifacts
      uses: actions/upload-artifact@v2
      with:
        name: LANCommander-v${{ steps.trim_tag_ref.outputs.replaced }}
        path: "./_Build"
    # Client
    - uses: actions/checkout@v3
    - name: Setup MSBuild
      uses: microsoft/setup-msbuild@v1.3.1
    - name: Setup NuGet
      uses: NuGet/setup-nuget@v1.1.1
    - name: Restore NuGet packages
      run: nuget restore LANCommander.sln
    - name: Build and Publish Library
      run: msbuild LANCommander.Playnite.Extension/LANCommander.PlaynitePlugin.csproj /p:Configuration=Release /p:OutputPath=Build /p:Version="${{ steps.trim_tag_ref.outputs.replaced }}"
    - name: Sign Windows Binary
      uses: nadeemjazmawe/Sign-action-signtool.exe@v0.1
      with:
        certificate: "${{ secrets.CERTIFICATE }}"
        cert-password: "${{ secrets.CERTIFICATE_PASSWORD }}"
        filepath: "./LANCommander.Playnite.Extension/Build/LANCommander.PlaynitePlugin.dll"
    - name: Download Playnite Release
      uses: robinraju/release-downloader@v1.7
      with:
        repository: JosefNemec/Playnite
        tag: 10.18
        fileName: Playnite1018.zip
    - name: Extract Playnite
      run: Expand-Archive -Path Playnite1018.zip -DestinationPath Playnite
    - name: Update Manifest Versioning
      uses: fjogeleit/yaml-update-action@main
      with:
        valueFile: "LANCommander.Playnite.Extension/Build/extension.yaml"
        propertyPath: "Version"
        value: "${{ steps.trim_tag_ref.outputs.replaced }}"
        commitChange: false
    - name: Run Playnite Toolbox
      run: Playnite/Toolbox.exe pack LANCommander.Playnite.Extension/Build .
    - name: Upload Artifact
      uses: actions/upload-artifact@v3.1.2
      with:
        name: LANCommander.PlaynitePlugin-v${{ steps.trim_tag_ref.outputs.replaced }}
        path: LANCommander.PlaynitePlugin_48e1bac7-e0a0-45d7-ba83-36f5e9e959fc_*.pext
    # Release
    
