Skip to content

Commit 2c830d8

Browse files
committed
Added a rule to check if packages are built in release configuration
1 parent 2d04c30 commit 2c830d8

File tree

7 files changed

+84
-32
lines changed

7 files changed

+84
-32
lines changed

src/NuGetPackageVerifier/CompositeRules/AdxVerificationCompositeRule.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class AdxVerificationCompositeRule : IPackageVerifierRule
1515
new AssemblyHasCommitHashAttributeRule(),
1616
new AssemblyHasCompanyAttributeRule(),
1717
new AssemblyHasCopyrightAttributeRule(),
18+
new AssemblyHasCorrectBuildConfigurationRule(),
1819
new AssemblyHasCorrectJsonNetVersionRule(),
1920
new AssemblyHasDocumentFileRule(),
2021
new AssemblyHasNeutralResourcesLanguageAttributeRule(),

src/NuGetPackageVerifier/CompositeRules/DefaultCompositeRule.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class DefaultCompositeRule : IPackageVerifierRule
1515
new AssemblyHasCommitHashAttributeRule(),
1616
new AssemblyHasCompanyAttributeRule(),
1717
new AssemblyHasCopyrightAttributeRule(),
18+
new AssemblyHasCorrectBuildConfigurationRule(),
1819
new AssemblyHasCorrectJsonNetVersionRule(),
1920
new AssemblyHasDocumentFileRule(),
2021
new AssemblyHasDescriptionAttributeRule(),

src/NuGetPackageVerifier/IssueReport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class IssueReport
1010
public IssueReport(PackageVerifierIssue packageIssue, bool ignore, string ignoreJustification)
1111
{
1212
PackageIssue = packageIssue;
13-
IssueLevel = ignore ? LogLevel.Info : packageIssue.Level == MyPackageIssueLevel.Warning ? LogLevel.Warning : LogLevel.Error;
13+
IssueLevel = ignore ? LogLevel.Info : packageIssue.Level == PackageIssueLevel.Warning ? LogLevel.Warning : LogLevel.Error;
1414
IgnoreJustification = ignoreJustification;
1515
}
1616

src/NuGetPackageVerifier/PackageIssueFactory.cs

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static PackageVerifierIssue AssemblyMissingServicingAttribute(string asse
1616
string.Format(
1717
@"The managed assembly '{0}' in this package is missing the '[assembly: AssemblyMetadata(""Serviceable"", ""True"")]' attribute.",
1818
assemblyPath),
19-
MyPackageIssueLevel.Error);
19+
PackageIssueLevel.Error);
2020
}
2121

2222
public static PackageVerifierIssue AssemblyMissingHashAttribute(string assemblyPath)
@@ -27,7 +27,16 @@ public static PackageVerifierIssue AssemblyMissingHashAttribute(string assemblyP
2727
string.Format(
2828
@"The managed assembly '{0}' in this package is missing the '[assembly: AssemblyMetadata(""CommitHash"", ""<text>"")]' attribute.",
2929
assemblyPath),
30-
MyPackageIssueLevel.Error);
30+
PackageIssueLevel.Error);
31+
}
32+
33+
public static PackageVerifierIssue AssemblyHasIncorrectBuildConfiguration(string assemblyPath)
34+
{
35+
return new PackageVerifierIssue(
36+
"WRONG_BUILD_CONFIGURATION",
37+
assemblyPath,
38+
string.Format("The assembly '{0}' was not built using 'Release' configuration.", assemblyPath),
39+
PackageIssueLevel.Error);
3140
}
3241

3342
public static PackageVerifierIssue AssemblyMissingNeutralResourcesLanguageAttribute(string assemblyPath)
@@ -38,7 +47,7 @@ public static PackageVerifierIssue AssemblyMissingNeutralResourcesLanguageAttrib
3847
string.Format(
3948
@"The managed assembly '{0}' in this package is missing the '[assembly: NeutralResourcesLanguage(""en-us"")]' attribute.",
4049
assemblyPath),
41-
MyPackageIssueLevel.Error);
50+
PackageIssueLevel.Error);
4251
}
4352

4453
public static PackageVerifierIssue AssemblyMissingCopyrightAttribute(string assemblyPath)
@@ -49,7 +58,7 @@ public static PackageVerifierIssue AssemblyMissingCopyrightAttribute(string asse
4958
string.Format(
5059
@"The managed assembly '{0}' in this package is missing the 'AssemblyCopyright' attribute.",
5160
assemblyPath),
52-
MyPackageIssueLevel.Error);
61+
PackageIssueLevel.Error);
5362
}
5463

5564
public static PackageVerifierIssue AssemblyMissingCompanyAttribute(string assemblyPath)
@@ -60,7 +69,7 @@ public static PackageVerifierIssue AssemblyMissingCompanyAttribute(string assemb
6069
string.Format(
6170
@"The managed assembly '{0}' in this package is missing the 'AssemblyCompany' attribute.",
6271
assemblyPath),
63-
MyPackageIssueLevel.Error);
72+
PackageIssueLevel.Error);
6473
}
6574

6675
public static PackageVerifierIssue AssemblyMissingProductAttribute(string assemblyPath)
@@ -71,7 +80,7 @@ public static PackageVerifierIssue AssemblyMissingProductAttribute(string assemb
7180
string.Format(
7281
@"The managed assembly '{0}' in this package is missing the 'AssemblyProduct' attribute.",
7382
assemblyPath),
74-
MyPackageIssueLevel.Error);
83+
PackageIssueLevel.Error);
7584
}
7685

7786
public static PackageVerifierIssue AssemblyMissingDescriptionAttribute(string assemblyPath)
@@ -82,7 +91,7 @@ public static PackageVerifierIssue AssemblyMissingDescriptionAttribute(string as
8291
string.Format(
8392
@"The managed assembly '{0}' in this package is missing the 'AssemblyDescription' attribute.",
8493
assemblyPath),
85-
MyPackageIssueLevel.Error);
94+
PackageIssueLevel.Error);
8695
}
8796

8897
public static PackageVerifierIssue AssemblyMissingFileVersionAttribute(string assemblyPath)
@@ -97,99 +106,99 @@ public static PackageVerifierIssue AssemblyMissingInformationalVersionAttribute(
97106

98107
private static PackageVerifierIssue AssemblyMissingVersionAttributeCore(string issueId, string assemblyPath, string attributeName)
99108
{
100-
return new PackageVerifierIssue(issueId, assemblyPath, string.Format("The managed assembly '{0}' in this package is missing the '{1}' attribute.", assemblyPath, attributeName), MyPackageIssueLevel.Error);
109+
return new PackageVerifierIssue(issueId, assemblyPath, string.Format("The managed assembly '{0}' in this package is missing the '{1}' attribute.", assemblyPath, attributeName), PackageIssueLevel.Error);
101110
}
102111

103112
public static PackageVerifierIssue AssemblyNotStrongNameSigned(string assemblyPath, int hResult)
104113
{
105114
// TODO: Translate common HRESULTS http://blogs.msdn.com/b/yizhang/
106-
return new PackageVerifierIssue("SIGN_STRONGNAME", assemblyPath, string.Format("The managed assembly '{0}' in this package is either not signed or is delay signed. HRESULT=0x{1:X}", assemblyPath, hResult), MyPackageIssueLevel.Error);
115+
return new PackageVerifierIssue("SIGN_STRONGNAME", assemblyPath, string.Format("The managed assembly '{0}' in this package is either not signed or is delay signed. HRESULT=0x{1:X}", assemblyPath, hResult), PackageIssueLevel.Error);
107116
}
108117

109118
public static PackageVerifierIssue AssemblyHasWrongPublicKeyToken(string assemblyPath, string expectedToken)
110119
{
111-
return new PackageVerifierIssue("WRONG_PUBLICKEYTOKEN", assemblyPath, string.Format("The managed assembly '{0}' in this package does not have the expected public key token ({1}).", assemblyPath, expectedToken), MyPackageIssueLevel.Error);
120+
return new PackageVerifierIssue("WRONG_PUBLICKEYTOKEN", assemblyPath, string.Format("The managed assembly '{0}' in this package does not have the expected public key token ({1}).", assemblyPath, expectedToken), PackageIssueLevel.Error);
112121
}
113122

114123
public static PackageVerifierIssue NotSemanticVersion(SemanticVersion version)
115124
{
116125
return new PackageVerifierIssue("VERSION_NOTSEMANTIC",
117-
string.Format("Version '{0}' does not follow semantic versioning guidelines.", version), MyPackageIssueLevel.Error);
126+
string.Format("Version '{0}' does not follow semantic versioning guidelines.", version), PackageIssueLevel.Error);
118127
}
119128

120129
public static PackageVerifierIssue Satellite_PackageSummaryNotLocalized()
121130
{
122-
return new PackageVerifierIssue("LOC_SUMMARY", "Package summary is not localized correctly", MyPackageIssueLevel.Error);
131+
return new PackageVerifierIssue("LOC_SUMMARY", "Package summary is not localized correctly", PackageIssueLevel.Error);
123132
}
124133

125134
public static PackageVerifierIssue Satellite_PackageTitleNotLocalized()
126135
{
127-
return new PackageVerifierIssue("LOC_TITLE", "Package title is not localized correctly", MyPackageIssueLevel.Error);
136+
return new PackageVerifierIssue("LOC_TITLE", "Package title is not localized correctly", PackageIssueLevel.Error);
128137
}
129138

130139
public static PackageVerifierIssue Satellite_PackageDescriptionNotLocalized()
131140
{
132-
return new PackageVerifierIssue("LOC_DESC", "Package description is not localized correctly", MyPackageIssueLevel.Error);
141+
return new PackageVerifierIssue("LOC_DESC", "Package description is not localized correctly", PackageIssueLevel.Error);
133142
}
134143

135144
public static PackageVerifierIssue RequiredCopyright()
136145
{
137-
return RequiredCore("NUSPEC_COPYRIGHT", "Copyright", MyPackageIssueLevel.Error);
146+
return RequiredCore("NUSPEC_COPYRIGHT", "Copyright", PackageIssueLevel.Error);
138147
}
139148

140149
public static PackageVerifierIssue RequiredLicenseUrl()
141150
{
142-
return RequiredCore("NUSPEC_LICENSEURL", "License Url", MyPackageIssueLevel.Error);
151+
return RequiredCore("NUSPEC_LICENSEURL", "License Url", PackageIssueLevel.Error);
143152
}
144153

145154
public static PackageVerifierIssue RequiredIconUrl()
146155
{
147-
return RequiredCore("NUSPEC_ICONURL", "Icon Url", MyPackageIssueLevel.Warning);
156+
return RequiredCore("NUSPEC_ICONURL", "Icon Url", PackageIssueLevel.Warning);
148157
}
149158

150159
public static PackageVerifierIssue RequiredTags()
151160
{
152-
return RequiredCore("NUSPEC_TAGS", "Tags", MyPackageIssueLevel.Warning);
161+
return RequiredCore("NUSPEC_TAGS", "Tags", PackageIssueLevel.Warning);
153162
}
154163

155164
public static PackageVerifierIssue RequiredTitle()
156165
{
157-
return RequiredCore("NUSPEC_TITLE", "Title", MyPackageIssueLevel.Error);
166+
return RequiredCore("NUSPEC_TITLE", "Title", PackageIssueLevel.Error);
158167
}
159168

160169
public static PackageVerifierIssue RequiredSummary()
161170
{
162-
return RequiredCore("NUSPEC_SUMMARY", "Summary", MyPackageIssueLevel.Warning);
171+
return RequiredCore("NUSPEC_SUMMARY", "Summary", PackageIssueLevel.Warning);
163172
}
164173

165174
public static PackageVerifierIssue RequiredProjectUrl()
166175
{
167-
return RequiredCore("NUSPEC_PROJECTURL", "Project Url", MyPackageIssueLevel.Warning);
176+
return RequiredCore("NUSPEC_PROJECTURL", "Project Url", PackageIssueLevel.Warning);
168177
}
169178

170179
public static PackageVerifierIssue RequiredRequireLicenseAcceptanceTrue()
171180
{
172-
return new PackageVerifierIssue("NUSPEC_ACCEPTLICENSE", string.Format("NuSpec Require License Acceptance is not set to true"), MyPackageIssueLevel.Error);
181+
return new PackageVerifierIssue("NUSPEC_ACCEPTLICENSE", string.Format("NuSpec Require License Acceptance is not set to true"), PackageIssueLevel.Error);
173182
}
174183

175-
private static PackageVerifierIssue RequiredCore(string issueId, string attributeName, MyPackageIssueLevel issueLevel)
184+
private static PackageVerifierIssue RequiredCore(string issueId, string attributeName, PackageIssueLevel issueLevel)
176185
{
177186
return new PackageVerifierIssue(issueId, string.Format("NuSpec {0} attribute is missing", attributeName), issueLevel);
178187
}
179188

180189
public static PackageVerifierIssue PowerShellScriptNotSigned(string scriptPath)
181190
{
182-
return new PackageVerifierIssue("SIGN_POWERSHELL", scriptPath, string.Format("The PowerShell script '{0}' is not signed.", scriptPath), MyPackageIssueLevel.Error);
191+
return new PackageVerifierIssue("SIGN_POWERSHELL", scriptPath, string.Format("The PowerShell script '{0}' is not signed.", scriptPath), PackageIssueLevel.Error);
183192
}
184193

185194
public static PackageVerifierIssue PEFileNotAuthenticodeSigned(string assemblyPath)
186195
{
187-
return new PackageVerifierIssue("SIGN_AUTHENTICODE", assemblyPath, string.Format("The PE file '{0}' in this package is not authenticode signed.", assemblyPath), MyPackageIssueLevel.Error);
196+
return new PackageVerifierIssue("SIGN_AUTHENTICODE", assemblyPath, string.Format("The PE file '{0}' in this package is not authenticode signed.", assemblyPath), PackageIssueLevel.Error);
188197
}
189198

190199
public static PackageVerifierIssue AssemblyHasNoDocFile(string assemblyPath)
191200
{
192-
return new PackageVerifierIssue("DOC_MISSING", assemblyPath, string.Format("The assembly '{0}' doesn't have a corresponding XML document file.", assemblyPath), MyPackageIssueLevel.Warning);
201+
return new PackageVerifierIssue("DOC_MISSING", assemblyPath, string.Format("The assembly '{0}' doesn't have a corresponding XML document file.", assemblyPath), PackageIssueLevel.Warning);
193202
}
194203

195204
public static PackageVerifierIssue AssemblyHasWrongJsonNetVersion(string assemblyPath, string targetFramework, string currentVersion)
@@ -198,7 +207,7 @@ public static PackageVerifierIssue AssemblyHasWrongJsonNetVersion(string assembl
198207
"WRONG_JSONNET_VERSION",
199208
string.Format("{0}; {1}", assemblyPath, targetFramework),
200209
string.Format("The assembly '{0}' references the wrong Json.NET version. Current version '{1}'; Expected version '8.0.2'.", assemblyPath, currentVersion),
201-
MyPackageIssueLevel.Error);
210+
PackageIssueLevel.Error);
202211
}
203212
}
204213
}

src/NuGetPackageVerifier/PackageIssueLevel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace NuGetPackageVerifier
55
{
6-
public enum MyPackageIssueLevel
6+
public enum PackageIssueLevel
77
{
88
Warning,
99
Error

src/NuGetPackageVerifier/PackageVerifierIssue.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ namespace NuGetPackageVerifier
55
{
66
public class PackageVerifierIssue
77
{
8-
public PackageVerifierIssue(string issueId, string issue, MyPackageIssueLevel level)
8+
public PackageVerifierIssue(string issueId, string issue, PackageIssueLevel level)
99
: this(issueId, instance: null, issue: issue, level: level)
1010
{
1111
}
1212

13-
public PackageVerifierIssue(string issueId, string instance, string issue, MyPackageIssueLevel level)
13+
public PackageVerifierIssue(string issueId, string instance, string issue, PackageIssueLevel level)
1414
{
1515
Instance = instance;
1616
IssueId = issueId;
1717
Issue = issue;
1818
Level = level;
1919
}
2020

21-
public MyPackageIssueLevel Level
21+
public PackageIssueLevel Level
2222
{
2323
get;
2424
private set;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using System.Diagnostics;
6+
using System.Linq;
7+
using Mono.Cecil;
8+
using Mono.Collections.Generic;
9+
10+
namespace NuGetPackageVerifier.Rules
11+
{
12+
public class AssemblyHasCorrectBuildConfigurationRule : AssemblyHasAttributeRuleBase
13+
{
14+
public override IEnumerable<PackageVerifierIssue> ValidateAttribute(string currentFilePath, Collection<CustomAttribute> assemblyAttributes)
15+
{
16+
if (!HasReleaseConfiguration(assemblyAttributes))
17+
{
18+
yield return PackageIssueFactory.AssemblyHasIncorrectBuildConfiguration(currentFilePath);
19+
}
20+
}
21+
22+
private static bool HasReleaseConfiguration(Collection<CustomAttribute> assemblyAttributes)
23+
{
24+
var foundAttr = assemblyAttributes.SingleOrDefault(attr => attr.AttributeType.FullName == typeof(DebuggableAttribute).FullName);
25+
if (foundAttr == null)
26+
{
27+
return false;
28+
}
29+
30+
var foundAttrArg = foundAttr.ConstructorArguments.SingleOrDefault();
31+
var attrValue = (DebuggableAttribute.DebuggingModes)foundAttrArg.Value;
32+
if (attrValue.HasFlag(DebuggableAttribute.DebuggingModes.Default) ||
33+
attrValue.HasFlag(DebuggableAttribute.DebuggingModes.DisableOptimizations))
34+
{
35+
return false;
36+
}
37+
38+
return true;
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)