Functions.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

function _Initalize() {
    $script:_marksPath = Get-ProfileDataFile bookmarks "Bookmarks"
}


<#
 .Synopsis
   Add folder to bookmarks list.
 
 .Description
  Add folder to bookmarks list.
 
 .Parameter Name
  The bookmark name.
 
 .Parameter Path
  The Path to folder.
 
 .Example
   # Add bookmark with name.
   ./Add-PSBookmark [ ba ] BookmarkName (Opt)Directory
 
 .Example
   # Add bookmark from pipeline.
   $pwd | Add-PSBookmark -Name "ThisDirectory"
#>

function Add-PSBookmark () {
    Param (
        [Parameter(Position = 0, Mandatory = $true)]
        [Alias("Bookmark")]
        $Name,
        [Parameter(Position = 1, ValueFromPipeline  = $True)]
        [Alias("Path")]
        [string]$dir = $null
    )
    if ( Test-Empty $dir ) {
        $dir = (Get-Location).Path
    }

    $_marks = Import-PSBookmarks

    if( $_marks.ContainsKey("$Name") ){
        throw "Folder bookmark ''$Name'' already exist"
    }

    $_marks["$Name"] = $dir
    Save-PSBookmarks $_marks
    Write-Output ("Location '{1}' saved to bookmark '{0}'" -f $Name, $dir)     
}


<#
 .Synopsis
  Delete bookmark from list.
 
 .Description
  Removes folder from bookmarks list.
 
 .Parameter Name
  The bookmark name.
 .Example
   # Add bookmark with name.
   ./Remove-PSBookmark [ br ] BookmarkName
#>

function Remove-PSBookmark () {
    Param (
        [Parameter(Position = 0, Mandatory = $true)]
        [Alias("Bookmark")]
        [ArgumentCompleter(
            {
                param($Command, $Parameter, $WordToComplete, $CommandAst, $FakeBoundParams)
                return @($_marks) -like "$WordToComplete*"
            }
        )]
        $Name
    )

    $_marks = Import-PSBookmarks

    $_marks.Remove($Name)
    Save-PSBookmarks $_marks
    Write-Output ("Location '{0}' removed from bookmarks" -f $Name)     
}

function Remove-AllPSBookmarks {
    $_marks = @{ }
    Save-PSBookmarks $_marks
}

<#
 .Synopsis
  Open saved bookmark.
 
 .Description
  Open saved bookmark.
 
 .Parameter Name
  The bookmark name.
 .Example
   # Add bookmark with name.
   ./Open-PSBookmark [ bo ] BookmarkName
#>

function Open-PSBookmark() {
    Param(
    [Parameter(Position = 0, ValueFromPipelineByPropertyName  = $True, Mandatory = $true)]
    [Alias("Bookmark")]
    [ArgumentCompleter(
        {
            param($Command, $Parameter, $WordToComplete, $CommandAst, $FakeBoundParams)
            return @($_marks) -like "$WordToComplete*"
        }
    )]
    $Name
    )
    $_marks = Import-PSBookmarks
    Set-Location $_marks["$Name"]
}

function Import-PSBookmarks {
    $_marks = @{ }
    if (test-path "$script:_marksPath" -PathType leaf) {
        Import-Csv  $script:_marksPath | ForEach-Object { $_marks[$_.key] = $_.value }
    }
    return $_marks
}

function Save-PSBookmarks {
    param (
        $marks
    )    
    $marks.getenumerator() | export-csv "$_marksPath" -notype
}


<#
 .Synopsis
  List saved bookmark.
 
 .Description
  List saved bookmark.
 
 .Example
   # Add bookmark with name.
   ./Get-PSBookmarks [ bl ] [bv]
#>

function Get-PSBookmarks {
    $_marks = Import-PSBookmarks
    $_marks.Clone()
}



<#
 .Synopsis
   Update folder location in the bookmarks list
 
 .Description
  Update folder location in the bookmarks list
 
 .Parameter Name
  The bookmark name.
 
 .Parameter Path
  The Path to folder.
 
 .Example
   # Update bookmark with name.
   ./Update-PSBookmark [ bu ] BookmarkName (Opt)Directory
 
 .Example
   # Update bookmark from pipeline.
   $pwd | Update-PSBookmark -Name "ThisDirectory"
#>

function Update-PSBookmark () {
    Param (
        [Parameter(Position = 0, Mandatory = $true)]
        [Alias("Bookmark")]
        $Name,
        [Parameter(Position = 1, ValueFromPipeline  = $True)]
        [Alias("Path")]
        [string]$dir = $null
    )
    if ( Test-Empty $dir ) {
        $dir = (Get-Location).Path
    }
    
    $_marks = Import-PSBookmarks

    if( -not  $_marks.ContainsKey("$Name") ){
        throw "Folder bookmark ''$Name'' doesn't exist"
    }

    $_marks["$Name"] = $dir
    Save-PSBookmarks $_marks
    Write-Output ("The bookmark {0} updated with location '{1}'" -f $Name, $dir)     
}