Skip to content

Commit

Permalink
Merge pull request #2 from ethanbergstrom/logicConsolidation
Browse files Browse the repository at this point in the history
Logic consolidation
  • Loading branch information
ethanbergstrom authored Jun 27, 2022
2 parents d1e214e + 3cb539f commit a0366f5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 62 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
94 changes: 33 additions & 61 deletions src/Croze.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 (?<name>\S+)(?<=\w)(-+)(?<version>\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 '(?<name>\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
Expand Down Expand Up @@ -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 (?<name>\S+)(?<=\w)(-+)(?<version>\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 '(?<name>\S+) was successfully' | ForEach-Object -MemberName Matches | ForEach-Object {
$match = ($_.Groups | Where-Object Name -eq 'name').Value
[PSCustomObject]@{
Name = $match
}
}
}
}
}
OutputHandlers = $PackageInstallHandlers
},
@{
Verb = 'Get'
Expand Down Expand Up @@ -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 (?<name>\S+)(?=--)--(?<version>\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 '(?<name>\S+) (?<oldVerison>\S+) -> (?<version>\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'
Expand Down Expand Up @@ -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
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Croze.psd1
Original file line number Diff line number Diff line change
@@ -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'
Expand Down

0 comments on commit a0366f5

Please sign in to comment.