Get-ModuleBase.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
<#PSScriptInfo
 
.VERSION 0.4
 
.GUID 01a27130-5e5f-41f7-a699-b6e9546fdede
 
.AUTHOR David Walker, Sitecore Dave, Radical Dave
 
.COMPANYNAME David Walker, Sitecore Dave, Radical Dave
 
.COPYRIGHT David Walker, Sitecore Dave, Radical Dave
 
.TAGS powershell file io
 
.LICENSEURI https://github.com/Radical-Dave/Get-ModuleBase/blob/main/LICENSE
 
.PROJECTURI https://github.com/Radical-Dave/Get-ModuleBase
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
Get path to ModuleBase of current script
In dev, there is extra level for solution that is removed
 
.EXAMPLE
PS> .\Get-ModuleBase
#>

$ErrorActionPreference = 'Continue'
$moduleName = $MyInvocation.MyCommand.Module.Name
if(!$moduleName) {
    $moduleName = $pwd #Split-Path $pwd -Leaf
    $srcFolders = "src public private tests"
    do {
        if (!$moduleName) {return}
        $moduleName = (Get-Item $moduleName).Parent.Name
        if (!$moduleName) {return}
    } While ($srcFolders.contains($moduleName.ToLower()))
}
$moduleBase = "$($MyInvocation.MyCommand.Module.ModuleBase)"
if(!$moduleBase) {$moduleBase = "$pwd"}
$ndx = $moduleBase.IndexOf($moduleName)
if($ndx -ne -1) {$moduleBase = $moduleBase.Substring(0, $ndx + $moduleName.Length)}
#Write-Output "moduleName:$moduleName"
return $moduleBase