From 329a01c89e052fdb1e31e427d905b88c2b04d23f Mon Sep 17 00:00:00 2001 From: Florian Carrier Date: Mon, 20 Jan 2020 12:06:09 +0000 Subject: [PATCH] Add ConvertTo-JavaProperty Add function to format a property for Java --- PSTK.psd1 | 1 + Public/ConvertTo-JavaProperty.ps1 | 70 +++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 Public/ConvertTo-JavaProperty.ps1 diff --git a/PSTK.psd1 b/PSTK.psd1 index 1d2dd22..8e328e0 100644 --- a/PSTK.psd1 +++ b/PSTK.psd1 @@ -76,6 +76,7 @@ FunctionsToExport = @( "Complete-RelativePath", "Confirm-Prompt", "Convert-FileEncoding", + "ConvertTo-JavaProperty", "ConvertTo-NaturalSort", "ConvertTo-PDF", "Copy-OrderedHashtable", diff --git a/Public/ConvertTo-JavaProperty.ps1 b/Public/ConvertTo-JavaProperty.ps1 new file mode 100644 index 0000000..ddb5215 --- /dev/null +++ b/Public/ConvertTo-JavaProperty.ps1 @@ -0,0 +1,70 @@ +function ConvertTo-JavaProperty { + <# + .SYNOPSIS + Convert to Java property + + .DESCRIPTION + Format property key-value pair as Java property + + .PARAMETER Property + The property parameter corresponds to the property key-value pair to format. + + .PARAMETER Prefix + The optional prefix parameter corresponds to the prefix of the expected Java property. The default value is "D". + + .INPUTS + System.Collections.Specialized.OrderedDictionary. You can pipe the property key-value pair to ConvertTo-JavaProperty. + + .OUTPUTS + System.String. ConvertTo-JavaProperty returns the formatted Java property. + + .NOTES + File name: ConvertTo-JavaProperty.ps1 + Author: Florian Carrier + Creation date: 15/10/2019 + Last modified: 16/01/2020 + #> + [CmdletBinding ()] + Param( + [Parameter ( + Position = 1, + Mandatory = $true, + HelpMessage = "Property key-value pair", + ValueFromPipeline = $true + )] + [ValidateNotNullOrEmpty()] + [Alias ("Properties")] + [System.Collections.Specialized.OrderedDictionary] + $Property, + [Parameter ( + Position = 2, + Mandatory = $false, + HelpMessage = "Java property prefix" + )] + [String] + $Prefix = "D" + ) + Begin { + # Get global preference variables + Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState + } + Process { + if ($Property.Count -gt 0) { + $JavaString = New-Object -TypeName "System.String" -ArgumentList "" + # Loop through properties (if multiple have been provided) + foreach ($SingleProperty in $Property.GetEnumerator()) { + # Add space between consecutive properties + if ($JavaString) { + $JavaString += " " + } + # Format property + $JavaString += "-" + $Prefix + '"' + $SingleProperty.Name + '"' + "=" + '"' + $SingleProperty.Value + '"' + } + # Return Java property string + return $JavaString + } else { + # If property is invalid + return $null + } + } +}