Skip to content

Commit 2121fe2

Browse files
Merge pull request #255 from PowershellFrameworkCollective/Import-AzDev
fixed license cuncurrent access issue
2 parents b593a27 + 5d7dcbe commit 2121fe2

7 files changed

Lines changed: 55 additions & 56 deletions

File tree

PSFramework/bin/PSFramework.dll

512 Bytes
Binary file not shown.

PSFramework/bin/PSFramework.pdb

0 Bytes
Binary file not shown.

PSFramework/bin/PSFramework.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PSFramework/functions/license/Get-PSFLicense.ps1

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
3232
Returns a list of all registered licenses for products that have commercial licenses and are libraries.
3333
#>
34-
3534
[CmdletBinding(PositionalBinding = $false, HelpUri = 'https://psframework.org/documentation/commands/PSFramework/Get-PSFLicense')]
3635
[OutputType([PSFramework.License.License])]
3736
param (
@@ -50,31 +49,11 @@
5049
$Manufacturer = "*"
5150
)
5251

53-
$licenses = [PSFramework.License.LicenseHost]::Get() | Where-Object { ($_.Product -like $Filter) -and ($_.Manufacturer -like $Manufacturer) }
54-
if ($PSBoundParameters.ContainsKey("ProductType"))
55-
{
56-
$temp = $licenses
57-
$licenses = @()
58-
59-
:main foreach ($l in $temp)
60-
{
61-
foreach ($type in $ProductType)
62-
{
63-
if ($l.ProductType -eq $type)
64-
{
65-
$licenses += $l
66-
continue main
67-
}
68-
}
69-
}
70-
}
71-
72-
if ($PSBoundParameters.ContainsKey("LicenseType"))
73-
{
74-
$licenses | Where-Object { ($_.LicenseType.ToString() -match $LicenseType.ToString()) -and ($_.Manufacturer -like $Manufacturer) -and ($_.Product -like $Filter) }
75-
}
76-
else
77-
{
78-
$licenses | Where-Object { ($_.Manufacturer -like $Manufacturer) -and ($_.Product -like $Filter) }
52+
[PSFramework.License.LicenseHost]::Get() | Where-Object {
53+
if ($_.Product -notlike $Filter) { return $false }
54+
if ($_.Manufacturer -notlike $Manufacturer) { return $false }
55+
if ($ProductType -and ($_.ProductType -notin $ProductType)) { return $false }
56+
if ($licenseType -and -not ($_.LicenseType -band $LicenseType)) { return $false }
57+
return $true
7958
}
8059
}

PSFramework/functions/license/New-PSFLicense.ps1

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
8181
This registers the Awesome Test Product as licensed under the common FreeBSD license.
8282
#>
83-
8483
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low', HelpUri = 'https://psframework.org/documentation/commands/PSFramework/New-PSFLicense')]
8584
[OutputType([PSFramework.License.License])]
8685
param
@@ -123,21 +122,22 @@
123122
)
124123

125124
# Create and fill object
126-
$license = New-Object PSFramework.License.License
127-
$license.Product = $Product
128-
$license.Manufacturer = $Manufacturer
129-
$license.ProductVersion = $ProductVersion
130-
$license.ProductType = $ProductType
131-
$license.LicenseName = $Name
132-
$license.LicenseVersion = $Version
133-
$license.LicenseDate = $Date
134-
$license.LicenseType = $Type
135-
$license.LicenseText = $Text
136-
$license.Description = $Description
137-
$license.Parent = $Parent
125+
$license = New-Object PSFramework.License.License -Property @{
126+
Product = $Product
127+
Manufacturer = $Manufacturer
128+
ProductVersion = $ProductVersion
129+
ProductType = $ProductType
130+
LicenseName = $Name
131+
LicenseVersion = $Version
132+
LicenseDate = $Date
133+
LicenseType = $Type
134+
LicenseText = $Text
135+
Description = $Description
136+
Parent = $Parent
137+
}
138138
if ($PSCmdlet.ShouldProcess("$($license.Product) $($license.ProductVersion) ($($license.LicenseName))", "Create License"))
139139
{
140-
if (-not ([PSFramework.License.LicenseHost]::Get() | Where-Object { ($_.Product -eq $license.Product) -and ($_.ProductVersion -eq $license.ProductVersion) }))
140+
if (-not ([PSFramework.License.LicenseHost]::Get($license)))
141141
{
142142
[PSFramework.License.LicenseHost]::Add($license)
143143
}
Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Concurrent;
2+
using System.Collections.Generic;
23

34
namespace PSFramework.License
45
{
@@ -10,7 +11,7 @@ public static class LicenseHost
1011
/// <summary>
1112
/// The list containing all registered licenses.
1213
/// </summary>
13-
public static List<License> Licenses = new List<License>();
14+
public static ConcurrentDictionary<string, License> Licenses = new ConcurrentDictionary<string, License>();
1415

1516
#region Default methods
1617
/// <summary>
@@ -19,26 +20,38 @@ public static class LicenseHost
1920
/// <param name="License">The license to add</param>
2021
public static void Add(License License)
2122
{
22-
Licenses.Add(License);
23-
24-
// TODO: Add code to respect policy settings to log added licenses
23+
string key = string.Format("{0}|{1}", License.Product, License.ProductVersion);
24+
Licenses[key] = License;
2525
}
2626

2727
/// <summary>
2828
/// Removes all registered Licenses
2929
/// </summary>
3030
public static void Clear()
3131
{
32-
Licenses = new List<License>();
32+
Licenses = new ConcurrentDictionary<string, License>();
3333
}
3434

3535
/// <summary>
3636
/// Returns all registered licenses
3737
/// </summary>
3838
/// <returns>All registerd licenses</returns>
39-
public static List<License> Get()
39+
public static ICollection<License> Get()
40+
{
41+
return Licenses.Values;
42+
}
43+
44+
/// <summary>
45+
/// Returns a license that matches the specified license in content
46+
/// </summary>
47+
/// <param name="ReferenceLicense">The license based on which to search</param>
48+
/// <returns>The matching license object</returns>
49+
public static License Get(License ReferenceLicense)
4050
{
41-
return Licenses;
51+
License tempLicense = null;
52+
try { Licenses.TryGetValue(string.Format("{0}|{1}", ReferenceLicense.Product, ReferenceLicense.ProductVersion), out tempLicense); }
53+
catch { }
54+
return tempLicense;
4255
}
4356

4457
/// <summary>
@@ -47,10 +60,10 @@ public static List<License> Get()
4760
/// <param name="License">License to remove</param>
4861
public static void Remove(License License)
4962
{
50-
Licenses.Remove(License);
63+
string key = string.Format("{0}|{1}", License.Product, License.ProductVersion);
64+
License tempLicense;
65+
Licenses.TryRemove(key, out tempLicense);
5166
}
5267
#endregion Default methods
53-
54-
5568
}
5669
}

library/PSFramework/License/LicenseType.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ public enum LicenseType
1111
/// <summary>
1212
/// No special type is present
1313
/// </summary>
14-
Free = 0,
14+
Free = 1,
1515

1616
/// <summary>
1717
/// The license is for a commercial product. This means you have to acquire use permission, such as software licenses, user CALs et al.
1818
/// </summary>
19-
Commercial = 1,
19+
Commercial = 2,
2020

2121
/// <summary>
2222
/// Reusing this product requires no attribution. Just use it and call it your own.
2323
/// </summary>
24-
NoAttribution = 2,
24+
NoAttribution = 4,
2525

2626
/// <summary>
2727
/// This product may be used, but must not be modified in any way.
2828
/// </summary>
29-
NoModify = 4,
29+
NoModify = 8,
3030
}
3131
}

0 commit comments

Comments
 (0)