Skip to content

Commit 552d106

Browse files
v1.0.0
1 parent 2178790 commit 552d106

19 files changed

+151
-107
lines changed

ChangeLog.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# ChangeLog for PSFunctionInfo
22

3+
## 1.0.0
4+
5+
+ Restructured module layout.
6+
+ Added a property set called `TagInfo`.
7+
+ Added command `Edit-PSFunctionInfo` with an alias of `efpi`. ([Issue #7](https://github.com/jdhitsolutions/PSFunctionInfo/issues/7))
8+
+ Added better error handling to `Get-PSFunctionInfo` where function can't be found.
9+
+ Help updates.
10+
+ First official release to the PowerShell Gallery.
11+
+ Updated `README.md`.
12+
313
## 0.6.0-preview
414

515
+ Fixed the `-Name` parameter argument completer in `Get-PSFunctionInfo`. The function parameter changed from `Name` to `FunctionName` which is why it broke. ([Issue #6](https://github.com/jdhitsolutions/PSFunctionInfo/issues/6))

PSFunctionInfo.psd1

194 Bytes
Binary file not shown.

README.md

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
## Synopsis
66

7+
![documents](images/psfunctioninfo-icon.png)
8+
79
This module contains a set of PowerShell commands to add and manage metadata in stand-alone PowerShell functions.
810

911
## Installation
@@ -18,19 +20,19 @@ The module should work on both Windows PowerShell and PowerShell 7.x, even cross
1820

1921
## Description
2022

21-
The purpose of this code is to provide a way to get versioning and other metadata information for functions that may not belong to a module. This is information you want to get after the function has been loaded into your PowerShell session. I have numerous stand-alone functions. These functions don't belong to a module, so there is no version or source information. However, I'd like to use that type of information for non-module files.
23+
The purpose of this code is to provide a way to get versioning and other metadata information for functions that do not belong to a module. This is information you want to get after the function has been loaded into your PowerShell session. I have numerous stand-alone functions. These functions don't belong to a module, so there is no version or source information. However, I'd like to have that type of information for non-module functions.
2224

23-
The code in this module isn't concerned with loading, running, or finding functions. By default, [Get-PSFunctionInfo](docs/Get-PSFunctionInfo.md) queries whatever is in the `Function:` PSDrive. If the PowerShell function belongs to a module, then you'll get the module version and source. Otherwise, you can use the function metadata.
25+
The code in this module isn't concerned with loading, running, or finding functions. By default, [Get-PSFunctionInfo](docs/Get-PSFunctionInfo.md) queries whatever is in the `Function:` PSDrive. If the PowerShell function belongs to a module, then you'll get the module version and source. Otherwise, the command will use the function metadata.
2426

25-
![Get a single function](assets/get-psfunctioninfo-1.png)
27+
![Get a single function](images/get-psfunctioninfo-1.png)
2628

2729
The default behavior is to show all functions that __don't__ belong to a module.
2830

29-
![Get stand-alone functions](assets/get-psfunctioninfo-2.png)
31+
![Get stand-alone functions](images/get-psfunctioninfo-2.png)
3032

31-
You can also get functions by tag. Use `Get-PSFunctionInfoTag` to get a list of tags currently in use.
33+
You can also get currently loaded functions by tag. Use `Get-PSFunctionInfoTag` to get a list of tags currently in use.
3234

33-
![Get functions by tag](assets/get-psfunctioninfo-3.png)
35+
![Get functions by tag](images/get-psfunctioninfo-3.png)
3436

3537
The PSFunctionInfo object includes a PropertySet called `AuthorInfo`.
3638

@@ -50,12 +52,12 @@ LastUpdate : 4/23/2021 9:21:00 AM
5052
Or you can use the `TagInfo` property set. This gives you the same result as using the `tag` named view with `Format-Table`.,
5153

5254
```powershell
53-
Get-psfunctioninfo | Select-object taginfo
55+
Get-PSFunctionInfo | Select-Object taginfo
5456
```
5557

5658
Finally, you can also search .ps1 files for PSFunctionInfo metadata.
5759

58-
![Get function info from file](assets/get-psfunctioninfo-file.png)
60+
![Get function info from file](images/get-psfunctioninfo-file.png)
5961

6062
## Creating PSFunctionInfo
6163

@@ -65,7 +67,7 @@ Use the [New-PSFunctionInfo](docs/New-PSFunctionInfo.md) command to insert the m
6567
New-PSFunctionInfo -Path c:\scripts\Test-ConsoleColors.ps1 -Description "show console color combinations" -Name Test-ConsoleColor -Author "Jeff Hicks" -CompanyName "JDH IT Solutions" -Copyright "2021 JDH IT Solutions, Inc." -Tags "scripting","console"
6668
```
6769

68-
The default behavior is to insert the metadata tag immediately after the opening brace ({) into the file. **This command will update the file**. Or you can use the `ToClipBoard` parameter which will copy the metatadata to the clipboard and you can manually insert it into your script file that defines the function.
70+
The default behavior is to insert the metadata tag immediately after the opening brace (`{`) into the file. **This command will update the file**. Or you can use the `ToClipBoard` parameter which will copy the metatadata to the clipboard. You can then manually insert it into your script file that defines the function. You should avoid changing the formatting of the comment block.
6971

7072
You should get something like this:
7173

@@ -86,13 +88,13 @@ Source C:\scripts\Test-ConsoleColors.ps1
8688
#>
8789
```
8890

89-
This command not work with functions defined in a single line like this:
91+
This command **not work** with functions defined in a single line like:
9092

9193
```powershell
9294
Function Get-Foo { Get-Date }
9395
```
9496

95-
You can still run `New-PSFunctionInfo` with the `ToClipboard` parameter and manually edit your function to insert the metadata.
97+
However, you could run `New-PSFunctionInfo` with the `ToClipboard` parameter and manually edit your function to insert the metadata.
9698

9799
```powershell
98100
Function Get-Foo {
@@ -114,11 +116,11 @@ Source C:\scripts\FooStuff.ps1
114116
}
115117
```
116118

117-
There are no commands to modify or remove function metadata. It is assumed that when you update the function, you can update or remove the metadata.
119+
Currently, there are no commands to modify or remove function metadata. It is assumed that when you update the function, you can update or remove the metadata.
118120

119121
### Backup
120122

121-
Because creating a PSFunctionInfo metadata comment block modifies the file, you might feel safer with a backup. `New-PSFunctionInfo` has a `-BackupParameter` which will create a backup copy of the source file before inserting the metadata comment block. The file will be created in the same directory, appending an extension of .bak1. If there are previous backups, the number will increment, i.e. .bak2. You have to manually delete the backup files.
123+
Because creating a PSFunctionInfo metadata comment block modifies the file, you might feel safer with a file backup. `New-PSFunctionInfo` has a `-BackupParameter` which will create a backup copy of the source file before inserting the metadata comment block. The file will be created in the same directory, appending an extension of .bak1. If there are previous backups, the number will increment, i.e. .bak2. You have to manually delete the backup files.
122124

123125
The `-Backup` parameter has no effect if you use `-Clipboard`.
124126

@@ -142,23 +144,23 @@ When you import the module into an editor, you will get additional features to m
142144

143145
If you have an open file, in the integrated PowerShell console, you can run `New-PSFunctionfo` and press <kbd>TAB</kbd> to tab-complete the detected functions in the current file. The file path will automatically be detected. You can enter other values such as version, or simply press <kbd>ENTER</kbd> to insert the metadata, which you can then edit.
144146

145-
![vscode integration](assets/psfunctioninfo-vscode.png)
147+
![vscode integration](images/psfunctioninfo-vscode.png)
146148

147149
This example is taking advantage of saved defaults.
148150

149151
### PowerShell ISE
150152

151153
When you import the module in the PowerShell ISE, it will add a menu shortcut.
152154

153-
![ISE Menu](assets/ise-psfunction-menu.png)
155+
![ISE Menu](images/ise-psfunction-menu.png)
154156

155157
With a loaded file, you could run `New-PSFunctionInfo` in the console specifying the function name. The Path will be auto-detected. Or use the menu shortcut which will give you a graphical "function picker"
156158

157-
![function picker](assets/ise-function-picker.png)
159+
![function picker](images/ise-function-picker.png)
158160

159161
Select a function and click <kbd>OK</kbd>. The metadata block will be inserted into the file. This will not work with a file that has unsaved changes. When you insert new function metadata, the file in the ISE will be closed, re-opened, and focus should jump to the function.
160162

161-
![ISE metadata](assets/ise-psfunctioninfo.png)
163+
![ISE metadata](images/ise-psfunctioninfo.png)
162164

163165
### Editing Source Files
164166

@@ -176,7 +178,7 @@ Or pipe to it.
176178
Get-PSFunctionInfo Get-QOTD | Edit-PSFunctionInfo -editor ise
177179
```
178180

179-
Once opened, you will need to navigate to the function and metadata section.
181+
Once opened, you will need to navigate to the appropriate function and metadata section.
180182

181183
## Background
182184

@@ -187,5 +189,7 @@ This module was first described at <https://jdhitsolutions.com/blog/powershell/8
187189
## Roadmap
188190

189191
+ Add function metadata by file, autodetecting the function name.
192+
+ Consider a bulk editing command.
193+
+ Consider a bulk removal command to clean PSFunctionInfo metadata from files.
190194

191-
Last Updated 2021-04-29 17:39:48Z
195+
Last Updated 2021-05-03 10:10:39Z

en-us/about_PSFunctionInfo.help.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
TOPIC
2-
about_psfunctioninfo
2+
about_PSFunctionInfo
33

44
SHORT DESCRIPTION
55
This module contains a set of PowerShell commands to add and manage metadata

functions/Edit-PSFunctionInfo.ps1

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@ Function Edit-PSFunctionInfo {
33
[Outputtype("None")]
44
[Alias("epfi")]
55
Param(
6-
[Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName,HelpMessage = "Specify the path to the source file.",ParameterSetName="source")]
6+
[Parameter(
7+
ValueFromPipeline,
8+
ValueFromPipelineByPropertyName,
9+
HelpMessage = "Specify the path to the source file.",
10+
ParameterSetName="source"
11+
)]
712
[ValidateNotNullOrEmpty()]
813
[string]$Source,
914
[parameter(HelpMessage = "Specify the name of a loaded function.")]
10-
[Parameter(Mandatory,Position=0,ParameterSetName="name")]
15+
[Parameter(
16+
Mandatory,
17+
Position=0,
18+
ParameterSetName="name"
19+
)]
1120
[string]$Name,
1221
[Parameter(HelpMessage = "Specify the editor you want to use. On non-Windows systems enter the value in lower case.")]
1322
[ValidateSet("code","ise","notepad")]
@@ -96,7 +105,6 @@ Function Edit-PSFunctionInfo {
96105
else {
97106
Write-Warning "Failed to load function source."
98107
}
99-
100108
} #process
101109

102110
End {

functions/Get-PSFunctionInfo.ps1

Lines changed: 79 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ Function Get-PSFunctionInfo {
4545
#get location of PSFunctionInfo
4646
$start = 0
4747

48-
#[regex]$rxName = "(\s+)?Function\s+\S+"
4948
#need to ignore case
50-
$rxname =[System.Text.RegularExpressions.Regex]::new("(\s+)?[Ff]unction\s+\S+","IgnoreCase")
49+
$rxname = [System.Text.RegularExpressions.Regex]::new("(\s+)?Function\s+\S+", "IgnoreCase")
5150
do {
5251

5352
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Searching $path at $start"
@@ -96,87 +95,91 @@ Function Get-PSFunctionInfo {
9695

9796
}
9897
else {
99-
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Getting function $FunctionName"
100-
101-
# filter out functions with a module source and that pass the private filtering test
102-
$functions = (Get-ChildItem -Path Function:\$FunctionName).where( { -Not $_.source -And (test_functionname $_.name) })
103-
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Found $($functions.count) functions"
104-
Foreach ($fun in $functions) {
105-
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] $($fun.name)"
106-
$definition = $fun.definition -split "`n"
107-
$m = $definition | Select-String -Pattern "#(\s+)?PSFunctionInfo"
108-
if ($m.count -gt 1) {
109-
Write-Warning "Multiple matches found for PSFunctionInfo in $($fun.name). Will only process the first one."
110-
}
111-
if ($m) {
112-
#get the starting line number
113-
$i = $m[0].LineNumber
114-
115-
$meta = While ($definition[$i] -notmatch "#\>") {
116-
$raw = $definition[$i]
117-
if ($raw -match "\w+") {
118-
$raw
119-
}
120-
$i++
98+
if (Test-Path Function:\$FunctionName) {
99+
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Getting function $FunctionName"
100+
# filter out functions with a module source and that pass the private filtering test
101+
$functions = (Get-ChildItem -Path Function:\$FunctionName).where( { -Not $_.source -And (test_functionname $_.name) })
102+
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Found $($functions.count) functions"
103+
Foreach ($fun in $functions) {
104+
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] $($fun.name)"
105+
$definition = $fun.definition -split "`n"
106+
$m = $definition | Select-String -Pattern "#(\s+)?PSFunctionInfo"
107+
if ($m.count -gt 1) {
108+
Write-Warning "Multiple matches found for PSFunctionInfo in $($fun.name). Will only process the first one."
121109
}
110+
if ($m) {
111+
#get the starting line number
112+
$i = $m[0].LineNumber
113+
114+
$meta = While ($definition[$i] -notmatch "#\>") {
115+
$raw = $definition[$i]
116+
if ($raw -match "\w+") {
117+
$raw
118+
}
119+
$i++
120+
}
122121

123-
#Define a hashtable that will eventually become a custom object
124-
$h = @{
125-
Name = $fun.name
126-
CommandType = $fun.CommandType
127-
Module = $fun.Module
128-
}
129-
#parse the metadata using regular expressions
130-
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Parsing metadata"
131-
for ($i = 0; $i -lt $meta.count; $i++) {
132-
$groups = $rx.Match($meta[$i]).groups
133-
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] $($groups[1].value) = $($groups[2].value)"
134-
$h.add($groups[1].value, $groups[2].value.trim())
135-
}
136-
#check for required properties
137-
if (-Not ($h.ContainsKey("Source")) ) {
138-
$h.add("Source", "")
139-
}
140-
if (-Not ($h.ContainsKey("version"))) {
141-
$h.add("Version", "")
142-
}
143-
#$h | Out-String | Write-Verbose
144-
#write the custom object to the pipeline
145-
$fi = New-Object -TypeName PSFunctionInfo -ArgumentList $h.name, $h.version
146-
147-
#update the object with hash table properties
148-
foreach ($key in $h.keys) {
149-
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Updating $key [$($h.$key)]"
150-
$fi.$key = $h.$key
151-
}
152-
if ($tag) {
153-
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Filtering for tag $tag"
154-
# write-verbose "$($fi.name) tag: $($fi.tags)"
155-
if ($fi.tags -match $tag) {
122+
#Define a hashtable that will eventually become a custom object
123+
$h = @{
124+
Name = $fun.name
125+
CommandType = $fun.CommandType
126+
Module = $fun.Module
127+
}
128+
#parse the metadata using regular expressions
129+
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Parsing metadata"
130+
for ($i = 0; $i -lt $meta.count; $i++) {
131+
$groups = $rx.Match($meta[$i]).groups
132+
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] $($groups[1].value) = $($groups[2].value)"
133+
$h.add($groups[1].value, $groups[2].value.trim())
134+
}
135+
#check for required properties
136+
if (-Not ($h.ContainsKey("Source")) ) {
137+
$h.add("Source", "")
138+
}
139+
if (-Not ($h.ContainsKey("version"))) {
140+
$h.add("Version", "")
141+
}
142+
#$h | Out-String | Write-Verbose
143+
#write the custom object to the pipeline
144+
$fi = New-Object -TypeName PSFunctionInfo -ArgumentList $h.name, $h.version
145+
146+
#update the object with hash table properties
147+
foreach ($key in $h.keys) {
148+
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Updating $key [$($h.$key)]"
149+
$fi.$key = $h.$key
150+
}
151+
if ($tag) {
152+
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Filtering for tag $tag"
153+
# write-verbose "$($fi.name) tag: $($fi.tags)"
154+
if ($fi.tags -match $tag) {
155+
$fi
156+
}
157+
}
158+
else {
156159
$fi
157160
}
158-
}
161+
#clear the variable so it doesn't get reused
162+
Remove-Variable m, h
163+
164+
} #if metadata found
159165
else {
160-
$fi
161-
}
162-
#clear the variable so it doesn't get reused
163-
Remove-Variable m, h
164-
165-
} #if metadata found
166-
else {
167-
#insert the custom type name and write the object to the pipeline
168-
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Creating a new and temporary PSFunctionInfo object."
169-
$fi = New-Object PSFunctionInfo -ArgumentList $fun.name, $fun.source
170-
$fi.version = $fun.version
171-
$fi.module = $fun.Module
172-
$fi.Commandtype = $fun.CommandType
173-
$fi.Description = $fun.Description
174-
175-
#Write the object depending on the parameter set and if it belongs to a module AND has a source
176-
if (-Not $tag) {
177-
$fi
166+
#insert the custom type name and write the object to the pipeline
167+
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Creating a new and temporary PSFunctionInfo object."
168+
$fi = New-Object PSFunctionInfo -ArgumentList $fun.name, $fun.source
169+
$fi.version = $fun.version
170+
$fi.module = $fun.Module
171+
$fi.Commandtype = $fun.CommandType
172+
$fi.Description = $fun.Description
173+
174+
#Write the object depending on the parameter set and if it belongs to a module AND has a source
175+
if (-Not $tag) {
176+
$fi
177+
}
178178
}
179179
}
180+
} #if Test-Path
181+
Else {
182+
Write-Warning "Can't find $Functionname as a loaded function."
180183
}
181184
} #foreach
182185

functions/Get-PSFunctionInfoDefaults.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Function Get-PSFunctionInfoDefaults {
22
[cmdletbinding()]
33
[outputtype("PSFunctionInfoDefault")]
44

5-
Param( )
5+
Param()
66
Begin {
77
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
88
$Outfile = Join-Path $home -ChildPath psfunctioninfo-defaults.json

0 commit comments

Comments
 (0)