From d86985677d4c3494119c029f30bd30c6a77b409c Mon Sep 17 00:00:00 2001 From: Dmitry Stadub Date: Mon, 9 Sep 2019 10:15:51 +0300 Subject: [PATCH 1/2] Fix 14 --- 7Zip/7zip-Archive.psd1 | 2 +- 7Zip/7zip-Archive.psm1 | 2 +- 7Zip/Aliases.ps1 | 1 + 7Zip/Functions.ps1 | 2 +- 7Zip/README.md | 1 + 7Zip/Shared-Functions.ps1 | 22 ++++++++++++++++++---- 6 files changed, 23 insertions(+), 7 deletions(-) diff --git a/7Zip/7zip-Archive.psd1 b/7Zip/7zip-Archive.psd1 index 398a2a5..0c9c539 100644 --- a/7Zip/7zip-Archive.psd1 +++ b/7Zip/7zip-Archive.psd1 @@ -11,7 +11,7 @@ # RootModule = '' # Version number of this module. - ModuleVersion = '1.0.2' + ModuleVersion = '1.0.4' # Supported PSEditions # CompatiblePSEditions = @() diff --git a/7Zip/7zip-Archive.psm1 b/7Zip/7zip-Archive.psm1 index f53a590..1cc4f6e 100644 --- a/7Zip/7zip-Archive.psm1 +++ b/7Zip/7zip-Archive.psm1 @@ -2,7 +2,7 @@ . $PSScriptRoot\Functions.ps1 -CheckPsGalleryUpdate 7Zip "1.0.2" +CheckPsGalleryUpdate 7Zip "1.0.4" _Initalize diff --git a/7Zip/Aliases.ps1 b/7Zip/Aliases.ps1 index 5bd8139..255a66e 100644 --- a/7Zip/Aliases.ps1 +++ b/7Zip/Aliases.ps1 @@ -3,4 +3,5 @@ Set-Alias sza Add-ZipFileContent -Scope Global Set-Alias szrm Remove-ZipFileContent -Scope Global Set-Alias szt Test-ZipFileContent -Scope Global Set-Alias br Get-ZipFileContent -Scope Global +Set-Alias szc New-ZipFile -Scope Global diff --git a/7Zip/Functions.ps1 b/7Zip/Functions.ps1 index 30efa4a..9f2574b 100644 --- a/7Zip/Functions.ps1 +++ b/7Zip/Functions.ps1 @@ -22,7 +22,7 @@ function Test-7ZipInstall { Write-Debug "Trying to locate 7-zip by the path: $script:_7zWin" if( -not (Test-Path "$script:_7zWin")){ - Write-Output '7Zip didn''t found.' + Write-Output '7Zip didn''t downloaded.' Write-Output 'Now script going to try to download it.' $file = Get-TempFileName diff --git a/7Zip/README.md b/7Zip/README.md index 32f9955..f71cb82 100644 --- a/7Zip/README.md +++ b/7Zip/README.md @@ -82,6 +82,7 @@ Test-ZipFileContent - Perform zip file check | Cmdlet | Alias | | ---------------------|:------:| +| New-ZipFile | szc | | Read-ZipFile | szr | | Add-ZipFileContent | sza | | Remove-ZipFileContent| szrm | diff --git a/7Zip/Shared-Functions.ps1 b/7Zip/Shared-Functions.ps1 index b4f4c14..7eab6f5 100644 --- a/7Zip/Shared-Functions.ps1 +++ b/7Zip/Shared-Functions.ps1 @@ -408,20 +408,34 @@ function CheckPsGalleryUpdate { Try { - Write-Output "Update check..." + Write-Console "Update check..." $feed = Invoke-WebRequest -Uri "https://www.powershellgallery.com/api/v2/FindPackagesById()?id=%27$moduleName%27" $last=([xml]$feed.Content).feed.entry |Sort-Object -Property updated | Last $version= $last.properties.Version if ($version -gt $currentVersion) { - Write-Output "Found a new module version {$version}." + Write-Console "Found a new module version {$version}." $notes=$last.properties.ReleaseNotes.'#text' - Write-Output "Release notes: {$notes}." - Write-Output "Recomendent to update module with command: Update-Module -Name $moduleName -Force" + Write-Console "Release notes: {$notes}." + Write-Console "Recomendent to update module with command: Update-Module -Name $moduleName -Force" } } Catch { } +} + +function Write-Console { + param ( + [string]$text, + [String[]]$arg=$null + ) + if($null -eq $arg){ + [Console]::WriteLine($text) + } + else{ + [Console]::WriteLine($text, $arg) + } + } \ No newline at end of file From 855d4f00db0a4a803189c840fc8efd453a91d674 Mon Sep 17 00:00:00 2001 From: Dmitry Stadub Date: Mon, 9 Sep 2019 11:10:58 +0300 Subject: [PATCH 2/2] Add smoke tests, fix issues #14 #15 --- 7Zip/7zip-Archive.SmokeTests.ps1 | 9 ++ 7Zip/7zip-Archive.psd1 | 2 +- 7Zip/7zip-Archive.psm1 | 2 +- 7Zip/Functions.ps1 | 23 ++- 7Zip/README.md | 7 + 7Zip/Shared-Functions.ps1 | 249 +++---------------------------- 6 files changed, 51 insertions(+), 241 deletions(-) create mode 100644 7Zip/7zip-Archive.SmokeTests.ps1 diff --git a/7Zip/7zip-Archive.SmokeTests.ps1 b/7Zip/7zip-Archive.SmokeTests.ps1 new file mode 100644 index 0000000..c8d0741 --- /dev/null +++ b/7Zip/7zip-Archive.SmokeTests.ps1 @@ -0,0 +1,9 @@ +Import-Module .\7zip-Archive.psm1 + + +ls -Exclude .git | New-ZipFile "scripts" +Test-ZipFileContent .\scripts.7z +Get-ZipFileContent -Archive .\scripts.7z +Remove-ZipFileContent -Archive .\scripts.7z -File "temp.log" +Read-ZipFile -Archive .\scripts.7z +Add-ZipFileContent -Path .\scripts.7z -FileName .\7zip-Archive.psd1 \ No newline at end of file diff --git a/7Zip/7zip-Archive.psd1 b/7Zip/7zip-Archive.psd1 index 0c9c539..fa3d4ca 100644 --- a/7Zip/7zip-Archive.psd1 +++ b/7Zip/7zip-Archive.psd1 @@ -11,7 +11,7 @@ # RootModule = '' # Version number of this module. - ModuleVersion = '1.0.4' + ModuleVersion = '1.1.0' # Supported PSEditions # CompatiblePSEditions = @() diff --git a/7Zip/7zip-Archive.psm1 b/7Zip/7zip-Archive.psm1 index 1cc4f6e..d27f864 100644 --- a/7Zip/7zip-Archive.psm1 +++ b/7Zip/7zip-Archive.psm1 @@ -2,7 +2,7 @@ . $PSScriptRoot\Functions.ps1 -CheckPsGalleryUpdate 7Zip "1.0.4" +szCheckPsGalleryUpdate 7Zip "1.1.0" _Initalize diff --git a/7Zip/Functions.ps1 b/7Zip/Functions.ps1 index 9f2574b..3f2dfb5 100644 --- a/7Zip/Functions.ps1 +++ b/7Zip/Functions.ps1 @@ -2,7 +2,7 @@ function _Initalize() { -$script:_binDir = Get-ProfileDir "7zip" "bin" +$script:_binDir = szGet-ProfileDir "7zip" "bin" Write-Debug "The 7-zip is dir set to $_binDir" $script:_7zWin = Join-Path $_binDir "7za.exe" $script:_7zWinDownloadDir='https://www.7-zip.org/a/7za920.zip' @@ -25,16 +25,16 @@ function Test-7ZipInstall { Write-Output '7Zip didn''t downloaded.' Write-Output 'Now script going to try to download it.' - $file = Get-TempFileName + $file = szGet-TempFileName Write-Debug "Temp file: ${file}" - Receive-File "7-Zip" $file $_7zWinDownloadDir + szReceive-File "7-Zip" $file $_7zWinDownloadDir Write-Debug "Extracting to directory ${_binDir}" if ( -not (Test-Path $_binDir )) { New-Item -Path $_binDir -ItemType 'Directory' } - Extract-ZipFile -FileName "$file" -Path "$_binDir" + szExtract-ZipFile -FileName "$file" -Path "$_binDir" } $script:_7zPath = $script:_7zWin @@ -56,7 +56,7 @@ function AddKeyArg { $AllArgs = $null, [string]$Key = $null ) - if( -not (Test-Empty $Key)){ + if( -not (szTest-Empty $Key)){ $AllArgs += "-p$Key" } return $AllArgs @@ -206,7 +206,6 @@ function Remove-ZipFileContent { [Alias("Path","Name", "Archive", "p")] [string]$ArchiveName, - [ValidateScript( { Test-Path $_ -pathType leaf })] [Parameter(Position = 1, Mandatory = $true)] [Alias("File", "f")] [string]$FileName, @@ -364,7 +363,7 @@ function New-ZipFile { process { $FileNames| ForEach-Object{ - if( -not (Test-Empty $_) ){ + if( -not (szTest-Empty $_) ){ Write-Debug "Adding file ""$_""" $files += $_ } @@ -432,7 +431,7 @@ function New-ZipFile { } - if( -not (Test-Empty $([string]$SplitSize) )){ + if( -not (szTest-Empty $([string]$SplitSize) )){ $factorName="b" switch ( $SplitSizeFactor ) @@ -518,7 +517,7 @@ function Test-ZipFileContent { $AllArgs = @('t', "$ArchiveName") - if( -not (Test-Empty $FileName) ){ + if( -not (szTest-Empty $FileName) ){ $AllArgs += "$FileName" } @@ -586,12 +585,12 @@ function Get-ZipFileContent { $AllArgs = @('e', "$ArchiveName") - if( -not (Test-Empty $FileName) ){ + if( -not (szTest-Empty $FileName) ){ $AllArgs += "$FileName" } - if( -not (Test-Empty $destFolder) ){ - CreateFolderIfNotExist "${destFolder}" + if( -not (szTest-Empty $destFolder) ){ + szCreateFolderIfNotExist "${destFolder}" $AllArgs += "-o$destFolder" } diff --git a/7Zip/README.md b/7Zip/README.md index f71cb82..b9cc8fc 100644 --- a/7Zip/README.md +++ b/7Zip/README.md @@ -180,6 +180,13 @@ Test-ZipFileContent - Perform zip file check ## Changelog + +### [v1.1.0](https://github.com/stadub/PowershellScripts/releases/tag/v0.9.5) Spet 9, 2019 + +* Fix Remove-ZipFileContent error #15 +* Fix szc Allias #14 +* Add smoke tests + ### [v1.0.0](https://github.com/stadub/PowershellScripts/releases/tag/v0.4.0) *First public release. diff --git a/7Zip/Shared-Functions.ps1 b/7Zip/Shared-Functions.ps1 index 7eab6f5..2a93fe5 100644 --- a/7Zip/Shared-Functions.ps1 +++ b/7Zip/Shared-Functions.ps1 @@ -1,24 +1,5 @@ -#Write-Debug "Loading ${script:MyInvocation.MyCommand.Name}" - -<# -.SYNOPSIS -Simple wrapper ower `Read-Host` intendend to show "[Yes]/[No]" questions - -.DESCRIPTION -Simple wrapper ower `Read-Host` intendend to show "[Yes]/[No]" questions - -.PARAMETER text -Text to be shown in the prompt - -.EXAMPLE - $reply = Show-ConfirmPrompt "Would you like to continue?" - if ( -not $reply ) { - return - } -#> - -function Show-ConfirmPrompt { +function szShow-ConfirmPrompt { param ( [Parameter(Position = 0, ParameterSetName = 'Positional', ValueFromPipeline = $True)] [Alias("Question", "Description")] @@ -34,57 +15,6 @@ function Show-ConfirmPrompt { return $false } -<# -.SYNOPSIS -Read apiKey(or other information) from input and store it to file -.DESCRIPTION -Long description - -.PARAMETER apiKey -Reference to the api key value received from the command prompt - -.PARAMETER fileName -Filename to save key - -.PARAMETER regUrl -Help url with description or registration - -.EXAMPLE - Read-ApiKey $ghApiKeyRef gh "https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line" - - $keyRef=([ref]$apiKey) - Read-ApiKey $keyRef nuget "https://www.powershellgallery.com/account/apikeys" - Publish-Module -Name ./*.psd1 -NuGetApiKey $keyRef.Value - -#> - -function Read-ApiKey { - param ( - [ref]$apiKey = $null, - [string]$fileName, - [string]$regUrl - ) - if(!!(("${apiKey.Value}").Trim())) - { - return $true - } - #get api key - $apiKeyFile = Join-Path (split-path -parent $profile) ".${fileName}_api_key" - - if ( !(Test-Path $apiKeyFile)) { - $apiKey = Read-Host -Prompt "Please Enter the api key received at $regUrl` - And paste it here: " - - $reply = Show-ConfirmPrompt "Would you like to store it?" - if ( $reply ) { - $apiKey.Value | Out-File -FilePath $apiKeyFile - } - } - else { - Get-Content -Path $apiKeyFile | ForEach-Object { $apiKey.Value = $_ } - } - return $true -} <# .SYNOPSIS @@ -99,7 +29,7 @@ Original implementation at https://devblogs.microsoft.com/powershell/converting- #> -function ToArray +function szToArray { begin { @@ -116,116 +46,7 @@ function ToArray } -<# -.SYNOPSIS -Merge two hashtables into one - -.DESCRIPTION -Add elemtns from $second hastable(or any dictionary based structure) to $first - -.PARAMETER first -Hashtable to add key/value pairs -.PARAMETER second -Hashtable key/values to be added to $first - -.EXAMPLE - - $q = @{a=1;b=4;c=3} - [ordered]$w = @{a=2;b=1;c=5;d=6} - MergeHashtable $q $w -#> - - function MergeHashtable { - param ( - [Alias("To")] - [Parameter(Position = 0, ValueFromPipelineByPropertyName = $True, Mandatory = $true)] - [System.Collections.IDictionary]$first, - - [Alias("From")] - [Parameter(Position = 1, ValueFromPipelineByPropertyName = $True, Mandatory = $true)] - [System.Collections.IDictionary]$second - ) - $second.Keys | ForEach-Object{ - $first[$_] = $second[$_]; - } - } - - function LoadModules { - param ( - [bool]$local = $false, - [Parameter(Mandatory = $true)] - [string]$functionsFolder, - [string]$sharedFolder = $PSScriptRoot - ) - - $functions = @( Get-ChildItem -Path $functionsFolder\*.ps1 -ErrorAction SilentlyContinue ) - $shared = @( Get-ChildItem -Path $sharedFolder\*.ps1 -ErrorAction SilentlyContinue ) - - Foreach($import in @($Public + $functions)) - { - Try - { - . $import.fullname - } - Catch - { - Write-Error -Message "Failed to import function $($import.fullname): $_" - } - } - - - ## - - - -# Add the functions into the runspace -GetScriptFunctions $functions | ForEach-Object { - $rs.SessionStateProxy.InvokeProvider.Item.Set( - 'function:\{0}' -f $_.Name, - $_.Body.GetScriptBlock()) -} - } -<# -.SYNOPSIS -# - -.DESCRIPTION -This function is based on https://stackoverflow.com/a/45929412/959779 - -.PARAMETER scriptFile -Parameter description - -.EXAMPLE -An example - -.NOTES -General notes -#> - -function GetScriptFunctions { - param ( - [string]$scriptFile - ) - # Get the AST of the file - $tokens = $errors = $null - $ast = [System.Management.Automation.Language.Parser]::ParseFile( - $scriptFile, - [ref]$tokens, - [ref]$errors) - - # Get only function definition ASTs - $functionDefinitions = $ast.FindAll({ - param([System.Management.Automation.Language.Ast] $Ast) - - $Ast -is [System.Management.Automation.Language.FunctionDefinitionAst] -and - # Class methods have a FunctionDefinitionAst under them as well, but we don't want them. - ($PSVersionTable.PSVersion.Major -lt 5 -or - $Ast.Parent -isnot [System.Management.Automation.Language.FunctionMemberAst]) - - }, $true) - return $functionDefinitions -} <# .SYNOPSIS @@ -244,7 +65,7 @@ An example General notes #> -function CreateFolderIfNotExist { +function szCreateFolderIfNotExist { param ([string]$Folder) if( Test-Path $Folder -PathType Leaf){ Write-Error "The destanation path ${Folder} is file." @@ -256,28 +77,9 @@ function CreateFolderIfNotExist { } -<# -.SYNOPSIS -Check and create folder - -.DESCRIPTION -Check if path and create folder if not exist - - -.EXAMPLE -An example - -.NOTES -General notes -#> -filter First { - $_ - Break - } - - filter Last { + filter szLast { BEGIN { $current=$null @@ -305,13 +107,13 @@ An example .NOTES General notes #> -function Receive-File { +function szReceive-File { param ( [string]$name, [string]$file, [string]$url ) - $reply = Show-ConfirmPrompt + $reply = szShow-ConfirmPrompt if ( -not $reply ) { Write-Error "Execution aborted" return -1; @@ -327,7 +129,7 @@ function Receive-File { } -function Extract-ZipFile { +function szExtract-ZipFile { param ( [ValidateScript( { Test-Path $_ -pathType leaf })] [Parameter(Mandatory = $true)] @@ -340,11 +142,11 @@ function Extract-ZipFile { [System.IO.Compression.ZipFile]::ExtractToDirectory($FileName, $Path) } -function Get-TempFileName() { +function szGet-TempFileName() { return [System.IO.Path]::GetTempFileName() } -function Test-Empty { +function szTest-Empty { param ( [Parameter(Position = 0)] [string]$string @@ -352,7 +154,7 @@ function Test-Empty { return [string]::IsNullOrWhitespace($string) } -function Combine-Path { +function szCombine-Path { param ( [string]$baseDir, [string]$path @@ -361,22 +163,15 @@ function Combine-Path { [IO.Path]::Combine([string[]]$allArgs) } -function Get-ProfileDataFile { - param ( - [string]$file, - [string]$moduleName = $null - ) - return Join-Path (Get-ProfileDir $moduleName) $file - -} -function Get-ProfileDir { + +function szGet-ProfileDir { param ( [string]$moduleName = $null ) $profileDir = $ENV:AppData - if( Test-Empty $moduleName ){ + if( szTest-Empty $moduleName ){ if ( $script:MyInvocation.MyCommand.Name.EndsWith('.psm1') ){ $moduleName = $script:MyInvocation.MyCommand.Name @@ -388,11 +183,11 @@ function Get-ProfileDir { } } - if( Test-Empty $moduleName ){ + if( szTest-Empty $moduleName ){ throw "Unable to read module name." } - $scriptProfile = Combine-Path $profileDir '.ps1' 'ScriptData' $moduleName + $scriptProfile = szCombine-Path $profileDir '.ps1' 'ScriptData' $moduleName if ( ! (Test-Path $scriptProfile -PathType Container )) { New-Item -Path $scriptProfile -ItemType 'Directory' } @@ -400,7 +195,7 @@ function Get-ProfileDir { } -function CheckPsGalleryUpdate { +function szCheckPsGalleryUpdate { param ( [string] $moduleName, [string] $currentVersion @@ -408,17 +203,17 @@ function CheckPsGalleryUpdate { Try { - Write-Console "Update check..." + szWrite-Console "Update check..." $feed = Invoke-WebRequest -Uri "https://www.powershellgallery.com/api/v2/FindPackagesById()?id=%27$moduleName%27" - $last=([xml]$feed.Content).feed.entry |Sort-Object -Property updated | Last + $last=([xml]$feed.Content).feed.entry |Sort-Object -Property updated | szLast $version= $last.properties.Version if ($version -gt $currentVersion) { - Write-Console "Found a new module version {$version}." + szWrite-Console "Found a new module version {$version}." $notes=$last.properties.ReleaseNotes.'#text' - Write-Console "Release notes: {$notes}." - Write-Console "Recomendent to update module with command: Update-Module -Name $moduleName -Force" + szWrite-Console "Release notes: {$notes}." + szWrite-Console "Recomendent to update module with command: Update-Module -Name $moduleName -Force" } } Catch @@ -426,7 +221,7 @@ function CheckPsGalleryUpdate { } } -function Write-Console { +function szWrite-Console { param ( [string]$text, [String[]]$arg=$null