コード例 #1
0
        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));
        }
コード例 #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);
            }
コード例 #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);
            }
コード例 #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();
            }
コード例 #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);
            }
コード例 #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);
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        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));
        }
コード例 #9
0
        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);
        }
コード例 #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));
            }
コード例 #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
            });
        }
コード例 #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);
        }
コード例 #13
0
            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));
            }
コード例 #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));
            }
コード例 #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);
                }
            }
        }
コード例 #16
0
        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));
            }
        }
コード例 #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));
        }
コード例 #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);
            }
コード例 #19
0
            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>()));
            }
コード例 #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);
            }
コード例 #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());
            }
コード例 #22
0
        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));
        }
コード例 #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);
        }
コード例 #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());
        }
コード例 #25
0
            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);
            }
コード例 #26
0
        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);
        }
コード例 #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
            });
        }
コード例 #28
0
        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);
        }
コード例 #29
0
 protected abstract bool ProcessLine(IFileParser parser, FileParserContext context, string line);
コード例 #30
0
 public void TestInitialize()
 {
     strategyProvider  = GetStrategy;
     fileParserContext = new FileParserContext(strategyProvider);
 }
コード例 #31
0
 protected virtual string GenerateCode(FileParserContext context)
 {
     Guard.AgainstNullArgument("context", context);
     return(string.Join(_fileSystem.NewLine, context.BodyLines));
 }
コード例 #32
0
 protected override bool ProcessLine(IFileParser parser, FileParserContext context, string line)
 {
     InheritedProcessLineCalled = true;
     return(true);
 }
コード例 #33
0
 protected abstract bool ProcessLine(IFileParser parser, FileParserContext context, string line);