Skip to content

Commit fb4c296

Browse files
authored
Merge pull request #130 from microsoft/FileExclusionCustomize
Replaces set exclusion list with user supplied list.
2 parents dbd82d1 + 9398f2b commit fb4c296

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

AppInspector/Commands/AnalyzeCommand.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ public class AnalyzeCommand : ICommand
2323
readonly int MAX_FILESIZE = 1024 * 1000 * 5; // Skip source files larger than 5 MB and log
2424
readonly int MAX_TEXT_SAMPLE_LENGTH = 200;//char bytes
2525

26-
readonly string[] EXCLUDEMATCH_FILEPATH = "sample,example,test,docs,.vs,.git".Split(",");
27-
2826
public enum ExitCode
2927
{
3028
NoMatches = 0,
@@ -65,7 +63,6 @@ public DateTime LastUpdated
6563
private bool _arg_outputUniqueTagsOnly;
6664
private string _arg_confidenceFilters;
6765
private bool _arg_simpleTagsOnly;
68-
private bool _arg_allowSampleFiles;
6966
private Confidence _arg_confidence;
7067
private WriteOnce.ConsoleVerbosity _arg_consoleVerbosityLevel;
7168

@@ -80,11 +77,14 @@ public AnalyzeCommand(AnalyzeCommandOptions opts)
8077
_arg_fileFormat = opts.OutputFileFormat;
8178
_arg_outputTextFormat = opts.TextOutputFormat;
8279
_arg_outputUniqueTagsOnly = !opts.AllowDupTags;
83-
_arg_allowSampleFiles = opts.AllowSampleFiles;
8480
_arg_customRulesPath = opts.CustomRulesPath;
8581
_arg_confidenceFilters = opts.ConfidenceFilters;
8682
_arg_ignoreDefaultRules = opts.IgnoreDefaultRules;
8783
_arg_simpleTagsOnly = opts.SimpleTagsOnly;
84+
_fileExclusionList = opts.FilePathExclusions.Split(",").ToList<string>();
85+
if (_fileExclusionList.Contains("none") || _fileExclusionList.Contains("None"))
86+
_fileExclusionList.Clear();
87+
8888
if (!Enum.TryParse(opts.ConsoleVerbosityLevel, true, out _arg_consoleVerbosityLevel))
8989
throw new OpException(String.Format(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_ARG_VALUE, "-x")));
9090
WriteOnce.Verbosity = _arg_consoleVerbosityLevel;
@@ -98,7 +98,6 @@ public AnalyzeCommand(AnalyzeCommandOptions opts)
9898
ConfigConfidenceFilters();
9999
ConfigRules();
100100

101-
_fileExclusionList = EXCLUDEMATCH_FILEPATH.ToList<string>();
102101
_uniqueTagsControl = new HashSet<string>();
103102
}
104103

@@ -606,46 +605,46 @@ void UnZipAndProcess(string filename, ArchiveFileType archiveFileType)
606605
/// <summary>
607606
/// Common validation called by ProcessAsFile and UnzipAndProcess to ensure same order and checks made
608607
/// </summary>
609-
/// <param name="filename"></param>
608+
/// <param name="filePath"></param>
610609
/// <param name="languageInfo"></param>
611610
/// <param name="fileLength">should be > zero if called from unzip method</param>
612611
/// <returns></returns>
613-
bool FileChecksPassed(string filename, ref LanguageInfo languageInfo, long fileLength = 0)
612+
bool FileChecksPassed(string filePath, ref LanguageInfo languageInfo, long fileLength = 0)
614613
{
615-
_appProfile.MetaData.FileExtensions.Add(Path.GetExtension(filename).Replace('.', ' ').TrimStart());
614+
_appProfile.MetaData.FileExtensions.Add(Path.GetExtension(filePath).Replace('.', ' ').TrimStart());
616615

617616
// 1. Skip files written in unknown language
618-
if (!Language.FromFileName(filename, ref languageInfo))
617+
if (!Language.FromFileName(filePath, ref languageInfo))
619618
{
620-
WriteOnce.SafeLog(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_LANGUAGE_NOTFOUND, filename), LogLevel.Warn);
619+
WriteOnce.SafeLog(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_LANGUAGE_NOTFOUND, filePath), LogLevel.Warn);
621620
_appProfile.MetaData.FilesSkipped++;
622621
return false;
623622
}
624623

625624
_appProfile.MetaData.AddLanguage(languageInfo.Name);
626625

627626
// 2. Skip excluded files i.e. sample, test or similar unless ignore filter requested
628-
if (!_arg_allowSampleFiles && _fileExclusionList.Any(v => filename.ToLower().Contains(v)))
627+
if (_fileExclusionList.Any(v => filePath.ToLower().Contains(v)))
629628
{
630-
WriteOnce.SafeLog(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_EXCLUDED_TYPE_SKIPPED, filename), LogLevel.Warn);
629+
WriteOnce.SafeLog(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_EXCLUDED_TYPE_SKIPPED, filePath), LogLevel.Warn);
631630
_appProfile.MetaData.FilesSkipped++;
632631
return false;
633632
}
634633

635634
// 3. Skip if exceeds file size limits
636635
try
637636
{
638-
fileLength = fileLength <= 0 ? new FileInfo(filename).Length : fileLength;
637+
fileLength = fileLength <= 0 ? new FileInfo(filePath).Length : fileLength;
639638
if (fileLength > MAX_FILESIZE)
640639
{
641-
WriteOnce.SafeLog(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_FILESIZE_SKIPPED, filename), LogLevel.Warn);
640+
WriteOnce.SafeLog(ErrMsg.FormatString(ErrMsg.ID.ANALYZE_FILESIZE_SKIPPED, filePath), LogLevel.Warn);
642641
_appProfile.MetaData.FilesSkipped++;
643642
return false;
644643
}
645644
}
646645
catch (Exception)
647646
{
648-
throw new OpException(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_FILE_OR_DIR, filename));
647+
throw new OpException(ErrMsg.FormatString(ErrMsg.ID.CMD_INVALID_FILE_OR_DIR, filePath));
649648
}
650649

651650
return true;

AppInspector/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public class AnalyzeCommandOptions
4949
[Option('c', "confidence-filters", Required = false, HelpText = "Output only matches with specified confidence <value>,<value> [high|medium|low]", Default = "high,medium")]
5050
public string ConfidenceFilters { get; set; }
5151

52-
[Option('k', "include-sample-paths", Required = false, HelpText = "Include source files with (sample,example,test,.vs,.git) in pathname in analysis", Default = false)]
53-
public bool AllowSampleFiles { get; set; }
52+
[Option('k', "file-path-exclusions", Required = false, HelpText = "Exclude source files (none|default: sample,example,test,docs,.vs,.git)", Default = "sample, example, test, docs,.vs,.git")]
53+
public string FilePathExclusions { get; set; }
5454

5555
[Option('x', "console-verbosity", Required = false, HelpText = "Console verbosity [high|medium|low|none]", Default = "medium")]
5656
public string ConsoleVerbosityLevel { get; set; }

0 commit comments

Comments
 (0)