public override void Handle(TemplateAction action) { if (!_oldValue.Equals(string.Empty)) action.OldValue = _oldValue; if (!_newValue.Equals(string.Empty)) action.NewValue = _newValue; var fileName = string.Empty; if (!string.IsNullOrEmpty(action.File)) { fileName = action.File; } else { if (!string.IsNullOrEmpty(action.RenderedFile)) { fileName = action.RenderedFile; } } var fileInfo = new FileInfo(fileName); if (fileInfo.Exists) { var content = File.ReadAllText(fileInfo.FullName); File.WriteAllText(fileInfo.FullName, content.Replace(action.OldValue, action.NewValue)); _logger.Info("Performed {0} action on {1}.", action.Action, fileInfo.Name); } else { if (action.TemplateName.Equals(string.Empty)) { _logger.Warn("Skipping {0} action. File '{1}' does not exist.", action.Action, fileName); } else { _logger.Warn("Skipping {0} action in {1} template. Niether file '{2}' nor rendered file '{3}' exist.", action.Action, action.TemplateName, action.File, action.RenderedFile); } } }
public override void Handle(TemplateAction action) { if (action.Connection == null) { _logger.Warn("No connection provided for {0}", action.Action); return; } if (!string.IsNullOrEmpty(action.Command)) { var response = action.Connection.ExecuteScript(action.Command, action.Timeout); if (response.Success) { _logger.Info("Command: {0} ran ok.", action.Command.Left(20)+"..."); _logger.Debug("Command: {0} affected {1} rows.", action.Command, response.RowsAffected < 0 ? 0 : response.RowsAffected); } else { _logger.Error("Failed to run command: {0}. {1} Error{2}.", action.Command, response.Messages.Count, response.Messages.Count.Plural()); foreach (var message in response.Messages) { _logger.Error(message); } } return; } var isTemplate = string.IsNullOrEmpty(action.File); var file = isTemplate ? action.RenderedFile : action.File; if (string.IsNullOrEmpty(file) && string.IsNullOrEmpty(action.Command)) { _logger.Warn("Skipping run action. It needs a template file, OR an action file attribute set. Note: the action file superceeds the template file."); return; } var fileInfo = new FileInfo(file); var fileExists = fileInfo.Exists; if (fileExists) { var script = File.ReadAllText(fileInfo.FullName); if (!string.IsNullOrEmpty(script)) { var response = action.Connection.ExecuteScript(script, action.Timeout); if (response.Success) { _logger.Info("{0} ran successfully.", fileInfo.Name); _logger.Debug("{0} affected {1} rows.", fileInfo.Name, response.RowsAffected < 0 ? 0 : response.RowsAffected); } else { _logger.Error("{0} failed. {1} Error{2}.", fileInfo.Name, response.Messages.Count, response.Messages.Count.Plural()); foreach (var message in response.Messages) { _logger.Error(message); } } } else { _logger.Warn("{0} is empty.", fileInfo.Name); } } else { if (isTemplate) _logger.Warn("file rendered output from {0} is not available. It will not run.", action.TemplateName); else _logger.Warn("file {0} output is not available. It will not run.", fileInfo.Name); } }
public override void Handle(TemplateAction action) { var from = string.Empty; var to = string.Empty; var rendered = false; if (!action.From.Equals(string.Empty) && !action.To.Equals(string.Empty)) { from = action.From; to = action.To; } else if (!action.TemplateName.Equals(string.Empty) && !action.RenderedFile.Equals(string.Empty)) { from = action.RenderedFile; to = action.File.Equals(string.Empty) ? action.To : action.File; rendered = true; } var copyType = DeterminCopyType(from, to); if (copyType.Equals(CopyType.Unknown)) { _process.Logger.Warn("Unable to determine copy operation, from {0} to {1}.", from, to); _process.Logger.Warn(action.ProcessName, string.Empty, "From must be a file name, a connection name, or blank to assume output from template."); _process.Logger.Warn(action.ProcessName, string.Empty, "To must be a file name, or a connection name."); _process.Logger.Warn(action.ProcessName, string.Empty, "Skipping {0} action.", action.Action); return; } FileInfo fromInfo; switch (copyType) { case CopyType.ConnectionToConnection: break; case CopyType.ConnectionToFile: break; case CopyType.FileToConnection: fromInfo = new FileInfo(from); if (fromInfo.Exists) { var output = _process.Connections.GetConnectionByName(to.ToLower()).Connection.Source; new FileImporter(_process.Logger).Import(fromInfo.FullName, output); _process.Logger.Info("Copied {0} to {1} connection.", fromInfo.Name, to); } else { _process.Logger.Warn("Unable to copy file {0}. It may not exist.", fromInfo.Name); } break; default: fromInfo = new FileInfo(from); var toInfo = new FileInfo(to); if (fromInfo.Exists && toInfo.Directory != null && toInfo.Directory.Exists) { File.Copy(fromInfo.FullName, toInfo.FullName, true); _process.Logger.Info("Copied {0} to {1}.", rendered ? action.TemplateName + " rendered output" : from, to); } else { _process.Logger.Warn("Unable to copy file {0} to folder {1}. The folder may not exist.", fromInfo.Name, toInfo.DirectoryName); } break; } }
public override void Handle(TemplateAction action) { var name = _logger.Name; var resource = string.IsNullOrEmpty(action.Url) ? action.File : action.Url; var processes = ProcessFactory.Create(resource, _logger, new Options()); foreach (var process in processes) { _logger.Warn("Executing {0}", process.Name); _logger.Name = process.Name; process.ExecuteScaler(); } _logger.Name = name; }
public override void Handle(TemplateAction action) { var actionFile = string.IsNullOrEmpty(action.File) ? string.Empty : new FileInfo(action.File).FullName; var file = actionFile.Equals(string.Empty) ? new FileInfo(action.RenderedFile).FullName : actionFile; if (!file.Equals(string.Empty) && File.Exists(file)) { System.Diagnostics.Process.Start(file); _logger.Info("Opened file {0}.", file); } else { _logger.Warn("Can't open '{0}'.", file); } }
public override void Handle(TemplateAction action) { var method = action.Method.ToLower(); if (!string.IsNullOrEmpty(action.Url)) { var response = method == "post" ? Web.Post(action.Url, action.Timeout, string.Empty) : Web.Get(action.Url, action.Timeout); if (response.Code == HttpStatusCode.OK) { _logger.Info("Made web request to {0}.", action.Url); } else { _logger.Warn("Web request to {0} returned {1}.", action.Url, response.Code); } } else { _logger.Warn("Missing url for web action."); } }
public override void Handle(TemplateAction action) { Dictionary<string, string> parameters = null; if (action.Url.IndexOf('?') > 0) { parameters = Common.ParseQueryString(new Uri(action.Url).Query); } var name = _logger.Name; var processes = ProcessFactory.Create(action.Url, _logger, new Options(), parameters); foreach (var process in processes) { _logger.Warn("Executing {0}", process.Name); _logger.Name = process.Name; process.ExecuteScaler(); } _logger.Name = name; }
public IEnumerable<TemplateAction> Read(List<TflAction> elements) { var actions = new List<TemplateAction>(); foreach (var action in elements) { var modes = action.GetModes(); if (modes.Length > 0 && !modes.Contains("*") && !modes.Any(m => m.Equals(_process.Mode, IC))) { _process.Logger.Debug("Bypassing {0} action in {1} mode.", action.Action, _process.Mode); continue; } var templateAction = new TemplateAction(_process, string.Empty, action); if (!String.IsNullOrEmpty(action.Connection)) { templateAction.Connection = _process.Connections.GetConnectionByName(action.Connection).Connection; } actions.Add(templateAction); } return actions.ToArray(); }
public Dictionary<string, Template> Read() { var templateElements = _elements; var templates = new Dictionary<string, Template>(); foreach (var element in templateElements) { if (!element.Enabled) continue; var reader = element.File.StartsWith("http", IC) ? (ContentsReader)new ContentsWebReader(_process.Logger) : new ContentsFileReader(element.Path, _process.Logger); var template = new Template(_process, element, reader.Read(element.File)); foreach (var parameter in element.Parameters) { template.Parameters[parameter.Name] = new Parameter(parameter.Name, _defaultFactory.Convert(parameter.Value, parameter.Type)); } foreach (var action in element.Actions) { var modes = action.GetModes(); if (modes.Length > 0 && !modes.Contains("*") && !modes.Any(m => m.Equals(_process.Mode, IC))) continue; var templateAction = new TemplateAction(_process, template.Name, action); if (!String.IsNullOrEmpty(action.Connection)) { templateAction.Connection = _process.Connections.GetConnectionByName(action.Connection).Connection; } template.Actions.Add(templateAction); } templates[element.Name] = template; _process.Logger.Debug("Loaded template {0} with {1} parameter{2}.", element.File, template.Parameters.Count, template.Parameters.Count.Plural()); } return templates; }
public IEnumerable <TemplateAction> Read(List <TflAction> elements) { var actions = new List <TemplateAction>(); foreach (var action in elements) { var modes = action.GetModes(); if (modes.Length > 0 && !modes.Contains("*") && !modes.Any(m => m.Equals(_process.Mode, IC))) { _process.Logger.Debug("Bypassing {0} action in {1} mode.", action.Action, _process.Mode); continue; } var templateAction = new TemplateAction(_process, string.Empty, action); if (!String.IsNullOrEmpty(action.Connection)) { templateAction.Connection = _process.Connections.GetConnectionByName(action.Connection).Connection; } actions.Add(templateAction); } return(actions.ToArray()); }
public override void Handle(TemplateAction action) { _process.Logger.Info("Running {0}.", action.File); var fileInfo = new FileInfo(action.File); if (fileInfo.Exists) { var executable = new System.Diagnostics.Process { StartInfo = { UseShellExecute = false, RedirectStandardOutput = true, FileName = fileInfo.FullName, Arguments = action.Arguments, CreateNoWindow = true } }; executable.OutputDataReceived += (sender, args) => _process.Logger.Info(args.Data); executable.Start(); executable.BeginOutputReadLine(); executable.WaitForExit(); } else { _process.Logger.Warn("Couldn't find and execute {0}.", action.File); } }
public override void Handle(TemplateAction action) { var isTemplate = string.IsNullOrEmpty(action.File); var file = isTemplate ? action.RenderedFile : action.File; if (action.To.Equals(string.Empty)) { _logger.Warn("Couldn't send email. No 'to' provided."); return; } var mail = new MailMessage { From = new MailAddress(string.IsNullOrEmpty(action.From) ? action.To : action.From) }; if (action.Connection == null) { _logger.Warn("Couldn't send email. Mail action needs a valid connection."); return; } foreach (var to in action.To.Split(_addressDelimiter)) { mail.To.Add(new MailAddress(to)); } if (!action.Cc.Equals(string.Empty)) { foreach (var cc in action.Cc.Split(_addressDelimiter)) { mail.CC.Add(new MailAddress(cc)); } } if (!action.Bcc.Equals(string.Empty)) { foreach (var bcc in action.Bcc.Split(_addressDelimiter)) { mail.Bcc.Add(new MailAddress(bcc)); } } mail.IsBodyHtml = action.Html; if (!string.IsNullOrEmpty(action.Body)) { mail.Body = action.Body; if (!string.IsNullOrEmpty(file)) { var fileInfo = new FileInfo(file); if (fileInfo.Exists) { mail.Attachments.Add(new Attachment(fileInfo.FullName)); } } } else { if (!string.IsNullOrEmpty(file)) { var fileInfo = new FileInfo(file); if (fileInfo.Exists) { mail.Body = File.ReadAllText(fileInfo.FullName); } } } mail.Subject = action.Subject; try { var connection = ((MailConnection)action.Connection).SmtpClient; connection.Send(mail); _logger.Info(isTemplate ? "Emailed rendered content to: {0}." : "Email sent to {0}.", action.To); } catch (Exception e) { _logger.Warn("Couldn't send mail. {0}", e.Message); _logger.Debug(e.StackTrace); } }
private static string FindFile(TemplateAction action) { var file = action.File; if (!string.IsNullOrEmpty(file)) return file; if (action.Connection != null && (action.Connection.Type == ProviderType.File || action.Connection.Type == ProviderType.Html)) { file = action.Connection.File; } return file; }
public override void Handle(TemplateAction action) { if (action.Connection == null) { _logger.Warn("No connection provided for {0}", action.Action); return; } if (!string.IsNullOrEmpty(action.Command)) { var response = action.Connection.ExecuteScript(action.Command, action.Timeout); if (response.Success) { _logger.Info("Command: {0} ran ok.", action.Command.Left(20) + "..."); _logger.Debug("Command: {0} affected {1} rows.", action.Command, response.RowsAffected < 0 ? 0 : response.RowsAffected); } else { _logger.Error("Failed to run command: {0}. {1} Error{2}.", action.Command, response.Messages.Count, response.Messages.Count.Plural()); foreach (var message in response.Messages) { _logger.Error(message); } } return; } var isTemplate = string.IsNullOrEmpty(action.File); var file = isTemplate ? action.RenderedFile : action.File; if (string.IsNullOrEmpty(file) && string.IsNullOrEmpty(action.Command)) { _logger.Warn("Skipping run action. It needs a template file, OR an action file attribute set. Note: the action file superceeds the template file."); return; } var fileInfo = new FileInfo(file); var fileExists = fileInfo.Exists; if (fileExists) { var script = File.ReadAllText(fileInfo.FullName); if (!string.IsNullOrEmpty(script)) { var response = action.Connection.ExecuteScript(script, action.Timeout); if (response.Success) { _logger.Info("{0} ran successfully.", fileInfo.Name); _logger.Debug("{0} affected {1} rows.", fileInfo.Name, response.RowsAffected < 0 ? 0 : response.RowsAffected); } else { _logger.Error("{0} failed. {1} Error{2}.", fileInfo.Name, response.Messages.Count, response.Messages.Count.Plural()); foreach (var message in response.Messages) { _logger.Error(message); } } } else { _logger.Warn("{0} is empty.", fileInfo.Name); } } else { if (isTemplate) { _logger.Warn("file rendered output from {0} is not available. It will not run.", action.TemplateName); } else { _logger.Warn("file {0} output is not available. It will not run.", fileInfo.Name); } } }