ProductivityTools.PSPublishModuleTo.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
function UpdateModuleVersion{
    [Cmdletbinding()]
    param(
        [string]$psd1Path
    )
    
    $psd1=Get-Content $psd1Path
    [version]$Version = [regex]::matches($psd1, "\s*ModuleVersion\s=\s'(\d*.\d*.\d*)'\s*").groups[1].value
    Write-Verbose "Old Version - $Version"
    [version]$NewVersion = "{0}.{1}.{2}" -f $Version.Major, $Version.Minor, ($Version.Build + 1)
    Write-Verbose "New Version - $NewVersion"
    $psd1 -replace $version, $NewVersion | Out-File $psd1Path -Force
}

function GetTypeOfModule{
     [Cmdletbinding()]
    param(
        [string]$psd1FullName
    )
    $psd1= Get-Content $psd1FullName -Raw
    $rootModule = [regex]::matches($psd1, "\s*RootModule\s=\s'(.*)'\s*")
    $rootModuleName=$rootModule.groups[1].value
    if($rootModuleName.EndsWith('.dll'))
    {
        return "binary"
    }
    
    if($rootModuleName.EndsWith('.psm1'))
    {
        return "text"
    }
    
    throw "In the Root module no dll nor psm1 type is defined"    
}

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

function Publish-ModuleTo{
    [Cmdletbinding()]
    param(
        [string]$PSRepositoryName="PSGallery", #PSGallery, PawelGallery
        [string]$NuGetApiKey,
        [switch]$IncreaseModuleVersion 
    )    

    $psd1s=@(Get-ChildItem -Recurse "*.psd1") 

    Write-Verbose "Found $($psd1s.Length) module manifests"

    foreach($psd1 in $psd1s)
    {
        $psd1FullName=$psd1.FullName
        if ($psd1FullName.Contains("bin")) {continue}

        $moduleType=GetTypeOfModule $psd1FullName
        if($IncreaseModuleVersion.IsPresent)
        {
            Write-Verbose "Update Module Version"
            UpdateModuleVersion $psd1FullName
        }
        
        $psd1ToBePublished;
        if ($moduleType -eq "binary")
        {
            Buildapplication
            $psd1sForBin=@(Get-ChildItem -Recurse "*.psd1") 
            $binPsd1=$psd1sForBin |where {$_.DirectoryName -like "*bin*" -and $_.Name -eq $psd1.Name}
            $psd1ToBePublished=$binPsd1
        }
        
        if ($moduleType -eq "text")
        {
            $psd1ToBePublished=$psd1FullName
        }

        Write-Verbose "Publish $fullPath"
        Write-Verbose "PSRepository: $PSRepositoryName"
        Write-Verbose "NuGetApiKey: $NuGetApiKey"
        Publish-Module -Repository $PSRepositoryName -NuGetApiKey $NuGetApiKey -Name $psd1ToBePublished  -Verbose:$VerbosePreference
    }
}

Export-ModuleMember Publish-ModuleTo