Skip to content

Commit 292a560

Browse files
committed
Updated commands to use AutocompleteSet attribute.
1 parent 2262315 commit 292a560

File tree

19 files changed

+65
-192
lines changed

19 files changed

+65
-192
lines changed

Cognifide.PowerShell/Commandlets/BaseCommand.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@
44
using System.Diagnostics.CodeAnalysis;
55
using System.Linq;
66
using System.Management.Automation;
7-
using System.Reflection;
8-
using System.Threading;
97
using System.Web;
108
using Cognifide.PowerShell.Core.Extensions;
119
using Cognifide.PowerShell.Core.Host;
1210
using Cognifide.PowerShell.Core.Provider;
1311
using Cognifide.PowerShell.Core.Utility;
1412
using Sitecore;
1513
using Sitecore.Configuration;
16-
using Sitecore.ContentSearch;
1714
using Sitecore.Data;
1815
using Sitecore.Data.Items;
1916
using Sitecore.Diagnostics;
@@ -24,22 +21,6 @@ namespace Cognifide.PowerShell.Commandlets
2421
{
2522
public class BaseCommand : PSCmdlet, IDynamicParameters
2623
{
27-
protected static ManualResetEvent _reentrancyLock = new ManualResetEvent(false);
28-
29-
private static readonly FieldInfo ValidValues = typeof(ValidateSetAttribute).GetField("validValues",
30-
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
31-
32-
public void SetValidationSetValues(string parameterName, IEnumerable<string> values)
33-
{
34-
if (!MyInvocation.MyCommand.Parameters.ContainsKey(parameterName)) return;
35-
36-
var attribute = MyInvocation.MyCommand.Parameters[parameterName].Attributes.FirstOrDefault(each => each is ValidateSetAttribute) as ValidateSetAttribute;
37-
if (attribute != null)
38-
{
39-
ValidValues.SetValue(attribute, values.ToArray());
40-
}
41-
}
42-
4324
private readonly RuntimeDefinedParameterDictionary parameters;
4425

4526
public BaseCommand()
Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
11
using System.Linq;
22
using System.Management.Automation;
3+
using Cognifide.PowerShell.Core.Validation;
34
using Sitecore.ContentSearch;
45

56
namespace Cognifide.PowerShell.Commandlets
67
{
78
public class BaseIndexCommand : BaseCommand
89
{
9-
private readonly string[] indexes = ContentSearchManager.Indexes.Select(i => i.Name).ToArray();
10+
public static readonly string[] Indexes = ContentSearchManager.Indexes.Select(i => i.Name).ToArray();
1011

11-
[ValidateSet("*")]
12+
[AutocompleteSet("Indexes")]
1213
[Parameter(ValueFromPipelineByPropertyName = true, ValueFromPipeline = true, Position = 0, ParameterSetName = "Name")]
1314
public string Name { get; set; }
14-
15-
public override object GetDynamicParameters()
16-
{
17-
if (!_reentrancyLock.WaitOne(0))
18-
{
19-
_reentrancyLock.Set();
20-
21-
SetValidationSetValues("Name", indexes);
22-
23-
_reentrancyLock.Reset();
24-
}
25-
26-
return base.GetDynamicParameters();
27-
}
2815
}
2916
}
Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
using System.Data;
22
using System.Management.Automation;
33
using Cognifide.PowerShell.Commandlets.Interactive;
4+
using Cognifide.PowerShell.Core.Validation;
45
using Sitecore.Configuration;
5-
using Sitecore.Data;
66
using Sitecore.Data.Items;
77

88
namespace Cognifide.PowerShell.Commandlets
99
{
1010
public abstract class BaseLanguageAgnosticItemCommand : BaseShellCommand
1111
{
12-
private static readonly string[] databases = Factory.GetDatabaseNames();
12+
private static readonly string[] Databases = Factory.GetDatabaseNames();
1313

1414
[Parameter(ValueFromPipeline = true, ValueFromPipelineByPropertyName = true,
1515
ParameterSetName = "Item from Pipeline", Mandatory = true, Position = 0)]
@@ -22,13 +22,12 @@ public abstract class BaseLanguageAgnosticItemCommand : BaseShellCommand
2222
[Parameter(ParameterSetName = "Item from ID", Mandatory = true)]
2323
public virtual string Id { get; set; }
2424

25-
[ValidateSet("*")]
25+
[AutocompleteSet("Databases")]
2626
[Parameter(ParameterSetName = "Item from ID")]
2727
public virtual string Database { get; set; }
2828

2929
protected override void ProcessRecord()
3030
{
31-
Factory.GetDatabase(Database);
3231
var sourceItem = FindItemFromParameters(Item, Path, Id, null, Database);
3332

3433
if (sourceItem == null)
@@ -45,20 +44,5 @@ protected override void ProcessRecord()
4544
}
4645

4746
protected abstract void ProcessItem(Item item);
48-
49-
public override object GetDynamicParameters()
50-
{
51-
if (!_reentrancyLock.WaitOne(0))
52-
{
53-
_reentrancyLock.Set();
54-
55-
SetValidationSetValues("Database", databases);
56-
57-
_reentrancyLock.Reset();
58-
}
59-
60-
return base.GetDynamicParameters();
61-
}
62-
6347
}
6448
}

Cognifide.PowerShell/Commandlets/Data/GetItemReferenceCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Linq;
22
using System.Management.Automation;
33
using Cognifide.PowerShell.Core.Extensions;
4+
using Cognifide.PowerShell.Core.Validation;
45
using Sitecore;
56
using Sitecore.Data;
67
using Sitecore.Data.Items;
@@ -33,7 +34,7 @@ public class GetItemReferenceCommand : BaseItemCommand
3334
[Parameter(ParameterSetName = "Item from ID, return ItemLink", Mandatory = true)]
3435
public override string Id { get; set; }
3536

36-
[ValidateSet("*")]
37+
[AutocompleteSet("Databases")]
3738
[Parameter(ParameterSetName = "Item from ID, return Item")]
3839
[Parameter(ParameterSetName = "Item from ID, return ItemLink")]
3940
public override string Database { get; set; }

Cognifide.PowerShell/Commandlets/Data/GetItemReferrerCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Linq;
22
using System.Management.Automation;
33
using Cognifide.PowerShell.Core.Extensions;
4+
using Cognifide.PowerShell.Core.Validation;
45
using Sitecore;
56
using Sitecore.Data;
67
using Sitecore.Data.Items;
@@ -34,7 +35,7 @@ public class GetItemReferrerCommand : BaseItemCommand
3435
[Parameter(ParameterSetName = "Item from ID, return ItemLink", Mandatory = true)]
3536
public override string Id { get; set; }
3637

37-
[ValidateSet("*")]
38+
[AutocompleteSet("Databases")]
3839
[Parameter(ParameterSetName = "Item from ID, return Item")]
3940
[Parameter(ParameterSetName = "Item from ID, return ItemLink")]
4041
public override string Database { get; set; }

Cognifide.PowerShell/Commandlets/Data/TestRuleCommand.cs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Management.Automation;
33
using Cognifide.PowerShell.Core.Extensions;
44
using Cognifide.PowerShell.Core.Settings;
5+
using Cognifide.PowerShell.Core.Validation;
56
using Sitecore.Configuration;
67
using Sitecore.Data;
78
using Sitecore.Data.Items;
@@ -13,7 +14,7 @@ namespace Cognifide.PowerShell.Commandlets.Data
1314
[OutputType(typeof (bool))]
1415
public class TestRuleCommand : BaseCommand
1516
{
16-
private static readonly string[] databases = Factory.GetDatabaseNames();
17+
private static readonly string[] Databases = Factory.GetDatabaseNames();
1718
private RuleList<RuleContext> rules;
1819

1920
[Parameter]
@@ -22,7 +23,7 @@ public class TestRuleCommand : BaseCommand
2223
[Parameter]
2324
public PSObject InputObject { get; set; }
2425

25-
[ValidateSet("*")]
26+
[AutocompleteSet("Databases")]
2627
[Parameter]
2728
public string RuleDatabase { get; set; }
2829

@@ -53,19 +54,5 @@ protected override void ProcessRecord()
5354

5455
WriteObject(!rules.Rules.Any() || rules.Rules.Any(rule => rule.Evaluate(ruleContext)));
5556
}
56-
57-
public override object GetDynamicParameters()
58-
{
59-
if (!_reentrancyLock.WaitOne(0))
60-
{
61-
_reentrancyLock.Set();
62-
63-
SetValidationSetValues("RuleDatabase", databases);
64-
65-
_reentrancyLock.Reset();
66-
}
67-
68-
return base.GetDynamicParameters();
69-
}
7057
}
7158
}

Cognifide.PowerShell/Commandlets/Interactive/ShowFieldEditorCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Linq;
22
using System.Management.Automation;
33
using Cognifide.PowerShell.Commandlets.Interactive.Messages;
4+
using Cognifide.PowerShell.Core.Validation;
45
using Sitecore;
56
using Sitecore.Data;
67
using Sitecore.Data.Items;
@@ -56,7 +57,7 @@ public ShowFieldEditorCommand()
5657
[Parameter(ParameterSetName = "Item from ID, Named Section", Mandatory = true)]
5758
public override string Id { get; set; }
5859

59-
[ValidateSet("*")]
60+
[AutocompleteSet("Databases")]
6061
[Parameter(ParameterSetName = "Item from ID, Preserve Sections")]
6162
[Parameter(ParameterSetName = "Item from ID, Named Section")]
6263
public override string Database { get; set; }

Cognifide.PowerShell/Commandlets/Modules/GetSpeModule.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.Management.Automation;
33
using Cognifide.PowerShell.Core.Modules;
4+
using Cognifide.PowerShell.Core.Validation;
45
using Sitecore.Data;
56
using Sitecore.Data.Items;
67

@@ -24,7 +25,7 @@ public class GetSpeModule : BaseItemCommand
2425
[Parameter(ParameterSetName = "Module from ID", Mandatory = true)]
2526
public override string Id { get; set; }
2627

27-
[ValidateSet("*")]
28+
[AutocompleteSet("Databases")]
2829
[Parameter(ParameterSetName = "Module from ID", ValueFromPipeline = true, Mandatory = true)]
2930
[Parameter(ParameterSetName = "Module from Database", ValueFromPipeline = true, Mandatory = true,
3031
ValueFromPipelineByPropertyName = true)]

Cognifide.PowerShell/Commandlets/Modules/GetSpeModuleFeature.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using System.Management.Automation;
44
using Cognifide.PowerShell.Core.Modules;
5+
using Cognifide.PowerShell.Core.Validation;
56
using Sitecore.Data.Items;
67

78
namespace Cognifide.PowerShell.Commandlets.Modules
@@ -10,14 +11,16 @@ namespace Cognifide.PowerShell.Commandlets.Modules
1011
[OutputType(typeof (Item), ParameterSetName = new[] {"By Feature Name", "Module from Pipeline"})]
1112
public class GetSpeModuleFeatureRoot : BaseCommand
1213
{
14+
public static readonly string[] Features = IntegrationPoints.Libraries.Keys.ToArray();
15+
1316
[Parameter(ValueFromPipeline = true, ValueFromPipelineByPropertyName = true,
1417
ParameterSetName = "Module from Pipeline")]
1518
public Module Module { get; set; }
1619

1720
[Parameter]
1821
public SwitchParameter ReturnPath { get; set; }
1922

20-
[ValidateSet("*")]
23+
[AutocompleteSet("Features")]
2124
[Parameter(Mandatory = true, Position = 0)]
2225
public string Feature { get; set; }
2326

@@ -48,19 +51,5 @@ protected override void ProcessRecord()
4851
}
4952
}
5053
}
51-
52-
public override object GetDynamicParameters()
53-
{
54-
if (!_reentrancyLock.WaitOne(0))
55-
{
56-
_reentrancyLock.Set();
57-
58-
SetValidationSetValues("Feature", IntegrationPoints.Libraries.Keys.ToArray());
59-
60-
_reentrancyLock.Reset();
61-
}
62-
63-
return base.GetDynamicParameters();
64-
}
6554
}
6655
}

Cognifide.PowerShell/Commandlets/Presentation/BaseRenderingCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Linq;
55
using System.Management.Automation;
6+
using Cognifide.PowerShell.Core.Validation;
67
using Sitecore.Data;
78
using Sitecore.Data.Items;
89
using Sitecore.Layouts;
@@ -34,7 +35,7 @@ public abstract class BaseRenderingCommand : BaseLayoutCommand
3435
[Parameter(ParameterSetName = "Rendering by unique ID, Item from ID")]
3536
public override string Id { get; set; }
3637

37-
[ValidateSet("*")]
38+
[AutocompleteSet("Databases")]
3839
[Parameter(ParameterSetName = "Rendering by filter, Item from ID")]
3940
[Parameter(ParameterSetName = "Rendering by instance, Item from ID")]
4041
[Parameter(ParameterSetName = "Rendering by unique ID, Item from ID")]

0 commit comments

Comments
 (0)