From 1a43eb608ae9cbb61abe0b3e078e464e12fec76f Mon Sep 17 00:00:00 2001 From: ethanbergstrom Date: Sun, 26 Jun 2022 21:18:05 -0500 Subject: [PATCH 1/3] Logic consolidation --- CHANGELOG.md | 4 +++ src/Croze.ps1 | 94 ++++++++++++++++++-------------------------------- src/Croze.psd1 | 2 +- 3 files changed, 38 insertions(+), 62 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9b5c9d..bae165a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.0.3] - 2022-06-26 +### Changed +- Refactored some duplicate logic + ## [0.0.2] - 2022-06-26 ### Added - Custom tap management diff --git a/src/Croze.ps1 b/src/Croze.ps1 index a4d2674..fddb0f8 100644 --- a/src/Croze.ps1 +++ b/src/Croze.ps1 @@ -11,6 +11,33 @@ $BaseOutputHandlers = @{ } } +$PackageInstallHandlers = @{ + ParameterSetName = 'Default' + Handler = { + param ( $output ) + + if ($output -match 'Pouring') { + # Formula output - capture package and dependency name and version + $output | Select-String 'Pouring (?\S+)(?<=\w)(-+)(?\d+\.{0,1}\d*\.(?=\d)\d*)' | ForEach-Object -MemberName Matches | ForEach-Object { + $match = ($_.Groups | Where-Object Name -in 'name','version').Value + + [PSCustomObject]@{ + Name = $match[0] + Version = $match[1] + } + } + } elseif ($output -match 'was successfully') { + # Cask output - capture package only + $output | Select-String '(?\S+) was successfully' | ForEach-Object -MemberName Matches | ForEach-Object { + $match = ($_.Groups | Where-Object Name -eq 'name').Value + [PSCustomObject]@{ + Name = $match + } + } + } + } +} + # The general structure of this hashtable is to define noun-level attributes, which are -probably- common across all commands for the same noun, but still allow for customization at more specific verb-level defition for that noun. # The following three command attributes have the following order of precedence: # OriginalCommandElements will be MERGED in the order of Noun + Verb + Base @@ -104,32 +131,7 @@ $Commands = @( Verb = 'Install' Description = 'Install a new package with HomeBrew' OriginalCommandElements = @('install') - OutputHandlers = @{ - ParameterSetName = 'Default' - Handler = { - param ( $output ) - - if ($output -match 'Pouring') { - # Formula output - capture package and dependency name and version - $output | Select-String 'Pouring (?\S+)(?<=\w)(-+)(?\d+\.{0,1}\d*\.(?=\d)\d*)' | ForEach-Object -MemberName Matches | ForEach-Object { - $match = ($_.Groups | Where-Object Name -in 'name','version').Value - - [PSCustomObject]@{ - Name = $match[0] - Version = $match[1] - } - } - } elseif ($output -match 'was successfully') { - # Cask output - capture package only - $output | Select-String '(?\S+) was successfully' | ForEach-Object -MemberName Matches | ForEach-Object { - $match = ($_.Groups | Where-Object Name -eq 'name').Value - [PSCustomObject]@{ - Name = $match - } - } - } - } - } + OutputHandlers = $PackageInstallHandlers }, @{ Verb = 'Get' @@ -224,35 +226,7 @@ $Commands = @( Verb = 'Update' Description = 'Updates an installed package to the latest version' OriginalCommandElements = @('upgrade') - OutputHandlers = @{ - ParameterSetName = 'Default' - Handler = { - param ( $output ) - - if ($output -match 'Pouring') { - # Formula output - capture package and dependency name and version - $packageInfo = @{} - - $output | Select-String 'Pouring (?\S+)(?=--)--(?\d+\.{0,1}\d*\.(?=\d)\d*)' | ForEach-Object -MemberName Matches | ForEach-Object { - $match = ($_.Groups | Select-Object -Skip 1).Value - $packageInfo.add($match[0],$match[1]) - } - - $packageInfo - } elseif ($output -match 'was successfully') { - # Successful Cask output. We should be able to get the new version of the upgraded package - $output | Select-String '(?\S+) (?\S+) -> (?\d+\.{0,1}\d*\.(?=\d)\d*)' | ForEach-Object -MemberName Matches | ForEach-Object { - $match = ($_.Groups | Select-Object -Skip 1).Value - $packageInfo.add($match[0],$match[1]) - } - - $packageInfo - } else { - # Cask output - does not have much useful output other than if installation fails - if ($output) {Write-Error ($output)} - } - } - } + OutputHandlers = $PackageInstallHandlers }, @{ Verb = 'Uninstall' @@ -294,12 +268,10 @@ $Commands = @( Handler = { param ( $output ) - $result = @() - - $result += $output | ConvertFrom-Json | Select-Object -ExpandProperty formulae - $result += $output | ConvertFrom-Json | Select-Object -ExpandProperty casks - - $result + $output | ConvertFrom-Json | ForEach-Object { + $_.formulae + $_.casks + } } } } diff --git a/src/Croze.psd1 b/src/Croze.psd1 index 2a1a018..e24dc0d 100644 --- a/src/Croze.psd1 +++ b/src/Croze.psd1 @@ -1,6 +1,6 @@ @{ RootModule = 'Croze.psm1' - ModuleVersion = '0.0.2' + ModuleVersion = '0.0.3' GUID = '46caec03-e808-4fa2-b464-14677bb60c52' Author = 'Ethan Bergstrom' Copyright = '2022' From 2533450df54b4cf63406b73b54e78a895617bb8f Mon Sep 17 00:00:00 2001 From: ethanbergstrom Date: Sun, 26 Jun 2022 21:23:50 -0500 Subject: [PATCH 2/3] Additional consolidation --- src/Croze.ps1 | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Croze.ps1 b/src/Croze.ps1 index fddb0f8..5f5864b 100644 --- a/src/Croze.ps1 +++ b/src/Croze.ps1 @@ -17,10 +17,9 @@ $PackageInstallHandlers = @{ param ( $output ) if ($output -match 'Pouring') { - # Formula output - capture package and dependency name and version - $output | Select-String 'Pouring (?\S+)(?<=\w)(-+)(?\d+\.{0,1}\d*\.(?=\d)\d*)' | ForEach-Object -MemberName Matches | ForEach-Object { + # Formula output - capture package and dependency name and version pairs + $output | Select-String 'Pouring (?\S+)(?<=\w)(-+)(?\d+\.{0,1}\d*\.(?=\d)\d*)' | ForEach-Object -MemberName Matches { $match = ($_.Groups | Where-Object Name -in 'name','version').Value - [PSCustomObject]@{ Name = $match[0] Version = $match[1] @@ -28,10 +27,9 @@ $PackageInstallHandlers = @{ } } elseif ($output -match 'was successfully') { # Cask output - capture package only - $output | Select-String '(?\S+) was successfully' | ForEach-Object -MemberName Matches | ForEach-Object { - $match = ($_.Groups | Where-Object Name -eq 'name').Value + $output | Select-String '(?\S+) was successfully' | ForEach-Object -MemberName Matches { [PSCustomObject]@{ - Name = $match + Name = $_.Groups | Where-Object Name -eq 'name' | Select-Object -ExpandProperty Value } } } From 3cb539f42267ad5986176c77b0594d1013e92394 Mon Sep 17 00:00:00 2001 From: ethanbergstrom Date: Sun, 26 Jun 2022 21:53:07 -0500 Subject: [PATCH 3/3] Revert "Additional consolidation" This reverts commit 2533450df54b4cf63406b73b54e78a895617bb8f. --- src/Croze.ps1 | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Croze.ps1 b/src/Croze.ps1 index 5f5864b..fddb0f8 100644 --- a/src/Croze.ps1 +++ b/src/Croze.ps1 @@ -17,9 +17,10 @@ $PackageInstallHandlers = @{ param ( $output ) if ($output -match 'Pouring') { - # Formula output - capture package and dependency name and version pairs - $output | Select-String 'Pouring (?\S+)(?<=\w)(-+)(?\d+\.{0,1}\d*\.(?=\d)\d*)' | ForEach-Object -MemberName Matches { + # Formula output - capture package and dependency name and version + $output | Select-String 'Pouring (?\S+)(?<=\w)(-+)(?\d+\.{0,1}\d*\.(?=\d)\d*)' | ForEach-Object -MemberName Matches | ForEach-Object { $match = ($_.Groups | Where-Object Name -in 'name','version').Value + [PSCustomObject]@{ Name = $match[0] Version = $match[1] @@ -27,9 +28,10 @@ $PackageInstallHandlers = @{ } } elseif ($output -match 'was successfully') { # Cask output - capture package only - $output | Select-String '(?\S+) was successfully' | ForEach-Object -MemberName Matches { + $output | Select-String '(?\S+) was successfully' | ForEach-Object -MemberName Matches | ForEach-Object { + $match = ($_.Groups | Where-Object Name -eq 'name').Value [PSCustomObject]@{ - Name = $_.Groups | Where-Object Name -eq 'name' | Select-Object -ExpandProperty Value + Name = $match } } }