Parts/Path.Macro.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
85
86
87
88
89
90
91
92
93
94
95
<#
.Synopsis
    The Macro for -Path based functions
.Description
    The process{} block for any macro that uses -Path.
.Notes
    Macros must be included inline, in order to get the correct invocation name.
#>


$callStack = @(Get-PSCallStack)

$isBeingAssigned = -not "$($callstack[-1].Position.Text)".Trim().StartsWith($MyInvocation.InvocationName)
$isBeingPiped = $MyInvocation.PipelinePosition -lt $MyInvocation.PipelineLength
$pathItem = Get-Item -ErrorAction SilentlyContinue -Path $path
$fileData =
    switch ($pathItem.Extension) {
        .json {
            [IO.File]::ReadAllText($pathItem.FullName) | ConvertFrom-Json
        }
        .psd1 {
            Import-LocalizedData -BaseDirectory $pathItem.Directory.FullName -FileName $pathItem.Name
        }
        {'.clixml', '.clix' -contains $_} {
            Import-Clixml -LiteralPath $pathItem.FullName
        }
        {$_ -like '*.xml' -or $_ -eq '.svg'} {
            [xml][IO.File]::ReadAllText($pathItem.FullName)
        }
        {'.csv', '.tsv' -contains $_} {
            if ($pathItem.Extension -eq '.tsv') {
                Import-Csv -LiteralPath $pathItem.FullName -Delimiter "`t"
            } else {
                Import-Csv -LiteralPath $pathItem.FullName
            }
        }
        {'.md', '.txt','.html','.htm' -contains $_} {
            [IO.File]::ReadAllText($pathItem.FullName)
        }

    }
    <#if ($pathItem.Extension -eq '.json')
    {
 
    }
    elseif ('.clixml', '.clix' -contains $pathItem.Extension)
    {
 
    }
    elseif
    {
 
    }
    elseif ($pathItem.Extension -eq '.psd1')
    {
 
    }
    elseif ()
    {
 
    }
    elseif ('.md', '.txt','.html','.htm' -contains $pathItem.Extension)
    {
 
    }#>

if ($isBeingAssigned -or $isBeingPiped) {
    if ($pathItem) {
        if ($fileData)
        {
            $fileData
        } else {
            $pathItem
        }
    } else {
        $path
    }
} else {
    if ($pathItem.PSIsContainer) {
        if ("$(Resolve-Path $path)" -eq "$(Resolve-Path $pwd)") {
            $path
        } else {
            Push-Location $path
        }

    } elseif ($pathItem) {
        if ($fileData)
        {
            $fileData
        } else {
            . $path @args
        }

    } else {
        $path
    }
}