Bookmark.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
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
Set-Variable -Name "config" -Value ([string]"$HOME/.cdbookmark") -Scope script -Option constant
Set-Variable -Name "cdBookmark" -Value ([hashtable]@{}) -Scope global
function save {
    $global:cdBookmark | ConvertTo-Json | Out-File $script:config
}
function load {
    $o = Get-Content $script:config -ErrorAction SilentlyContinue -ErrorVariable $e | ConvertFrom-Json
    if ($e) {
        $global:cdBookmark = $null
        save 
    }
    # convert to hashtable
    $o.psobject.properties | Foreach { $global:cdBookmark[$_.Name] = $_.Value }
}

function Get-CdBookmark {
    Write-Output $global:cdBookmark

    <#
.Synopsis
    Show bookmarks.
.Description
.Example
    Get-Bookmarks
     
    Name Value
    ---- -----
    cdb C:\ws\project\Cdbookmark
    ws C:\ws\project
#>


}
function Use-CdBookmark {

    [CmdletBinding()]
    Param(
    )

    # https://blogs.technet.microsoft.com/pstips/2014/06/09/dynamic-validateset-in-a-dynamic-parameter/
    DynamicParam {
        # Set the dynamic parameters' name
        $ParameterName = 'Name'
            
        # Create the dictionary
        $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary

        # Create the collection of attributes
        $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
            
        # Create and set the parameters' attributes
        $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
        $ParameterAttribute.Mandatory = $true
        $ParameterAttribute.Position = 1

        # Add the attributes to the attributes collection
        $AttributeCollection.Add($ParameterAttribute)

        # Generate and set the ValidateSet
        $arrSet = $global:cdBookmark.keys
        $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)

        # Add the ValidateSet to the attributes collection
        $AttributeCollection.Add($ValidateSetAttribute)

        # Create and return the dynamic parameter
        $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
        $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
        return $RuntimeParameterDictionary
    }
    begin {
        # Bind the parameter to a friendly variable
        $Name = $PsBoundParameters[$ParameterName]
    }

    process {
        Set-Location $global:cdBookmark[$Name]
    }
    <#
.Synopsis
    Set bookmark location.
.Description
.Parameter Name
    bookmark name
.Example
    Set-CdBookmark cdb
 
#>

}
 
function Add-CdBookmark {
    Param(
        $Name,
        $Path
    )

    $global:cdBookmark[$Name] = (Resolve-Path $Path).Path
    save

    <#
.Synopsis
    Add or update bookmark
.Description
.Parameter Name
    bookmark name
.Parameter Path
    bookmark location
.Example
    Add-CdBookmark -Name cdb -Path C:\ws\project\Cdbookmark
     
#>

}
  
function Remove-CdBookmark {
    Param(
        [string]$Name,
        [switch]$All = $false
    )

    if ($All) {
        $global:cdBookmark = [hashtable]@{}
    }
    else {
        if ($global:cdBookmark) {
            $global:cdBookmark.Remove($Name)
            save
        }
    }

    <#
.Synopsis
    Remove bookmark
.Description
.Parameter Name
    bookmark name
.Example
    Remove-CdBookmark -Name cdb
#>

}