public virtual void ParseFile(string path, FileParserContext context)
        {
            Guard.AgainstNullArgument("path", path);
            Guard.AgainstNullArgument("context", context);

            var fullPath = _fileSystem.GetFullPath(path);
            var filename = Path.GetFileName(path);

            if (context.LoadedScripts.Contains(fullPath))
            {
                _logger.DebugFormat("Skipping {0} because it's already been loaded.", filename);
                return;
            }

            _logger.DebugFormat("Processing {0}...", filename);

            if (context.ScriptPath == null)
            {
                context.ScriptPath = fullPath;
            }
            else
            {
                // Add script to loaded collection before parsing to avoid loop.
                context.LoadedScripts.Add(fullPath);
            }

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

            InsertLineDirective(fullPath, scriptLines);
            InDirectory(fullPath, () => ParseScript(scriptLines, context));
        }
Exemple #2
0
            public void SetsTheScriptPath()
            {
                var path      = @"c:\test\main.csx";
                var processor = GetFilePreProcessor();
                var context   = new FileParserContext();

                processor.ParseFile(path, context);
                _fileSystem.Verify(x => x.ReadFileLines(It.Is <string>(f => f == path)), Times.Exactly(1));
                context.ScriptPath.ShouldEqual(path);
            }
Exemple #3
0
            public void AddsLoadedScripts()
            {
                var path      = @"c:\test\child.csx";
                var processor = GetFilePreProcessor();
                var context   = new FileParserContext();

                context.ScriptPath = @"c:\test\main.csx";
                processor.ParseFile(path, context);
                _fileSystem.Verify(x => x.ReadFileLines(It.Is <string>(f => f == path)), Times.Exactly(1));
                context.LoadedScripts.ShouldContain(path);
            }
Exemple #4
0
            public void ShouldIgnoreUsingStatic(IFileParser parser, UsingLineProcessor processor)
            {
                // Arrange
                const string UsingLine = "using static System.Console;";
                var          context   = new FileParserContext();

                // Act
                var result = processor.ProcessLine(parser, new FileParserContext(), UsingLine, true);

                // Assert
                result.ShouldBeFalse();
            }
Exemple #5
0
            public void ShouldAddNamespaceToContext(IFileParser parser, UsingLineProcessor processor)
            {
                // Arrange
                const string UsingLine = "using System.Data;";
                var          context   = new FileParserContext();

                // Act
                processor.ProcessLine(parser, context, UsingLine, true);

                // Assert
                context.Namespaces.Count.ShouldEqual(1);
            }
Exemple #6
0
        protected override bool ProcessLine(IFileParser parser, FileParserContext context, string line)
        {
            var argument = GetDirectiveArgument(line);
            var filePath = Environment.ExpandEnvironmentVariables(argument);

            var fullPath = _fileSystem.GetFullPath(filePath);

            if (!string.IsNullOrWhiteSpace(fullPath))
            {
                parser.ParseFile(fullPath, context);
            }

            return(true);
        }
        protected override bool ProcessLine(IFileParser parser, FileParserContext context, string line)
        {
            var argument     = GetDirectiveArgument(line);
            var assemblyPath = Environment.ExpandEnvironmentVariables(argument);

            var referencePath = _fileSystem.GetFullPath(assemblyPath);

            if (!string.IsNullOrWhiteSpace(referencePath) && !context.References.Contains(referencePath))
            {
                context.References.Add(referencePath);
            }

            return(true);
        }
        public bool ProcessLine(IFileParser parser, FileParserContext context, string line, bool isBeforeCode)
        {
            if (!IsDirective(line))
            {
                return(false);
            }

            if (!isBeforeCode && IgnoreAfterCode)
            {
                return(true);
            }

            return(ProcessLine(parser, context, line));
        }
        public bool ProcessLine(IFileParser parser, FileParserContext context, string line, bool isBeforeCode)
        {
            if (!IsDirective(line))
            {
                return false;
            }

            if (!isBeforeCode && IgnoreAfterCode)
            {
                return true;
            }

            return ProcessLine(parser, context, line);
        }
Exemple #10
0
            public void ShouldExpandEnvironmentVariables(
                [Frozen] Mock <IFileSystem> fileSystem,
                ReferenceLineProcessor processor,
                IFileParser parser)
            {
                // Arrange
                var context = new FileParserContext();
                var line    = string.Format("#r %{0}%", EnvVarKey);

                // Act
                processor.ProcessLine(parser, context, line, true);

                // Assert
                fileSystem.Verify(x => x.GetFullPath(EnvVarValue));
            }
Exemple #11
0
        protected virtual FilePreProcessorResult Process(Action <FileParserContext> parseAction)
        {
            var context = new FileParserContext();

            parseAction(context);

            var code = GenerateCode(context);

            return(new FilePreProcessorResult
            {
                Namespaces = context.Namespaces,
                LoadedScripts = context.LoadedScripts,
                References = context.References,
                Code = code
            });
        }
Exemple #12
0
        public bool ProcessLine(IFileParser parser, FileParserContext context, string line, bool isBeforeCode)
        {
            if (!IsUsingLine(line))
            {
                return(false);
            }

            var @namespace = GetNamespace(line);

            if (!context.Namespaces.Contains(@namespace))
            {
                context.Namespaces.Add(@namespace);
            }

            return(true);
        }
            public void ShouldThrowExceptionIfAfterCode(
                [Frozen] Mock <IFileSystem> fileSystem,
                ReferenceLineProcessor processor,
                IFileParser parser)
            {
                // Arrange
                var context = new FileParserContext();

                const string RelativePath = "..\\script.csx";
                const string Line         = @"#r " + RelativePath;
                const string FullPath     = "C:\\script.csx";

                fileSystem.Setup(x => x.GetFullPath(RelativePath)).Returns(FullPath);

                // Act / Assert
                Xunit.Assert.Throws <InvalidDirectiveUseException>(() => processor.ProcessLine(parser, context, Line, false));
            }
Exemple #14
0
            public void ShouldThrowExceptionIfAfterCode(
                [Frozen] Mock <IFileParser> parser,
                [Frozen] Mock <IFileSystem> fileSystem,
                LoadLineProcessor processor)
            {
                // Arrange
                var context = new FileParserContext();

                const string RelativePath = "..\\script.csx";
                const string Line         = @"#load " + RelativePath;
                const string FullPath     = "C:\\script.csx";

                fileSystem.Setup(x => x.GetFullPath(RelativePath)).Returns(FullPath);

                // Act / Assert
                Assert.Throws(typeof(InvalidDirectiveUseException), () => processor.ProcessLine(parser.Object, context, Line, false));
            }
Exemple #15
0
        public virtual void ParseScript(List <string> scriptLines, FileParserContext context)
        {
            var codeIndex = scriptLines.FindIndex(IsNonDirectiveLine);

            for (var index = 0; index < scriptLines.Count; index++)
            {
                var line         = scriptLines[index];
                var isBeforeCode = index < codeIndex || codeIndex < 0;

                var wasProcessed = _lineProcessors.Any(x => x.ProcessLine(this, context, line, isBeforeCode));

                if (!wasProcessed)
                {
                    context.BodyLines.Add(line);
                }
            }
        }
        public override void ParseScript(List <string> scriptLines, FileParserContext context)
        {
            //hack: need to change this to reference a shared binary
            scriptLines.AddRange(_sharedCode);
            var scriptClass = GetScriptClassFromScript(Path.GetFileName(context.LoadedScripts.First()));

            base.ParseScript(scriptLines, context);
            var body = context.BodyLines;

            if (scriptClass != null)
            {
                body.Insert(0, string.Format("public class {0} : {1} {{\r\n", scriptClass.ClassName,
                                             scriptClass.BaseType));
                body.Add("}\r\n");
                body.Add(string.Format("typeof({0})", scriptClass.ClassName));
            }
        }
Exemple #17
0
        public virtual void ParseFile(string path, FileParserContext context)
        {
            var fullPath = _fileSystem.GetFullPath(path);
            var filename = Path.GetFileName(path);

            if (context.LoadedScripts.Contains(fullPath))
            {
                return;
            }

            // Add script to loaded collection before parsing to avoid loop.
            context.LoadedScripts.Add(fullPath);

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

            InsertLineDirective(fullPath, scriptLines);
            InDirectory(fullPath, () => ParseScript(scriptLines, context));
        }
Exemple #18
0
            public void ShouldAddReferenceToContext(
                [Frozen] Mock <IFileSystem> fileSystem,
                ReferenceLineProcessor processor,
                IFileParser parser)
            {
                // Arrange
                var context = new FileParserContext();

                const string RelativePath = "..\\Assembly.dll";
                const string Line         = @"#r " + RelativePath;
                const string FullPath     = "C:\\Assembly.dll";

                fileSystem.Setup(x => x.GetFullPath(RelativePath)).Returns(FullPath);

                // Act
                processor.ProcessLine(parser, context, Line, true);

                // Assert
                context.References.Count.ShouldEqual(1);
            }
            public void ShouldParseLoadedFile(
                [Frozen] Mock <IFileParser> parser,
                [Frozen] Mock <IFileSystem> fileSystem,
                LoadLineProcessor processor)
            {
                // Arrange
                var context = new FileParserContext();

                const string RelativePath = "..\\script.csx";
                const string Line         = @"#load " + RelativePath;
                const string FullPath     = "C:\\script.csx";

                fileSystem.Setup(x => x.GetFullPath(RelativePath)).Returns(FullPath);

                // Act
                processor.ProcessLine(parser.Object, context, Line, true);

                // Assert
                parser.Verify(x => x.ParseFile(FullPath, It.IsAny <FileParserContext>()));
            }
Exemple #20
0
            public void ShouldReturnTrueButNotAddReferenceIfAfterCode(
                [Frozen] Mock <IFileSystem> fileSystem,
                ReferenceLineProcessor processor,
                IFileParser parser)
            {
                // Arrange
                var context = new FileParserContext();

                const string RelativePath = "..\\script.csx";
                const string Line         = @"#r " + RelativePath;
                const string FullPath     = "C:\\script.csx";

                fileSystem.Setup(x => x.GetFullPath(RelativePath)).Returns(FullPath);

                // Act
                var result = processor.ProcessLine(parser, context, Line, false);

                // Assert
                result.ShouldBeTrue();
                context.References.Count.ShouldEqual(0);
            }
Exemple #21
0
            public void ShouldReturnTrueButNotParseFileIfAfterCode(
                [Frozen] Mock <IFileParser> parser,
                [Frozen] Mock <IFileSystem> fileSystem,
                LoadLineProcessor processor)
            {
                // Arrange
                var context = new FileParserContext();

                const string RelativePath = "..\\script.csx";
                const string Line         = @"#load " + RelativePath;
                const string FullPath     = "C:\\script.csx";

                fileSystem.Setup(x => x.GetFullPath(RelativePath)).Returns(FullPath);

                // Act
                var result = processor.ProcessLine(parser.Object, context, Line, false);

                // Assert
                result.ShouldBeTrue();
                parser.Verify(x => x.ParseFile(FullPath, It.IsAny <FileParserContext>()), Times.Never());
            }
        public bool ProcessLine(IFileParser parser, FileParserContext context, string line, bool isBeforeCode)
        {
            if (!Matches(line))
            {
                return(false);
            }

            if (!isBeforeCode)
            {
                if (BehaviorAfterCode == Contracts.BehaviorAfterCode.Throw)
                {
                    throw new InvalidDirectiveUseException(string.Format("Encountered directive '{0}' after the start of code. Please move this directive to the beginning of the file.", DirectiveString));
                }
                else if (BehaviorAfterCode == Contracts.BehaviorAfterCode.Ignore)
                {
                    return(true);
                }
            }

            return(ProcessLine(parser, context, line));
        }
Exemple #23
0
        public bool ProcessLine(IFileParser parser, FileParserContext context, string line, bool isBeforeCode)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (!IsUsingLine(line))
            {
                return(false);
            }

            var @namespace = GetNamespace(line);

            if (!context.Namespaces.Contains(@namespace))
            {
                context.Namespaces.Add(@namespace);
            }

            return(true);
        }
Exemple #24
0
        protected virtual string GenerateCode(FileParserContext context)
        {
            Guard.AgainstNullArgument("context", context);

            var stringBuilder = new StringBuilder();

            var usingLines = context.Namespaces
                             .Where(ns => !string.IsNullOrWhiteSpace(ns))
                             .Select(ns => string.Format("using {0};", ns))
                             .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.BodyLines));

            return(stringBuilder.ToString());
        }
            public void ShouldAddReferenceByNameFromGACIfLocalFileDoesntExist(
                [Frozen] Mock <IFileSystem> fileSystem,
                ReferenceLineProcessor processor,
                IFileParser parser)
            {
                // Arrange
                var context = new FileParserContext();

                var name     = "script.csx";
                var line     = @"#r " + name;
                var fullPath = "C:\\script.csx";

                fileSystem.Setup(x => x.GetFullPath(name)).Returns(fullPath);
                fileSystem.Setup(x => x.FileExists(fullPath)).Returns(false);

                // Act
                processor.ProcessLine(parser, context, line, true);

                // Assert
                context.References.Count(x => x == name).ShouldEqual(1);
            }
        public bool ProcessLine(IFileParser parser, FileParserContext context, string line, bool isBeforeCode)
        {
            if (!Matches(line))
            {
                return false;
            }

            if (!isBeforeCode)
            {
                if (BehaviorAfterCode == Contracts.BehaviorAfterCode.Throw)
                {
                    throw new InvalidDirectiveUseException(string.Format("Encountered directive '{0}' after the start of code. Please move this directive to the beginning of the file.", DirectiveString));
                }
                else if (BehaviorAfterCode == Contracts.BehaviorAfterCode.Ignore)
                {
                    return true;
                }
            }

            return ProcessLine(parser, context, line);
        }
Exemple #27
0
        protected virtual FilePreProcessorResult Process(Action <FileParserContext> parseAction)
        {
            Guard.AgainstNullArgument("parseAction", parseAction);

            var context = new FileParserContext();

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

            parseAction(context);

            var code = GenerateCode(context);

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

            return(new FilePreProcessorResult
            {
                Namespaces = context.Namespaces,
                LoadedScripts = context.LoadedScripts,
                References = context.References,
                Code = code
            });
        }
        public bool ProcessLine(IFileParser parser, FileParserContext context, string line, bool isBeforeCode)
        {
            Guard.AgainstNullArgument("context", context);

            if (!IsUsingLine(line))
            {
                return(false);
            }

            // for using static, we will not extract the import into the context, but rather let it be treated as code
            if (line.Contains(" static "))
            {
                return(false);
            }

            var @namespace = GetNamespace(line);

            if (!context.Namespaces.Contains(@namespace))
            {
                context.Namespaces.Add(@namespace);
            }

            return(true);
        }
 protected abstract bool ProcessLine(IFileParser parser, FileParserContext context, string line);
 public void TestInitialize()
 {
     strategyProvider  = GetStrategy;
     fileParserContext = new FileParserContext(strategyProvider);
 }
Exemple #31
0
 protected virtual string GenerateCode(FileParserContext context)
 {
     Guard.AgainstNullArgument("context", context);
     return(string.Join(_fileSystem.NewLine, context.BodyLines));
 }
Exemple #32
0
 protected override bool ProcessLine(IFileParser parser, FileParserContext context, string line)
 {
     InheritedProcessLineCalled = true;
     return(true);
 }
 protected abstract bool ProcessLine(IFileParser parser, FileParserContext context, string line);