Public/Get-PSModuleManifest.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
function Get-PSModuleManifest {
    <#
    .SYNOPSIS
        Get the PowerShell module manifest for a project

    .FUNCTIONALITY
        CI/CD

    .DESCRIPTION
        Get the PowerShell module manifest for a project

        Evaluates based on the following scenarios:
            * Subfolder with the same name as the current folder with a psd1 file in it
            * Subfolder with a <subfolder-name>.psd1 file in it
            * Current folder with a <currentfolder-name>.psd1 file in it

        Note: This does not handle paths in the format Folder\ModuleName\Version\

    .PARAMETER Path
        Path to project root. Defaults to the current working path

    .NOTES
        We assume you are in the project root, for several of the fallback options

    .EXAMPLE
        Get-PSModuleManifest

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

    .LINK
        Get-BuildVariables

    .LINK
        Set-BuildEnvironment

    .LINK
        about_BuildHelpers
    #>

    [cmdletbinding()]
    param(
        $Path = $PWD.Path
    )

    $CurrentFolder = Split-Path $Path -Leaf
    $ExpectedPath = Join-Path -Path $Path -ChildPath $CurrentFolder
    $ExpectedManifest = Join-Path -Path $ExpectedPath -ChildPath "$CurrentFolder.psd1"
    if(Test-Path $ExpectedManifest)
    {
        $ExpectedManifest
    }
    else
    {
        # Look for properly organized modules
        $ProjectPaths = Get-ChildItem $Path -Directory | 
            ForEach-Object {
                $ThisFolder = $_
                $ExpectedManifest = Join-Path $ThisFolder.FullName "$($ThisFolder.Name).psd1"
                If( Test-Path $ExpectedManifest)
                {
                    $ExpectedManifest
                }
            }

        if( @($ProjectPaths).Count -gt 1 )
        {
            Write-Warning "Found more than one project path via subfolders with psd1 files"
            $ProjectPaths
        }
        elseif( @($ProjectPaths).Count -eq 1 )
        {
            $ProjectPaths
        }
        #PSD1 in root of project - ick, but happens.
        elseif( Test-Path "$ExpectedPath.psd1" )
        {
            "$ExpectedPath.psd1"
        }
        else
        {
            Write-Warning "Could not find a PowerShell module manifest from $($Path)"
        }
    }
}