private void CreateCompilationDatabase() { File.WriteAllText(_targetDir + "\\" + _fileName + ".json", ""); File.AppendAllText(_targetDir + "\\" + _fileName + ".json", "[\n"); Utility.QueuedFileWriter fileWriter = new Utility.QueuedFileWriter(_fileName + ".json", _targetDir); fileWriter.StartWorking(); try { Multitasking.LimitedThreadsTaskScheduler scheduler = new Multitasking.LimitedThreadsTaskScheduler(_threadCount); TaskFactory factory = new TaskFactory(scheduler); List <Task> tasks = new List <Task>(); int projectsProcessed = 0; foreach (EnvDTE.Project project in _projects.GetRange(0, _projects.Count - 1)) { Logging.Logging.LogInfo("Scheduling project \"" + Logging.Obfuscation.NameObfuscator.GetObfuscatedName(project.Name) + "\" for parsing."); Task task = factory.StartNew(() => { CreateCompilationCommands(fileWriter, project, ref projectsProcessed); }); tasks.Add(task); } int threadCount = System.Diagnostics.Process.GetCurrentProcess().Threads.Count; Task.WaitAll(tasks.ToArray()); CreateCompilationCommands(fileWriter, _projects[_projects.Count - 1], ref projectsProcessed, true); fileWriter.StopWorking(); backgroundWorker1.ReportProgress(100, "Writing data to file. This might take several minutes..."); } catch (Exception e) { Logging.Logging.LogError("Failed to create CDB for solution with exception: " + e.Message); Logging.Logging.LogError("Stack Trace: " + e.StackTrace); } finally { File.AppendAllText(_targetDir + "\\" + _fileName + ".json", "\n]"); } }
private void CreateCompilationCommands(Utility.QueuedFileWriter fileWriter, EnvDTE.Project project, ref int projectsProcessed, bool lastProject = false) { try { SolutionParser.SolutionParser solutionParser = new SolutionParser.SolutionParser(new VsPathResolver(_targetDir)); solutionParser.CreateCompileCommands( project, _configurationName, _platformName, _cStandard, _additionalClangOptions, (CompileCommand command, bool lastFile) => { string serializedCommand = ""; foreach (string line in command.SerializeToJson().Split('\n')) { serializedCommand += " " + line + "\n"; } serializedCommand = serializedCommand.TrimEnd('\n'); fileWriter.PushMessage(serializedCommand + (lastProject && lastFile ? "\n" : ",\n")); } ); lock (_lockObject) { projectsProcessed++; } float relativProgress = (float)projectsProcessed / (float)_projects.Count; Logging.Logging.LogInfo("Processing project \"" + Logging.Obfuscation.NameObfuscator.GetObfuscatedName(project.Name) + "\""); backgroundWorker1.ReportProgress((int)(relativProgress * 100), "Processing project \"" + project.Name + "\""); } catch (Exception e) { Logging.Logging.LogError("Failed to create CDB for project with exception: " + e.Message); Logging.Logging.LogError("Stack Trace: " + e.StackTrace); throw; } }