Public/Set-ShieldsIoBadge.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
function Set-ShieldsIoBadge {
    <#
    .SYNOPSIS
        Modifies the link to a https://shields.io badge in a .md file. Can be used as part of a CI pipeline to update the status of
        badges such as Code Coverage.

    .FUNCTIONALITY
        CI/CD

    .DESCRIPTION
        This cmdlet can be used to update the link to a https://shields.io badge that has been created in a file such as readme.md.

        To use this function You need to have initially added the badge to your readme.md or specified file by adding the following
         string (ensuring 'Subject' matches what you specify for -Subject):

        ![Subject]()

    .PARAMETER Subject
        The label to assign to the badge. Default 'Build'.

    .PARAMETER Status
        The status text of value to assign to the badge. Default: 0.

    .PARAMETER Color
        The color to assign to the badge. If status is set to 0 - 100 and this parameter is not specified, the color is set
        automatically to either green, yellow, orange or red depending on the value, or light grey if it is not a 0 - 100 value.

    .PARAMETER AsPercentage
        Switch: Use to add a percentage sign after whatever you provide for -Status.

    .PARAMETER Path
        Path to the text file to update. By default this is $Env:BHProjectPath\Readme.md

    .EXAMPLE
        Set-ShieldsIoBadge -Subject 'Coverage' -Status ([math]::floor(100 - (($PesterResults.CodeCoverage.NumberOfCommandsMissed / $PesterResults.CodeCoverage.NumberOfCommandsAnalyzed) * 100))) -AsPercentage

    .LINK
        http://wragg.io/add-a-code-coverage-badge-to-your-powershell-deployment-pipeline/

    .LINK
        https://github.com/RamblingCookieMonster/BuildHelpers

    .LINK
        about_BuildHelpers
    #>

    [cmdletbinding(supportsshouldprocess)]
    param(
        [string]
        $Subject = 'Build',

        $Status = 0,

        [string]
        $Color,

        [switch]
        $AsPercentage,

        [string]
        $Path = "$Env:BHProjectPath\Readme.md"
    )
    Process
    {
        if (-not $Color)
        {
            $Color = switch ($Status)
            {
                {$_ -in 90..100 -or $_ -eq 'Pass'} { 'brightgreen' }
                {$_ -in 75..89}                    { 'yellow' }
                {$_ -in 60..74}                    { 'orange' }
                {$_ -in 0..59 -or $_ -eq 'Fail'}   { 'red' }
                default                            { 'lightgrey' }
            }
        }

        if ($AsPercentage)
        {
            $Percent = '%25'
        }

        if ($PSCmdlet.ShouldProcess($Path))
        {
            $ReadmeContent = (Get-Content $Path)
            $ReadmeContent = $ReadmeContent -replace "!\[$($Subject)\].+\)", "![$($Subject)](https://img.shields.io/badge/$Subject-$Status$Percent-$Color.svg)"
            $ReadmeContent | Set-Content -Path $Path
        }
    }
}