Пример #1
0
        public void TemplateCompilationTests(string path, string assemblyName)
        {
            using (var file = File.OpenRead(path))
                using (var reader = new StreamReader(file))
                {
                    var antlrDirectives = BlockDirectives
                                          .Select(x => new AntlrBlockDirectiveBuilder(x))
                                          .Concat(new CustomDirectiveBuilder[] { new ForeachDirectiveBuilder() })
                                          .ToImmutableList();

                    var expressionTreeFactory = new VelocityExpressionFactory(_binderFactory);
                    var parser = new AntlrVelocityParser(antlrDirectives, expressionTreeFactory);


                    var expressionTree = parser.Parse(reader, assemblyName);
                    if (Compile || ExecuteTemplate)
                    {
                        VelocityDiskCompiler diskCompiler = null;
                        VelocityCompiler     compiler;

                        if (SaveDlls || SaveIl)
                        {
                            diskCompiler = new VelocityDiskCompiler(new AssemblyName(assemblyName), OutputDir);
                            compiler     = diskCompiler;
                        }
                        else
                        {
                            compiler = new VelocityCompiler();
                        }

                        var result = compiler.CompileWithSymbols(expressionTree, assemblyName, path);

                        if (ExecuteTemplate)
                        {
                            var context = new VelocityContext();
                            using (var writer = new StringWriter())
                            {
                                var output = new VelocityOutput(writer);
                                result(context, output);
                            }
                        }

                        if (SaveDlls || SaveIl)
                        {
                            diskCompiler.SaveDll();

                            if (SaveIl)
                            {
                                diskCompiler.SaveIl();
                            }
                        }
                    }
                }
        }
Пример #2
0
        public VelocityRuntime(VelocityRuntimeOptions options)
        {
            Directives            = options.Directives?.ToImmutableArray() ?? DefaultDirectives;
            Globals               = options.Globals?.ToImmutableDictionary() ?? ImmutableDictionary <string, object> .Empty;
            ReduceWhitesapce      = options.ReduceWhitespace;
            OptimizeConstantTypes = true;

            var binderFactory      = options.BinderFactory ?? new ReusableBinderFactory(new BinderFactory());
            var expressionFactorry = OptimizeConstantTypes
                                ? new ConstantTypeOptimisingVelocityExpressionFactory(binderFactory, Globals)
                                : new VelocityExpressionFactory(binderFactory);

            Debug   = options.CompileInDebugMode;
            _parser = new AntlrVelocityParser(Directives, expressionFactorry, options.ReduceWhitespace);

#if NETFRAMEWORK
            _compiler = new VelocityCompiler(debugMode: options.CompileInDebugMode);
#endif
        }