コード例 #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);
        }
コード例 #2
0
        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;
        }
コード例 #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);
            }
コード例 #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;
        }
コード例 #5
0
        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;
        }
コード例 #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 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));
         }
 }
コード例 #9
0
            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));
            }
コード例 #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;
        }
コード例 #11
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;
        }
コード例 #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));
            }
コード例 #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);
            }
        }
コード例 #14
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>()));
            }
コード例 #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);
            }
コード例 #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);
        }
コード例 #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));
        }
コード例 #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);
            }
コード例 #19
0
ファイル: FilePreProcessor.cs プロジェクト: selony/scriptcs
        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
            };
        }
コード例 #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);
        }
コード例 #21
0
 protected abstract bool ProcessLine(IFileParser parser, FileParserContext context, string line);
コード例 #22
0
ファイル: FilePreProcessor.cs プロジェクト: selony/scriptcs
 protected virtual string GenerateCode(FileParserContext context)
 {
     Guard.AgainstNullArgument("context", context);
     return string.Join(_fileSystem.NewLine, context.BodyLines);
 }
コード例 #23
0
 protected abstract bool ProcessLine(IFileParser parser, FileParserContext context, string line);
コード例 #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);
            }
コード例 #25
0
 public bool ProcessLine(IFileParser parser, FileParserContext context, string line, bool isBeforeCode)
 {            
     return !isBeforeCode && IsShebang(line);
 }
コード例 #26
0
 protected override bool ProcessLine(IFileParser parser, FileParserContext context, string line)
 {
     return true;
 }
コード例 #27
0
 protected override bool ProcessLine(IFileParser parser, FileParserContext context, string line)
 {
     ArgumentParsedCorrectly = GetDirectiveArgument(line) == "argument";
     InheritedProcessLineCalled = true;
     return true;
 }
コード例 #28
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());
            }
コード例 #29
0
ファイル: FileProcessorTests.cs プロジェクト: selony/scriptcs
 protected override bool ProcessLine(IFileParser parser, FileParserContext context, string line)
 {
     InheritedProcessLineCalled = true;
     return true;
 }
コード例 #30
0
ファイル: FilePreProcessor.cs プロジェクト: jden/scriptcs
        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();
        }