# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

name: $(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)$(postfix)$(branchName) # NOTE: rev resets when the default retention period expires

trigger:
  branches:
    include:
    - '*'

pool:
  vmImage: windows-latest # ubuntu-latest - set to windows-latest or another Windows vmImage for Windows builds

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

  ${{ if eq(variables['Build.SourceBranchName'], 'master') }}:
    branchName: ''
  ${{ elseif startsWith(variables['Build.SourceBranch'], 'refs/heads/') }}:
    branchName: $[ replace(replace(variables['Build.SourceBranch'], 'refs/heads/', ''), '/', '-' ) ]
  ${{ elseif startsWith(variables['Build.SourceBranch'], 'refs/pull/') }}:
    branchName: $[ replace(replace(variables['System.PullRequest.TargetBranch'], 'refs/heads/', ''), '/', '-' ) ]

  ${{ if eq(variables['branchName'], '') }}:
    postfix: ''
  ${{ else }}:
    postfix: '-'

steps:
- task: UseDotNet@2
  displayName: 'Set .net core version'
  inputs:
    version: '6.0.x'

- task: PowerShell@2
  displayName: 'Update Build Version Number'
  inputs:
    targetType: 'inline'
    script: |
      $VersionRegex = "\d+\.\d+\.\d+\.\d+"      
      $VersionData = [string]::Format("{0}",[regex]::matches($Env:BUILD_BUILDNUMBER,$VersionRegex)[0]).substring(2)
      
      Write-Host "Versioning started"
      "Sources directory " + $Env:BUILD_SOURCESDIRECTORY      
      "Build number " + $VersionData
      $csprojfilename = $Env:BUILD_SOURCESDIRECTORY+"\eSuite.WorkBench\eSuite.WorkBench.csproj"
      "Project file to update " + $csprojfilename
      [xml]$csprojcontents = Get-Content -Path $csprojfilename;
      "Current version number is " + $csprojcontents.Project.PropertyGroup.Version
      $oldversionNumber = $csprojcontents.Project.PropertyGroup.Version
      $csprojcontents.Project.PropertyGroup.Version = $VersionData
      $csprojcontents.Save($csprojfilename)
      "Version number has been udated from " + $oldversionNumber + " to " + $VersionData
      Write-Host "Finished"
   
- task: DotNetCoreCLI@2
  displayName: 'Nuget Restore'
  inputs:
    command: 'restore'
    feedsToUse: config
    projects: '**/*.csproj'
    nugetConfigPath: nuget.config

- task: VSBuild@1
  displayName: 'Build'
  inputs:
    solution: '$(solution)'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    msbuildArgs: '/p:RunWixToolsOutOfProc=true'


- task: CopyFiles@2
  condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
  inputs:
    sourceFolder: eSuite.WorkBench.Installer\bin\$(buildConfiguration)
    contents: eSuite.WorkBench.Installer.msi
    targetFolder: $(build.artifactstagingdirectory)
    #cleanTargetFolder: false # Optional
    #overWrite: false # Optional
    #flattenFolders: false # Optional
    #preserveTimestamp: false # Optional
    #retryCount: 0 # Optional
    #ignoreMakeDirErrors: false # Optional

- task: PublishBuildArtifacts@1
  condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
  inputs:
    #pathToPublish: '$(Build.ArtifactStagingDirectory)' 
    artifactName: 'installer' 
    #publishLocation: 'Container' # Options: container, filePath
    #targetPath: # Required when publishLocation == FilePath
    #parallel: false # Optional
    #parallelCount: # Optional
    #fileCopyOptions: #Optional
    #storeAsTar: false # Optional
  
#Disabling this as the universal packages are not straight forward to work with.
#- task: UniversalPackages@0
#  displayName: 'Publish To artifacts feed'
#  inputs:
#    command: 'publish' # Options: download, publish
#    #downloadDirectory: '$(System.DefaultWorkingDirectory)' # Required when command == Download
#    feedsToUse: 'internal' # Options: internal, external
#    #externalFeedCredentials: # Optional
#    vstsFeed: 'e-suite/e-suite' # Required when feedsToUse == Internal
#    #vstsFeedPackage: # Required when feedsToUse == Internal
#    #vstsPackageVersion: # Required when feedsToUse == Internal
#    #feedDownloadExternal: # Required when feedsToUse == External
#    #packageDownloadExternal: # Required when feedsToUse == External
#    #versionDownloadExternal: # Required when feedsToUse == External
#    publishDirectory: '$(Build.ArtifactStagingDirectory)' # Required when command == Publish
#    feedsToUsePublish: 'internal' # Options: internal, external
#    #publishFeedCredentials: # Required when feedsToUsePublish == External
#    vstsFeedPublish: 'e-suite/e-suite'# Required when feedsToUsePublish == Internal
#    #publishPackageMetadata: true # Optional
#    vstsFeedPackagePublish: 'e-suite.workbench' # Required when feedsToUsePublish == Internal
#    #feedPublishExternal: # Required when feedsToUsePublish == External
#    #packagePublishExternal: # Required when feedsToUsePublish == External
#    #versionOption: 'patch' # Options: major, minor, patch, custom
#    #versionPublish: # Required when versionOption == Custom
#    packagePublishDescription: 'Easily choose and run your e-suite locally'
#    #verbosity: 'None' # Options: none, trace, debug, information, warning, error, critical
#    #publishedPackageVar: # Optional