ProductivityTools.PublishNugetToGallery.psm1

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
function IncreaseVersionPatch{
    [Cmdletbinding()]
    param(
        [switch]$IncreaseVersionPatch
    )
    
    if ($IncreaseVersionPatch.IsPresent)
    {
        IncreaseVersionPatch
    }
}


function GetPropertyGroup()
{
    [Cmdletbinding()]
    param(
        [xml]$proj
    )
    
    
    $array=$proj.Project.PropertyGroup -is [array]
    if($array)
    {
        Write-Verbose "In csproj we have couple of the property group nodes. Taking the first one!"
        $propertyGroup=$proj.Project.PropertyGroup[0]
    }
    else
    {
        $propertyGroup=$proj.Project.PropertyGroup
    }
    return $propertyGroup;
    
}

function IncreaseVersionPatch{
    $csprojs=Get-ChildItem *.csproj -Recurse
    foreach($csproj in $csprojs)
    {
        [xml]$proj=Get-Content $csproj

        $propertyGroup=GetPropertyGroup $proj
        
        $sVersion=$propertyGroup.Version
        Write-Verbose "Current version for project $csProj is $sVersion"
        if ($sVersion -ne $nuget -and $sVersion -ne "")
        {
            $sValue=$sVersion.Substring($sVersion.LastIndexOf('.')+1)
            $iValue=$sValue -as [int]
            $iValue++
            $mainVersion=$sVersion.Substring(0,$sVersion.LastIndexOf('.')+1)
            $updatedVersion=$mainVersion+$iValue.ToString();
            $propertyGroup.Version=$updatedVersion
            $proj.Save($csproj.FullName)
            Write-Verbose "Version in $($csproj.FullName) updated to $updatedVersion"
        }    
    }
}



function NugetVersionAlignedWithCsProj()
{
    [Cmdletbinding()]
    param(
        $nugetBaseName
    )
    
    $csprojs=Get-ChildItem *.csproj -Recurse
    foreach($csproj in $csprojs)
    {
        $csProjBaseName=$csproj.BaseName
        
        [xml]$proj=Get-Content $csproj
        $propertyGroup=GetPropertyGroup $proj
        $sVersion=$propertyGroup.Version
        Write-Verbose "Current version for project $($csProj.BaseName) is $sVersion"
        if ($sVersion -ne "")
        {
            $properVersion=$csProjBaseName + '.' + $sVersion
            if ($nugetBaseName -eq $properVersion)
            {
                Write-Verbose "$nugetBaseName nuget will be pushed to gallery ";
                return $true;
            }
        }
    }    
    return $false;
}

function CreateNugets{
    [Cmdletbinding()]
    param()
    
    dotnet pack
}

function PushNugets{
    [Cmdletbinding()]
    param()
    
    $nugetApiKey=Get-MasterConfiguration "NugetApiKey" -Verbose:$VerbosePreference
    $nugets=Get-ChildItem *.nupkg -Recurse
    foreach($nuget in $nugets)
    {
        $nugetBaseName=$nuget.BaseName
        Write-Verbose "Working with nuget $nugetBaseName"
        if(NugetVersionAlignedWithCsProj $nugetBaseName)
        {
            Write-Verbose "Nuget will be pushed $nuget"
            dotnet nuget push $nuget -s https://api.nuget.org/v3/index.json -k $nugetApiKey
        }
    }
}

function Publish-NugetToGallery {
    
    [Cmdletbinding()]
    param(
        [string]$Path=$(Get-Location),
        [switch]$IncreaseVersionPatch
    )

    Write-Verbose "Hello from Publish-NugetToGallery"
    Write-Verbose "Performing operation in directory $Path"
    cd $Path
    if ($IncreaseVersionPatch)
    {
        IncreaseVersionPatch 
    }
    CreateNugets
    PushNugets -Verbose:$VerbosePreference
}

Export-ModuleMember Publish-NugetToGallery