From c8b9f5d2dca54d7c22e43a4885685c261d41de72 Mon Sep 17 00:00:00 2001 From: Gal Szkolnik Date: Mon, 15 Nov 2021 14:42:46 -0500 Subject: [PATCH] Magick, choco packages + more --- ImageMagick/Convert-WithImageMagick.ps1 | 87 ++++++++++++++++++++++++ ImageMagick/Get-TiffPages.ps1 | 33 +++++++++ ImageMagick/_.package.json | Bin 0 -> 1750 bytes base/Get-HostOS.ps1 | 77 +++++++++++++++++++++ choco/_.package.json | Bin 0 -> 1672 bytes choco/profile.d/autocomplete.ps1 | 5 ++ 6 files changed, 202 insertions(+) create mode 100644 ImageMagick/Convert-WithImageMagick.ps1 create mode 100644 ImageMagick/Get-TiffPages.ps1 create mode 100644 ImageMagick/_.package.json create mode 100644 base/Get-HostOS.ps1 create mode 100644 choco/_.package.json create mode 100644 choco/profile.d/autocomplete.ps1 diff --git a/ImageMagick/Convert-WithImageMagick.ps1 b/ImageMagick/Convert-WithImageMagick.ps1 new file mode 100644 index 0000000..d0f4994 --- /dev/null +++ b/ImageMagick/Convert-WithImageMagick.ps1 @@ -0,0 +1,87 @@ +[CmdletBinding(SupportsShouldProcess,ConfirmImpact="Medium")]param( + [Parameter(ParameterSetName="Normal",ValueFromPipeline,ValueFromPipelineByPropertyName)] + [string[]]$ImageMagickPath, + [Parameter(ParameterSetName="Normal",ValueFromPipelineByPropertyName)] + [string]$PageOutputName, + [Parameter(ParameterSetName="Normal")] + [string]$OutputPath, + [Parameter(ParameterSetName="Normal")] + [string]$OutputImageFormat = "png", + [Parameter(ParameterSetName="Normal")] + [string]$OutputImageExtension, + [Parameter(ParameterSetName="Normal")] + [string[]]$MagickArgs, + [Parameter(ParameterSetName="Normal")] + [switch]$Force, + [Parameter(ParameterSetName="Reset")] + [switch]$ResetConfirm +) + +BEGIN{ + if( $ResetConfirm ) { + Remove-Variable imagemagick_* -Force + Remove-Variable imagemagick_* -Force + return + } + + if( -not $OutputImageExtension ) { + $OutputImageExtension = $OutputImageFormat.ToLower() + } + $OutputImageExtension = $OutputImageExtension -replace '\.*(.+)$','.$1' + + if( ! (Test-Path variable:global:imagemagick_noToAll) ) { + $imagemagick_noToAll = $PSBoundParameters.ContainsKey('WhatIf'); + $imagemagick_yesToAll = $ConfirmPreference -gt 'Low' -and -not $imagemagick_noToAll + } +} +PROCESS{ + if( $ResetConfirm ) { return } + $ImageMagickPath | ForEach-Object { + $local:i = [PSCustomObject]@{ + ImageMagickPath = $_ + PageOutputName = $( + if( $PageOutputName ) { + "$($PageOutputName)$OutputImageExtension" + } else { + [System.IO.Path]::GetFileName($_) -replace "(\.[A-Z]+)(?:\[(\d+)\])?$",'_$2.' -replace '_?\.$',$OutputImageExtension + } + ) + OutputPath = $OutputPath + } + + if( -not $i.OutputPath ) { + $i.OutputPath = Join-Path ([System.IO.Path]::GetDirectoryName($i.ImageMagickPath)) $OutputImageFormat + } + + if( !$imagemagick_noToAll ) { try { + if( -not (Test-Path -LiteralPath $i.OutputPath) ) { + if( $Force ) { + New-Item -ItemType Directory -Path $i.OutputPath -Force + } else { + throw "Output directory $($i.OutputPath) does not exist!" + } + } + } catch { + Write-Error $_ + $imagemagick_noToAll = $true + return + }} + + $local:outFilePath = Join-Path $i.OutputPath $i.PageOutputName + $local:magickCmdLine=@("convert",$i.ImageMagickPath)+$MagickArgs+@($outFilePath) | Where-Object {$_} + Write-Verbose "$(if( $imagemagick_noToAll ) { "Skipping" } else {"Running"} ): & magick $magickCmdLine" + if( $PSCmdlet.ShouldContinue( "Convert", "$($i.ImageMagickPath) to $outFilePath", [ref]$imagemagick_yesToAll, [ref]$imagemagick_noToAll ) ) { + & magick $magickCmdLine + + Write-Verbose "Error state: $?" + if( $? -and (Test-Path $outFilePath) ) { + Get-Item $outFilePath + } + } + } +} +END{ + if( $ResetConfirm ) { return } + $global:imagemagick_yesToAll = $imagemagick_yesToAll + $global:imagemagick_noToAll = $imagemagick_noToAll +} diff --git a/ImageMagick/Get-TiffPages.ps1 b/ImageMagick/Get-TiffPages.ps1 new file mode 100644 index 0000000..68ea7e3 --- /dev/null +++ b/ImageMagick/Get-TiffPages.ps1 @@ -0,0 +1,33 @@ +[CmdletBinding()]param( + [string[]]$SrcPath, + [string]$FilePattern="*.tif", + [int]$PageNumberDigits=3, + [switch]$Force +) + +Remove-TypeData -TypeName "ImageMagickPagesz" -ErrorAction Ignore -Confirm:$false +Update-TypeData -TypeName "ImageMagickPagesz" -DefaultDisplayPropertySet 'ImageMagickPath', 'Page' -Force -Confirm:$false + +$local:PageNumberFormat="D$PageNumberDigits" +Get-ChildItem $(Join-Path $SrcPath $FilePattern) -Force:$Force | + ForEach-Object { + $local:f = $_; + & magick identify $f.FullName | ForEach-Object { + $local:p = [ordered]@{ + PSTypeName = "ImageMagickPagesz" + PSFile = $f + BaseName = $f.BaseName + ImageMagickPath = $_ -ireplace " TIFF \d+x\d+ \d+x\d+\+\d+\+\d .*$","" + Page = 0 + PageOutputName = [string]::Empty + } + if( $p.ImageMagickPath -match '.*\[(\d+)\]$' ) { + $p.Page = $matches[1] + } + + $p.Page = $(0+$($p.Page)).ToString($PageNumberFormat) + $p.PageOutputName = "$($p.BaseName)_$($p.Page)" + + [PSCustomObject]$p + } + } \ No newline at end of file diff --git a/ImageMagick/_.package.json b/ImageMagick/_.package.json new file mode 100644 index 0000000000000000000000000000000000000000..24258c11e6d34b571c83672d8731ef3745c62b35 GIT binary patch literal 1750 zcmc&!O$&lR5Pj#M{}6NT(79VkP*7c>Q+NnnBr20ED5Ae!Jy$nOR1!>C+;P9&n>RZ< zujd_IbkIwhAixF=cI-VU2!?v)dzaZ=WCaVJKcvnJrX0tZb8WYpMbQ7^FG4i)0t-j@ zaIwXiU-WN$TvtLeh-J2#>N2G4no#`(AvGKs7o))E+@7skOtRQzW5E(P&8r&bolt3{ z*Uw}|6Pj0KRyei7Z86R+?^IFy7+29U5S+8%KggbaoxO<&3L=7f5)Yz5MQz0w74g^ANo%AHS}jy1Bx$qRot?=h z*UJf6WRSZz!5SlsvEVp`f}rACqPxuQSF@l^|5eCe7;^@fqsy522bI?MvitbX)pX zg(I4@YL5|?Y)SRB^NRZybcv_2R48YMJT!8$bBx2WZGr|>(zuRQNx#7E6Io&N7wj5! zo={sw?sa&qcdf$qQ7^yXU*#*X_R-bT9MZpa_W8g655Fiys^zgu>aW53Jj{;aS8qd$ Zw_(a!7(eQuoI2NYY7vJYMGEkO@dm+zl5YS2 literal 0 HcmV?d00001 diff --git a/choco/profile.d/autocomplete.ps1 b/choco/profile.d/autocomplete.ps1 new file mode 100644 index 0000000..995a0cd --- /dev/null +++ b/choco/profile.d/autocomplete.ps1 @@ -0,0 +1,5 @@ +# Chocolatey profile +$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1" +if (Test-Path($ChocolateyProfile)) { + Import-Module "$ChocolateyProfile" +}