Exemplo n.º 1
0
        protected virtual void ParseFile(string path, FilePreProcessorContext context)
        {
            _logger.DebugFormat("Processing {0}...", Path.GetFileName(path));

            var scriptLines = _fileSystem.ReadFileLines(path).ToList();

            ParseScript(scriptLines, context, path);
        }
Exemplo n.º 2
0
        protected virtual void ProcessLine(FilePreProcessorContext context, string line, bool isBeforeCode)
        {
            Guard.AgainstNullArgument("context", context);

            if (PreProcessorUtil.IsUsingLine(line))
            {
                var @using = PreProcessorUtil.GetPath(PreProcessorUtil.UsingString, line);
                if (!context.UsingStatements.Contains(@using))
                {
                    context.UsingStatements.Add(@using);
                }

                return;
            }

            if (PreProcessorUtil.IsRLine(line))
            {
                if (isBeforeCode)
                {
                    var reference = PreProcessorUtil.GetPath(PreProcessorUtil.RString, line);
                    if (!string.IsNullOrWhiteSpace(reference) && !context.References.Contains(reference))
                    {
                        context.References.Add(reference);
                    }
                }

                return;
            }

            if (PreProcessorUtil.IsLoadLine(line))
            {
                if (isBeforeCode)
                {
                    var filePath = PreProcessorUtil.GetPath(PreProcessorUtil.LoadString, line);
                    if (!string.IsNullOrWhiteSpace(filePath) && !context.LoadedScripts.Contains(filePath))
                    {
                        ParseFile(filePath, context);
                    }
                }

                return;
            }

            // If we've reached this, the line is part of the body...
            context.Body.Add(line);
        }
Exemplo n.º 3
0
        private string GenerateCode(FilePreProcessorContext context)
        {
            var stringBuilder = new StringBuilder();

            var usingLines = context.UsingStatements
                .Select(item => string.Format("using {0};", item))
                .ToList();

            if (usingLines.Count > 0)
            {
                stringBuilder.AppendLine(string.Join(_fileSystem.NewLine, usingLines));
                stringBuilder.AppendLine(); // Insert a blank separator line
            }

            stringBuilder.Append(string.Join(_fileSystem.NewLine, context.Body));

            return stringBuilder.ToString();
        }
Exemplo n.º 4
0
        private FilePreProcessorResult Parse(Action<FilePreProcessorContext> parseAction)
        {
            var context = new FilePreProcessorContext();

            _logger.DebugFormat("Starting pre-processing");

            parseAction(context);

            var code = GenerateCode(context);

            _logger.DebugFormat("Pre-processing finished successfully");

            return new FilePreProcessorResult
            {
                UsingStatements = context.UsingStatements,
                LoadedScripts = context.LoadedScripts,
                References = context.References,
                Code = code
            };
        }
Exemplo n.º 5
0
        protected virtual string GenerateCode(FilePreProcessorContext context)
        {
            Guard.AgainstNullArgument("context", context);

            var stringBuilder = new StringBuilder();

            var usingLines = context.UsingStatements
                             .Select(item => string.Format("using {0};", item))
                             .ToList();

            if (usingLines.Count > 0)
            {
                stringBuilder.AppendLine(string.Join(_fileSystem.NewLine, usingLines));
                stringBuilder.AppendLine(); // Insert a blank separator line
            }

            stringBuilder.Append(string.Join(_fileSystem.NewLine, context.Body));

            return(stringBuilder.ToString());
        }
Exemplo n.º 6
0
        protected virtual FilePreProcessorResult Parse(Action<FilePreProcessorContext> parseAction)
        {
            Guard.AgainstNullArgument("parseAction", parseAction);

            var context = new FilePreProcessorContext();

            _logger.DebugFormat("Starting pre-processing");

            parseAction(context);

            var code = GenerateCode(context);

            _logger.DebugFormat("Pre-processing finished successfully");

            return new FilePreProcessorResult
            {
                UsingStatements = context.UsingStatements,
                LoadedScripts = context.LoadedScripts,
                References = context.References,
                Code = code
            };
        }
Exemplo n.º 7
0
        protected virtual FilePreProcessorResult Parse(Action <FilePreProcessorContext> parseAction)
        {
            Guard.AgainstNullArgument("parseAction", parseAction);

            var context = new FilePreProcessorContext();

            _logger.DebugFormat("Starting pre-processing");

            parseAction(context);

            var code = GenerateCode(context);

            _logger.DebugFormat("Pre-processing finished successfully");

            return(new FilePreProcessorResult
            {
                UsingStatements = context.UsingStatements,
                LoadedScripts = context.LoadedScripts,
                References = context.References,
                Code = code
            });
        }
Exemplo n.º 8
0
        protected virtual void ParseScript(List <string> scriptLines, FilePreProcessorContext context, string path = null)
        {
            Guard.AgainstNullArgument("scriptLines", scriptLines);
            Guard.AgainstNullArgument("context", context);

            // Insert line directive if there's a path
            if (path != null)
            {
                InsertLineDirective(path, scriptLines);
            }

            var codeIndex = scriptLines.FindIndex(PreProcessorUtil.IsNonDirectiveLine);

            for (var index = 0; index < scriptLines.Count; index++)
            {
                ProcessLine(context, scriptLines[index], index < codeIndex || codeIndex < 0);
            }

            if (path != null)
            {
                context.LoadedScripts.Add(path);
            }
        }
Exemplo n.º 9
0
        protected virtual void ParseFile(string path, FilePreProcessorContext context)
        {
            _logger.DebugFormat("Processing {0}...", Path.GetFileName(path));

            var scriptLines = _fileSystem.ReadFileLines(path).ToList();

            ParseScript(scriptLines, context, path);
        }
Exemplo n.º 10
0
        protected virtual void ProcessLine(FilePreProcessorContext context, string line, bool isBeforeCode)
        {
            Guard.AgainstNullArgument("context", context);

            if (PreProcessorUtil.IsUsingLine(line))
            {
                var @using = PreProcessorUtil.GetPath(PreProcessorUtil.UsingString, line);
                if (!context.UsingStatements.Contains(@using))
                {
                    context.UsingStatements.Add(@using);
                }

                return;
            }

            if (PreProcessorUtil.IsRLine(line))
            {
                if (isBeforeCode)
                {
                    var reference = PreProcessorUtil.GetPath(PreProcessorUtil.RString, line);
                    if (!string.IsNullOrWhiteSpace(reference) && !context.References.Contains(reference))
                    {
                        context.References.Add(reference);
                    }
                }

                return;
            }

            if (PreProcessorUtil.IsLoadLine(line))
            {
                if (isBeforeCode)
                {
                    var filePath = PreProcessorUtil.GetPath(PreProcessorUtil.LoadString, line);
                    if (!string.IsNullOrWhiteSpace(filePath) && !context.LoadedScripts.Contains(filePath))
                    {
                        ParseFile(filePath, context);
                    }
                }

                return;
            }

            // If we've reached this, the line is part of the body...
            context.Body.Add(line);
        }
Exemplo n.º 11
0
        protected virtual void ParseScript(List<string> scriptLines, FilePreProcessorContext context, string path = null)
        {
            Guard.AgainstNullArgument("scriptLines", scriptLines);
            Guard.AgainstNullArgument("context", context);

            // Insert line directive if there's a path
            if (path != null) InsertLineDirective(path, scriptLines);

            var codeIndex = scriptLines.FindIndex(PreProcessorUtil.IsNonDirectiveLine);

            for (var index = 0; index < scriptLines.Count; index++)
            {
                ProcessLine(context, scriptLines[index], index < codeIndex || codeIndex < 0);
            }

            if (path != null) context.LoadedScripts.Add(path);
        }
Exemplo n.º 12
0
        private void ParseScript(List<string> scriptLines, FilePreProcessorContext context, string path = null)
        {
            // Insert line directive if there's a path
            if (path != null) InsertLineDirective(path, scriptLines);

            var codeIndex = scriptLines.FindIndex(PreProcessorUtil.IsNonDirectiveLine);

            for (var index = 0; index < scriptLines.Count; index++)
            {
                ProcessLine(context, scriptLines[index], index < codeIndex || codeIndex < 0);
            }

            if (path != null) context.LoadedScripts.Add(path);
        }