parameters:
- name: ApplicationName
  type: string
- name: SolutionFile
  type: string
- name: ProjectFile
  type: string
- name: PublishFolder
  type: string
- name: BuildConfiguration
  type: string
  default: Release
- name: BuildPlatform
  type: string
  default: AnyCPU

stages:
- stage: build
  displayName: Build and Publish

  jobs:
  - job: build
    displayName: Build and Publish
    workspace:
      clean: all

    steps:
    - task: PowerShell@2
      displayName: Write Build Parameters
      condition: contains(variables['system.debug'], 'true')
      inputs:
        targetType: inline
        script: |
          Write-Host "ApplicationName: ${{parameters.ApplicationName}}"
          Write-Host "SolutionFile: ${{parameters.SolutionFile}}"
          Write-Host "ProjectFile: ${{parameters.ProjectFile}}"
          Write-Host "PublishFolder: ${{parameters.PublishFolder}}"
          Write-Host "BuildConfiguration: ${{parameters.BuildConfiguration}}"
          Write-Host "BuildPlatform: ${{parameters.BuildPlatform}}"
        pwsh: true

    - template: create-agent-capabilities-variables.yml

    - task: PowerShell@2
      displayName: Write Environment Variables
      condition: contains(variables['system.debug'], 'true')
      inputs:
        targetType: inline
        script: '(gci env:*).GetEnumerator() | Sort-Object Name | Out-String'
        pwsh: true

    - task: PowerShell@2
      displayName: Tag Files
      env:
        DEPLOY_BUILDNUMBER: $(Build.BuildNumber)
      inputs:
        targetType: inline
        script: |
          $appVersion = $env:DEPLOY_BUILDNUMBER
          $tag = $appVersion

          Write-Host "Performing Git Tag Delete"
          git tag -d $tag
          git push origin :refs/tags/$tag

          Write-Host "Performing Git Tag"
          git tag $tag

          Write-Host "Performing Git Tag Push"
          git push --tags
        pwsh: true
        workingDirectory: '$(Build.SourcesDirectory)'

    - task: NuGetToolInstaller@1
      displayName: Install NuGet 6.7.0
      inputs:
        versionSpec: '6.7.0'

    - task: NuGetCommand@2
      displayName: NuGet Restore
      inputs:
        restoreSolution: '${{parameters.SolutionFile}}'

    - template: update-semantic-versioning-in-assembyinfo-files.yml
      parameters:
        ApplicationName: ${{parameters.ApplicationName}}
        PathToSearch: '$(Build.SourcesDirectory)/src'

    - task: VSBuild@1
      displayName: Build ${{parameters.ApplicationName}} Project
      inputs:
        solution: '${{parameters.ProjectFile}}'
        msbuildArgs: '/t:publish /p:PublishDir="$(Build.BinariesDirectory)"'
        platform: '${{parameters.BuildPlatform}}'
        configuration: '${{parameters.BuildConfiguration}}'
        clean: true

    - task: PowerShell@2
      displayName: Clean-up Published Files
      env:
        BUILD_BINARIESDIRECTORY: $(Build.BinariesDirectory)
      inputs:
        targetType: inline
        script: |
          # Remove unwanted files
          Remove-Item -Path "$env:BUILD_BINARIESDIRECTORY/ServerManager.Plugin.Common.dll" -Force -ErrorAction Ignore
          Remove-Item -Path "$env:BUILD_BINARIESDIRECTORY/*.config" -Force -ErrorAction Ignore
        pwsh: true

    - task: CopyFiles@2
      displayName: Copy Artifact Files
      inputs:
        SourceFolder: '$(Build.SourcesDirectory)'
        Contents: |
          src/${{parameters.ApplicationName}}/VersionFeed.xml
          src/${{parameters.ApplicationName}}/VersionFeedBeta.xml
          src/${{parameters.ApplicationName}}/Globalization/en-US/en-US.xaml
        TargetFolder: '$(Build.ArtifactStagingDirectory)'
        OverWrite: true
        flattenFolders: true
        preserveTimestamp: true

    - task: PowerShell@2
      displayName: Create Latest Version Files
      env:
        BUILD_BUILDNUMBER: $(Build.BuildNumber)
        BUILD_ARTIFACTSTAGINGDIRECTORY: $(Build.ArtifactStagingDirectory)
      inputs:
        targetType: inline
        script: |
          $appVersion = $env:BUILD_BUILDNUMBER
          $appVersionShort = $appVersion.Substring(0, $appVersion.LastIndexOf('.'))

          $versionFile = "$env:BUILD_ARTIFACTSTAGINGDIRECTORY/latest.txt"
          $AppVersionShort | Set-Content "$versionFile"

          $versionFile = "$env:BUILD_ARTIFACTSTAGINGDIRECTORY/latestBeta.txt"
          $AppVersion | Set-Content "$versionFile"
        pwsh: true

    - task: PowerShell@2
      displayName: Archive Binary Files
      env:
        BUILD_BUILDNUMBER: $(Build.BuildNumber)
        BUILD_ARTIFACTSTAGINGDIRECTORY: $(Build.ArtifactStagingDirectory)
        BUILD_BINARIESDIRECTORY: $(Build.BinariesDirectory)
      inputs:
        targetType: inline
        script: |
          $appVersion = $env:BUILD_BUILDNUMBER
          $appVersionShort = $appVersion.Substring(0, $appVersion.LastIndexOf('.'))

          $sourcePath = "$env:BUILD_BINARIESDIRECTORY"
          $zipFile = "$env:BUILD_ARTIFACTSTAGINGDIRECTORY/ServerManager.${{parameters.ApplicationName}}_$($appVersionShort).zip"

          Add-Type -Assembly System.IO.Compression.FileSystem
          $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
          [System.IO.Compression.ZipFile]::CreateFromDirectory($sourcePath, $zipFile, $compressionLevel, $false)

          Copy-Item -Path $zipFile -Destination "$env:BUILD_ARTIFACTSTAGINGDIRECTORY/latest.zip" -Force
        pwsh: true

    - task: PublishBuildArtifacts@1
      displayName: Publish Artifacts
      enabled: true
      inputs:
        PathtoPublish: '$(Build.ArtifactStagingDirectory)'
        ArtifactName: '${{parameters.PublishFolder}}'

    - template: clean-agent-folders.yml
      parameters:
        CleanArtifactsFolder: true
        CleanBinariesFolder: true
        CleanSourcesFolder: true
        CleanPublishFolder: true
        PublishFolder: ${{parameters.PublishFolder}}
