public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType != JsonToken.Null) { JObject jObject = JObject.Load(reader); object result; if (objectType == typeof(Message)) { JToken token = jObject[nameof(MessageType)]; var messageType = (MessageType)Enum.Parse(typeof(MessageType), token.ToString()); switch (messageType) { case MessageType.Progress: result = new ProgressMessage(); break; case MessageType.Error: result = new ErrorMessage(); break; case MessageType.Result: result = new StatisticsMessage(); break; default: throw new NotImplementedException($"{token} message type is not supported"); } } else if (objectType == typeof(LanguageStatistics)) { JToken token = jObject[nameof(Language)]; Language language = LanguageUtils.ParseLanguages(token.ToString()).FirstOrDefault(); if (language == Language.CSharp) { result = new CSharpStatistics(); } else if (language == Language.Java) { result = new JavaStatistics(); } else if (language == Language.Php) { result = new PhpStatistics(); } else { throw new NotImplementedException($"{token} language is not supported"); } } else { throw new FormatException("Invalid JSON"); } serializer.Populate(jObject.CreateReader(), result); return(result); } return(null); }
private void CollectStatistics(PhpStatistics phpStatistics, JavaStatistics javaStatistics, CSharpStatistics csharpStatistics, string filePath, int totalFilesCount, ref int processedCount) { CollectPhpStatistics(phpStatistics, filePath); CollectJavaStatistics(javaStatistics, filePath); CollectCSharpStatistics(csharpStatistics, filePath); Interlocked.Increment(ref processedCount); Logger?.LogInfo(new ProgressMessage(processedCount, totalFilesCount) { LastFileName = filePath }); }
public StatisticsMessage CollectStatistics(string directoryPath, int startInd = 0, int length = 0) { IEnumerable <string> fileNames = Enumerable.Empty <string>(); if (Directory.Exists(directoryPath)) { fileNames = Directory.GetFiles(directoryPath, "*.*", SearchOption.AllDirectories); if (startInd != 0) { fileNames = fileNames.Skip(startInd); } if (length != 0) { fileNames = fileNames.Take(length); } } else { fileNames = new string[] { directoryPath }; } int totalFilesCount = fileNames.Count(); var phpStatistics = new PhpStatistics(); var javaStatistics = new JavaStatistics(); var csharpStatistics = new CSharpStatistics(); int processedCount = 0; if (!Multithreading) { foreach (var filePath in fileNames) { CollectStatistics(phpStatistics, javaStatistics, csharpStatistics, filePath, totalFilesCount, ref processedCount); } } else { var options = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }; Parallel.ForEach(fileNames, options, filePath => { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture; CollectStatistics(phpStatistics, javaStatistics, csharpStatistics, filePath, totalFilesCount, ref processedCount); }); } var result = new StatisticsMessage { Id = Guid.NewGuid().ToString(), ErrorCount = Logger?.ErrorCount ?? 0, LanguageStatistics = new List <LanguageStatistics>() { phpStatistics, javaStatistics, csharpStatistics } }; return(result); }
private void CollectPhpStatistics(PhpStatistics phpStatistics, string filePath) { try { var fileName = Path.GetFileName(filePath).ToLowerInvariant(); if (fileName == "composer.json" || fileName == "composer.lock" || fileName == "php.ini") { var lines = File.ReadAllLines(filePath); string content; if (fileName == "php.ini") { content = string.Join(Environment.NewLine, lines.Where(line => line.StartsWith("extension") || line.StartsWith("zend_extension"))); } else { content = string.Join(Environment.NewLine, lines.Where(line => line.StartsWith("require") || line.StartsWith("require-dev") || line.StartsWith("packages") || line.StartsWith("packages-dev"))); } phpStatistics.FilesContent[fileName] = content; } else if (filePath.ToLowerInvariant().EndsWith(".htaccess")) { var lines = File.ReadAllLines(filePath); foreach (var line in lines) { if (line.StartsWith("php_value") || line.StartsWith("php_flag") || line.StartsWith("RewriteCond") || line.StartsWith("RewriteRule")) { phpStatistics.HtaccessStrings.Add(line); } } } else if (filePath.EndsWith(".php")) { var fileCollector = new PhpInfoCollectorVisitor(); fileCollector.Logger = Logger; FileStatistics fileStat = fileCollector.CollectInfo(filePath); foreach (var classUsing in fileStat.ClassUsings) { int count = 0; phpStatistics.ClassUsings.TryGetValue(classUsing.Key, out count); phpStatistics.ClassUsings[classUsing.Key] = count + classUsing.Value; } foreach (var invoke in fileStat.MethodInvocations) { int count = 0; phpStatistics.MethodInvocations.TryGetValue(invoke.Key, out count); phpStatistics.MethodInvocations[invoke.Key] = count + invoke.Value; } foreach (var include in fileStat.Includes) { int count = 0; phpStatistics.Includes.TryGetValue(include.Key, out count); phpStatistics.Includes[include.Key] = count + include.Value; } } } catch (Exception ex) { Logger?.LogInfo(new ErrorMessage(ex.ToString())); } }