name: build

on:
  workflow_call:
    inputs:
      target: # windows linux macos
        required: true
        type: string
      project:
        required: false
        type: string
        default: './v2rayN.Desktop/v2rayN.Desktop.csproj'

jobs:
  build:
    name: build x64 arm64
    strategy:
      matrix:
        arch: [ x64, arm64 ]
    runs-on: |-
      ${{
        case(
          inputs.target == 'macos', 'macos-latest',
          inputs.target == 'linux', 'ubuntu-24.04',
          'ubuntu-latest'
        )
      }}
    env:
      Output: "${{ github.workspace }}/${{ matrix.arch }}"
      RID: |-
        ${{
          case(
            inputs.target == 'macos', format('osx-{0}', matrix.arch),
            inputs.target == 'windows', format('win-{0}', matrix.arch),
            format('{0}-{1}', inputs.target, matrix.arch)
          )
        }}
      Project: ${{ inputs.project }}
      ExtOpt: |-
        ${{
          case(
            inputs.target == 'windows', '-p:EnableWindowsTargeting=true',
            ''
          )
        }}

    steps:
    - name: Checkout
      uses: actions/checkout@v6
      with:
        submodules: 'recursive'
        fetch-depth: '0'

    - name: Setup .NET
      uses: actions/setup-dotnet@v5.2.0
      with:
        dotnet-version: '10.0.1xx'

    - name: Build v2rayN
      working-directory: ./v2rayN
      run: dotnet publish $Project -c Release -r $RID -p:SelfContained=true $ExtOpt -o $Output

    - name: Build AmazTool
      working-directory: ./v2rayN
      run: dotnet publish ./AmazTool/AmazTool.csproj -c Release -r $RID -p:SelfContained=true -p:PublishTrimmed=true $ExtOpt -o $Output

    - name: Upload build artifacts
      uses: actions/upload-artifact@v7.0.1
      with:
        name: ${{ matrix.arch }}
        path: ${{ matrix.arch }}