Skip to content

Commit

Permalink
v1.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jdhitsolutions committed Mar 10, 2021
1 parent 554ccad commit b093ac6
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 42 deletions.
8 changes: 7 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# Change Log for PSTypeExtensionTools

## v1.8.0

+ Fixed bugs in `Get-PSTypeExtension` and `Export-PSTypeExtension` where I needed to let the user force recognizing a type name. ([Issue #20](https://github.com/jdhitsolutions/PSTypeExtensionTools/issues/20))
+ Help updates.
+ Updated `README.md`.

## v1.7.1

+ Fixed bug in `New-PSPropertySet` that failed to correct typename case. ([Issue #19](https://github.com/jdhitsolutions/PSTypeExtensionTools/issues/19))
+ Fixed bug in `New-PSPropertySet` that failed to correct typename case. ([Issue #19](https://github.com/jdhitsolutions/PSTypeExtensionTools/issues/19))
+ Updated online help link for `New-PSPropertySet`.
+ Added sample file `process.types.ps1xml`.
+ Updated `README.md`.
Expand Down
Binary file modified PSTypeExtensionTools.psd1
Binary file not shown.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Install-Module PSTypeExtensionTools
Let's say you want to update a number object, but you have no idea what the type name is. Once you have read help for the commands in this module, you could run a PowerShell command like this:

```powershell
123 | Get-PSType | Add-PSTypeExtension -MemberType ScriptProperty -MemberName SquareRoot -Value { [math]::Sqrt($this)}
123 | Get-PSType | Add-PSTypeExtension -MemberType ScriptProperty -MemberName SquareRoot -Value {[math]::Sqrt($this)}
```

Use `$this` to reference the object instead of `$_`. Now you can get the new property.
Expand Down Expand Up @@ -55,6 +55,26 @@ PS C:\> $x.GetPercent(110,4)
34.5455
```

### Go GUI

As an alternative to the command-line, you can use the native `Show-Command` cmdlet to display a graphical interface.

```powershell
Show-Command Add-PSTypeExtension
```

![Add via GUI](images/show-add.png)

Clicking Run will insert this code at your prompt.

```powershell
Add-PSTypeExtension -MemberName ToTitleCase -MemberType ScriptMethod -TypeName System.String -Value { (Get-Culture).TextInfo.ToTitleCase($this.tolower())}
```

If you like this extension, you can export it and re-import it later.

## Get Type Extensions

To see current type extensions, you can use `Get-PSTypeExtension`. You can choose to see all extensions or selected ones by member name. CodeProperty extensions are hidden by default.

```powershell
Expand Down Expand Up @@ -300,4 +320,4 @@ There is an about help topic you can read:
help about_PSTypeExtensionTools
```

Last Updated 2021-03-09 21:03:55Z
Last Updated 2021-03-10 21:49:24Z
8 changes: 8 additions & 0 deletions docs/Add-PSTypeExtension.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ PS C:\ Add-PSTypeExtension -TypeName system.io.fileinfo -MemberType AliasPropert

Create an alias called Size using the Length property on file objects. This expression will also create it for the deserialized version of the type so that you can use it with remoting results.

### EXAMPLE 4

```powershell
PS C:\> Show-Command Add-PSTypeExtension
```

Here is an easy way to get a GUI to create a new type extension.

## PARAMETERS

### -MemberName
Expand Down
10 changes: 4 additions & 6 deletions docs/Export-PSTypeExtension.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ Export type extensions to a file.
### Object (Default)

```yaml
Export-PSTypeExtension -Path <String> [-InputObject <Object>] [-Passthru] [Append] [-WhatIf] [-Confirm] [<CommonParameters>]
Export-PSTypeExtension -Path <String> [-InputObject <Object>] [-Force] [-Passthru] [-WhatIf] [-Confirm] [<CommonParameters>]
```

### Name

```yaml
Export-PSTypeExtension [-TypeName] <String> -MemberName <String[]>
-Path <String> [-Passthru] [Append] [-WhatIf] [-Confirm] [<CommonParameters>]
Export-PSTypeExtension [-TypeName] <String> -MemberName <String[]> -Path <String> [-Force] [-Passthru] [-WhatIf] [-Confirm] [<CommonParameters>]
```

## DESCRIPTION
Expand Down Expand Up @@ -191,9 +190,9 @@ Accept pipeline input: False
Accept wildcard characters: False
```
### -Append
### -Force
If you are exporting to a .ps1xml file, you can append to it. You might want to do this if you have created a property set using New-PSPropertySet and want to export your type extensions to the same file. Or you might want to export all of your type extensions to a single .ps1xml file so that anyone can import it using Update-TypeData. See examples.
Force the command to accept the name as a type.
```yaml
Type: SwitchParameter
Expand All @@ -208,7 +207,6 @@ Accept wildcard characters: False
```
### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
## INPUTS
Expand Down
20 changes: 18 additions & 2 deletions docs/Get-PSTypeExtension.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ Get selected type extensions.
## SYNTAX

```yaml
Get-PSTypeExtension [-TypeName] <String> [-Members <String[]>] [-CodeProperty] [<CommonParameters>]
Get-PSTypeExtension [-TypeName] <String> [-Members <String[]>] [-CodeProperty] [-Force] [<CommonParameters>]
```

## DESCRIPTION

Use this command to list defined type extensions. You can either select individual ones or all of them. Do not specify any members to retrieve all of them. This command is very similar to Get-TypeData, except that it makes it easier to see the extension value.
Use this command to list defined type extensions. You can either select individual ones or all of them. Do not specify any members to retrieve all of them. This command is very similar to Get-TypeData, except that it makes it easier to see the extension value. If you are specifying a custom type name, you may need to use -Force for the command to accept it as written.

By default, CodeProperty members are not displayed because they can't be exported.

Expand Down Expand Up @@ -162,6 +162,22 @@ Accept pipeline input: False
Accept wildcard characters: False
```
### -Force
Force the command to accept the name as a type.
```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
Expand Down
44 changes: 37 additions & 7 deletions en-us/PSTypeExtensionTools-help.xml
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ True</dev:code>
<maml:para>Create an alias called Size using the Length property on file objects. This expression will also create it for the deserialized version of the type so that you can use it with remoting results.</maml:para>
</dev:remarks>
</command:example>
<command:example>
<maml:title>-------------------------- EXAMPLE 4 --------------------------</maml:title>
<dev:code>PS C:\&gt; Show-Command Add-PSTypeExtension</dev:code>
<dev:remarks>
<maml:para>Here is an easy way to get a GUI to create a new type extension.</maml:para>
</dev:remarks>
</command:example>
</command:examples>
<command:relatedLinks>
<maml:navigationLink>
Expand Down Expand Up @@ -338,9 +345,9 @@ True</dev:code>
<dev:defaultValue>False</dev:defaultValue>
</command:parameter>
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none">
<maml:name>Append</maml:name>
<maml:name>Force</maml:name>
<maml:description>
<maml:para>If you are exporting to a .ps1xml file, you can append to it. You might want to do this if you have created a property set using New-PSPropertySet and want to export your type extensions to the same file. Or you might want to export all of your type extensions to a single .ps1xml file so that anyone can import it using Update-TypeData. See examples.</maml:para>
<maml:para>Force the command to accept the name as a type.</maml:para>
</maml:description>
<dev:type>
<maml:name>SwitchParameter</maml:name>
Expand Down Expand Up @@ -409,9 +416,9 @@ True</dev:code>
<dev:defaultValue>False</dev:defaultValue>
</command:parameter>
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none">
<maml:name>Append</maml:name>
<maml:name>Force</maml:name>
<maml:description>
<maml:para>If you are exporting to a .ps1xml file, you can append to it. You might want to do this if you have created a property set using New-PSPropertySet and want to export your type extensions to the same file. Or you might want to export all of your type extensions to a single .ps1xml file so that anyone can import it using Update-TypeData. See examples.</maml:para>
<maml:para>Force the command to accept the name as a type.</maml:para>
</maml:description>
<dev:type>
<maml:name>SwitchParameter</maml:name>
Expand Down Expand Up @@ -507,9 +514,9 @@ True</dev:code>
<dev:defaultValue>False</dev:defaultValue>
</command:parameter>
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none">
<maml:name>Append</maml:name>
<maml:name>Force</maml:name>
<maml:description>
<maml:para>If you are exporting to a .ps1xml file, you can append to it. You might want to do this if you have created a property set using New-PSPropertySet and want to export your type extensions to the same file. Or you might want to export all of your type extensions to a single .ps1xml file so that anyone can import it using Update-TypeData. See examples.</maml:para>
<maml:para>Force the command to accept the name as a type.</maml:para>
</maml:description>
<command:parameterValue required="false" variableLength="false">SwitchParameter</command:parameterValue>
<dev:type>
Expand Down Expand Up @@ -748,7 +755,7 @@ Win10Ent 10/16/2020 9:30:46 AM 25.00:49:58.1206825</dev:code>
</maml:description>
</command:details>
<maml:description>
<maml:para>Use this command to list defined type extensions. You can either select individual ones or all of them. Do not specify any members to retrieve all of them. This command is very similar to Get-TypeData, except that it makes it easier to see the extension value.</maml:para>
<maml:para>Use this command to list defined type extensions. You can either select individual ones or all of them. Do not specify any members to retrieve all of them. This command is very similar to Get-TypeData, except that it makes it easier to see the extension value. If you are specifying a custom type name, you may need to use -Force for the command to accept it as written.</maml:para>
<maml:para>By default, CodeProperty members are not displayed because they can't be exported.</maml:para>
</maml:description>
<command:syntax>
Expand Down Expand Up @@ -789,6 +796,17 @@ Win10Ent 10/16/2020 9:30:46 AM 25.00:49:58.1206825</dev:code>
</dev:type>
<dev:defaultValue>False</dev:defaultValue>
</command:parameter>
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none">
<maml:name>Force</maml:name>
<maml:description>
<maml:para>Force the command to accept the name as a type.</maml:para>
</maml:description>
<dev:type>
<maml:name>SwitchParameter</maml:name>
<maml:uri />
</dev:type>
<dev:defaultValue>False</dev:defaultValue>
</command:parameter>
</command:syntaxItem>
</command:syntax>
<command:parameters>
Expand Down Expand Up @@ -828,6 +846,18 @@ Win10Ent 10/16/2020 9:30:46 AM 25.00:49:58.1206825</dev:code>
</dev:type>
<dev:defaultValue>False</dev:defaultValue>
</command:parameter>
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none">
<maml:name>Force</maml:name>
<maml:description>
<maml:para>Force the command to accept the name as a type.</maml:para>
</maml:description>
<command:parameterValue required="false" variableLength="false">SwitchParameter</command:parameterValue>
<dev:type>
<maml:name>SwitchParameter</maml:name>
<maml:uri />
</dev:type>
<dev:defaultValue>False</dev:defaultValue>
</command:parameter>
</command:parameters>
<command:inputTypes>
<command:inputType>
Expand Down
3 changes: 1 addition & 2 deletions functions/private.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ Function _convertTypeName {
($Typename -as [type]).fullname
}
Catch {
$Typename
$typename
}

}
63 changes: 41 additions & 22 deletions functions/public.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,48 @@ Function Get-PSTypeExtension {
ValueFromPipeline
)]
[ValidateNotNullorEmpty()]
[ValidateScript( {
#check if typename can be found with Get-TypeData
if ((Get-TypeData).typename -contains "$_") {
$True
}
elseif ($_ -as [type]) {
#test if string resolves as a typename
$True
}
else {
Throw "$_ does not appear to be a valid type."
}
})]
[string]$TypeName,
[Parameter(
HelpMessage = "Enter a comma separated list of member names",
ParameterSetName = "members"
)]
[string[]]$Members,
[Parameter(HelpMessage = "Show CodeProperty custom properties")]
[switch]$CodeProperty
[switch]$CodeProperty,
[parameter(HelpMessage="Force the command to accept the name as a type.")]
[switch]$Force
)

Begin {
Write-Verbose "Starting: $($MyInvocation.Mycommand)"
$typedata = @()
$TypeName=_convertTypeName $TypeName

} #begin
Process {
if ($force) {
Write-Verbose "Using typename AS-IS."
}
else {
$TypeName=_convertTypeName $TypeName
}

Write-Verbose "Analyzing $typename"
$typedata += Get-TypeData -TypeName $typename
if ($TypeName) {
Write-Verbose "Getting type data"
$typedata += Get-TypeData -TypeName $typename
}
else {
Write-Warning "Failed to get a typename"
#bail out
$typedata = $False
return
}

} #process
End {
$typedata = $typedata | Select-Object -Unique

if ($typedata) {
$typedata = $typedata | Select-Object -Unique
$out = [System.Collections.Generic.List[object]]::new()
if (-Not $Members) {
Write-Verbose "Getting all member names"
Expand Down Expand Up @@ -214,6 +219,9 @@ Function Export-PSTypeExtension {
[Parameter(ParameterSetName = "Object", ValueFromPipeline)]
[object]$InputObject,

[parameter(HelpMessage="Force the command to accept the name as a type.")]
[switch]$Force,

[switch]$Passthru
)
DynamicParam {
Expand Down Expand Up @@ -267,17 +275,28 @@ Function Export-PSTypeExtension {

if ($TypeName) {
#convert typename to a properly cased name
Write-Verbose "Converting $typename to properly cased type name."
$TypeName=_convertTypeName $TypeName
}
if ($force) {
Write-Verbose "Using typename $typename AS-IS."
}
else {
Write-Verbose "Converting $typename to properly cased type name."
$TypeName=_convertTypeName $TypeName
}
}
}
Process {
#test if parent path exists
If ($validPath) {
if ($Inputobject) {
Write-Verbose "Processing input type: $($InputObject.TypeName)"
#convert the type name just in case
$typeName = _convertTypeName $InputObject.TypeName
if ($force) {
Write-Verbose "Using typename AS-IS."
$typename = $InputObject.typename
}
else {
Write-Verbose "Converting $typename to properly cased type name."
$TypeName=_convertTypeName $TypeName
}
$data.Add($InputObject)
}
else {
Expand Down
Binary file added images/show-add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b093ac6

Please sign in to comment.