Exemple #1
0
        public static void ExportTable(DataTable labelTable, string fileName)
        {
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

            // Construct the excel package with header and contents.
            using (ExcelPackage pck = new ExcelPackage())
            {
                // Create labels sheet & load table.
                ExcelWorksheet labelSheet = pck.Workbook.Worksheets.Add("References");
                labelSheet.Cells["A1"].LoadFromDataTable(labelTable, true);

                // Save the file.
                FileInfo fileInfo = new FileInfo(fileName);
                pck.SaveAs(fileInfo);

                ConsoleUtility.WriteInfo($"Exported to file '{fileInfo.Name}'");

                try
                {
                    // Open the file in the default editor.
                    using (Process proc = new Process())
                    {
                        proc.StartInfo.FileName    = ExcelExe;
                        proc.StartInfo.Arguments   = fileName;
                        proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                        proc.Start();

                        SetForegroundWindow(proc.MainWindowHandle);
                    }
                }
                catch (Exception exp)
                {
                    ConsoleUtility.WriteError(exp.Message);
                    ConsoleUtility.WriteInfo("Error in opening the file. Please open it manually");
                }
            }
        }
        public static void Run()
        {
            try
            {
                ConsoleUtility.WriteInfo("-- Started running reference finder tool --", true);

                string fileSearchPattern = ConfigurationManager.AppSettings["FileSearchPattern"];
                GeneralUtility.ThrowIfInvalid(fileSearchPattern, "file search pattern");

                bool exportFullFileName = Convert.ToBoolean(ConfigurationManager.AppSettings["ExportFullFileName"]);

                DataTable referenceTable = ExportUtility.GetReferenceTable();

                // Process all project base paths.
                List <string> projectBasePaths = ConfigUtil.GetSectionValues("ProjectBasePath");

                if (projectBasePaths == null || !projectBasePaths.Any())
                {
                    ConsoleUtility.WriteWarning("No project base paths found to process");
                    return;
                }

                int tableRowNo = 1;

                foreach (string projectPath in projectBasePaths)
                {
                    string        projectPathFolderName = new DirectoryInfo(projectPath).Name;
                    List <string> files = FileManager.GetFiles(projectPath, fileSearchPattern);

                    ConsoleUtility.WriteInfo($"Processing project path '{projectPathFolderName}'");

                    if (files == null || !files.Any())
                    {
                        ConsoleUtility.WriteWarning($"No files found to process for project path '{projectPathFolderName}'");
                        continue;
                    }

                    ConsoleUtility.WriteInfo($"{files.Count} files found for project path '{projectPathFolderName}'");
                    int cursorTop = Console.CursorTop;
                    int fileIndex = 0;

                    foreach (string file in files)
                    {
                        fileIndex++;

                        ConsoleUtility.ClearLine(cursorTop);
                        ConsoleUtility.GoToLine(cursorTop);
                        ConsoleUtility.WriteStep($"Processing file {fileIndex} of {files.Count} - {Path.GetFileName(file)} of project path '{projectPathFolderName}'");

                        // Delay.
                        GeneralUtility.ProcessDelay();

                        TryConstructDotNetFrameworkReferences(projectPath, file, ref tableRowNo, referenceTable);

                        TryConstructDotNetCoreReferences(projectPath, file, ref tableRowNo, referenceTable);
                    }

                    // Clear the processing line.
                    ConsoleUtility.ClearLine(cursorTop);
                    ConsoleUtility.GoToLine(cursorTop);

                    ConsoleUtility.WriteInfo($"Completed processing {files.Count} files for project path '{projectPathFolderName}'");
                    ConsoleUtility.NewLine();
                }

                if (referenceTable.Rows.Count > 0)
                {
                    ConsoleUtility.WriteInfo($"{referenceTable.Rows.Count} rows identified to export");

                    string fileName = $"{Guid.NewGuid()}.xlsx";
                    ExportUtility.ExportTable(referenceTable, fileName);
                }

                ConsoleUtility.NewLine();
                ConsoleUtility.WriteInfo("-- Reference finder tool ran successfully --");
            }
            catch (Exception exp)
            {
                ConsoleUtility.WriteError(exp.Message);
            }
        }