-
Notifications
You must be signed in to change notification settings - Fork 6
/
Atlassian.Bitbucket.Tools.psm1
102 lines (92 loc) · 3.41 KB
/
Atlassian.Bitbucket.Tools.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
function Compare-CustomObject {
[OutputType('System.Boolean')]
[CmdletBinding()]
param(
[Parameter(Position=0)]
[PSObject]$ReferenceObject,
[Parameter(Position=1)]
[PSObject]$DifferenceObject,
[Parameter()]
[String[]]$IgnoreProperty
)
if($null -eq $ReferenceObject -and $null -eq $DifferenceObject){
return $true
}elseif($null -eq $ReferenceObject -or $null -eq $DifferenceObject){
return $false
}
# Compare Object Type
if($ReferenceObject.GetType() -ne $DifferenceObject.GetType()){
return $false
}
# Check for basic types that allow direct equality comparison
if(Confirm-IsComparableType $ReferenceObject){
# Compare Value of basic type
if($ReferenceObject -ne $DifferenceObject){
return $false
}
}elseif($ReferenceObject.GetType().BaseType.Name -eq 'Array'){
# Handle Array
# Check Array Length Matches
if($ReferenceObject.Length -ne $DifferenceObject.Length){
return $false
}elseif($ReferenceObject.Length -ne 0){
# Check for Object arrays
if(!(Confirm-IsComparableType $ReferenceObject[0])){
$properties = ($ReferenceObject[0] | Get-Member | Where-Object -Property MemberType -In -Value @('Property', 'NoteProperty')).Name
# Sort the object arrays before comparing
$referenceArray = $ReferenceObject | Sort-Object -Property $properties
$differenceArray = $DifferenceObject | Sort-Object -Property $properties
}else{
# Sort the arrays before comparing
$referenceArray = $ReferenceObject | Sort-Object
$differenceArray = $DifferenceObject | Sort-Object
}
# Check Values
for ($i = 0; $i -lt $referenceArray.Count; $i++) {
if(!(Compare-CustomObject $referenceArray[$i] $differenceArray[$i])){
return $false
}
}
}
}else{
# Object Comparison
# Compare Members
$referenceMembers = ($ReferenceObject | Get-Member | `
Where-Object -Property MemberType -In -Value @('Property', 'NoteProperty') | `
Where-Object -Property Name -NotIn -Value $IgnoreProperty).Name | Sort-Object
$differenceMembers = ($DifferenceObject | Get-Member | `
Where-Object -Property MemberType -In -Value @('Property', 'NoteProperty') | `
Where-Object -Property Name -NotIn -Value $IgnoreProperty).Name | Sort-Object
if($null -eq $referenceMembers -and $null -eq $differenceMembers){
Write-Warning 'The objects being compared contain no properties. Thus the objects will always be equal.'
}elseif($null -eq $referenceMembers -or $null -eq $differenceMembers){
return $false
}else{
if(Compare-Object $referenceMembers $differenceMembers){
return $false
}
foreach ($member in $referenceMembers) {
# Compare Object
if(!(Compare-CustomObject $ReferenceObject.$member $DifferenceObject.$member)){
return $false
}
}
}
}
# All Checks Passed
return $true
}
function Confirm-IsComparableType{
[OutputType('System.Boolean')]
[CmdletBinding()]
param(
[Parameter(Position=0)]
$Object
)
# Primatives + String
$ComparableTypes = @('Boolean', 'Byte', 'SByte', 'Int16', 'UInt16', 'Int32', 'UInt32', 'Int64', 'UInt64', 'IntPtr', 'UIntPtr', 'Char', 'Double', 'Single', 'String')
if($Object.GetType().BaseType.Name -eq 'ValueType' -or $Object.GetType().Name -in $ComparableTypes){
return $true
}
return $false
}