Skip to content

Commit 2e27554

Browse files
committed
Merge pull request #177 from PowerShell/AvoidUsingDeprecatedManifestFields
Avoid using deprecated manifest fields
2 parents b8d4d6b + a3ec054 commit 2e27554

7 files changed

+311
-3
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
//
2+
// Copyright (c) Microsoft Corporation.
3+
//
4+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10+
// THE SOFTWARE.
11+
//
12+
13+
using System;
14+
using System.Collections.Generic;
15+
using System.Management.Automation.Language;
16+
using System.Management.Automation;
17+
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
18+
using System.ComponentModel.Composition;
19+
using System.Globalization;
20+
21+
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules
22+
{
23+
/// <summary>
24+
/// AvoidUsingDeprecatedManifestFields: Run Test Module Manifest to check that no deprecated fields are being used.
25+
/// </summary>
26+
[Export(typeof(IScriptRule))]
27+
public class AvoidUsingDeprecatedManifestFields : IScriptRule
28+
{
29+
/// <summary>
30+
/// AnalyzeScript: Run Test Module Manifest to check that no deprecated fields are being used.
31+
/// </summary>
32+
/// <param name="ast">The script's ast</param>
33+
/// <param name="fileName">The script's file name</param>
34+
/// <returns>A List of diagnostic results of this rule</returns>
35+
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
36+
{
37+
if (ast == null)
38+
{
39+
throw new ArgumentNullException(Strings.NullAstErrorMessage);
40+
}
41+
42+
if (String.Equals(System.IO.Path.GetExtension(fileName), ".psd1", StringComparison.OrdinalIgnoreCase))
43+
{
44+
var ps = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace);
45+
IEnumerable<PSObject> result = null;
46+
try
47+
{
48+
ps.AddCommand("Test-ModuleManifest");
49+
ps.AddParameter("Path", fileName);
50+
51+
// Suppress warnings emitted during the execution of Test-ModuleManifest
52+
// ModuleManifest rule must catch any violations (warnings/errors) and generate DiagnosticRecord(s)
53+
ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
54+
ps.AddParameter("WarningVariable", "Message");
55+
ps.AddScript("$Message");
56+
result = ps.Invoke();
57+
58+
}
59+
catch
60+
{}
61+
62+
if (result != null)
63+
{
64+
foreach (var warning in result)
65+
{
66+
if (warning.BaseObject != null)
67+
{
68+
yield return
69+
new DiagnosticRecord(
70+
String.Format(CultureInfo.CurrentCulture, warning.BaseObject.ToString()), ast.Extent,
71+
GetName(), DiagnosticSeverity.Warning, fileName);
72+
}
73+
}
74+
}
75+
76+
}
77+
78+
}
79+
80+
/// <summary>
81+
/// GetName: Retrieves the name of this rule.
82+
/// </summary>
83+
/// <returns>The name of this rule</returns>
84+
public string GetName()
85+
{
86+
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingDeprecatedManifestFieldsName);
87+
}
88+
89+
/// <summary>
90+
/// GetCommonName: Retrieves the common name of this rule.
91+
/// </summary>
92+
/// <returns>The common name of this rule</returns>
93+
public string GetCommonName()
94+
{
95+
return String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingDeprecatedManifestFieldsCommonName);
96+
}
97+
98+
/// <summary>
99+
/// GetDescription: Retrieves the description of this rule.
100+
/// </summary>
101+
/// <returns>The description of this rule</returns>
102+
public string GetDescription()
103+
{
104+
return String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingDeprecatedManifestFieldsDescription);
105+
}
106+
107+
/// <summary>
108+
/// Method: Retrieves the type of the rule: builtin, managed or module.
109+
/// </summary>
110+
public SourceType GetSourceType()
111+
{
112+
return SourceType.Builtin;
113+
}
114+
115+
/// <summary>
116+
/// GetSeverity: Retrieves the severity of the rule: error, warning of information.
117+
/// </summary>
118+
/// <returns></returns>
119+
public RuleSeverity GetSeverity()
120+
{
121+
return RuleSeverity.Warning;
122+
}
123+
124+
/// <summary>
125+
/// Method: Retrieves the module/assembly name the rule is from.
126+
/// </summary>
127+
public string GetSourceName()
128+
{
129+
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
130+
}
131+
}
132+
}

Rules/ScriptAnalyzerBuiltinRules.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<Compile Include="AvoidReservedParams.cs" />
6060
<Compile Include="AvoidShouldContinueWithoutForce.cs" />
6161
<Compile Include="AvoidTrapStatement.cs" />
62+
<Compile Include="AvoidUsingDeprecatedManifestFields.cs" />
6263
<Compile Include="ProvideDefaultParameterValue.cs" />
6364
<Compile Include="AvoidUninitializedVariable.cs" />
6465
<Compile Include="AvoidUsernameAndPasswordParams.cs" />

Rules/Strings.Designer.cs

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

Rules/Strings.resx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,4 +726,13 @@
726726
<data name="ProvideDefaultParameterValueName" xml:space="preserve">
727727
<value>ProvideDefaultParameterValue</value>
728728
</data>
729+
<data name="AvoidUsingDeprecatedManifestFieldsCommonName" xml:space="preserve">
730+
<value>Avoid Using Deprecated Manifest Fields</value>
731+
</data>
732+
<data name="AvoidUsingDeprecatedManifestFieldsDescription" xml:space="preserve">
733+
<value>"ModuleToProcess" is obsolete in the latest PowerShell version. Please update with the latest field "RootModule" in manifest files to avoid PowerShell version inconsistency.</value>
734+
</data>
735+
<data name="AvoidUsingDeprecatedManifestFieldsName" xml:space="preserve">
736+
<value>AvoidUsingDeprecatedManifestFields</value>
737+
</data>
729738
</root>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Import-Module PSScriptAnalyzer
2+
$violationName = "PSAvoidUsingDeprecatedManifestFields"
3+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
4+
$violations = Invoke-ScriptAnalyzer $directory\TestBadModule\TestDeprecatedManifestFields.psd1 | Where-Object {$_.RuleName -eq $violationName}
5+
$noViolations = Invoke-ScriptAnalyzer $directory\TestGoodModule\TestGoodModule.psd1 | Where-Object {$_.RuleName -eq $violationName}
6+
7+
Describe "AvoidUsingDeprecatedManifestFields" {
8+
Context "When there are violations" {
9+
It "has 1 violations" {
10+
$violations.Count | Should Be 1
11+
}
12+
}
13+
14+
Context "When there are no violations" {
15+
It "returns no violations" {
16+
$noViolations.Count | Should Be 0
17+
}
18+
}
19+
}
38 Bytes
Binary file not shown.

Tests/Rules/TestBadModule/TestDeprecatedManifestFields.psd1

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#
2+
# Module manifest for module 'Deprecated Module manifest fields"
3+
#
4+
# Generated by: Microsoft PowerShell Team
5+
#
6+
# Generated on: 5/18/2015
7+
#
8+
9+
@{
10+
11+
# Script module or binary module file associated with this manifest.
12+
ModuleToProcess ='psscriptanalyzer'
13+
14+
# Version number of this module.
15+
ModuleVersion = '1.0'
16+
17+
# ID used to uniquely identify this module
18+
GUID = 'a9f79c02-4503-4300-a022-5e8c01f3449f'
19+
20+
# Author of this module
21+
Author = ''
22+
23+
# Company or vendor of this module
24+
CompanyName = ''
25+
26+
# Copyright statement for this module
27+
Copyright = '(c) 2015 Microsoft. All rights reserved.'
28+
29+
# Description of the functionality provided by this module
30+
# Description = ''
31+
32+
# Minimum version of the Windows PowerShell engine required by this module
33+
# PowerShellVersion = ''
34+
35+
# Name of the Windows PowerShell host required by this module
36+
# PowerShellHostName = ''
37+
38+
# Minimum version of the Windows PowerShell host required by this module
39+
# PowerShellHostVersion = ''
40+
41+
# Minimum version of Microsoft .NET Framework required by this module
42+
# DotNetFrameworkVersion = ''
43+
44+
# Minimum version of the common language runtime (CLR) required by this module
45+
# CLRVersion = ''
46+
47+
# Processor architecture (None, X86, Amd64) required by this module
48+
# ProcessorArchitecture = ''
49+
50+
# Modules that must be imported into the global environment prior to importing this module
51+
# RequiredModules = @()
52+
53+
# Assemblies that must be loaded prior to importing this module
54+
# RequiredAssemblies = @()
55+
56+
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
57+
# ScriptsToProcess = @()
58+
59+
# Type files (.ps1xml) to be loaded when importing this module
60+
# TypesToProcess = @()
61+
62+
# Format files (.ps1xml) to be loaded when importing this module
63+
# FormatsToProcess = @()
64+
65+
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
66+
# NestedModules = @()
67+
68+
# Functions to export from this module
69+
FunctionsToExport = '*'
70+
71+
# Cmdlets to export from this module
72+
CmdletsToExport = '*'
73+
74+
# Variables to export from this module
75+
VariablesToExport = '*'
76+
77+
# Aliases to export from this module
78+
AliasesToExport = '*'
79+
80+
# DSC resources to export from this module
81+
# DscResourcesToExport = @()
82+
83+
# List of all modules packaged with this module
84+
# ModuleList = @()
85+
86+
# List of all files packaged with this module
87+
# FileList = @()
88+
89+
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable
90+
#with additional module metadata used by PowerShell.
91+
PrivateData = @{
92+
93+
PSData = @{
94+
95+
# Tags applied to this module. These help with module discovery in online galleries.
96+
# Tags = @()
97+
98+
# A URL to the license for this module.
99+
# LicenseUri = ''
100+
101+
# A URL to the main website for this project.
102+
# ProjectUri = ''
103+
104+
# A URL to an icon representing this module.
105+
# IconUri = ''
106+
107+
# ReleaseNotes of this module
108+
# ReleaseNotes = ''
109+
110+
} # End of PSData hashtable
111+
112+
} # End of PrivateData hashtable
113+
114+
# HelpInfo URI of this module
115+
# HelpInfoURI = ''
116+
117+
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
118+
# DefaultCommandPrefix = ''
119+
120+
}

0 commit comments

Comments
 (0)