Export-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
function Export-Macro
{
    <#
    .Synopsis
        Exports Macros
    .Description
        Exports Macro definitions.
 
        Exported Macros are fully self contained and do not require the original module to run.
    .Notes
        By default, this will recreate each macro in it's own module, which will have PrivateData containing the Macro input.
 
        To export macros inline as a series of scripts and functions, use -Inline
    #>

    param(
    # The name of the macro.
    [Parameter(Mandatory,ValueFromPipelineByPropertyName)]
    [string[]]
    $Name,

    # The macro prefix.
    # The default prefix is an underscore _.
    [string]
    $Prefix = '_',

    # If set, will declare the functions inline, instead of declaring the macro module.
    [switch]
    $Inline
    )


    process {
        $macroSplat = @{} + $PSBoundParameters
        $macroSplat.Remove('Inline')
        $macroModules = Get-Macro @macroSplat

        foreach ($lm in $macroModules) {
            if ($Inline) {
                foreach ($cmd in $lm.ExportedCommands.Values) {
                    if ($cmd -is [Management.Automation.FunctionInfo])
                    {
                        "function $($cmd.Name) {
$($cmd.Definition)}"

                    }
                    elseif ($cmd -is [Management.Automation.AliasInfo])
                    {
                        "Set-Alias -Name '$($cmd.Name.Replace("'","''"))' -Value '$($cmd.Definition.Replace("'","''"))'"
                    }
                }
            } else {
                @"
`${$($lm.Name)} =
    New-Module -Name '$($lm.Name)' -ScriptBlock {
$($lm.Definition)
}
`${$($lm.Name)}.PrivateData = ConvertFrom-Json @'
$($lm.PrivateData | ConvertTo-Json -Depth 100)
'@
`${$($lm.Name)} | Import-Module -Global
"@

            }

        }
    }
}