-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPSNotes.ps1
More file actions
297 lines (237 loc) · 8.05 KB
/
PSNotes.ps1
File metadata and controls
297 lines (237 loc) · 8.05 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
Set-Alias -Name npp -Value "C:\Program Files (x86)\Notepad++\notepad++.exe"
# Where to store notebooks if no path is provided
$DefaultNotePath = "E:\Scripts\PSNotes"
# Where to store list of notebooks
$NoteBookList = Join-Path $DefaultNotePath NotesList.json
Function Get-NotebooksDynamicParam($Name) {
<#
.DESCRIPTION
Called by other functions in this script to add a dynamic parameter with a ValidateSet of the
existing notebooks. This allows tab completion.
#>
$attrib = New-Object System.Management.Automation.ParameterAttribute
$attrib.ParameterSetName = "__AllParameterSets"
$attrib.Position = 0
$attrib.Mandatory = $true
$items = (Get-Notebooks).Keys
$validate = New-Object System.Management.Automation.ValidateSetAttribute -ArgumentList $items
$AttributeCollection = New-Object 'Collections.ObjectModel.Collection[System.Attribute]'
$AttributeCollection.Add($attrib)
$AttributeCollection.Add($validate)
$DynParameter = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter -ArgumentList @($Name, [string], $AttributeCollection)
$ParamDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$ParamDictionary.Add($Name, $DynParameter)
$ParamDictionary
}
Function Get-Notebooks([switch]$Force) {
<#
.SYNOPSIS
Gets hashmap of notebooks currently configured.
.DESCRIPTION
Gets hashmap of notebooks currently configured. It attempts to load them from the json file
configured in $NoteBookList. If this file doesn't exist, and the Force parameter is present,
it will attempt to create this file.
.PARAMETER Force
If the file containing the list of notebooks (defined in $NoteBookList) doesn't exist, -Force
will attempt to create it.
.EXAMPLE
$ > Get-Notebooks
Name Value
---- -----
KMD D:\Notes\KMD.txt
QRS D:\Projects\PS\QRSNotes.txt
XYZ D:\Notes\XYZ.txt
.EXAMPLE
$ > Get-Notebooks -Force
Notebook list doesn't exist. Creating it at D:\Notes\Notebooklist.json
.NOTES
Written by Kevin Doblosky (kdoblosky@gmail.com). Licensed under an MIT license.
#>
# Make sure there is a variable defined for NoteBookList
try {
Get-Variable NoteBookList -ErrorAction Stop | Out-Null
} catch {
throw "`$NoteBookList is undefined. It should contain a path to a file containing json formatted list of notebooks."
}
$notebooks = [ordered]@{}
if (! (Test-Path $NoteBookList) ) {
if ($Force) {
try {
Write-Host "Notebook list doesn't exist. Creating it at $NoteBookList..."
New-Item $NoteBookList -ItemType File -Force -ErrorAction Stop | Out-Null
} catch {
throw "Error creating NoteBookList at $NoteBookList"
}
} else {
throw "Notebook list not found at $NoteBookList"
}
} else {
try {
(Get-Content $NoteBookList -Raw | ConvertFrom-Json).PSObject.Properties | Sort-Object Name | ForEach-Object {
$notebooks[$_.Name] = $_.Value
}
} catch {
# Need to validate this, for now ignoring any errors, so that it will return an empty hash
}
}
$notebooks
}
Function Get-NotebookPath {
[CmdletBinding()]
param()
DynamicParam{
Get-NotebooksDynamicParam -Name Alias
}
Process {
<#
.SYNOPSIS
Retrieves the path to the specified notebook.
.DESCRIPTION
Retrieves the path to the specified notebook.
.PARAMETER Alias
The alias for the notebook to retrieve.
.EXAMPLE
$ > Get-NotebookPath KMD
D:\Notes\KMD.txt
.NOTES
Written by Kevin Doblosky (kdoblosky@gmail.com). Licensed under an MIT license.
#>
$Alias = $PSBoundParameters.Alias
# TODO: Handle any errors from Get-Notebooks
$notebooks = Get-Notebooks
$notebooks[$Alias]
}
}
Function Add-Notebook($Alias, $NotebookPath) {
<#
.SYNOPSIS
Adds a notebook to the list.
.DESCRIPTION
Adds a new notebook to the list of notebooks.
.PARAMETER Alias
Alias of the notebook to add
.PARAMETER NotebookPath
Filesystem path to the notebook to be added.
.EXAMPLE
$ > Add-Notebook ZYX D:\Notes\ZYX-Notes.txt
.NOTES
Written by Kevin Doblosky (kdoblosky@gmail.com). Licensed under an MIT license.
#>
# Get Existing Notebooks
$notebooks = Get-Notebooks
# Create $notebooks if it doesn't already exist
if ($notebooks -eq $null) { $notebooks = @{} }
$notebooks[$Alias] = $NotebookPath
$notebooks | ConvertTo-Json | Set-Content $NoteBookList
}
Function Get-Notes{
[CmdletBinding()]
param()
DynamicParam{
Get-NotebooksDynamicParam -Name Notebook
}
process {
<#
.SYNOPSIS
Retrieves all notes for the specified notebook.
.DESCRIPTION
Retrieves all notes for the specified notebook.
.PARAMETER Notebook
The notebook alias to retreive the notes from
.EXAMPLE
$ > Get-Notes KMD
2014-02-18 - Almost time to immanentize the eschaton
2014-02-18 - I can see the fnords!
.NOTES
Written by Kevin Doblosky (kdoblosky@gmail.com). Licensed under an MIT license.
#>
$Notebook = $PSBoundParameters.Notebook
$notebooks = Get-Notebooks
if ($notebooks.Contains($Notebook)) {
Get-Content $notebooks[$Notebook]
} else {
"Notebook does not exist"
}
}
}
Function Open-Notebook {
<#
.SYNOPSIS
Opens a notebook in a text editor.
.DESCRIPTION
Opens a notebook in a text editor. If the alias npp is defined, it will use the editor defined there,
otherwise it will open the notebook in notepad.
.PARAMETER Notebook
Notebook alias to open.
.EXAMPLE
$ > Open-Notebook KMD
.NOTES
Written by Kevin Doblosky (kdoblosky@gmail.com). Licensed under an MIT license.
#>
[CmdletBinding()]
param()
DynamicParam{
Get-NotebooksDynamicParam -Name Notebook
}
process {
$Notebook = $PSBoundParameters.Notebook
try {
Get-Alias npp -ErrorAction Stop | Out-Null
npp (Get-NotebookPath -Alias $Notebook)
} catch {
notepad (Get-NotebookPath -Alias $Notebook)
}
}
}
Function Add-NewNote {
<#
.SYNOPSIS
Adds a new note to a notebook.
.DESCRIPTION
Adds a new note, prepended by a datestamp, to a notebook. If notebook doesn't exist, and
the -Force parameter is provided, will create the notebook at the default path.
.PARAMETER Notebook
Notebook alias to add the note to.
.PARAMETER Note
Note to add.
.PARAMETER Force
If provided, and the notebook doesn't exist, creates it.
.EXAMPLE
$ > Add-NewNote KMD "'Everything is theoretically impossible, until it is done.' - Robert A. Heinlein"
.EXAMPLE
$ > Add-NewNote QQQ "'Time is a drug. Too much of it kills you.' - Terry Prachtett" -Force
Notebook doesn't exist. Creating it at D:\Notes\QQQ.txt
.NOTES
Written by Kevin Doblosky (kdoblosky@gmail.com). Licensed under an MIT license.
#>
[CmdletBinding()]
param(
$Note,
[switch]$Force
)
DynamicParam{
Get-NotebooksDynamicParam -Name Notebook
}
Process {
$Notebook = $PSBoundParameters.Notebook
$NotebookPath = Get-NotebookPath -Alias $Notebook
if ($NotebookPath -eq $null) {
if ($Force) {
$NotebookPath = Join-Path $DefaultNotePath "$Notebook.txt"
Write-Host "Notebook doesn't exist. Creating it at $NotebookPath."
New-Item $NotebookPath -ItemType File -Force | Out-Null
Add-Notebook $Notebook $NotebookPath
}
else {
throw "Notebook doesn't exist"
}
}
$datestamp = (Get-Date).ToString("yyyy-MM-dd")
Add-Content -Path $NotebookPath -Value "$datestamp - $Note"
}
}
New-Alias -Name note -Value Add-NewNote
New-Alias -Name anb -Value Add-Notebook
New-Alias -Name gn -Value Get-Notes
New-Alias -Name nbs -Value Get-Notebooks
New-Alias -Name onb -Value Open-Notebook