110 lines
2.8 KiB
C#
110 lines
2.8 KiB
C#
using SQLLinter.Common;
|
|
using SQLLinter.Core.Interfaces;
|
|
using SQLLinter.Infrastructure.Rules.RuleExceptions;
|
|
|
|
namespace SQLLinter.Infrastructure.Parser;
|
|
|
|
public class SqlFileProcessor : ISqlFileProcessor
|
|
{
|
|
private readonly IRuleVisitor ruleVisitor;
|
|
|
|
private readonly IReporter reporter;
|
|
|
|
private readonly IPluginHandler pluginHandler;
|
|
|
|
private readonly IRuleExceptionFinder ruleExceptionFinder;
|
|
|
|
public SqlFileProcessor(
|
|
IRuleVisitor ruleVisitor,
|
|
IPluginHandler pluginHandler,
|
|
IReporter reporter)
|
|
{
|
|
this.ruleVisitor = ruleVisitor;
|
|
this.pluginHandler = pluginHandler;
|
|
this.reporter = reporter;
|
|
ruleExceptionFinder = new RuleExceptionFinder(pluginHandler.RuleWithNames);
|
|
}
|
|
|
|
private int _fileCount;
|
|
public int FileCount
|
|
{
|
|
get
|
|
{
|
|
return _fileCount;
|
|
}
|
|
}
|
|
|
|
public void ProcessList(List<string> filePaths)
|
|
{
|
|
foreach (var path in filePaths)
|
|
{
|
|
ProcessFile(path);
|
|
}
|
|
}
|
|
|
|
public void ProcessPath(string path)
|
|
{
|
|
ProcessFile(path);
|
|
}
|
|
|
|
private void ProcessFile(string filePath)
|
|
{
|
|
var fileStream = GetFileContents(filePath);
|
|
HandleProcessing(filePath, fileStream);
|
|
}
|
|
|
|
public void ProcessList(Dictionary<string, Stream> files)
|
|
{
|
|
foreach (var file in files)
|
|
{
|
|
HandleProcessing(file.Key, file.Value);
|
|
}
|
|
}
|
|
|
|
private bool IsWholeFileIgnored(string filePath, IEnumerable<IExtendedRuleException> ignoredRules)
|
|
{
|
|
var ignoredRulesEnum = ignoredRules.ToArray();
|
|
if (!ignoredRulesEnum.Any())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var lineOneRuleIgnores = ignoredRulesEnum.OfType<GlobalRuleException>().Where(x => 1 == x.StartLine).ToArray();
|
|
if (!lineOneRuleIgnores.Any())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var lineCount = 0;
|
|
using (var reader = new StreamReader(GetFileContents(filePath)))
|
|
{
|
|
while (reader.ReadLine() != null)
|
|
{
|
|
lineCount++;
|
|
}
|
|
}
|
|
|
|
return lineOneRuleIgnores.Any(x => x.EndLine == lineCount);
|
|
}
|
|
|
|
private void HandleProcessing(string filePath, Stream fileStream)
|
|
{
|
|
var ignoredRules = ruleExceptionFinder.GetIgnoredRuleList(fileStream).ToList();
|
|
if (IsWholeFileIgnored(filePath, ignoredRules))
|
|
{
|
|
return;
|
|
}
|
|
ProcessRules(fileStream, ignoredRules, filePath);
|
|
}
|
|
|
|
private void ProcessRules(Stream fileStream, IEnumerable<IRuleException> ignoredRules, string filePath)
|
|
{
|
|
ruleVisitor.VisitRules(filePath, ignoredRules, fileStream);
|
|
}
|
|
|
|
private Stream GetFileContents(string filePath)
|
|
{
|
|
return File.OpenRead(filePath);
|
|
}
|
|
}
|