/// <summary> /// Performs the main work of this task. /// </summary> /// <param name="LogWriter">A log interface that you can use to report messages.</param> /// <returns>a status code: did it succeed or fail with errors?</returns> public RunStatus Run(ISASTaskTextWriter LogWriter) { try { string code = ""; SAS.Tasks.Toolkit.Data.SasData sd = new SAS.Tasks.Toolkit.Data.SasData(Consumer.ActiveData as ISASTaskData2); DatasetConverter.AssignLocalLibraryIfNeeded(Consumer, this); DatasetConverter convert = new DatasetConverter( sd.Server, sd.Libref, sd.Member); code = convert.GetCompleteSasProgram(OutputData); // this builds up the result structure. It contains the SAS program // that was constructed. By tagging it with the // "application/x-sas" mime type, that serves as a cue // to SAS Enterprise Guide to treat this as the generated SAS // program in the project. _resultInfo = new SAS.Tasks.Toolkit.Helpers.ResultInfo(); if (PreserveEncoding) { _resultInfo.Bytes = System.Text.Encoding.UTF8.GetBytes(code.ToCharArray()); } else { _resultInfo.Bytes = System.Text.Encoding.ASCII.GetBytes(code.ToCharArray()); } _resultInfo.MimeType = "application/x-sas"; _resultInfo.OriginalFileName = string.Format("{0}.sas", Consumer.ActiveData.Member); // this puts a summary message into the Log portion of the // task in SAS Enterprise Guide SAS.Tasks.Toolkit.Helpers.FormattedLogWriter.WriteNormalLine(LogWriter, Messages.ConvertedData); SAS.Tasks.Toolkit.Helpers.FormattedLogWriter.WriteNoteLine(LogWriter, string.Format(Messages.ConvertedDataMetrics, convert.ColumnCount, convert.RowCount)); } catch (Exception ex) { // catch any loose exceptions and report in the log SAS.Tasks.Toolkit.Helpers.FormattedLogWriter.WriteErrorLine(LogWriter, string.Format(Messages.ErrorDuringConversion, ex.ToString())); return(RunStatus.Error); } return(RunStatus.Success); }
/// <summary> /// Performs the main work of this task. /// </summary> /// <param name="LogWriter">A log interface that you can use to report messages.</param> /// <returns>a status code: did it succeed or fail with errors?</returns> public RunStatus Run(ISASTaskTextWriter LogWriter) { try { string code = ""; SAS.Tasks.Toolkit.Data.SasData sd = new SAS.Tasks.Toolkit.Data.SasData(Consumer.ActiveData as ISASTaskData2); DatasetConverter.AssignLocalLibraryIfNeeded(Consumer, this); DatasetConverter convert = new DatasetConverter( sd.Server, sd.Libref, sd.Member); code = convert.GetCompleteSasProgram(OutputData); // this builds up the result structure. It contains the SAS program // that was constructed. By tagging it with the // "application/x-sas" mime type, that serves as a cue // to SAS Enterprise Guide to treat this as the generated SAS // program in the project. _resultInfo = new SAS.Tasks.Toolkit.Helpers.ResultInfo(); if (PreserveEncoding) _resultInfo.Bytes = System.Text.Encoding.UTF8.GetBytes(code.ToCharArray()); else _resultInfo.Bytes = System.Text.Encoding.ASCII.GetBytes(code.ToCharArray()); _resultInfo.MimeType = "application/x-sas"; _resultInfo.OriginalFileName = string.Format("{0}.sas", Consumer.ActiveData.Member); // this puts a summary message into the Log portion of the // task in SAS Enterprise Guide SAS.Tasks.Toolkit.Helpers.FormattedLogWriter.WriteNormalLine(LogWriter, Messages.ConvertedData); SAS.Tasks.Toolkit.Helpers.FormattedLogWriter.WriteNoteLine(LogWriter, string.Format(Messages.ConvertedDataMetrics, convert.ColumnCount, convert.RowCount)); } catch (Exception ex) { // catch any loose exceptions and report in the log SAS.Tasks.Toolkit.Helpers.FormattedLogWriter.WriteErrorLine(LogWriter, string.Format(Messages.ErrorDuringConversion, ex.ToString())); return RunStatus.Error; } return RunStatus.Success; }
/// <summary> /// Note: In the Run method it's a good idea to put *anything* /// that might throw an exception inside a try-catch block. /// And do not throw/rethrow any exceptions! /// /// The host application might run this routine on a different /// thread other than the main application thread, and /// it might not be able to handle an exception thrown /// within here. /// /// Best practice: catch any potential exceptions and then /// write the appropriate information to the log using the /// SAS.Tasks.Toolkit.Helpers.FormattedLogWriter class. If you /// encounter an error, return RunStatus.Error. /// </summary> /// <param name="LogWriter"></param> /// <returns></returns> public RunStatus Run(ISASTaskTextWriter LogWriter) { RunStatus rc = RunStatus.Success; // to keep track of elapsed time for the system commands DateTime start = DateTime.Now; // seed the machine name for use in the log // We need to make it clear that the system commands // are executed on the local machine, not on a // remote SAS server machine. string machineName = "local machine"; try { // Environment.MachineName can throw an InvalidOperationException machineName = Environment.MachineName; } catch (InvalidOperationException) { // couldn't get the machine name } // the "FormattedLogWriter" helps color-code your log output // for NOTE, ERROR, WARNING lines. SAS.Tasks.Toolkit.Helpers.FormattedLogWriter.WriteNoteLine(LogWriter, string.Format("NOTE: Running system commands on {0}. \nOutput:", machineName)); try { string log = ExecuteCommands(); // write the output collected from stdout SAS.Tasks.Toolkit.Helpers.FormattedLogWriter.WriteNormalLine(LogWriter, log); } catch (Exception ex) { // if there is an error, place it in the log SAS.Tasks.Toolkit.Helpers.FormattedLogWriter.WriteErrorLine(LogWriter, string.Format("ERROR: Could not run commands \n{0}",ex.Message)); // return error status so that it gets the "red X" treatment rc = RunStatus.Error; } TimeSpan elapsedTime = TimeSpan.FromTicks(DateTime.Now.Ticks - start.Ticks); SAS.Tasks.Toolkit.Helpers.FormattedLogWriter.WriteNoteLine(LogWriter, string.Format("NOTE: System commands completed. \n\tReal time: {0:F2} seconds", elapsedTime.TotalSeconds)); return rc; }