Skip to content

Commit ab4d61a

Browse files
guyacostagfsdaalcant
authored
Fix for #163 (#165)
* Fix for #163 * Cleared local args testing * Completes automatic packing of default rules. * Seems to resolve packer issues on mac * Update core-pipeline.yml Workaround for duplicate pre-build events on publish Co-authored-by: Gabe Stocco <[email protected]> Co-authored-by: David Alcantar <[email protected]>
1 parent a2a7ae9 commit ab4d61a

34 files changed

+17651
-252
lines changed

AppInspector.CLI/AppInspector.CLI.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<ItemGroup>
3535
<PackageReference Include="CommandLineParser" Version="2.6.0" />
3636
<PackageReference Include="DotLiquid" Version="2.0.314" />
37-
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
37+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
3838
<PackageReference Include="NLog" Version="4.6.7" />
3939
</ItemGroup>
4040

@@ -163,7 +163,7 @@
163163
</ItemGroup>
164164

165165
<ItemGroup>
166-
<None Include="..\LICENSE.txt" Pack="true" PackagePath=""/>
167-
<None Include="..\icon-128.png" Pack="true" PackagePath=""/>
166+
<None Include="..\LICENSE.txt" Pack="true" PackagePath="" />
167+
<None Include="..\icon-128.png" Pack="true" PackagePath="" />
168168
</ItemGroup>
169169
</Project>

AppInspector.CLI/Program.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ static int Main(string[] args)
3333
TagDiffCommandOptions,
3434
TagTestCommandOptions,
3535
ExportTagsCommandOptions,
36-
VerifyRulesCommandOptions>(args)
36+
VerifyRulesCommandOptions,
37+
PackRulesCommandOptions>(args)
3738
.MapResult(
3839
(AnalyzeCommandOptions opts) => RunAnalyzeCommand(opts),
3940
(TagDiffCommandOptions opts) => RunTagDiffCommand(opts),
4041
(TagTestCommandOptions opts) => RunTagTestCommand(opts),
4142
(ExportTagsCommandOptions opts) => RunExportTagsCommand(opts),
4243
(VerifyRulesCommandOptions opts) => RunVerifyRulesCommand(opts),
44+
(PackRulesCommandOptions opts) => RunPackRulesCommand(opts),
4345
errs => 1
4446
);
4547

@@ -102,6 +104,12 @@ private static int RunVerifyRulesCommand(VerifyRulesCommandOptions opts)
102104
}
103105

104106

107+
private static int RunPackRulesCommand(PackRulesCommandOptions opts)
108+
{
109+
SetupLogging(opts);
110+
return new PackRulesCommand(opts).Run();
111+
}
112+
105113
static void SetupLogging(AllCommandOptions opts)
106114
{
107115
var config = new NLog.Config.LoggingConfiguration();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
https://go.microsoft.com/fwlink/?LinkID=208121.
4+
-->
5+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
6+
<PropertyGroup>
7+
<PublishProtocol>FileSystem</PublishProtocol>
8+
<Configuration>Release</Configuration>
9+
<Platform>Any CPU</Platform>
10+
<TargetFramework>netcoreapp3.1</TargetFramework>
11+
<PublishDir>bin\Release\netcoreapp3.1\publish\</PublishDir>
12+
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
13+
<SelfContained>false</SelfContained>
14+
<PublishSingleFile>False</PublishSingleFile>
15+
<PublishReadyToRun>False</PublishReadyToRun>
16+
</PropertyGroup>
17+
</Project>

AppInspector.sln

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MultiExtractor", "MultiExtr
1616
EndProject
1717
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AppInspector.CLI", "AppInspector.CLI\AppInspector.CLI.csproj", "{824ED27E-A4CF-46A6-A01F-98B0821EB61C}"
1818
EndProject
19+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "RulesPacker", "RulesPacker", "{C464D0CE-5254-4EA5-87C9-C0C96E40C3CB}"
20+
ProjectSection(SolutionItems) = preProject
21+
RulesPacker\ApplicationInspector.CLI.deps.json = RulesPacker\ApplicationInspector.CLI.deps.json
22+
RulesPacker\ApplicationInspector.CLI.dll = RulesPacker\ApplicationInspector.CLI.dll
23+
RulesPacker\ApplicationInspector.CLI.exe = RulesPacker\ApplicationInspector.CLI.exe
24+
RulesPacker\ApplicationInspector.CLI.pdb = RulesPacker\ApplicationInspector.CLI.pdb
25+
RulesPacker\ApplicationInspector.CLI.runtimeconfig.dev.json = RulesPacker\ApplicationInspector.CLI.runtimeconfig.dev.json
26+
RulesPacker\ApplicationInspector.CLI.runtimeconfig.json = RulesPacker\ApplicationInspector.CLI.runtimeconfig.json
27+
RulesPacker\ApplicationInspector.Commands.dll = RulesPacker\ApplicationInspector.Commands.dll
28+
RulesPacker\ApplicationInspector.Commands.pdb = RulesPacker\ApplicationInspector.Commands.pdb
29+
RulesPacker\ApplicationInspector.RulesEngine.dll = RulesPacker\ApplicationInspector.RulesEngine.dll
30+
RulesPacker\ApplicationInspector.RulesEngine.pdb = RulesPacker\ApplicationInspector.RulesEngine.pdb
31+
RulesPacker\CommandLine.dll = RulesPacker\CommandLine.dll
32+
RulesPacker\DotLiquid.dll = RulesPacker\DotLiquid.dll
33+
RulesPacker\MultiExtractor.dll = RulesPacker\MultiExtractor.dll
34+
RulesPacker\MultiExtractor.pdb = RulesPacker\MultiExtractor.pdb
35+
RulesPacker\Newtonsoft.Json.dll = RulesPacker\Newtonsoft.Json.dll
36+
RulesPacker\NLog.dll = RulesPacker\NLog.dll
37+
EndProjectSection
38+
EndProject
1939
Global
2040
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2141
Debug|Any CPU = Debug|Any CPU
@@ -42,6 +62,9 @@ Global
4262
GlobalSection(SolutionProperties) = preSolution
4363
HideSolutionNode = FALSE
4464
EndGlobalSection
65+
GlobalSection(NestedProjects) = preSolution
66+
{C464D0CE-5254-4EA5-87C9-C0C96E40C3CB} = {876245F3-D7C5-4AA3-A288-2CE94BF63B8E}
67+
EndGlobalSection
4568
GlobalSection(ExtensibilityGlobals) = postSolution
4669
SolutionGuid = {1D8F88F7-47D4-475A-B1AF-713132222341}
4770
EndGlobalSection

AppInspector/AppInspector.Commands.csproj

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Library</OutputType>
@@ -28,7 +28,7 @@
2828

2929
<Target DependsOnTargets="ResolveReferences" Name="CopyProjectReferencesToPackage">
3030
<ItemGroup>
31-
<BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))"/>
31+
<BuildOutputInPackage Include="@(ReferenceCopyLocalPaths-&gt;WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))" />
3232
</ItemGroup>
3333
</Target>
3434

@@ -46,7 +46,7 @@
4646
<ItemGroup>
4747
<PackageReference Include="CommandLineParser" Version="2.6.0" />
4848
<PackageReference Include="DotLiquid" Version="2.0.314" />
49-
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
49+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
5050
<PackageReference Include="NLog" Version="4.6.7" />
5151
</ItemGroup>
5252

@@ -65,10 +65,8 @@
6565
</EmbeddedResource>
6666
</ItemGroup>
6767

68-
<ItemGroup>
69-
<None Update="rules\**">
70-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
71-
</None>
68+
<ItemGroup>
69+
<EmbeddedResource Include="Resources\defaultRules.json" Link="defaultRules.json" />
7270
</ItemGroup>
7371

7472
<ItemGroup>
@@ -84,9 +82,12 @@
8482
</ItemGroup>
8583

8684
<ItemGroup>
87-
<None Include="..\LICENSE.txt" Pack="true" PackagePath=""/>
88-
<None Include="..\icon-128.png" Pack="true" PackagePath=""/>
89-
<None Update="rules\**" Pack="true" PackagePath="lib\$(TargetFramework)\"/>
85+
<None Include="..\LICENSE.txt" Pack="true" PackagePath="" />
86+
<None Include="..\icon-128.png" Pack="true" PackagePath="" />
9087
</ItemGroup>
9188

89+
<Target Name="PreBuild" BeforeTargets="BeforeCompile">
90+
<Exec Command="dotnet ../RulesPacker/ApplicationInspector.CLI.dll packrules -r rules/default -o Resources/defaultRules.json" />
91+
</Target>
92+
9293
</Project>

AppInspector/Commands/AllCommandOptions.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,6 @@ public class TagTestCommandOptions : AllCommandOptions
100100
[Option('r', "custom-rules-path", Required = false, HelpText = "Custom rules path")]
101101
public string CustomRulesPath { get; set; }
102102

103-
[Option('i', "ignore-default-rules", Required = false, HelpText = "Exclude default rules bundled with application", Default = true)]
104-
public bool IgnoreDefaultRules { get; set; }
105-
106103
[Option('o', "output-file-path", Required = false, HelpText = "Path to output file")]
107104
public string OutputFilePath { get; set; }
108105

@@ -128,15 +125,12 @@ public class ExportTagsCommandOptions : AllCommandOptions
128125
}
129126

130127

131-
[Verb("verifyrules", HelpText = "Verify rules syntax is valid")]
128+
[Verb("verifyrules", HelpText = "Verify custom rules syntax is valid")]
132129
public class VerifyRulesCommandOptions : AllCommandOptions
133130
{
134131
[Option('r', "custom-rules-path", Required = false, HelpText = "Custom rules path")]
135132
public string CustomRulesPath { get; set; }
136133

137-
[Option('i', "ignore-default-rules", Required = false, HelpText = "Exclude default rules bundled with application", Default = false)]
138-
public bool IgnoreDefaultRules { get; set; }
139-
140134
[Option('o', "output-file-path", Required = false, HelpText = "Path to output file")]
141135
public string OutputFilePath { get; set; }
142136

@@ -146,5 +140,24 @@ public class VerifyRulesCommandOptions : AllCommandOptions
146140

147141
}
148142

143+
[Verb("packrules", HelpText = "Combine multiple rules into one file for ease in distribution")]
144+
public class PackRulesCommandOptions : AllCommandOptions
145+
{
146+
[Option('d', "pack-default-rules", Required = false, HelpText = "Repack default rules")]
147+
public bool RepackDefaultRules { get; set; }
148+
149+
[Option('r', "custom-rules-path", Required = false, HelpText = "Custom rules path")]
150+
public string CustomRulesPath { get; set; }
151+
152+
[Option('o', "output-file-path", Required = false, HelpText = "Path to output file")]
153+
public string OutputFilePath { get; set; }
154+
155+
[Option('i', "not-indented", Required = false, HelpText = "Remove indentation from json output", Default = false)]
156+
public bool NotIndented { get; set; }
157+
158+
[Option('x', "console-verbosity", Required = false, HelpText = "Console verbosity [high|medium|low|none]", Default = "medium")]
159+
public string ConsoleVerbosityLevel { get; set; }
160+
161+
}
149162

150163
}

AppInspector/Commands/AnalyzeCommand.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,18 @@ void ConfigRules()
152152

153153
RuleSet rulesSet = new RuleSet(_arg_logger);
154154
List<string> rulePaths = new List<string>();
155+
155156
if (!_arg_ignoreDefaultRules)
156-
rulePaths.Add(Utils.GetPath(Utils.AppPath.defaultRules));
157+
{
158+
Assembly assembly = Assembly.GetExecutingAssembly();
159+
string[] resourceName = assembly.GetManifestResourceNames();
160+
string filePath = "Microsoft.ApplicationInspector.Commands.defaultRules.json";
161+
Stream resource = assembly.GetManifestResourceStream(filePath);
162+
using (StreamReader file = new StreamReader(resource))
163+
{
164+
rulesSet.AddString(file.ReadToEnd(), filePath, null);
165+
}
166+
}
157167

158168
if (!string.IsNullOrEmpty(_arg_customRulesPath))
159169
rulePaths.Add(_arg_customRulesPath);

AppInspector/Commands/ExportTagsCommand.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,16 @@ void ConfigRules()
6262
{
6363
List<string> rulePaths = new List<string>();
6464
if (!_arg_ignoreDefaultRules)
65-
rulePaths.Add(Utils.GetPath(Utils.AppPath.defaultRules));
65+
{
66+
Assembly assembly = Assembly.GetExecutingAssembly();
67+
string[] resourceName = assembly.GetManifestResourceNames();
68+
string filePath = "Microsoft.ApplicationInspector.Commands.defaultRules.json";
69+
Stream resource = assembly.GetManifestResourceStream(filePath);
70+
using (StreamReader file = new StreamReader(resource))
71+
{
72+
_rules.AddString(file.ReadToEnd(), filePath, null);
73+
}
74+
}
6675

6776
if (!string.IsNullOrEmpty(_arg_customRulesPath))
6877
rulePaths.Add(_arg_customRulesPath);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (C) Microsoft. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
using Microsoft.ApplicationInspector.RulesEngine;
5+
using Newtonsoft.Json;
6+
using NLog;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.IO;
10+
using System.Linq;
11+
12+
namespace Microsoft.ApplicationInspector.Commands
13+
{
14+
public class PackRulesCommand : Command
15+
{
16+
public enum ExitCode
17+
{
18+
NoIssues = 0,
19+
NotVerified = 1,
20+
CriticalError = 2
21+
}
22+
23+
private string _arg_consoleVerbosityLevel;
24+
25+
public PackRulesCommand(PackRulesCommandOptions opt)
26+
{
27+
_path = opt.RepackDefaultRules ? Utils.GetPath(Utils.AppPath.defaultRulesSrc) : opt.CustomRulesPath;
28+
_outputfile = opt.RepackDefaultRules ? Utils.GetPath(Utils.AppPath.defaultRulesPackedFile) : opt.OutputFilePath;
29+
_arg_consoleVerbosityLevel = opt.ConsoleVerbosityLevel;
30+
31+
if (!opt.RepackDefaultRules && string.IsNullOrEmpty(opt.CustomRulesPath) || string.IsNullOrEmpty(_outputfile))
32+
throw new OpException(ErrMsg.FormatString(ErrMsg.ID.PACK_MISSING_OUTPUT_ARG));
33+
34+
_outputfile = Path.GetFullPath(_outputfile);
35+
_indent = !opt.NotIndented;
36+
37+
WriteOnce.ConsoleVerbosity verbosity = WriteOnce.ConsoleVerbosity.Medium;
38+
if (!Enum.TryParse(_arg_consoleVerbosityLevel, true, out verbosity))
39+
throw new OpException(String.Format(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_ARG_VALUE, "-x")));
40+
WriteOnce.Verbosity = verbosity;
41+
}
42+
43+
public override int Run()
44+
{
45+
WriteOnce.SafeLog("PackRules::Run", LogLevel.Trace);
46+
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_RUNNING, "PackRules"));
47+
Verifier verifier = new Verifier(_path);
48+
49+
if (!verifier.Verify())
50+
return (int)ExitCode.CriticalError;
51+
52+
List<Rule> list = new List<Rule>(verifier.CompiledRuleset.AsEnumerable());
53+
54+
JsonSerializerSettings settings = new JsonSerializerSettings();
55+
settings.Formatting = (_indent) ? Formatting.Indented : Formatting.None;
56+
57+
using (FileStream fs = File.Open(_outputfile, FileMode.Create, FileAccess.Write))
58+
{
59+
StreamWriter sw = new StreamWriter(fs);
60+
sw.Write(JsonConvert.SerializeObject(list, settings));
61+
sw.Close();
62+
fs.Close();
63+
}
64+
65+
WriteOnce.Operation(ErrMsg.FormatString(ErrMsg.ID.CMD_COMPLETED, "PackRules"));
66+
67+
return (int)ExitCode.NoIssues;
68+
}
69+
70+
private string _path;
71+
private string _outputfile;
72+
private bool _indent;
73+
}
74+
}

AppInspector/Commands/TagDiffCommand.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ public override int Run()
122122
CustomRulesPath = _arg_rulesPath,
123123
IgnoreDefaultRules = _arg_ignoreDefault,
124124
SimpleTagsOnly = true,
125-
AllowDupTags = false,
126-
FilePathExclusions = "sample,example,test,docs,.vs,.git",
127125
ConsoleVerbosityLevel = "none",
128126
Log = _arg_logger
129127
});
@@ -135,8 +133,6 @@ public override int Run()
135133
CustomRulesPath = _arg_rulesPath,
136134
IgnoreDefaultRules = _arg_ignoreDefault,
137135
SimpleTagsOnly = true,
138-
AllowDupTags = false,
139-
FilePathExclusions = "sample,example,test,docs,.vs,.git",
140136
ConsoleVerbosityLevel = "none",
141137
Log = _arg_logger
142138
});

0 commit comments

Comments
 (0)