Beispiel #1
0
Datei: xr.cs Projekt: uffefl/xr
        static void Main(string[] raws)
        {
            var cwd  = new DirectoryInfo(".");
            var args = raws.Select(arg => new Arg {
                arg = arg, cwd = cwd
            }).ToList();
            var text = "";

            while (!args.Empty())
            {
                var arg = args.Pop();
                switch (arg.option)
                {
                // include arguments from external file
                case "args":
                {
                    var filename = args.Pop().arg;
                    var path     = Path.Combine(arg.cwd.FullName, filename);
                    var file     = new FileInfo(path);
                    using (var tr = file.OpenText())
                    {
                        args.Push(tr.Lines().Select(line => new Arg {
                                arg = line, cwd = file.Directory
                            }));
                    }
                    break;
                }

                // include files matching a given pattern
                case "f":                         // current directory only
                case "s":                         // recursive
                {
                    var pathAndPattern = args.Pop().arg;
                    var path           = Path.Combine(arg.cwd.FullName, pathAndPattern);
                    var dir            = new DirectoryInfo(Path.GetDirectoryName(path));
                    var pattern        = Path.GetFileName(path);
                    text = dir.EnumerateFiles(pattern, arg.option == "f" ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories).Aggregate(text, (current, file) => current + file.All());
                    break;
                }

                // perform a regular expression replace operation
                case "re":
                {
                    var regex   = new Regex(args.Pop().arg);
                    var replace = args.Pop().arg.Replace("\\n", "\n").Replace("\\t", "\t").Replace("\\\\", "\\");
                    text = regex.Replace(text, replace);
                    break;
                }

                // keep performing a regular expression replace operation until the buffer stops changing
                case "rei":
                {
                    var regex   = new Regex(args.Pop().arg);
                    var replace = args.Pop().arg.Replace("\\n", "\n").Replace("\\t", "\t").Replace("\\\\", "\\");
                    var was     = text;
                    for (var i = 0; i < 1000; i++)
                    {
                        text = regex.Replace(text, replace);
                        if (was == text)
                        {
                            break;
                        }
                        was = text;
                    }
                    break;
                }

                // run the buffer through an xsl transform
                case "xsl":
                {
                    var filename = Path.Combine(arg.cwd.FullName, args.Pop().arg);
                    var xsl      = new XslCompiledTransform();
                    xsl.Load(filename);
                    var xr = XmlReader.Create(new StringReader(text));
                    var sb = new StringBuilder();
                    var xw = new StringWriter(sb);
                    xsl.Transform(xr, null, xw);
                    text = sb.ToString();
                    break;
                }

                // output text buffer
                case "o":
                {
                    var file = args.Pop().arg;
                    if (file == "-")
                    {
                        Console.Out.Write(text);
                    }
                    else
                    {
                        using (var tw = new FileInfo(Path.Combine(arg.cwd.FullName, file)).CreateText())
                        {
                            tw.Write(text);
                            tw.Flush();
                            tw.Close();
                        }
                    }
                    text = "";
                    break;
                }

                default:
                {
                    Syntax();
                    return;
                }
                }
            }

            if (text != "")
            {
                Console.Out.Write(text);
            }
        }