Exemplo n.º 1
0
        /// <summary>
        /// Save the report to a temp file
        /// </summary>
        /// <returns>The path to the saved temp file.</returns>
        private static string GetReportTempPath(ToolMacroInfo toolMacroInfo)
        {
            SrmDocument doc        = toolMacroInfo.Doc;
            string      reportName = toolMacroInfo.ReportName;
            string      toolTitle  = toolMacroInfo.ToolTitle;

            if (String.IsNullOrEmpty(reportName))
            {
                throw new Exception(string.Format(Resources.ToolMacros_GetReportTempPath_The_selected_tool_0_requires_a_selected_report_Please_select_a_report_for_this_tool_,
                                                  toolTitle));
            }

            var tempFilePath = GetReportTempPath(reportName, toolTitle);

            try
            {
                using (var saver = new FileSaver(tempFilePath))
                {
                    if (!saver.CanSave())
                    {
                        throw new IOException();
                    }
                    using (var writer = new StreamWriter(saver.SafeName))
                    {
                        ToolDescriptionHelpers.GetReport(doc, reportName, toolTitle, toolMacroInfo.ProgressMonitor, writer);
                    }
                    saver.Commit();
                    return(tempFilePath);
                }
            }
            catch (Exception)
            {
                throw new IOException(Resources.ToolMacros_GetReportTempPath_Error_exporting_the_report__tool_execution_canceled_);
            }
        }
Exemplo n.º 2
0
        private void PostToLinkBackground(string url, SrmDocument doc, IProgressMonitor progressMonitor, IWebHelpers webHelpers)
        {
            StringWriter report = new StringWriter();

            ToolDescriptionHelpers.GetReport(doc, ReportTitle, Title, progressMonitor, report);
            webHelpers.PostToLink(url, report.ToString());
        }
Exemplo n.º 3
0
        private void PostToLinkBackground(string url, SrmDocument doc, IProgressMonitor progressMonitor, IWebHelpers webHelpers)
        {
            string report = ToolDescriptionHelpers.GetReport(doc, ReportTitle, Title, progressMonitor);

            if (report != null)
            {
                webHelpers.PostToLink(url, report);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        ///  Method used to encapsulate the running of a executable for threading.
        /// </summary>
        /// <param name="document"> Contains the document to base reports off of, as well as to serve as the parent for args collector forms. </param>
        /// <param name="toolMacroProvider"> Interface for determining what to replace macros with. </param>
        /// <param name="textWriter"> A textWriter to write to if outputting to the immediate window. </param>
        /// <param name="progressMonitor"> Progress monitor. </param>
        /// <param name="parent">If there is an Args Collector form, it will be showed on this control. Can be null. </param>
        private void RunExecutableBackground(SrmDocument document, IToolMacroProvider toolMacroProvider, TextWriter textWriter, IProgressMonitor progressMonitor, Control parent)
        {
            // Need to know if $(InputReportTempPath) is an argument to determine if a report should be piped to stdin or not.
            bool   containsInputReportTempPath = Arguments.Contains(ToolMacros.INPUT_REPORT_TEMP_PATH);
            string command = GetCommand(document, toolMacroProvider, progressMonitor);

            if (command == null) // Has already thrown the error.
            {
                return;
            }
            string args    = GetArguments(document, toolMacroProvider, progressMonitor);
            string initDir = GetInitialDirectory(document, toolMacroProvider, progressMonitor); // If either of these fails an Exception is thrown.

            if (args != null && initDir != null)
            {
                ProcessStartInfo startInfo = new ProcessStartInfo(command, args)
                {
                    WorkingDirectory = initDir
                };
                if (OutputToImmediateWindow)
                {
                    startInfo.RedirectStandardOutput = true;
                    startInfo.RedirectStandardError  = true;
                    startInfo.CreateNoWindow         = true;
                    startInfo.UseShellExecute        = false;
                    startInfo.StandardOutputEncoding = Encoding.UTF8;
                    startInfo.StandardErrorEncoding  = Encoding.UTF8;
                }

                // if it has a selected report title and its doesn't have a InputReportTempPath macro then the report needs to be piped to stdin.
                string reportCsv = null;
                if (!string.IsNullOrEmpty(ReportTitle) && !containsInputReportTempPath) // Then pipe to stdin.
                {
                    reportCsv = ToolDescriptionHelpers.GetReport(document, ReportTitle, Title, progressMonitor);
                    startInfo.RedirectStandardInput = true;
                }

                //Consider: Maybe throw an error if one is not null but the other is?
                //If there is an IToolArgsCollector run it!
                if (!string.IsNullOrEmpty(ArgsCollectorDllPath) && !string.IsNullOrEmpty(ArgsCollectorClassName))
                {
                    string pathReportCsv = !string.IsNullOrEmpty(ReportTitle) && containsInputReportTempPath
                        ? ToolMacros.GetReportTempPath(ReportTitle, Title)
                        : null;

                    if (!CallArgsCollector(parent, args, reportCsv, pathReportCsv, startInfo))
                    {
                        return;
                    }
                }

                Process p = new Process {
                    StartInfo = startInfo
                };
                if (OutputToImmediateWindow)
                {
                    p.EnableRaisingEvents = true;
                    TextBoxStreamWriterHelper boxStreamWriterHelper = textWriter as TextBoxStreamWriterHelper;
                    if (boxStreamWriterHelper == null)
                    {
                        p.OutputDataReceived += (sender, dataReceivedEventArgs) => textWriter.WriteLine(p.Id +
                                                                                                        ">" + dataReceivedEventArgs.Data); // Not L10N
                        p.ErrorDataReceived += (sender, dataReceivedEventArgs) => textWriter.WriteLine(p.Id +
                                                                                                       ">" + dataReceivedEventArgs.Data);  // Not L10N
                    }
                    else
                    {
                        p.OutputDataReceived += (sender, dataReceivedEventArgs) => boxStreamWriterHelper.WriteLineWithIdentifier(p.Id, dataReceivedEventArgs.Data);
                        p.ErrorDataReceived  += (sender, dataReceivedEventArgs) => boxStreamWriterHelper.WriteLineWithIdentifier(p.Id, dataReceivedEventArgs.Data);
                        //p.Refresh();
                        p.Exited += (sender, processExitedEventArgs) => boxStreamWriterHelper.HandleProcessExit(p.Id);
                    }
                }
//                else
//                {
//                    startInfo.RedirectStandardOutput = true;
//                    startInfo.RedirectStandardError = true;
//                    startInfo.CreateNoWindow = true;
//                    startInfo.UseShellExecute = false;
//                    p.EnableRaisingEvents = true;
//                    p.OutputDataReceived +=
//                        (sender, dataReceivedEventArgs) => Console.WriteLine(dataReceivedEventArgs.Data);
//                    p.ErrorDataReceived +=
//                        (sender, dataReceivedEventArgs) => Console.WriteLine(dataReceivedEventArgs.Data);
//                }
                try
                {
                    p.StartInfo.UseShellExecute = false;
                    p.Start();
                    if (OutputToImmediateWindow)
                    {
                        p.BeginOutputReadLine();
                        p.BeginErrorReadLine();
                    }

                    // write the reportCsv string to stdin.
                    // need to only check one of these conditions.
                    if (startInfo.RedirectStandardInput && (reportCsv != null))
                    {
                        StreamWriter streamWriter = p.StandardInput;
                        streamWriter.Write(reportCsv);
                        streamWriter.Flush();
                        streamWriter.Close();
                    }
                }
                catch (Exception ex)
                {
                    if (ex is Win32Exception)
                    {
                        throw new ToolExecutionException(
                                  TextUtil.LineSeparate(
                                      Resources.ToolDescription_RunTool_File_not_found_,
                                      Resources.ToolDescription_RunTool_Please_check_the_command_location_is_correct_for_this_tool_),
                                  ex);
                    }
                    else
                    {
                        throw new ToolExecutionException(
                                  TextUtil.LineSeparate(
                                      Resources.ToolDescription_RunTool_Please_reconfigure_that_tool__it_failed_to_execute__,
                                      ex.Message),
                                  ex);
                    }
                }

                // CONSIDER: We don't delete the temp path here, because the file may be open
                //           in a long running application like Excel.
//                if (ReportTempPath_toDelete != null)
//                {
//                    FileEx.SafeDelete(ReportTempPath_toDelete, true);
//                    ReportTempPath_toDelete = null;
//                }
            }
        }