AzurePipelinesUtils.psm1

#Region './Private/Test-AzurePipelinesContext.ps1' -1

# Private helper function to validate Azure Pipelines context
function Test-PipelineContext {
    <#
    .SYNOPSIS
    Tests if the current session is running in an Azure DevOps Pipeline.
    
    .DESCRIPTION
    This private function checks for the presence of Azure DevOps environment variables
    to determine if the code is running within a pipeline context.
    
    .OUTPUTS
    [bool] Returns $true if running in Azure Pipelines, $false otherwise.
    #>

    [CmdletBinding()]
    [OutputType([bool])]
    param()
    
    $azureDevOpsVariables = @(
        'TF_BUILD',
        'AGENT_ID',
        'BUILD_BUILDID'
    )
    
    foreach ($variable in $azureDevOpsVariables) {
        if (Get-Item -Path "Env:$variable" -ErrorAction SilentlyContinue) {
            return $true
        }
    }
    
    return $false
}
#EndRegion './Private/Test-AzurePipelinesContext.ps1' 32
#Region './Public/Add-PipelineBuildTag.ps1' -1

function Add-PipelineBuildTag {
    <#
    .SYNOPSIS
    Adds a tag to the current Azure DevOps Pipeline build.
    
    .DESCRIPTION
    This function adds a tag to the current build using Azure DevOps Pipelines logging commands.
    Tags can be used to categorize and filter builds.
    
    .PARAMETER Tag
    The tag to add to the build.
    
    .EXAMPLE
    Add-PipelineBuildTag -Tag "release"
    
    .EXAMPLE
    Add-PipelineBuildTag -Tag "hotfix"
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, Position = 0)]
        [string]$Tag
    )
    
    Write-Output "##vso[build.addbuildtag]$Tag"
}
#EndRegion './Public/Add-PipelineBuildTag.ps1' 27
#Region './Public/Set-PipelineVariable.ps1' -1

function Set-PipelineVariable {
    <#
    .SYNOPSIS
    Sets a variable in Azure DevOps Pipelines.
    
    .DESCRIPTION
    This function sets a pipeline variable using Azure DevOps Pipelines logging commands.
    The variable can be used in subsequent tasks and jobs.
    
    .PARAMETER Name
    The name of the variable to set.
    
    .PARAMETER Value
    The value to assign to the variable.
    
    .PARAMETER Secret
    Indicates whether the variable should be treated as a secret.
    
    .PARAMETER Output
    Indicates whether the variable should be available to subsequent jobs.
    
    .EXAMPLE
    Set-PipelineVariable -Name "BuildNumber" -Value "1.0.42"
    
    .EXAMPLE
    Set-PipelineVariable -Name "ApiKey" -Value "secret123" -Secret
    
    .EXAMPLE
    Set-PipelineVariable -Name "DeploymentTarget" -Value "Production" -Output
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [string]$Name,
        
        [Parameter(Mandatory = $true)]
        [string]$Value,
        
        [Parameter(Mandatory = $false)]
        [switch]$Secret,
        
        [Parameter(Mandatory = $false)]
        [switch]$Output
    )
    
    $properties = ''

    if ($Secret) {
        $properties += ";issecret=true"
    }
    if ($Output) {
        $properties += ";isoutput=true"
    }

    Write-Output "##vso[task.setvariable variable=$Name$properties]$Value"
}
#EndRegion './Public/Set-PipelineVariable.ps1' 57
#Region './Public/Write-PipelineError.ps1' -1

function Write-PipelineError {
    <#
    .SYNOPSIS
    Writes an error message to Azure DevOps Pipelines output.
    
    .DESCRIPTION
    This function writes an error message using Azure DevOps Pipelines logging commands.
    The message will appear as an error in the pipeline logs and may cause the task to fail.
    
    .PARAMETER Message
    The error message to display.
    
    .PARAMETER SourcePath
    Optional source file path where the error occurred.
    
    .PARAMETER LineNumber
    Optional line number where the error occurred.
    
    .EXAMPLE
    Write-PipelineError -Message "File not found"
    
    .EXAMPLE
    Write-PipelineError -Message "Compilation failed" -SourcePath "build.ps1" -LineNumber 25
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, Position = 0)]
        [string]$Message,
        
        [Parameter(Mandatory = $false)]
        [string]$SourcePath,
        
        [Parameter(Mandatory = $false)]
        [int]$LineNumber
    )
    
    $properties = @()
    if ($SourcePath) {
        $properties += "sourcepath=$SourcePath"
    }
    if ($LineNumber) {
        $properties += "linenumber=$LineNumber"
    }
    
    $propertyString = if ($properties.Count -gt 0) { ";$($properties -join ';')" } else { "" }
    
    Write-Output "##vso[task.logissue type=error$propertyString]$Message"
}
#EndRegion './Public/Write-PipelineError.ps1' 49
#Region './Public/Write-PipelineSection.ps1' -1

function Write-PipelineSection {
    <#
    .SYNOPSIS
    Starts or ends a collapsible section in Azure DevOps Pipeline logs.
    
    .DESCRIPTION
    This function creates collapsible sections in the pipeline logs to organize output.
    Each section must be started and ended with the same section name.
    
    .PARAMETER Name
    The name of the section.
    
    .PARAMETER Start
    Indicates this is the start of a section.
    
    .PARAMETER End
    Indicates this is the end of a section.
    
    .EXAMPLE
    Write-PipelineSection -Name "Build" -Start
    # ... build commands ...
    Write-PipelineSection -Name "Build" -End
    #>

    [CmdletBinding(DefaultParameterSetName = 'Start')]
    param(
        [Parameter(Mandatory = $true)]
        [string]$Name,
        
        [Parameter(ParameterSetName = 'Start')]
        [switch]$Start,
        
        [Parameter(ParameterSetName = 'End')]
        [switch]$End
    )
    
    if ($Start -or $PSCmdlet.ParameterSetName -eq 'Start') {
        Write-Output "##[section]Starting: $Name"
    }
    else {
        Write-Output "##[section]Finishing: $Name"
    }
}
#EndRegion './Public/Write-PipelineSection.ps1' 43
#Region './Public/Write-PipelineTaskProgress.ps1' -1

function Write-PipelineTaskProgress {
    <#
    .SYNOPSIS
    Updates the progress of the current Azure DevOps Pipeline task.
    
    .DESCRIPTION
    This function updates the progress indicator for the current task using Azure DevOps Pipelines logging commands.
    
    .PARAMETER CurrentOperation
    The current operation being performed.
    
    .PARAMETER PercentComplete
    The percentage of completion (0-100).
    
    .EXAMPLE
    Write-PipelineTaskProgress -CurrentOperation "Installing dependencies" -PercentComplete 25
    
    .EXAMPLE
    Write-PipelineTaskProgress -CurrentOperation "Running tests" -PercentComplete 75
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [string]$CurrentOperation,
        
        [Parameter(Mandatory = $false)]
        [ValidateRange(0, 100)]
        [int]$PercentComplete
    )
    
    $properties = "currentoperation=$CurrentOperation"
    if ($PSBoundParameters.ContainsKey('PercentComplete')) {
        $properties += ";percentcomplete=$PercentComplete"
    }
    
    Write-Output "##vso[task.setprogress $properties]"
}
#EndRegion './Public/Write-PipelineTaskProgress.ps1' 38
#Region './Public/Write-PipelineWarning.ps1' -1

function Write-PipelineWarning {
    <#
    .SYNOPSIS
    Writes a warning message to Azure DevOps Pipelines output.
    
    .DESCRIPTION
    This function writes a warning message using Azure DevOps Pipelines logging commands.
    The message will appear as a warning in the pipeline logs.
    
    .PARAMETER Message
    The warning message to display.
    
    .PARAMETER SourcePath
    Optional source file path where the warning occurred.
    
    .PARAMETER LineNumber
    Optional line number where the warning occurred.
    
    .EXAMPLE
    Write-PipelineWarning -Message "This is a warning"
    
    .EXAMPLE
    Write-PipelineWarning -Message "Deprecated function used" -SourcePath "script.ps1" -LineNumber 42
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, Position = 0)]
        [string]$Message,
        
        [Parameter(Mandatory = $false)]
        [string]$SourcePath,
        
        [Parameter(Mandatory = $false)]
        [int]$LineNumber
    )
    
    $properties = @()
    if ($SourcePath) {
        $properties += "sourcepath=$SourcePath"
    }
    if ($LineNumber) {
        $properties += "linenumber=$LineNumber"
    }
    
    $propertyString = if ($properties.Count -gt 0) { ";$($properties -join ';')" } else { "" }
    
    Write-Output "##vso[task.logissue type=warning$propertyString]$Message"
}
#EndRegion './Public/Write-PipelineWarning.ps1' 49