Skip to content

Commit f5f66db

Browse files
committed
Issue #439 + improved documentation for various cmdlets.
1 parent 3472cd0 commit f5f66db

24 files changed

+2374
-2146
lines changed

Cognifide.PowerShell/Commandlets/BaseCommand.cs

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,11 @@ protected virtual Database FindDatabaseFromParameters(Item item, string path, Da
186186
}
187187

188188
protected virtual Item FindItemFromParameters(Item item, string path, string id, Language language,
189-
Database database)
189+
string databaseName)
190190
{
191191
if (item == null)
192192
{
193+
Database database = Factory.GetDatabase(databaseName);
193194
if (!String.IsNullOrEmpty(id))
194195
{
195196
var currentDb = database ?? CurrentDatabase;
@@ -240,79 +241,6 @@ protected void WriteItem(Item item)
240241
}
241242
}
242243

243-
protected void AddDynamicParameter<T>(string name, params Attribute[] attributes)
244-
{
245-
// create a parameter of type T.
246-
var parameter = new RuntimeDefinedParameter
247-
{
248-
Name = name,
249-
ParameterType = typeof (T)
250-
};
251-
252-
if (attributes != null)
253-
{
254-
foreach (var attribute in attributes)
255-
{
256-
parameter.Attributes.Add(attribute);
257-
}
258-
}
259-
260-
parameters.Add(name, parameter);
261-
}
262-
263-
protected bool TryGetSwitchParameter(string name, out bool isPresent)
264-
{
265-
bool value;
266-
return TryGetSwitchParameter(name, out isPresent, out value);
267-
}
268-
269-
protected bool TryGetSwitchParameter(string name, out bool isPresent, out bool value)
270-
{
271-
RuntimeDefinedParameter parameter;
272-
273-
if (TryGetDynamicParameter(name, out parameter))
274-
{
275-
isPresent = parameter.IsSet;
276-
value = parameter.IsSet && (SwitchParameter) parameter.Value;
277-
return true;
278-
}
279-
280-
isPresent = false;
281-
value = false;
282-
return false;
283-
}
284-
285-
// get a parameter of type T.
286-
public bool TryGetParameter<T>(string name, out T value)
287-
{
288-
RuntimeDefinedParameter parameter;
289-
290-
if (TryGetDynamicParameter(name, out parameter))
291-
{
292-
value = (T) parameter.Value;
293-
return true;
294-
}
295-
296-
value = default(T);
297-
298-
return false;
299-
}
300-
301-
// try to get a dynamically added parameter
302-
internal bool TryGetDynamicParameter(string name, out RuntimeDefinedParameter value)
303-
{
304-
if (parameters.ContainsKey(name))
305-
{
306-
value = parameters[name];
307-
return true;
308-
}
309-
310-
// need to set this before leaving the method
311-
value = null;
312-
313-
return false;
314-
}
315-
316244
public virtual object GetDynamicParameters()
317245
{
318246
return parameters;

Cognifide.PowerShell/Commandlets/BaseLanguageAgnosticItemCommand.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
using System.Data;
22
using System.Management.Automation;
33
using Cognifide.PowerShell.Commandlets.Interactive;
4+
using Sitecore.Configuration;
45
using Sitecore.Data;
56
using Sitecore.Data.Items;
67

78
namespace Cognifide.PowerShell.Commandlets
89
{
910
public abstract class BaseLanguageAgnosticItemCommand : BaseShellCommand
1011
{
12+
private static readonly string[] databases = Factory.GetDatabaseNames();
13+
1114
[Parameter(ValueFromPipeline = true, ValueFromPipelineByPropertyName = true,
1215
ParameterSetName = "Item from Pipeline", Mandatory = true, Position = 0)]
1316
public virtual Item Item { get; set; }
@@ -19,11 +22,13 @@ public abstract class BaseLanguageAgnosticItemCommand : BaseShellCommand
1922
[Parameter(ParameterSetName = "Item from ID", Mandatory = true)]
2023
public virtual string Id { get; set; }
2124

25+
[ValidateSet("*")]
2226
[Parameter(ParameterSetName = "Item from ID")]
23-
public virtual Database Database { get; set; }
27+
public virtual string Database { get; set; }
2428

2529
protected override void ProcessRecord()
2630
{
31+
Factory.GetDatabase(Database);
2732
var sourceItem = FindItemFromParameters(Item, Path, Id, null, Database);
2833

2934
if (sourceItem == null)
@@ -40,5 +45,20 @@ protected override void ProcessRecord()
4045
}
4146

4247
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+
4363
}
4464
}

Cognifide.PowerShell/Commandlets/Data/GetItemReferenceCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ public class GetItemReferenceCommand : BaseItemCommand
3333
[Parameter(ParameterSetName = "Item from ID, return ItemLink", Mandatory = true)]
3434
public override string Id { get; set; }
3535

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

4041
[Alias("Languages")]
4142
[Parameter(ParameterSetName = "Item from Path, return Item")]

Cognifide.PowerShell/Commandlets/Data/GetItemReferrerCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ public class GetItemReferrerCommand : BaseItemCommand
3434
[Parameter(ParameterSetName = "Item from ID, return ItemLink", Mandatory = true)]
3535
public override string Id { get; set; }
3636

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

4142
[Alias("Languages")]
4243
[Parameter(ParameterSetName = "Item from Path, return Item")]

Cognifide.PowerShell/Commandlets/Data/Search/FindItemCommand.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Cognifide.PowerShell.Commandlets.Data.Search
1717
[OutputType(typeof(Item))]
1818
public class FindItemCommand : BaseCommand
1919
{
20-
private readonly string[] indexes = ContentSearchManager.Indexes.Select(i => i.Name).ToArray();
20+
private static readonly string[] indexes = ContentSearchManager.Indexes.Select(i => i.Name).ToArray();
2121

2222
[ValidateSet("*")]
2323
[Parameter(Mandatory = true, Position = 0)]
@@ -46,11 +46,8 @@ public class FindItemCommand : BaseCommand
4646

4747
protected override void EndProcessing()
4848
{
49-
string index;
50-
if (!TryGetParameter("Index", out index))
51-
{
52-
index = "sitecore_master_index";
53-
}
49+
string index = string.IsNullOrEmpty(Index) ? "sitecore_master_index" : Index;
50+
5451
using (var context = ContentSearchManager.GetIndex(index).CreateSearchContext())
5552
{
5653
// get all items in medialibrary

Cognifide.PowerShell/Commandlets/Data/TestRuleCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Cognifide.PowerShell.Commandlets.Data
1313
[OutputType(typeof (bool))]
1414
public class TestRuleCommand : BaseCommand
1515
{
16-
private readonly string[] databases = Factory.GetDatabaseNames();
16+
private static readonly string[] databases = Factory.GetDatabaseNames();
1717
private RuleList<RuleContext> rules;
1818

1919
[Parameter]
@@ -30,8 +30,8 @@ protected override void BeginProcessing()
3030
{
3131
Item currentItem = InputObject.BaseObject() as Item;
3232

33-
string ruleDatabaseName;
34-
if (!TryGetParameter("RuleDatabase", out ruleDatabaseName))
33+
string ruleDatabaseName = RuleDatabase;
34+
if (!string.IsNullOrEmpty(ruleDatabaseName))
3535
{
3636
ruleDatabaseName = currentItem != null
3737
? currentItem.Database.Name

Cognifide.PowerShell/Commandlets/Interactive/ShowFieldEditorCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ public ShowFieldEditorCommand()
5656
[Parameter(ParameterSetName = "Item from ID, Named Section", Mandatory = true)]
5757
public override string Id { get; set; }
5858

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

6364
[Alias("Languages")]
6465
[Parameter(ParameterSetName = "Item from Path, Preserve Sections")]

Cognifide.PowerShell/Commandlets/Modules/GetSpeModule.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ public class GetSpeModule : BaseItemCommand
2424
[Parameter(ParameterSetName = "Module from ID", Mandatory = true)]
2525
public override string Id { get; set; }
2626

27+
[ValidateSet("*")]
2728
[Parameter(ParameterSetName = "Module from ID", ValueFromPipeline = true, Mandatory = true)]
2829
[Parameter(ParameterSetName = "Module from Database", ValueFromPipeline = true, Mandatory = true,
2930
ValueFromPipelineByPropertyName = true)]
3031
[Parameter(ParameterSetName = "Module from Name", ValueFromPipeline = true)]
31-
public override Database Database { get; set; }
32+
public override string Database { get; set; }
3233

3334
[Parameter(ParameterSetName = "Module from Name", Mandatory = true)]
3435
public string Name { get; set; }
@@ -45,7 +46,7 @@ protected override void ProcessRecord()
4546

4647
if (databaseDefined)
4748
{
48-
modules = WildcardFilter(Database.Name, modules, m => m.Database);
49+
modules = WildcardFilter(Database, modules, m => m.Database);
4950
}
5051

5152
if (nameDefined)

Cognifide.PowerShell/Commandlets/Modules/GetSpeModuleFeature.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,28 @@ public class GetSpeModuleFeatureRoot : BaseCommand
2323

2424
protected override void ProcessRecord()
2525
{
26-
string feature;
27-
var featureDefined = TryGetParameter("Feature", out feature);
28-
if (Module != null)
26+
if (!string.IsNullOrEmpty(Feature))
2927
{
30-
if (ReturnPath)
31-
{
32-
WriteObject(Module.GetProviderFeaturePath(feature));
33-
}
34-
else
35-
{
36-
WriteItem(Module.GetFeatureRoot(feature));
37-
}
38-
return;
39-
}
40-
if (featureDefined)
41-
{
42-
if (!String.IsNullOrEmpty(feature))
28+
if (Module != null)
4329
{
4430
if (ReturnPath)
4531
{
46-
ModuleManager.Modules.ForEach(m => WriteObject(m.GetProviderFeaturePath(feature)));
32+
WriteObject(Module.GetProviderFeaturePath(Feature));
4733
}
4834
else
4935
{
50-
ModuleManager.GetFeatureRoots(feature).ForEach(WriteItem);
36+
WriteItem(Module.GetFeatureRoot(Feature));
5137
}
38+
return;
39+
}
40+
41+
if (ReturnPath)
42+
{
43+
ModuleManager.Modules.ForEach(m => WriteObject(m.GetProviderFeaturePath(Feature)));
44+
}
45+
else
46+
{
47+
ModuleManager.GetFeatureRoots(Feature).ForEach(WriteItem);
5248
}
5349
}
5450
}

Cognifide.PowerShell/Commandlets/Presentation/BaseRenderingCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ public abstract class BaseRenderingCommand : BaseLayoutCommand
3434
[Parameter(ParameterSetName = "Rendering by unique ID, Item from ID")]
3535
public override string Id { get; set; }
3636

37+
[ValidateSet("*")]
3738
[Parameter(ParameterSetName = "Rendering by filter, Item from ID")]
3839
[Parameter(ParameterSetName = "Rendering by instance, Item from ID")]
3940
[Parameter(ParameterSetName = "Rendering by unique ID, Item from ID")]
40-
public override Database Database { get; set; }
41+
public override string Database { get; set; }
4142

4243
[Parameter(ParameterSetName = "Rendering by filter, Item from Pipeline")]
4344
[Parameter(ParameterSetName = "Rendering by filter, Item from Path")]

0 commit comments

Comments
 (0)