Ejemplo n.º 1
0
        public void Write(IEnumerable <IRow> rows)
        {
            var l = new Cfg.Net.Loggers.MemoryLogger();

            _output.Debug(() => $"Loading template {_output.Connection.Template}");
            var template = _templateReader.Read(_output.Connection.Template, new Dictionary <string, string>(), l);

            if (l.Errors().Any())
            {
                foreach (var error in l.Errors())
                {
                    _output.Error(error);
                }
            }
            else
            {
                using (var service = RazorEngineService.Create(_config)) {
                    //File.WriteAllText(_output.Connection.File, service.RunCompile(template, _output.Connection.Name, typeof(RazorModel), new RazorModel(_output.Process, _output.Entity, rows)));

                    using (var file = new StreamWriter(_output.Connection.File)) {
                        service.RunCompile(template, _output.Connection.Name, file, typeof(RazorModel), new RazorModel(_output.Process, _output.Entity, rows));
                    }

                    // the template must set Model.Entity.Inserts
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Read the script.  The script could be from the content attribute,
        /// from a file referenced in the file attribute, or a combination.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="reader"></param>
        /// <param name="script"></param>
        /// <returns></returns>
        private static string ReadScript(IContext context, IReader reader, Script script)
        {
            var content = string.Empty;

            if (script.Content != string.Empty)
            {
                content += script.Content + "\r\n";
            }

            if (script.File != string.Empty)
            {
                var p        = new Dictionary <string, string>();
                var l        = new Cfg.Net.Loggers.MemoryLogger();
                var response = reader.Read(script.File, p, l);
                if (l.Errors().Any())
                {
                    foreach (var error in l.Errors())
                    {
                        context.Error(error);
                    }
                    context.Error($"Could not load {script.File}.");
                }
                else
                {
                    content += response + "\r\n";
                }
            }

            return(content);
        }
Ejemplo n.º 3
0
        public void Write(IEnumerable <IRow> rows)
        {
            var l = new Cfg.Net.Loggers.MemoryLogger();

            _output.Debug(() => $"Loading template {_output.Connection.Template}");
            var template = _templateReader.Read(_output.Connection.Template, new Dictionary <string, string>(), l);

            template = Regex.Replace(template, "^@model .+$", string.Empty, RegexOptions.Multiline);

            if (l.Errors().Any())
            {
                foreach (var error in l.Errors())
                {
                    _output.Error(error);
                }
            }
            else
            {
                var engine = new RazorEngine();
                IRazorEngineCompiledTemplate <RazorEngineTemplateBase <RazorModel> > compiledTemplate;

                try {
                    compiledTemplate = engine.Compile <RazorEngineTemplateBase <RazorModel> >(template, builder => {
                        builder.AddAssemblyReference(typeof(Configuration.Process));
                        builder.AddAssemblyReference(typeof(Cfg.Net.CfgNode));
                        builder.AddAssemblyReferenceByName("System.Collections");
                    });
                    // doesn't appear to be a way to stream output yet (in this library), so will just write to string and then file
                    var output = compiledTemplate.Run(instance => {
                        instance.Model = new RazorModel()
                        {
                            Process = _output.Process,
                            Entity  = _output.Entity,
                            Rows    = rows
                        };
                    });
                    File.WriteAllText(_output.Connection.File, output);
                } catch (RazorEngineCompilationException ex) {
                    foreach (var error in ex.Errors)
                    {
                        var line = error.Location.GetLineSpan();
                        _output.Error($"C# error on line {line.StartLinePosition.Line}, column {line.StartLinePosition.Character}.");
                        _output.Error(error.GetMessage());
                    }
                    _output.Error(ex.Message.Replace("{", "{{").Replace("}", "}}"));
                    Utility.CodeToError(_output, template);
                } catch (System.AggregateException ex) {
                    _output.Error(ex.Message.Replace("{", "{{").Replace("}", "}}"));
                    foreach (var error in ex.InnerExceptions)
                    {
                        _output.Error(error.Message.Replace("{", "{{").Replace("}", "}}"));
                    }
                    Utility.CodeToError(_output, template);
                }

                // the template must set Model.Entity.Inserts
            }
        }
Ejemplo n.º 4
0
        public string Render()
        {
            var p = new Dictionary <string, string>();
            var l = new Cfg.Net.Loggers.MemoryLogger();

            // get template
            _context.Debug(() => $"Reading {_template.File}");
            var templateContent = _templateReader.Read(_template.File, p, l);

            if (l.Errors().Any())
            {
                foreach (var error in l.Errors())
                {
                    _context.Error(error);
                }
                return(string.Empty);
            }

            // get parameters (other than process)
            var parameters = new ExpandoObject();

            foreach (var parameter in _template.Parameters)
            {
                ((IDictionary <string, object>)parameters).Add(parameter.Name, parameter.Value);
            }
            if (p.Any())
            {
                foreach (var parameter in p)
                {
                    ((IDictionary <string, object>)parameters)[parameter.Key] = parameter.Value;
                }
            }

            try {
                _context.Debug(() => $"Compiling {_template.Name}.");
                return(_service.RunCompile(templateContent, _template.Name, null, new {
                    _context.Process,
                    Parameters = parameters
                }));
            } catch (TemplateCompilationException ex) {
                _context.Error($"Error parsing template {_template.Name}.");
                _context.Error($"There are {ex.CompilerErrors.Count} errors.");
                foreach (var error in ex.CompilerErrors)
                {
                    _context.Error(error.ErrorText);
                }
                Utility.CodeToError(_context, ex.SourceCode);
                return(string.Empty);
            }
        }
Ejemplo n.º 5
0
        public string Render()
        {
            var p = new Dictionary <string, string>();
            var l = new Cfg.Net.Loggers.MemoryLogger();

            // get template
            _context.Debug(() => $"Reading {_template.File}");
            var templateContent = _templateReader.Read(_template.File, p, l);

            if (l.Errors().Any())
            {
                foreach (var error in l.Errors())
                {
                    _context.Error(error);
                }
                return(string.Empty);
            }


            try {
                _context.Debug(() => $"Compiling {_template.Name}.");

                var context = new VelocityContext();
                context.Put("Process", _context.Process);
                foreach (var parameter in _template.Parameters)
                {
                    context.Put(parameter.Name, Constants.ConversionMap[parameter.Type](parameter.Value));
                }
                if (p.Any())
                {
                    foreach (var parameter in p)
                    {
                        context.Put(parameter.Key, parameter.Value);
                    }
                }

                var sb = new StringBuilder();
                using (var sw = new StringWriter(sb)) {
                    NVelocity.App.Velocity.Evaluate(context, sw, string.Empty, templateContent);
                    sw.Flush();
                }
                return(sb.ToString());
            } catch (Exception ex) {
                _context.Error($"Error parsing template {_template.Name}.");
                _context.Error(ex, ex.Message.Replace("{", "{{").Replace("}", "}}"));
                return(string.Empty);
            }
        }
Ejemplo n.º 6
0
        public ActionResponse Execute()
        {
            var response = new ActionResponse();

            using (var cn = _cf.GetConnection()) {
                cn.Open();
                try {
                    if (_node.Command == string.Empty)
                    {
                        var logger = new Cfg.Net.Loggers.MemoryLogger();
                        _node.Command = _commandReader.Read(_node.Url == string.Empty ? _node.File : _node.Url, new Dictionary <string, string>(), logger);
                        foreach (var warning in logger.Warnings())
                        {
                            _context.Warn(warning);
                        }
                        foreach (var error in logger.Errors())
                        {
                            _context.Error(error);
                        }
                    }
                    _node.RowCount = cn.Execute(_node.Command, commandTimeout: _node.TimeOut);
                    var message = $"{(_node.Description == string.Empty ? _node.Type + " action" : "'" + _node.Description + "'")} affected {(_node.RowCount == -1 ? 0 : _node.RowCount)} row{_node.RowCount.Plural()}.";
                    response.Message = message;
                    _context.Info(message);
                } catch (Exception ex) {
                    response.Code    = 500;
                    response.Message = ex.Message + " " + ex.StackTrace + " " + _node.Command.Replace("{", "{{").Replace("}", "}}");
                }
            }
            return(response);
        }
Ejemplo n.º 7
0
        public string Render()
        {
            var p = new Dictionary <string, string>();
            var l = new Cfg.Net.Loggers.MemoryLogger();

            // get template
            _context.Debug(() => $"Reading {_template.File}");
            var templateContent = _templateReader.Read(_template.File, p, l);

            if (l.Errors().Any())
            {
                foreach (var error in l.Errors())
                {
                    _context.Error(error);
                }
                return(string.Empty);
            }

            // get parameters (other than process)
            var parameters = new ExpandoObject();

            foreach (var parameter in _template.Parameters)
            {
                ((IDictionary <string, object>)parameters).Add(parameter.Name, parameter.Value);
            }
            if (p.Any())
            {
                foreach (var parameter in p)
                {
                    ((IDictionary <string, object>)parameters)[parameter.Key] = parameter.Value;
                }
            }

            IRazorEngineCompiledTemplate template;

            try {
                template = _engine.Compile(_context.Operation.Template);
                return(template.Run(new {
                    _context.Process,
                    Parameters = parameters
                }));
            } catch (Exception ex) {
                _context.Error(ex.Message.Replace("{", "{{").Replace("}", "}}"));
                Utility.CodeToError(_context, _context.Operation.Template);
                return(string.Empty);
            }
        }
Ejemplo n.º 8
0
        public ActionResponse Execute()
        {
            var response = new ActionResponse {
                Action = _node
            };

            using (var cn = _cf.GetConnection()) {
                cn.Open();
                try {
                    if (_node.Command == string.Empty)
                    {
                        var logger = new Cfg.Net.Loggers.MemoryLogger();
                        _node.Command = _commandReader.Read(_node.Url == string.Empty ? _node.File : _node.Url, new Dictionary <string, string>(), logger);
                        foreach (var warning in logger.Warnings())
                        {
                            _context.Warn(warning);
                        }
                        foreach (var error in logger.Errors())
                        {
                            _context.Error(error);
                        }
                    }

                    if (_node.Command.Contains("@"))
                    {
                        var parameters = new ExpandoObject();
                        var editor     = (IDictionary <string, object>)parameters;
                        var active     = _context.Process.GetActiveParameters();
                        foreach (var parameter in active)
                        {
                            if (parameter.Name.Contains("."))
                            {
                                parameter.Name = parameter.Name.Replace(".", "_");
                            }
                        }
                        foreach (var name in new AdoParameterFinder().Find(_node.Command).Distinct().ToList())
                        {
                            var match = active.FirstOrDefault(p => p.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
                            if (match != null)
                            {
                                editor[match.Name] = match.Convert(match.Value);
                            }
                        }
                        _node.RowCount = cn.Execute(_node.Command, parameters, commandTimeout: _node.TimeOut);
                    }
                    else
                    {
                        _node.RowCount = cn.Execute(_node.Command, commandTimeout: _node.TimeOut);
                    }
                    var message = $"{(_node.Description == string.Empty ? _node.Type + " action" : "'" + _node.Description + "'")} affected {(_node.RowCount == -1 ? 0 : _node.RowCount)} row{_node.RowCount.Plural()}.";
                    response.Message = message;
                    _context.Info(message);
                } catch (Exception ex) {
                    response.Code    = 500;
                    response.Message = ex.Message + " " + ex.StackTrace + " " + _node.Command.Replace("{", "{{").Replace("}", "}}");
                }
            }
            return(response);
        }
Ejemplo n.º 9
0
        public string Render()
        {
            var p = new Dictionary <string, string>();
            var l = new Cfg.Net.Loggers.MemoryLogger();

            // get template
            _context.Debug(() => string.Format("Reading {0}", _template.File));
            var template = _templateReader.Read(_template.File, p, l);

            if (l.Errors().Any())
            {
                foreach (var error in l.Errors())
                {
                    _context.Error(error);
                }
                return(string.Empty);
            }

            // get parameters (other than process)
            var parameters = new ExpandoObject();

            foreach (var parameter in _template.Parameters)
            {
                ((IDictionary <string, object>)parameters).Add(parameter.Name, parameter.Value);
            }
            if (p.Any())
            {
                foreach (var parameter in p)
                {
                    ((IDictionary <string, object>)parameters)[parameter.Key] = parameter.Value;
                }
            }

            try {
                _context.Debug(() => string.Format("Compiling {0}.", _template.Name));
                return(_templateProcessor.Process(template, _template.Name, null, new {
                    _context.Process,
                    Parameters = parameters
                }));
            } catch (Exception ex) {
                _context.Error("Error parsing template {0}.", _template.Name);
                _context.Error(ex, ex.Message.Replace("{", "{{").Replace("}", "}}"));
                return(string.Empty);
            }
        }
        public void Write(IEnumerable <IRow> rows)
        {
            var l = new Cfg.Net.Loggers.MemoryLogger();

            _output.Debug(() => $"Loading template {_output.Connection.Template}");
            var template = _templateReader.Read(_output.Connection.Template, new Dictionary <string, string>(), l);

            if (l.Errors().Any())
            {
                foreach (var error in l.Errors())
                {
                    _output.Error(error);
                }
            }
            else
            {
                var context = new VelocityContext();
                context.Put("Model", new VelocityModel(_output.Process, _output.Entity, rows));

                using (var file = new StreamWriter(_output.Connection.File)) {
                    NVelocity.App.Velocity.Evaluate(context, file, RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, template);
                }
            }
        }