public async Task <ActionResult> AddFileTypeReference([FromBody] FileTypeReference extensionReference) { if (!ModelState.IsValid) { return(BadRequest(ModelState.Values)); } using (var generalService = new GeneralService().BaseRepository <FileTypeReference>()) { var extRef = await generalService.AddDocument(extensionReference).ConfigureAwait(false); return(Ok(extRef)); } }
public async Task <ActionResult> ProcessMethodDetails(string projectId) { var projectMaster = _floKaptureService.ProjectMasterRepository.GetById(projectId); if (projectMaster == null) { return(BadRequest($@"Project with id {projectId} not found!")); } var methodDetailsService = new GeneralService().BaseRepository <MethodDetails>(); var fieldOrPropertyService = new GeneralService().BaseRepository <FieldAndPropertyDetails>(); var allCsFiles = _floKaptureService.FileMasterRepository.GetAllListItems(f => f.ProjectId == projectId && f.FileTypeReferenceId == "60507f66591cfa72c53a859e"); foreach (var fileMaster in allCsFiles) { if (!System.IO.File.Exists(fileMaster.FilePath)) { continue; } if (Regex.IsMatch(fileMaster.FilePath, "reference.cs|Service References|AssemblyInfo.cs", RegexOptions.IgnoreCase)) { continue; } var allLines = System.IO.File.ReadAllText(fileMaster.FilePath); var returnedTuple = CsharpHelper.ExtractMemberDetails(allLines); var methodDetails = returnedTuple.Item1; var patternList = (from mr in methodDetails where mr.ClassName != mr.MethodName select $@"\b{mr.ClassName}.{mr.MethodName}\b").ToList(); if (methodDetails.Any() && patternList.Any()) { string pattern = string.Concat("(?<FoundMatch>", string.Join("|", patternList), ")"); methodDetails.Last().MethodMatchRegex = pattern; } foreach (var methodDetail in methodDetails) { methodDetail.ProjectId = fileMaster.ProjectId; methodDetail.FileId = fileMaster._id; await methodDetailsService.AddDocument(methodDetail).ConfigureAwait(false); } var fieldOrPropertyDetails = returnedTuple.Item2; foreach (var fieldOrProperty in fieldOrPropertyDetails) { fieldOrProperty.ProjectId = fileMaster.ProjectId; fieldOrProperty.FileId = fileMaster._id; await fieldOrPropertyService.AddDocument(fieldOrProperty).ConfigureAwait(false); } } return(Ok(projectMaster)); }
public ActionResult CollectActionWorkflows(string projectId) { var projectMaster = _floKaptureService.ProjectMasterRepository.GetById(projectId); if (projectMaster == null) { return(BadRequest()); } var awRepository = new GeneralService().BaseRepository <ActionWorkflows>(); var allCsFiles = _floKaptureService.FileMasterRepository.GetAllListItems(d => d.FileTypeReferenceId == "60507f66591cfa72c53a859e" && d.ProjectId == projectMaster._id).ToList(); foreach (var fileMaster in allCsFiles) { if (!fileMaster.FileName.EndsWith("svc.cs")) { continue; } var allMethods = _floKaptureService.StatementReferenceMasterRepository.GetAllListItems(d => d.BaseCommandId == 8 && d.FileId == fileMaster._id).ToList(); foreach (var method in allMethods) { if (!Regex.IsMatch(method.ResolvedStatement, @"^public\s*", RegexOptions.IgnoreCase)) { continue; } var aw = new ActionWorkflows { WorkflowName = method.MethodName, FileId = fileMaster._id, OriginEventMethod = method.OriginalStatement, ProjectId = fileMaster.ProjectId, OriginStatementId = method._id }; var actionWorkflow = awRepository.AddDocument(aw).GetAwaiter().GetResult(); Console.WriteLine($"Added action workflow: {actionWorkflow.WorkflowName}"); } } return(Ok($"Collected all action workflows for project: {projectMaster.ProjectName}")); }
private async Task <bool> ProcessFileMasterDetails(ProjectMaster projectMaster) { var entitiesToExcludeService = new GeneralService().BaseRepository <EntitiesToExclude>(); var fileTypeReferences = _floKaptureService.FileTypeReferenceRepository .GetAllListItems(p => p.LanguageId == projectMaster.LanguageId).ToList(); var directoryList = new List <string> { projectMaster.PhysicalPath }; var regExCommented = new Regex(@"^\/\/\*|^\/\*|^\*|^\'", RegexOptions.CultureInvariant); foreach (var directory in directoryList) { try { var fileExtensions = fileTypeReferences.Select(extension => extension.FileExtension); var allFiles = Directory.GetFiles(directory, "*.*", SearchOption.AllDirectories) .Where(s => fileExtensions.Any(e => s.EndsWith(e) || s.ToUpper().EndsWith(e.ToUpper()))).ToList(); foreach (var currentFile in allFiles) { var fileLines = System.IO.File.ReadAllLines(currentFile).ToList(); int lineCount = fileLines.Count(line => !regExCommented.IsMatch(line) || !string.IsNullOrWhiteSpace(line)); // TODO: Look for this in old code // if (ignoredFile.Any(f => f.FileName == Path.GetFileName(currentFile))) continue; var fileName = Path.GetFileName(currentFile); if (string.IsNullOrEmpty(fileName)) { continue; } if (fileName.Contains(".dll.config")) { continue; } var extension = Path.GetExtension(currentFile); var extensionId = fileTypeReferences.First(e => e.FileExtension == extension || string.Equals(e.FileExtension, extension, StringComparison.CurrentCultureIgnoreCase))._id; var fileMaster = new FileMaster { FileName = fileName, FilePath = currentFile, FileTypeReferenceId = extensionId, ProjectId = projectMaster._id, DoneParsing = false, LinesCount = lineCount, Processed = 0 }; var addedDocument = await _floKaptureService.FileMasterRepository.AddDocument(fileMaster) .ConfigureAwait(false); if (projectMaster.IsCtCode && extension == ".icd" && fileMaster.FileName.StartsWith("I_", StringComparison.CurrentCultureIgnoreCase)) { await entitiesToExcludeService.AddDocument(new EntitiesToExclude { FileId = addedDocument._id, FileName = Path.GetFileNameWithoutExtension(currentFile), ProjectId = addedDocument.ProjectId }).ConfigureAwait(false); } } } catch (Exception exception) { Console.WriteLine(exception); } } return(true); }
public async Task <ActionResult> ProcessReferences(string projectId) { MSBuildLocator.RegisterDefaults(); using (var workspace = MSBuildWorkspace.Create()) { try { var projectMaster = _floKaptureService.ProjectMasterRepository.GetById(projectId); if (projectMaster == null) { return(BadRequest($@"Project with id {projectId} not found!")); } var methodReferenceMaster = new GeneralService().BaseRepository <MethodReferenceMaster>(); /* * var previousFile = Path.Combine(projectMaster.PhysicalPath, "reference.json"); * var rawJson = System.IO.File.ReadAllText(previousFile); * var jsonData = JsonConvert.DeserializeObject<Dictionary<string, List<MethodReferenceData>>>(rawJson); */ string slnDirPath = projectMaster.PhysicalPath; var allCsFiles = _floKaptureService.FileMasterRepository.GetAllListItems(d => d.ProjectId == projectMaster._id); workspace.WorkspaceFailed += (o, we) => Console.WriteLine(we.Diagnostic.Message); var solutionFiles = Directory.GetFiles(slnDirPath, "*.sln", SearchOption.TopDirectoryOnly).ToList(); var referenceList = new List <MethodReferenceMaster>(); foreach (var slnFile in solutionFiles) { var solutionPath = Path.Combine(slnDirPath, slnFile); Console.WriteLine($@"Loading solution '{solutionPath}'"); var solution = await workspace.OpenSolutionAsync(solutionPath); Console.WriteLine($@"Finished loading solution '{solutionPath}'"); foreach (var project in solution.Projects) { var compilation = await project.GetCompilationAsync().ConfigureAwait(false); foreach (var document in project.Documents) { if (Regex.IsMatch(document.FilePath, "reference.cs|Service References|AssemblyInfo.cs", RegexOptions.IgnoreCase)) { continue; } var syntaxTree = await document.GetSyntaxTreeAsync().ConfigureAwait(false); // var classDeclarationsOnly = (from d in syntaxTree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>() select d).ToList(); var fileName = Path.GetFileNameWithoutExtension(document.FilePath); var fileMaster = allCsFiles.Find(d => d.FilePath == document.FilePath && d.FileNameWithoutExt == fileName); if (fileMaster == null) { continue; } var methodReferences = new List <MethodReferenceMaster>(); // foreach (var classDeclaration in classDeclarationsOnly) // { var methodList = (from field in syntaxTree.GetRoot().DescendantNodes().OfType <MethodDeclarationSyntax>() select field).ToList(); foreach (var method in methodList) { var functionSymbol = compilation.GetSymbolsWithName(method.Identifier.ValueText).ToList(); foreach (var funDef in functionSymbol) { if (funDef.Locations.Any(d => d.SourceTree.FilePath != document.FilePath)) { continue; } try { var references = await SymbolFinder.FindReferencesAsync(funDef, solution, CancellationToken.None).ConfigureAwait(false); foreach (var referenced in references) { foreach (var location in referenced.Locations) { int lineIndex = location.Location.GetLineSpan().StartLinePosition.Line; var docText = await location.Document.GetTextAsync().ConfigureAwait(false); string callingLine = docText.Lines[lineIndex].ToString(); Console.WriteLine(@"=========================================="); Console.WriteLine($@"Method Name: {method.Identifier.ValueText} "); Console.WriteLine($@"Source File: {Path.GetFileName(document.FilePath)}"); Console.WriteLine($@"Reference: {Path.GetFileName(location.Location.SourceTree.FilePath)}"); Console.WriteLine($@"Actual Line: {callingLine}"); Console.WriteLine($@"Line Index: {lineIndex + 1}"); Console.WriteLine(@"==========================================="); var refFileMaster = allCsFiles.Find(d => d.FilePath == location.Location.SourceTree.FilePath && d.FileName == Path.GetFileName(location.Location.SourceTree.FilePath)); if (refFileMaster == null) { continue; } methodReferences.Add(new MethodReferenceMaster { MethodName = method.Identifier.ValueText, InvocationLocation = location.Location.GetLineSpan().StartLinePosition.Line + 1, ReferencedFileId = refFileMaster._id }); Thread.Sleep(10); } } } catch (Exception exception) { Console.WriteLine(exception.Message); Console.WriteLine($@"There is an issue in method definition: {method.Identifier.ValueText}. \nFile Location: {document.FilePath}"); } } } // } if (!methodReferences.Any()) { continue; } referenceList.Add(new MethodReferenceMaster { SourceFileId = fileMaster._id, SourceFileName = fileMaster.FileName, // MethodReferences = methodReferences }); await methodReferenceMaster.AddDocument(new MethodReferenceMaster { SourceFileId = fileMaster._id, SourceFileName = fileMaster.FileName, // MethodReferences = methodReferences }).ConfigureAwait(false); } } var referenceData = JsonConvert.SerializeObject(referenceList, Formatting.Indented); var slnFileName = Path.GetFileNameWithoutExtension(slnFile); string jsonFile = Path.Combine(slnDirPath, $"{slnFileName}.json"); if (System.IO.File.Exists(jsonFile)) { System.IO.File.Delete(jsonFile); } System.IO.File.WriteAllText(jsonFile, referenceData); } return(Ok($"Reference data has been processed successfully for project: {projectMaster.ProjectName}")); } catch (Exception exception) { return(StatusCode(500, exception)); } } }