Skip to content

Commit cc20b04

Browse files
authored
Merge pull request #138 from PrincipleStudios/refactor/action-registry
Refactor action registry
2 parents 22e6022 + c55150d commit cc20b04

24 files changed

+516
-553
lines changed

utils/actions/Invoke-FinalizeAction.psm1

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ Import-Module -Scope Local "$PSScriptRoot/finalize/Register-FinalizeActionChecko
44
Import-Module -Scope Local "$PSScriptRoot/finalize/Register-FinalizeActionSetBranches.psm1"
55
Import-Module -Scope Local "$PSScriptRoot/finalize/Register-FinalizeActionTrack.psm1"
66

7-
$finalizeActions = @{}
8-
Register-FinalizeActionCheckout $finalizeActions
9-
Register-FinalizeActionSetBranches $finalizeActions
10-
Register-FinalizeActionTrack $finalizeActions
7+
$finalizeActions = @{
8+
'checkout' = ${function:Invoke-CheckoutFinalizeAction}
9+
'set-branches' = ${function:Invoke-SetBranchesFinalizeAction}
10+
'track' = ${function:Invoke-TrackFinalizeAction}
11+
}
1112

1213
function Invoke-FinalizeAction(
1314
[PSObject] $actionDefinition,

utils/actions/Invoke-LocalAction.psm1

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ Import-Module -Scope Local "$PSScriptRoot/local/Register-LocalActionValidateBran
1616
Import-Module -Scope Local "$PSScriptRoot/local/Register-LocalActionAssertExistence.psm1"
1717

1818
$localActions = Get-LocalActionsRegistry
19-
Register-LocalActionAddDiagnostic $localActions
20-
Register-LocalActionAssertPushed $localActions
21-
Register-LocalActionAssertUpdated $localActions
22-
Register-LocalActionEvaluate $localActions
23-
Register-LocalActionGetAllUpstreams $localActions
24-
Register-LocalActionGetUpstream $localActions
25-
Register-LocalActionGetDownstream $localActions
26-
Register-LocalActionFilterBranches $localActions
27-
Register-LocalActionMergeBranches $localActions
28-
Register-LocalActionRecurse $localActions
29-
Register-LocalActionSetUpstream $localActions
30-
Register-LocalActionSimplifyUpstreamBranches $localActions
31-
Register-LocalActionUpstreamsUpdated $localActions
32-
Register-LocalActionValidateBranchNames $localActions
33-
Register-LocalActionAssertExistence $localActions
19+
$localActions['add-diagnostic'] = ${function:Invoke-AddDiagnosticLocalAction}
20+
$localActions['assert-existence'] = ${function:Invoke-AssertBranchExistenceLocalAction}
21+
$localActions['assert-pushed'] = ${function:Invoke-AssertBranchPushedLocalAction}
22+
$localActions['assert-updated'] = ${function:Invoke-AssertBranchUpToDateLocalAction}
23+
$localActions['evaluate'] = ${function:Invoke-EvaluateLocalAction}
24+
$localActions['filter-branches'] = ${function:Invoke-FilterBranchesLocalAction}
25+
$localActions['get-all-upstreams'] = ${function:Invoke-GetAllUpstreamsLocalAction}
26+
$localActions['get-downstream'] = ${function:Invoke-GetDownstreamLocalAction}
27+
$localActions['get-upstream'] = ${function:Invoke-GetUpstreamLocalAction}
28+
$localActions['merge-branches'] = ${function:Invoke-MergeBranchesLocalAction}
29+
$localActions['recurse'] = ${function:Invoke-RecursiveScriptLocalAction}
30+
$localActions['set-upstream'] = ${function:Invoke-SetUpstreamLocalAction}
31+
$localActions['simplify-upstream'] = ${function:Invoke-SimplifyUpstreamLocalAction}
32+
$localActions['upstreams-updated'] = ${function:Invoke-UpstreamsUpdatedLocalAction}
33+
$localActions['validate-branch-names'] = ${function:Invoke-AssertBranchNamesLocalAction}
3434

3535
Export-ModuleMember -Function Invoke-LocalAction

utils/actions/finalize/Register-FinalizeActionCheckout.psm1

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,21 @@ Import-Module -Scope Local "$PSScriptRoot/../../query-state.psm1"
33
Import-Module -Scope Local "$PSScriptRoot/../../git.psm1"
44

55
# TODO: should check out the branch from remote as a local tracking branch
6-
function Register-FinalizeActionCheckout([PSObject] $finalizeActions) {
7-
$finalizeActions['checkout'] = {
8-
param(
9-
[string] $HEAD,
10-
[Parameter()][AllowNull()][AllowEmptyCollection()][System.Collections.ArrayList] $diagnostics,
11-
[switch] $dryRun
12-
)
6+
function Invoke-CheckoutFinalizeAction{
7+
param(
8+
[string] $HEAD,
9+
[Parameter()][AllowNull()][AllowEmptyCollection()][System.Collections.ArrayList] $diagnostics,
10+
[switch] $dryRun
11+
)
1312

14-
if ($dryRun) {
15-
"git checkout $HEAD"
16-
return
17-
}
18-
Assert-CleanWorkingDirectory -diagnostics $diagnostics
19-
if (-not (Get-HasErrorDiagnostic $diagnostics)) {
20-
Invoke-CheckoutBranch $HEAD -diagnostics $diagnostics
21-
}
13+
if ($dryRun) {
14+
"git checkout $HEAD"
15+
return
16+
}
17+
Assert-CleanWorkingDirectory -diagnostics $diagnostics
18+
if (-not (Get-HasErrorDiagnostic $diagnostics)) {
19+
Invoke-CheckoutBranch $HEAD -diagnostics $diagnostics
2220
}
2321
}
2422

25-
Export-ModuleMember -Function Register-FinalizeActionCheckout
23+
Export-ModuleMember -Function Invoke-CheckoutFinalizeAction
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function ConvertTo-PushBranchList([Parameter(Mandatory)][Hashtable] $branches) {
2+
$result = $branches.Keys | Sort-Object | Foreach-Object {
3+
"$($branches[$_]):refs/heads/$($_)"
4+
}
5+
return $result
6+
}
7+
8+
# Not to be re-exported; used for testing
9+
Export-ModuleMember -Function ConvertTo-PushBranchList

utils/actions/finalize/Register-FinalizeActionSetBranches.mocks.psm1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Import-Module -Scope Local "$PSScriptRoot/../../input.mocks.psm1"
33
Import-Module -Scope Local "$PSScriptRoot/../../query-state.mocks.psm1"
44
Import-Module -Scope Local "$PSScriptRoot/../../query-state.psm1"
55
Import-Module -Scope Local "$PSScriptRoot/Register-FinalizeActionSetBranches.psm1"
6+
Import-Module -Scope Local "$PSScriptRoot/Register-FinalizeActionSetBranches.helpers.psm1"
67

78
function Invoke-MockGit([string] $gitCli, [object] $MockWith) {
89
return Invoke-MockGitModule -ModuleName 'Register-FinalizeActionSetBranches' @PSBoundParameters

utils/actions/finalize/Register-FinalizeActionSetBranches.psm1

Lines changed: 50 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,66 @@ Import-Module -Scope Local "$PSScriptRoot/../../framework.psm1"
33
Import-Module -Scope Local "$PSScriptRoot/../../input.psm1"
44
Import-Module -Scope Local "$PSScriptRoot/../../query-state.psm1"
55
Import-Module -Scope Local "$PSScriptRoot/../../git.psm1"
6+
Import-Module -Scope Local "$PSScriptRoot/Register-FinalizeActionSetBranches.helpers.psm1"
67

7-
function ConvertTo-PushBranchList([Parameter(Mandatory)][Hashtable] $branches) {
8-
$result = $branches.Keys | Sort-Object | Foreach-Object {
9-
"$($branches[$_]):refs/heads/$($_)"
10-
}
11-
return $result
12-
}
13-
14-
# Not to be re-exported; used for testing
15-
Export-ModuleMember -Function ConvertTo-PushBranchList
16-
17-
function Register-FinalizeActionSetBranches([PSObject] $finalizeActions) {
18-
$finalizeActions['set-branches'] = {
19-
param(
20-
[Parameter()] $branches,
21-
[Parameter()] $force = $false,
22-
[Parameter()][AllowNull()][AllowEmptyCollection()][System.Collections.ArrayList] $diagnostics,
23-
[switch] $dryRun
24-
)
8+
function Invoke-SetBranchesFinalizeAction {
9+
param(
10+
[Parameter()] $branches,
11+
[Parameter()] $force = $false,
12+
[Parameter()][AllowNull()][AllowEmptyCollection()][System.Collections.ArrayList] $diagnostics,
13+
[switch] $dryRun
14+
)
2515

26-
$branches = ConvertTo-Hashtable $branches
27-
$config = Get-Configuration
16+
$branches = ConvertTo-Hashtable $branches
17+
$config = Get-Configuration
2818

29-
$branches.Keys | Assert-ValidBranchName -diagnostics $diagnostics
19+
$branches.Keys | Assert-ValidBranchName -diagnostics $diagnostics
3020

31-
if ($null -ne $config.remote) {
32-
$atomicPart = $config.atomicPushEnabled ? @("--atomic") : @()
33-
$forcePart = $force ? @("--force") : @()
34-
[string[]]$branchList = ConvertTo-PushBranchList $branches
35-
if ($dryRun) {
36-
"git push $($config.remote) $atomicPart $forcePart $branchList"
37-
return
38-
}
39-
Invoke-ProcessLogs "git push $($config.remote) $atomicPart $forcePart $branchList" {
40-
git push $config.remote @atomicPart @forcePart @branchList
41-
}
42-
if ($global:LASTEXITCODE -ne 0) {
43-
Add-ErrorDiagnostic $diagnostics "Unable to push updates to $($config.remote)"
44-
}
45-
} else {
46-
$currentBranch = Get-CurrentBranch
21+
if ($null -ne $config.remote) {
22+
$atomicPart = $config.atomicPushEnabled ? @("--atomic") : @()
23+
$forcePart = $force ? @("--force") : @()
24+
[string[]]$branchList = ConvertTo-PushBranchList $branches
25+
if ($dryRun) {
26+
"git push $($config.remote) $atomicPart $forcePart $branchList"
27+
return
28+
}
29+
Invoke-ProcessLogs "git push $($config.remote) $atomicPart $forcePart $branchList" {
30+
git push $config.remote @atomicPart @forcePart @branchList
31+
}
32+
if ($global:LASTEXITCODE -ne 0) {
33+
Add-ErrorDiagnostic $diagnostics "Unable to push updates to $($config.remote)"
34+
}
35+
} else {
36+
$currentBranch = Get-CurrentBranch
4737

48-
foreach ($key in $branches.Keys) {
49-
if ($currentBranch -eq $key) {
50-
Assert-CleanWorkingDirectory -diagnostics $diagnostics
51-
if (Get-HasErrorDiagnostic $diagnostics) { continue }
38+
foreach ($key in $branches.Keys) {
39+
if ($currentBranch -eq $key) {
40+
Assert-CleanWorkingDirectory -diagnostics $diagnostics
41+
if (Get-HasErrorDiagnostic $diagnostics) { continue }
5242

53-
# update head, since it matches the branch to be "pushed"
54-
if ($dryRun) {
55-
"git reset --hard `"$($branches[$key])`""
56-
continue
57-
}
58-
Invoke-ProcessLogs "git reset --hard $($branches[$key])" {
59-
git reset --hard "$($branches[$key])"
60-
}
61-
} else {
62-
# just update the branch
63-
if ($dryRun) {
64-
"git branch $key `"$($branches[$key])`" -f"
65-
continue
66-
}
67-
Invoke-ProcessLogs "git branch $key $($branches[$key])" {
68-
git branch $key "$($branches[$key])" -f
69-
}
43+
# update head, since it matches the branch to be "pushed"
44+
if ($dryRun) {
45+
"git reset --hard `"$($branches[$key])`""
46+
continue
47+
}
48+
Invoke-ProcessLogs "git reset --hard $($branches[$key])" {
49+
git reset --hard "$($branches[$key])"
7050
}
71-
if ($global:LASTEXITCODE -ne 0) {
72-
Add-ErrorDiagnostic $diagnostics "Unable to update local branches"
51+
} else {
52+
# just update the branch
53+
if ($dryRun) {
54+
"git branch $key `"$($branches[$key])`" -f"
55+
continue
7356
}
57+
Invoke-ProcessLogs "git branch $key $($branches[$key])" {
58+
git branch $key "$($branches[$key])" -f
59+
}
60+
}
61+
if ($global:LASTEXITCODE -ne 0) {
62+
Add-ErrorDiagnostic $diagnostics "Unable to update local branches"
7463
}
7564
}
7665
}
7766
}
7867

79-
Export-ModuleMember -Function Register-FinalizeActionSetBranches
68+
Export-ModuleMember -Function Invoke-SetBranchesFinalizeAction

utils/actions/finalize/Register-FinalizeActionTrack.psm1

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,69 +4,67 @@ Import-Module -Scope Local "$PSScriptRoot/../../input.psm1"
44
Import-Module -Scope Local "$PSScriptRoot/../../query-state.psm1"
55
Import-Module -Scope Local "$PSScriptRoot/../../git.psm1"
66

7-
function Register-FinalizeActionTrack([PSObject] $finalizeActions) {
8-
$finalizeActions['track'] = {
9-
param(
10-
[Parameter()][AllowEmptyCollection()][string[]] $branches,
11-
[Parameter()][bool] $createIfNotTracked,
12-
[Parameter()][AllowNull()][AllowEmptyCollection()][System.Collections.ArrayList] $diagnostics,
13-
[switch] $dryRun
14-
)
15-
[string[]] $tracked = @()
16-
$config = Get-Configuration
17-
if ($null -eq $config.remote) {
18-
if ($dryRun) {
19-
return
20-
}
21-
return $tracked
7+
function Invoke-TrackFinalizeAction {
8+
param(
9+
[Parameter()][AllowEmptyCollection()][string[]] $branches,
10+
[Parameter()][bool] $createIfNotTracked,
11+
[Parameter()][AllowNull()][AllowEmptyCollection()][System.Collections.ArrayList] $diagnostics,
12+
[switch] $dryRun
13+
)
14+
[string[]] $tracked = @()
15+
$config = Get-Configuration
16+
if ($null -eq $config.remote) {
17+
if ($dryRun) {
18+
return
2219
}
20+
return $tracked
21+
}
2322

24-
$currentBranch = Get-CurrentBranch
25-
foreach ($branch in $branches) {
26-
$localBranch = Get-LocalBranchForRemote $branch
27-
if (-not $localBranch -AND $currentBranch -eq $branch) {
28-
# current branch matches tracked one, but doesn't currently track the remote
29-
$localBranch = $currentBranch
30-
if ($dryRun) {
31-
"git branch $localBranch --set-upstream-to `"refs/remotes/$($config.remote)/$branch`""
32-
} else {
33-
Invoke-ProcessLogs "git branch $localBranch --set-upstream-to refs/remotes/$($config.remote)/$branch" {
34-
git branch $localBranch --set-upstream-to "refs/remotes/$($config.remote)/$branch"
35-
}
23+
$currentBranch = Get-CurrentBranch
24+
foreach ($branch in $branches) {
25+
$localBranch = Get-LocalBranchForRemote $branch
26+
if (-not $localBranch -AND $currentBranch -eq $branch) {
27+
# current branch matches tracked one, but doesn't currently track the remote
28+
$localBranch = $currentBranch
29+
if ($dryRun) {
30+
"git branch $localBranch --set-upstream-to `"refs/remotes/$($config.remote)/$branch`""
31+
} else {
32+
Invoke-ProcessLogs "git branch $localBranch --set-upstream-to refs/remotes/$($config.remote)/$branch" {
33+
git branch $localBranch --set-upstream-to "refs/remotes/$($config.remote)/$branch"
3634
}
3735
}
38-
39-
if ($currentBranch -eq $localBranch) {
40-
# update head
41-
Assert-CleanWorkingDirectory -diagnostics $diagnostics
42-
if (Get-HasErrorDiagnostic $diagnostics) { continue }
43-
if ($dryRun) {
44-
"git reset --hard `"refs/remotes/$($config.remote)/$branch`""
45-
} else {
46-
Invoke-ProcessLogs "git reset --hard refs/remotes/$($config.remote)/$branch" {
47-
git reset --hard "refs/remotes/$($config.remote)/$branch"
48-
}
49-
}
50-
} elseif ($localBranch -OR $createIfNotTracked) {
51-
$localBranch = $localBranch ? $localBranch : $branch
52-
# update
53-
if ($dryRun) {
54-
"git branch $localBranch `"refs/remotes/$($config.remote)/$branch`" -f"
55-
} else {
56-
Invoke-ProcessLogs "git branch $localBranch refs/remotes/$($config.remote)/$branch -f" {
57-
git branch $localBranch "refs/remotes/$($config.remote)/$branch" -f
58-
}
36+
}
37+
38+
if ($currentBranch -eq $localBranch) {
39+
# update head
40+
Assert-CleanWorkingDirectory -diagnostics $diagnostics
41+
if (Get-HasErrorDiagnostic $diagnostics) { continue }
42+
if ($dryRun) {
43+
"git reset --hard `"refs/remotes/$($config.remote)/$branch`""
44+
} else {
45+
Invoke-ProcessLogs "git reset --hard refs/remotes/$($config.remote)/$branch" {
46+
git reset --hard "refs/remotes/$($config.remote)/$branch"
5947
}
48+
}
49+
} elseif ($localBranch -OR $createIfNotTracked) {
50+
$localBranch = $localBranch ? $localBranch : $branch
51+
# update
52+
if ($dryRun) {
53+
"git branch $localBranch `"refs/remotes/$($config.remote)/$branch`" -f"
6054
} else {
61-
continue
55+
Invoke-ProcessLogs "git branch $localBranch refs/remotes/$($config.remote)/$branch -f" {
56+
git branch $localBranch "refs/remotes/$($config.remote)/$branch" -f
57+
}
6258
}
63-
$tracked += $branch
59+
} else {
60+
continue
6461
}
62+
$tracked += $branch
63+
}
6564

66-
if (-not $dryRun) {
67-
return $tracked
68-
}
65+
if (-not $dryRun) {
66+
return $tracked
6967
}
7068
}
7169

72-
Export-ModuleMember -Function Register-FinalizeActionTrack
70+
Export-ModuleMember -Function Invoke-TrackFinalizeAction
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
Import-Module -Scope Local "$PSScriptRoot/../../core.psm1"
22
Import-Module -Scope Local "$PSScriptRoot/../../framework.psm1"
33

4-
function Register-LocalActionAddDiagnostic([PSObject] $localActions) {
5-
$localActions['add-diagnostic'] = {
6-
param(
7-
[Parameter()][string] $message,
8-
[switch] $isWarning,
9-
[Parameter()][AllowNull()][AllowEmptyCollection()][System.Collections.ArrayList] $diagnostics
10-
)
11-
12-
if ($isWarning) {
13-
Add-WarningDiagnostic $diagnostics $message
14-
} else {
15-
Add-ErrorDiagnostic $diagnostics $message
16-
}
4+
function Invoke-AddDiagnosticLocalAction {
5+
param(
6+
[Parameter()][string] $message,
7+
[switch] $isWarning,
8+
[Parameter()][AllowNull()][AllowEmptyCollection()][System.Collections.ArrayList] $diagnostics
9+
)
1710

18-
return @{}
11+
if ($isWarning) {
12+
Add-WarningDiagnostic $diagnostics $message
13+
} else {
14+
Add-ErrorDiagnostic $diagnostics $message
1915
}
16+
17+
return @{}
2018
}
2119

22-
Export-ModuleMember -Function Register-LocalActionAddDiagnostic
20+
Export-ModuleMember -Function Invoke-AddDiagnosticLocalAction

0 commit comments

Comments
 (0)