# ASP.NET Core (.NET Framework)
# Build and test ASP.NET Core projects targeting the full .NET Framework.
# Add steps that publish symbols, save build artifacts, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core

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

trigger:
  branches:
    include:
    - '*'

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  nugetProject: 'e-suite.Service.Sentinel/e-suite.Service.Sentinel.csproj'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  ${{ if eq(variables['Build.SourceBranchName'], 'master') }}:
    prerelease: ''
  ${{ elseif eq(variables['Build.SourceBranchName'], 'develop') }}:
    prerelease: '-beta'    
  ${{ else }}:
    prerelease: '-alpha'

steps:
- task: NuGetToolInstaller@1
  displayName: 'Install Nuget'

- task: NuGetCommand@2
  displayName: 'Nuget Restore'
  inputs:
    restoreSolution: '$(solution)'
    feedsToUse: config
    includeNuGetOrg: true
    packagesdirectory: '..\packages'

- task: VSBuild@1
  displayName: 'Build Solution'
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="Default Web Site"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: VSTest@2
  displayName: 'Run Tests'
  inputs:
    testAssemblyVer2: |
     **\*Tests.dll
     !**\obj\**
    runOnlyImpactedTests: false
    runInParallel: true
    codeCoverageEnabled: true
    runSettingsFile: .runsettings
    platform: '$(BuildPlatform)'
    configuration: '$(BuildConfiguration)'

- task: BuildQualityChecks@9
  displayName: 'Check build quality'
  inputs:
    # ===== Warnings Policy Inputs =====
    checkWarnings: true # Optional
    warningFailOption: fixed # Optional; Valid values: build, fixed
    warningThreshold: '0' # Optional
    #forceFewerWarnings: false # Optional
    #allowWarningVariance: false # Optional
    #warningVariance: # Required if allowWarningVariance = true
    #showStatistics: false # Optional
    #evaluateTaskWarnings: true # Optional
    #warningTaskSelectors: '/^((vs|ms)build|ant(\s+.+)?|gradle(w)?(\s+.+)?|grunt|gulp|maven(\s+.+)?|xamarin(android|ios)|xcode(\s+.+)?|cmake|build\s+.+)$/i' # Optional (alias: warningTaskFilters)
    #warningSelectors: # Optional (alias: warningFilters)
    #inclusiveSelection: false # Optional (alias: inclusiveFiltering)
    #evaluateFileWarnings: false # Optional
    #warningFilesFolder: # Optional
    #warningFiles: # Required if evaluateFileWarnings = true
    #fileWarningSelectors: # Required if evaluateFileWarnings = true (alias: warningFileFilters)
    #warningFilesArtifact: # Required if evaluateFileWarnings = true and (warningFailOption = build or showStatistics = true)
    # ===== Code Coverage Policy Inputs =====
    checkCoverage: true # Optional
    coverageFailOption: fixed # Optional; Valid values: build, fixed
    coverageType: lines # Optional; Valid values: blocks, lines, branches, custom
    #customCoverageType: # Required if coverageType = custom
    #treat0of0as100: false # Optional
    coverageThreshold: '80' # Optional
    forceCoverageImprovement: true # Optional
    #coverageUpperThreshold: '80' # Optional
    #ignoreDecreaseAboveUpperThreshold: true # Optional
    #useUncoveredElements: false # Optional
    #allowCoverageVariance: false # Optional
    #coverageVariance: # Required if allowCoverageVariance = true
    #coverageDeltaType: percentage # Optional; Valid values: absolute, percentage
    #coveragePrecision: '4' # Optional
    #buildConfiguration: # Optional
    #buildPlatform: # Optional
    #explicitSelector: false # Optional (alias: explicitFilter)
    # ===== Baseline Inputs =====
    #includePartiallySucceeded: true # Optional
    #fallbackOnPRTargetBranch: true # Optional
    #baseDefinitionSelector: # Ignored - only used by UI editor
    #baseDefinitionId: # Optional
    #baseRepoId: # Ignored - only used by UI editor
    #baseBranchRef: # Optional
    # ===== Reporting Inputs =====
    #runTitle: # Optional
    #fileAnalysisTitle: # Optional
    # ===== Advanced Inputs =====
    #disableCertCheck: false # Optional
    #createBuildIssues: true # Optional

- task: DotNetCoreCLI@2
  displayName: "Package Nuget Artifact"
  condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
  inputs:
    command: 'pack'
    arguments: '--configuration $(buildConfiguration)'
    packagesToPack: '$(nugetProject)'
    nobuild: true
    versionEnvVar: 'build.BuildNumber'
    versioningScheme: 'byEnvVar'
    #versioningScheme: byBuildNumber # https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/build/dotnet-core-cli?view=azure-devops#yaml-snippet
    
- task: NuGetCommand@2
  displayName: 'Publish Nuget Artifact'
  condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
  inputs:
    command: 'push'
    feedsToUse: 'select'
    packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
    nuGetFeedType: 'internal'
    publishVstsFeed: 'e-suite/e-suite'
    versioningScheme: 'off'
    allowPackageConflicts: true