Exemplo n.º 1
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);
        }
        protected override bool ProcessLine(IFileParser parser, FileParserContext context, string line)
        {
            var url = GetDirectiveArgument(line);
            var client = new HttpClient();

            var response = client.GetStringAsync(url).Result;

            parser.ParseScript(response.Split(Environment.NewLine.ToCharArray()).ToList(), context);

            return true;
        }
Exemplo n.º 3
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);
            }
Exemplo n.º 4
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;
        }
        protected override bool ProcessLine(IFileParser parser, FileParserContext context, string line)
        {
            var gistId = GetDirectiveArgument(line);
            var files = _Downloader.DownloadGistFiles(gistId);

            foreach (var file in files)
            {
                parser.ParseFile(file, context);
            }

            return true;
        }
Exemplo n.º 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;
        }
Exemplo n.º 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;
        }
 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));
         }
 }
            public void ShouldExpandEnvironmentVariables(
                [Frozen] Mock<IFileSystem> fileSystem,
                LoadLineProcessor processor,
                IFileParser parser)
            {
                // Arrange
                var context = new FileParserContext();
                var line = string.Format("#load %{0}%", EnvVarKey);

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

                // Assert
                fileSystem.Verify(x => x.GetFullPath(EnvVarValue));
            }
Exemplo n.º 10
0
        public bool ProcessLine(IFileParser parser, FileParserContext context, string line, bool isBeforeCode)
        {
            Guard.AgainstNullArgument("context", context);

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

            var @namespace = GetNamespace(line);
            if (!context.Namespaces.Contains(@namespace))
            {
                context.Namespaces.Add(@namespace);
            }

            return true;
        }
        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;
        }
Exemplo n.º 12
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));
            }
Exemplo n.º 13
0
        public virtual void ParseScript(List<string> scriptLines, FileParserContext context)
        {
            Guard.AgainstNullArgument("scriptLines", scriptLines);
            Guard.AgainstNullArgument("context", 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 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>()));
            }
Exemplo n.º 15
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);
            }
Exemplo n.º 16
0
        public bool ProcessLine(IFileParser parser, FileParserContext context, string line, bool isBeforeCode)
        {
            if (!IsDirective(line))
            {
                return false;
            }

            if (!isBeforeCode)
            {
                if (BehaviorAfterCode == Contracts.BehaviorAfterCode.Throw)
                {
                    throw new InvalidDirectiveUseException(string.Format("Encountered {0}directive 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);
        }
Exemplo n.º 17
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));
        }
Exemplo n.º 18
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
                var result = processor.ProcessLine(parser, context, line, true);

                // Assert
                context.References.Count(x => x == name).ShouldEqual(1);
            }
Exemplo n.º 19
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
            };
        }
Exemplo n.º 20
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);

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

            InsertLineDirective(fullPath, scriptLines);
            InDirectory(fullPath, () => ParseScript(scriptLines, context));

            context.LoadedScripts.Add(fullPath);
        }
Exemplo n.º 21
0
 protected abstract bool ProcessLine(IFileParser parser, FileParserContext context, string line);
Exemplo n.º 22
0
 protected virtual string GenerateCode(FileParserContext context)
 {
     Guard.AgainstNullArgument("context", context);
     return string.Join(_fileSystem.NewLine, context.BodyLines);
 }
Exemplo n.º 23
0
 protected abstract bool ProcessLine(IFileParser parser, FileParserContext context, string line);
Exemplo n.º 24
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);
            }
 public bool ProcessLine(IFileParser parser, FileParserContext context, string line, bool isBeforeCode)
 {            
     return !isBeforeCode && IsShebang(line);
 }
Exemplo n.º 26
0
 protected override bool ProcessLine(IFileParser parser, FileParserContext context, string line)
 {
     return true;
 }
Exemplo n.º 27
0
 protected override bool ProcessLine(IFileParser parser, FileParserContext context, string line)
 {
     ArgumentParsedCorrectly = GetDirectiveArgument(line) == "argument";
     InheritedProcessLineCalled = true;
     return true;
 }
            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());
            }
Exemplo n.º 29
0
 protected override bool ProcessLine(IFileParser parser, FileParserContext context, string line)
 {
     InheritedProcessLineCalled = true;
     return true;
 }
Exemplo n.º 30
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();
        }