Public/Add-CommandToManifest.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
using namespace Microsoft.PowerShell.EditorServices.Extensions
using namespace System.Collections.Generic
using namespace System.Linq

function Add-CommandToManifest {
    <#
    .EXTERNALHELP EditorServicesCommandSuite-help.xml
    #>

    [CmdletBinding()]
    [EditorCommand(DisplayName='Add Closest Function To Manifest')]
    param()

    $commandAst = Find-Ast -AtCursor |
        Find-Ast -Ancestor -First -IncludeStartingAst { $PSItem.Name -and $PSItem.Name -match '\w+-\w+'}

    $settings     = GetSettings
    $functionName = $commandAst.Name
    $filePath     = $psEditor.GetEditorContext().CurrentFile.Path

    try {
        $fileListEntry = $PSCmdlet.SessionState.Path.NormalizeRelativePath(
            $filePath,
            (ResolveRelativePath $settings.MainModuleDirectory))

        $manifestFile = ResolveRelativePath $settings.SourceManifestPath
    } catch {
        ThrowError -Exception ([ArgumentException]::new($Strings.InvalidSettingValue -f 'SourceManifestPath')) `
                   -Id        InvalidSettingValue `
                   -Category  InvalidArgument `
                   -Target    $settings
    }

    SetEditorLocation $manifestFile

    function GetManifestField ([string]$Name) {
        $field = Find-Ast -First { $PSItem.Value -eq $Name } | Find-Ast -First
        # This transforms a literal string array expression into it's output without invoking.
        $valueString = $field.ToString() -replace '@\(\)' `
                                         -split   '[,\n\s]' `
                                         -replace '['',\s]' `
                                         -match   '.' `
                                         -as      [List[string]]
        # yield
        [PSCustomObject]@{
            Ast    = $field
            Extent = $field.Extent
            Value  = $valueString
        }
    }

    $functions = GetManifestField -Name FunctionsToExport
    $functions.Value.Add($functionName)
    $functions.Value.Sort({ $args[0].CompareTo($args[1]) })
    $functions.Extent | Set-ScriptExtent -Text ([Enumerable]::Distinct($functions.Value)) -AsArray

    $fileList = GetManifestField -Name FileList

    $fileList.Value.Add($fileListEntry)
    $fileList.Value.Sort({ $args[0].CompareTo($args[1]) })
    $fileList.Extent | Set-ScriptExtent -Text ([Enumerable]::Distinct($fileList.Value)) -AsArray

    #SetEditorLocation $filePath
}