PSBookmark.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#TODO: Don't like this method; will update this without breaking the module later
$dataPath = "${env:USERPROFILE}\PSBookmarkData.ps1"

#Credit: June Blender - https://www.sapien.com/blog/2014/10/21/a-better-tostring-method-for-hash-tables/
function Convert-HashToString
{
    param
    (
        [Parameter(Mandatory = $true)]
        [System.Collections.Hashtable]
        $Hash
    )
    $hashstr = "@{"
    $keys = $Hash.keys
    foreach ($key in $keys)
    {
        $v = $Hash[$key]
        if ($key -match "\s")
        {
            $hashstr += "`"$key`"" + "=" + "`"$v`"" + ";"
        }
        else
        {
            $hashstr += $key + "=" + "`"$v`"" + ";"
        }
    }
    $hashstr += "}"
    return $hashstr
}

function New-DynamicParameter
{
    [CmdletBinding()]
    param (
        [Parameter()]
        [String] $Name,

        [Parameter()]
        $actionObject
    )

        $ParameterName = $Name
        $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
        $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
        $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
        $ParameterAttribute.Mandatory = $true
        $ParameterAttribute.Position = 0

        $AttributeCollection.Add($ParameterAttribute)
            
        $arrSet = $actionObject.Keys
        $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)

        $AttributeCollection.Add($ValidateSetAttribute)

        $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
        $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
        return $RuntimeParameterDictionary
}

function Save-LocationBookmark
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory, Position=0)]
        [string] $Alias,

        [Parameter(Position=1)]
        [String] $Location = $PWD.Path
    )

    if (Test-Path -Path $dataPath)
    {
        $locationHash = .$dataPath
    }
    else
    {
        $locationHash = @{}
    }
    $locationHash.Add($Alias,$Location)
    Convert-HashToString -Hash $locationHash | Out-File -FilePath $dataPath -Force
}

function Set-LocationBookmarkAsPWD
{
    [CmdletBinding()]
    param (

    )

    DynamicParam
    {
        if (Test-Path -Path $dataPath)
        {
            $locationHash = .$dataPath
            return (New-DynamicParameter -Name 'Alias' -actionObject $locationHash)
        }
    }

    begin
    {
        $alias = $PsBoundParameters['Alias']
    }

    process
    {
        if ($locationHash[$alias])
        {
            Set-Location -Path $locationHash[$alias]
        }
    }
}

function Get-LocationBookmark
{
    if (Test-Path -Path $dataPath)
    {
        $locationHash = .$dataPath
        return $locationHash
    }
    else
    {
        Write-Warning -Message 'No location aliases or bookmarks exist yet. Create one using either Save-LocationBookmark or save'
    }
}

function Remove-LocationBookmark
{
    [CmdletBinding()]
    param (

    )

    DynamicParam
    {
        if (Test-Path -Path $dataPath)
        {
            $locationHash = .$dataPath
            return (New-DynamicParameter -Name 'Alias' -actionObject $locationHash)
        }
    }

    begin
    {
        $alias = $PsBoundParameters['Alias']
    }

    process
    {
        $locationHash.Remove($alias)
        Convert-HashToString -Hash $locationHash | Out-File -FilePath $dataPath -Force
    }
}

Set-Alias -Name goto -Value Set-LocationBookmarkAsPWD
Set-Alias -Name save -Value Save-LocationBookmark
Set-Alias -Name glb -Value Get-LocationBookmark
Set-Alias -Name rlb -Value Remove-LocationBookmark

Export-ModuleMember -Function Save-LocationBookmark,Set-LocationBookmarkAsPWD,Get-LocationBookmark,Remove-LocationBookmark -Alias goto,save,glb,rlb