예제 #1
0
        public void Process(string root, Options options)
        {
            try
            {
                var configPath = Path.Combine(root, "Site.config");
                if (File.Exists(configPath))
                {
                    _config = SiteConfiguration.Load(configPath);
                }
                else
                {
                    _config = new SiteConfiguration();
                }

                if (options.Local)
                    _config.Local = true;
                if (options.Verbose)
                    _config.Verbose = true;

                _context = new SiteContext()
                {
                    Config = _config,
                    Options = options,
                    ProjectDir = root,
                    SourceDir = Path.Combine(root, _siteDirName),
                    DestinationDir = Path.Combine(root, _htmlDirName),
                    CodeDir = Path.Combine(root, _codeDirName),
                    LayoutsDir = Path.Combine(root, _layoutsDirName),
                    IncludesDir = Path.Combine(root, _includesDirName),
                    TemplatesDir = Path.Combine(root, _templatesDirName),
                };

                var resolver = new TemplateResolver(_context);
                var activator = new TemplateActivator(_context);

                var pageConfiguration = new RazorEngine.Configuration.TemplateServiceConfiguration()
                {
                    BaseTemplateType = typeof(PageTemplate<>),
                    Resolver = resolver,
                    Activator = activator
                };

                var startConfiguration = new RazorEngine.Configuration.TemplateServiceConfiguration()
                {
                    BaseTemplateType = typeof(StartTemplate<>),
                    Resolver = resolver,
                    Activator = activator
                };

                _pluginManager = new PluginManager(_context);
                _pluginManager.LoadPlugins();

                _startProcessor = new StartProcessor(_context, startConfiguration);
                _context.InitializeService(pageConfiguration);

                ProcessPages();
                RunGenerators();
            }
            catch (PageProcessingException ex)
            {
                // NOTE: Line number information is inaccurate due to file changes.
                if (ex.InnerException is RazorEngine.Templating.TemplateCompilationException)
                {
                    var inner = (RazorEngine.Templating.TemplateCompilationException)ex.InnerException;
                    
                    Console.Error.WriteLine(ex.Message);

                    foreach (var err in inner.Errors)
                    {
                        string[] lines = ex.Text.Split('\n');
                        Console.Error.WriteLine("{0}({1}): {2}", ex.FileName, err.Line, err.ErrorText);

                        // FIXME: The underlying generated file that caused 
                        // the error is gone at this point.
                        // Console.Error.WriteLine("{0}", GetErrorLines(err));
                    }
                }
                else if (ex.InnerException is RazorEngine.Templating.TemplateParsingException)
                {
                    var inner = (RazorEngine.Templating.TemplateParsingException)ex.InnerException;

                    Console.Error.WriteLine(ex.Message);
                    Console.Error.WriteLine("{0}: {1}", ex.FileName, inner.Message);
                }
                else
                {
                    Console.Error.WriteLine("{0}: {1}", ex.FileName, ex.Message);
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
            }
        }