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 CollectJavaStatistics(JavaStatistics javaStatistics, string filePath) { try { var fileName = Path.GetFileName(filePath).ToLowerInvariant(); var extension = Path.GetExtension(fileName).ToLowerInvariant(); if (extension == ".xhtml") { javaStatistics.XHtmlFileCount++; } else if (extension == ".java" || extension == ".class" || extension == ".jsp") { var sourceSize = new FileInfo(filePath).Length; javaStatistics.SourceFilesCount++; javaStatistics.SourceCodeLinesCount += sourceSize; switch (extension) { case ".java": javaStatistics.JavaFilesCount++; javaStatistics.JavaSourceSize += sourceSize; break; case ".class": javaStatistics.ClassFilesCount++; javaStatistics.ClassSourceSize += sourceSize; break; case ".jsp": javaStatistics.JspFilesCount++; javaStatistics.JspSourceSize += sourceSize; break; } } else if (extension == ".war" || extension == ".ear") { string tempDir = Path.Combine(Path.GetTempPath(), fileName); if (Directory.Exists(tempDir)) { Directory.Delete(tempDir, true); } ZipFile.ExtractToDirectory(filePath, tempDir); var fileNames = Directory.GetFiles(tempDir, "*.*", SearchOption.AllDirectories); foreach (var fileName2 in fileNames) { CollectJavaStatistics(javaStatistics, fileName2); } Directory.Delete(tempDir, true); } else if (extension == ".jar") { javaStatistics.Dependencies.Add(Path.GetFileNameWithoutExtension(fileName)); } else if (fileName == "pom.xml") { var doc = XDocument.Load(filePath); var ns = doc.Root.Name.NamespaceName == "" ? "" : "{" + doc.Root.Name.NamespaceName + "}"; IEnumerable <string> repositories = doc.Element(XName.Get($"{ns}project")) ?.Element(XName.Get($"{ns}repositories")) ?.Elements() .Select(elem => elem.Element(XName.Get($"{ns}id")).Value + " " + elem.Element(XName.Get($"{ns}url")).Value); if (repositories != null) { foreach (var repository in repositories) { var buildTool = ExtractBuildTool(repository.Trim()); if (buildTool != null) { javaStatistics.DependencyManagers.Add(buildTool); } javaStatistics.Repositories.Add(repository.Trim()); } } var build = doc.Element(XName.Get($"{ns}project"))?.Element(XName.Get($"{ns}build")); var plugins = build?.Element(XName.Get($"{ns}plugins"))?.Elements(); IEnumerable <string> buildTools = plugins.Select(elem => elem.Element(XName.Get($"{ns}artifactId")).Value); if (buildTools != null) { foreach (var buildTool in buildTools) { var tool = ExtractBuildTool(buildTool.Trim()); if (tool != null) { javaStatistics.BuildTools.Add(tool); } javaStatistics.BuildToolsPlugins.Add(buildTool.Trim()); } } plugins = build?.Element(XName.Get($"{ns}pluginManagement"))?.Element(XName.Get($"{ns}plugins"))?.Elements(); buildTools = plugins?.Select(elem => elem.Element(XName.Get($"{ns}artifactId")).Value); if (buildTools != null) { foreach (var buildTool in buildTools) { javaStatistics.BuildTools.RemoveWhere(t => buildTool.Contains(t)); var tool = ExtractBuildTool(buildTool.Trim()); if (tool != null) { javaStatistics.BuildTools.Add(tool); } javaStatistics.BuildToolsPlugins.Add(buildTool.Trim()); } } var pluginDependencies = plugins?.SelectMany(plugin => plugin.Element(XName.Get($"{ns}dependencies"))?.Elements() ?? new XElement[0]) .Select(elem => elem.Element(XName.Get($"{ns}artifactId")).Value + "-" + elem.Element(XName.Get($"{ns}version")).Value); if (pluginDependencies != null) { foreach (var dependency in pluginDependencies) { javaStatistics.Dependencies.Add(dependency); } } IEnumerable <string> dependencies = doc.Element(XName.Get($"{ns}project")) ?.Element(XName.Get($"{ns}dependencies")) ?.Elements() .Select(elem => elem.Element(XName.Get($"{ns}artifactId")).Value + "-" + elem.Element(XName.Get($"{ns}version")).Value); if (dependencies != null) { foreach (var dependency in dependencies) { javaStatistics.Dependencies.Add(dependency); } } } else if (fileName == "build.xml" || fileName == "ivy.xml" || fileName == "build.grandle") { } javaStatistics.FilesCount++; } catch (Exception ex) { Logger?.LogInfo(new ErrorMessage(ex.ToString())); } }