Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Feb 4, 2019. It is now read-only.
/PashPublic archive

Commite0bde1e

Browse files
committed
REFACTOR: Fix duplicated code in Set-Variable/Get-Variable commands
Introduce a VariableCommandBase class which is part of the officialPowerShell API. This class contains the wildcard filtering logic forInclude and Exclude parameters.
1 parent7fbacaa commite0bde1e

File tree

4 files changed

+69
-69
lines changed

4 files changed

+69
-69
lines changed

‎Source/Microsoft.PowerShell.Commands.Utility/GetVariableCommand.cs

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@ namespace Microsoft.PowerShell.Commands
88
{
99
[Cmdlet("Get","Variable")]
1010
[OutputType(typeof(PSVariable))]
11-
publicclassGetVariableCommand:PSCmdlet
11+
publicclassGetVariableCommand:VariableCommandBase
1212
{
1313
[Parameter]
14-
publicstring[]Exclude{get;set;}
14+
publicstring[]Exclude
15+
{
16+
get{returnExcludeFilters;}
17+
set{ExcludeFilters=value;}
18+
}
1519

1620
[Parameter]
17-
publicstring[]Include{get;set;}
21+
publicstring[]Include
22+
{
23+
get{returnIncludeFilters;}
24+
set{IncludeFilters=value;}
25+
}
1826

1927
[Parameter(Position=0,ValueFromPipeline=true,ValueFromPipelineByPropertyName=true),ValidateNotNull]
2028
publicstring[]Name{get;set;}
@@ -38,7 +46,7 @@ protected override void ProcessRecord()
3846
.Where(v=>v.Visibility==SessionStateEntryVisibility.Public)
3947
.OrderBy(v=>v.Name))
4048
{
41-
if(!IsExcluded(variable))
49+
if(!IsExcluded(variable.Name))
4250
{
4351
WriteVariable(variable);
4452
}
@@ -114,37 +122,6 @@ private IEnumerable<PSVariable> GetAllVariables()
114122
returnSessionState.PSVariable.GetAllAtScope(Scope).Values;
115123
}
116124

117-
privateboolIsExcluded(PSVariablevariable)
118-
{
119-
if(Include!=null)
120-
{
121-
if(!Include.Any(name=>IsMatch(name,variable)))
122-
{
123-
returntrue;
124-
}
125-
}
126-
127-
if(Exclude!=null)
128-
{
129-
if(Exclude.Any(name=>IsMatch(name,variable)))
130-
{
131-
returntrue;
132-
}
133-
}
134-
135-
returnfalse;
136-
}
137-
138-
staticboolIsMatch(stringname,PSVariablevariable)
139-
{
140-
if(WildcardPattern.ContainsWildcardCharacters(name))
141-
{
142-
varwildcard=newWildcardPattern(name,WildcardOptions.IgnoreCase);
143-
returnwildcard.IsMatch(variable.Name);
144-
}
145-
returnvariable.Name.Equals(name,StringComparison.CurrentCultureIgnoreCase);
146-
}
147-
148125
privatevoidWriteVariable(PSVariablevariable)
149126
{
150127
if(ValueOnly.ToBool())

‎Source/Microsoft.PowerShell.Commands.Utility/Microsoft.PowerShell.Commands.Utility.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
<CompileInclude="StyleInfo.cs" />
105105
<CompileInclude="TeeObjectCommand.cs" />
106106
<CompileInclude="PropertyEqualityComparer.cs" />
107+
<CompileInclude="VariableCommandBase.cs" />
107108
<CompileInclude="WhereObjectCommand.cs" />
108109
<CompileInclude="WriteDebugCommand.cs" />
109110
<CompileInclude="WriteErrorCommand.cs" />

‎Source/Microsoft.PowerShell.Commands.Utility/SetVariableCommand.cs

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,27 @@ namespace Microsoft.PowerShell.Commands
99
{
1010
[Cmdlet("Set","Variable",SupportsShouldProcess=true)]
1111
[OutputType(typeof(PSVariable))]
12-
publicsealedclassSetVariableCommand:PSCmdlet
12+
publicsealedclassSetVariableCommand:VariableCommandBase
1313
{
1414
[Parameter]
1515
publicstringDescription{get;set;}
1616

1717
[Parameter]
18-
publicstring[]Exclude{get;set;}
18+
publicstring[]Exclude
19+
{
20+
get{returnExcludeFilters;}
21+
set{ExcludeFilters=value;}
22+
}
1923

2024
[Parameter]
2125
publicSwitchParameterForce{get;set;}
2226

2327
[Parameter]
24-
publicstring[]Include{get;set;}
28+
publicstring[]Include
29+
{
30+
get{returnIncludeFilters;}
31+
set{IncludeFilters=value;}
32+
}
2533

2634
[Parameter(Position=0,ValueFromPipelineByPropertyName=true,Mandatory=true)]
2735
publicstring[]Name{get;set;}
@@ -164,36 +172,5 @@ private void WriteError(SessionStateException ex, string name)
164172

165173
WriteError(error);
166174
}
167-
168-
privateboolIsExcluded(stringvariableName)
169-
{
170-
if(Include!=null)
171-
{
172-
if(!Include.Any(name=>IsMatch(name,variableName)))
173-
{
174-
returntrue;
175-
}
176-
}
177-
178-
if(Exclude!=null)
179-
{
180-
if(Exclude.Any(name=>IsMatch(name,variableName)))
181-
{
182-
returntrue;
183-
}
184-
}
185-
186-
returnfalse;
187-
}
188-
189-
staticboolIsMatch(stringname,stringvariableName)
190-
{
191-
if(WildcardPattern.ContainsWildcardCharacters(name))
192-
{
193-
varwildcard=newWildcardPattern(name,WildcardOptions.IgnoreCase);
194-
returnwildcard.IsMatch(variableName);
195-
}
196-
returnvariableName.Equals(name,StringComparison.CurrentCultureIgnoreCase);
197-
}
198175
}
199176
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (C) Pash Contributors. License: GPL/BSD. See https://github.com/Pash-Project/Pash/
2+
usingSystem;
3+
usingSystem.Collections.Generic;
4+
usingSystem.Linq;
5+
usingSystem.Management.Automation;
6+
7+
namespaceMicrosoft.PowerShell.Commands
8+
{
9+
publicabstractclassVariableCommandBase:PSCmdlet
10+
{
11+
protectedstring[]ExcludeFilters{get;set;}
12+
protectedstring[]IncludeFilters{get;set;}
13+
14+
protectedinternalboolIsExcluded(stringvariableName)
15+
{
16+
if(IncludeFilters!=null)
17+
{
18+
if(!IncludeFilters.Any(name=>IsMatch(name,variableName)))
19+
{
20+
returntrue;
21+
}
22+
}
23+
24+
if(ExcludeFilters!=null)
25+
{
26+
if(ExcludeFilters.Any(name=>IsMatch(name,variableName)))
27+
{
28+
returntrue;
29+
}
30+
}
31+
32+
returnfalse;
33+
}
34+
35+
staticboolIsMatch(stringname,stringvariableName)
36+
{
37+
if(WildcardPattern.ContainsWildcardCharacters(name))
38+
{
39+
varwildcard=newWildcardPattern(name,WildcardOptions.IgnoreCase);
40+
returnwildcard.IsMatch(variableName);
41+
}
42+
returnvariableName.Equals(name,StringComparison.CurrentCultureIgnoreCase);
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp