dfinke / PowerShellHumanizer

PowerShell Humanizer wraps Humanizer: meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities
Apache License 2.0
120 stars 18 forks source link

Add support for PowerShell 5.1, extend TimeSpan type #19

Open kborowinski opened 2 months ago

kborowinski commented 2 months ago

@cdhunt @dfinke This PR amends update2024 branch with the following:

  1. Updates Humanizer package to latest beta.

  2. Adds support for PowerShell 5.1 by adding specific versions of System.Buffer and System.Runtime.CompilerServices.Unsafe assemblies (other option would be to register them in GAC manually)

  3. Tries to locate Hummanizer.resources.dll by two letter ISO language name, as last resort, when folder specified in $PSCulture does not exist. For example, Polish culture can be reported either as pl-PL or pl, and resource file for Polish culture is stored in pl folder. This fails to load on Windows 11 where Polish culture is reported as pl-PL. I'm using CurrentUICulture rather than CurrentCulture to be in line with current GUI language and not locale.

  4. Extends TimeSpan.types so user can specify maximum TimeUnit, CultureInfo, separator and numeric to word conversion. This is handy, when one want's to use different culuture that current:

    $PSCulture
    pl-PL
    6 años, 8 meses, 5 días
  5. Fixes ConvertTo-Quantity so this example from TryPowerShellHumanizer.ps1 works as expected:

    "ConvertTo-Quantity: {0}"       -f (1111 | ConvertTo-Quantity test -showQuantityAs Words)

EDIT:

I don't know if that changed in latest beta but Humanizer.dll automatically loads proper resource file. You can test is by commenting out this block of code in PowerShellHumanizer.psm1:

# Load  dlls
if ($PSVersionTable.PSVersion.Major -lt 6) {
    Add-Type -Path "$PSScriptRoot/lib/System.Buffers.4.0.2.0.dll"
    Add-Type -Path "$PSScriptRoot/lib/System.Runtime.CompilerServices.Unsafe.4.0.4.1.dll"
}
Add-Type -Path "$PSScriptRoot/lib/Humanizer.dll"

<#
if ([CultureInfo]::CurrentUICulture.Name -ne 'en-US') {
    $resourcePath = foreach (
        $culture in
            [CultureInfo]::CurrentUICulture,
            [CultureInfo]::CurrentUICulture.TwoLetterISOLanguageName
    ) {
        $pathCandidate = "$PSScriptRoot/lib/$culture/Humanizer.resources.dll"
        if (Test-Path $pathCandidate) {
            $pathCandidate
            break
        }
    }

    if ($resourcePath) {
        Add-Type -Path $resourcePath
    } else {
        Write-Warning "Humanizer doesn't currently support '$PSCulture'."
    }
}
#>

if (Get-Module -Name Terminal-Icons -ListAvailable -ErrorAction SilentlyContinue) {
    Update-FormatData -PrependPath "$PSSCriptRoot/formats/FileInfoIcons.format.ps1xml"
} else {
    Update-FormatData -PrependPath "$PSSCriptRoot/formats/FileInfo.format.ps1xml"
}

Update-FormatData -PrependPath "$PSSCriptRoot/formats/TimeSpan.format.ps1xml"

image

dfinke commented 2 months ago

@cdhunt Thoughts?

@kborowinski Thanks for the PR. I'd prefer smaller PRs, easier to test :)

kborowinski commented 2 months ago

@dfinke I can close that PR and prepare bunch of smaller ones, if you like.

dfinke commented 2 months ago

Thanks, let's hold off till @cdhunt can respond. He was planning a bunch of things.

cdhunt commented 2 months ago

I'm pretty swamped this week, but I kicked off the tests and it did hit one failure for the 5.1 run.

https://github.com/dfinke/PowerShellHumanizer/actions/runs/9827401928/job/27170421774#step:4:67

kborowinski commented 2 months ago

The test fails because on PS 5.1 range operator does not support characters :

BeforeAll {
    $listAlphaLower = 'a'..'z'
    $listAlphaUpper = 'A'..'Z'

I'll fix tests to work on PS 5.1

kborowinski commented 2 months ago

@dfinke @cdhunt: FYI, the latest Humanizer pre-releases v3.0.0-beta.13 and v3.0.0-beta.54 do not work on PowerShell 7.5.0-preview.3 and PowerShell daily builds. I had to revert to v2.14.1 stable on my private fork of PowerShellHumanizer to get it to work on PS 5.1, 7.4.3 and 7.5.0