src/module/MKModuleInfo.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
class MKModuleInfo {
    [string]$Name
    [string]$Path
    [string]$ModuleFolderName
    [string]$ManifestFilePath
    [string]$RootModuleFilePath
    [object]$Version
    [bool]$IsValid
        
    MKModuleInfo(
        [string]$Path,
        [string]$Name
    ) {
        $this.Path = $Path
        $this.Name = $Name
        
        $this.AssignRemainingFields()
    }

    [void]AssignRemainingFields() {
        $PSModuleInfo = $null
        
        try {
            if (-not $this.Name) {
                if (-not $this.Path) {
                    $this.Path = '.'
                }
                else {
                    # if backslash is at the end and this value is used with Import-Module, it will fail to import
                    $this.Path.TrimEnd('\', '/')
                }
                $this.Path = Resolve-Path $this.Path | Select-Object -ExpandProperty Path

                # it may be a .psd1 or .psm1 file
                if ($(Test-Path ($this.Path) -PathType Leaf)) {
                    $Item = Get-Item -Path ($this.Path)

                    if ($Item.Extension -eq '.psd1') {
                        $PSModuleInfo = Test-ModuleManifest $Item
                    }
                    elseif ($Item.Extension -eq '.psm1') {
                        # seek root folder only for .psd1 file
                        $PredicatedManifestPath = $this.PredicatedManifestPath($Item.Directory.FullName)
                    
                        $PSModuleInfo = Test-ModuleManifest $PredicatedManifestPath
                    }
                }
                else {
                    $PredicatedManifestPath = $this.PredicatedManifestPath($this.Path)
                    
                    $PSModuleInfo = Test-ModuleManifest $PredicatedManifestPath
                }
            }
            elseif ($this.Name) {
                $PSModuleInfo = Get-Module -Name $this.Name
            }
        }
        catch {
            $PSModuleInfo = $null
        }

        if ($PSModuleInfo) {
            $this.Name = $PSModuleInfo.Name
            $this.Path = $PSModuleInfo.ModuleBase
            $this.ModuleFolderName = Split-Path $PSModuleInfo.ModuleBase -Leaf
            $this.ManifestFilePath = $this.PredicatedManifestPath($this.Path)
            $this.RootModuleFilePath = Join-Path -Path ($this.Path) -ChildPath ($PSModuleInfo.RootModule)
            $this.Version = $PSModuleInfo.Version
        }
        else {
            if (-not $this.Name) {
                # Write-Host "Unable to acquire information about module with the given Path: "$this.Path
                $this.IsValid = $false
            }
            else {
                # Write-Host "Unable to acquire information about module with the given Name: "$this.Name
                $this.IsValid = $false
            }
        }

        $this.IsValid = $true
    }

    [string]PredicatedManifestPath([string]$Path) {
        # seek root folder only for .psd1 file
        return Get-ChildItem -Path $Path -Include '*.psd1' -Depth 0 | `
            Select-Object -ExpandProperty FullName
    }
}