Пример #1
0
        /// <summary>
        /// Return true if any of the files been modified since last run
        /// </summary>
        /// <param name="filter">Filter.</param>
        public static bool Modified(Rule rule, DateTime last)
        {
            foreach (var filter in rule.Files)
            {
                var watched = filter.Watched;
                filter.Watched = new List<string>();

                SearchOption options = SearchOption.TopDirectoryOnly;
                if (filter.IncludeSubdirectories)
                    options = SearchOption.AllDirectories;
                string[] files;
                if (Directory.Exists(filter.Path))
                    files = Directory.GetFiles(filter.Path, filter.Pattern, options);
                else
                    files = new string[0];
                foreach (string file in files)
                {
                    watched.Remove(file);
                    filter.Watched.Add(file);

                    if (Modified(file, last))
                        return true;
                }
                if (watched.Count > 0)
                    return true;
            }
            return false;
        }
Пример #2
0
        /// <summary>
        /// Froms the text reader.
        /// </summary>
        /// <returns>The text reader.</returns>
        /// <param name="reader"></param>
        /// <param name="path">Base path, location of rules file</param>
        static Rules FromTextReader(TextReader reader, string path)
        {
            var rules = new Rules();
            var list = rules.List;

            Rule r = null;
            string line;
            while (true)
            {
                line = reader.ReadLine();
                if (line == null) //EOF
                    return rules;

                if (line.StartsWith("#!")) //to ignore first #!/... in autoex script file
                    continue; //Ignore comments
                if (line.StartsWith("#runall"))
                {
                    rules.RunAllAtStart = true;
                    continue; //Ignore comments
                }

                string lineTrim = line.Trim(' ', '\t');
                if (lineTrim.StartsWith("//"))
                    continue; //Ignore comments

                if (lineTrim == "")
                    continue;

                if (r == null)
                {
                    r = new Rule();
                    list.Add(r);
                }

                if (line[0] == '\t') //Commands are indented
                    ParseCommand(r, line);
                else
                {
                    //Skip comments
                    if (line[0] == '#')
                        continue;

                    if (r.Commands.Count != 0)
                    {
                        r = new Rule();
                        list.Add(r);
                    }
                    ParsePath(r, line, path);
                }

            }
        }
Пример #3
0
 static void ParseCommand(Rule r, string line)
 {
     line = line.Trim('\t', ' ');
     int sep = line.IndexOf(" ");
     var c = new CommandArgument();
     if (sep < 0)
     {
         c.Command = line;
         c.Arguments = "";
     }
     else
     {
         c.Command = line.Substring(0, sep);
         c.Arguments = line.Substring(sep + 1);
     }
     r.Commands.Add(c);
 }
Пример #4
0
        public void Run(Rule r)
        {
            if (r.Commands.Count == 0)
            {
                r.ExitCode = -1;
                ColorConsole.WriteLine("Warning: no commands for rule", ConsoleColor.Red);
                return;
            }

            r.ExitCode = 0;

            string first = r.Commands[0].Command;

            if (first.StartsWith("#!"))
                RunWithInterpreter(r);
            else
                RunOneByOne(r);
        }
Пример #5
0
        void RunOneByOne(Rule r)
        {
            foreach (CommandArgument args in r.Commands)
            {
                var psi = new ProcessStartInfo();
                psi.FileName = args.Command;
                psi.Arguments = args.Arguments;
                psi.WorkingDirectory = basePath;
                psi.RedirectStandardInput = true;
                psi.RedirectStandardOutput = true;
                psi.RedirectStandardError = true;
                psi.UseShellExecute = false;

                ColorConsole.WriteLine("Executing: " + psi.FileName + " " + psi.Arguments, ConsoleColor.DarkGray);
                Process p = new Process();
                p.StartInfo = psi;
                p.ErrorDataReceived += HandleErrorDataReceived;
                p.OutputDataReceived += HandleOutputDataReceived;

                try
                {
                    p.Start();
                    p.BeginErrorReadLine();
                    p.BeginOutputReadLine();
                    p.WaitForExit();
                    if (p.ExitCode != 0)
                        r.ExitCode = p.ExitCode;
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine(e.Message);
                    r.ExitCode = -1;
                }
                if (p.ExitCode == 0)
                    ColorConsole.WriteLine("Last succeeded", ConsoleColor.Green);
                else
                    ColorConsole.WriteLine("Failed: " + p.ExitCode, ConsoleColor.Red);
            }
        }
Пример #6
0
        void RunWithInterpreter(Rule r)
        {
            var psi = new ProcessStartInfo();
            psi.FileName = r.Commands[0].Command.Substring(2);
            psi.Arguments = r.Commands[0].Arguments;
            psi.WorkingDirectory = basePath;
            psi.RedirectStandardInput = true;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError = true;
            psi.UseShellExecute = false;

            ColorConsole.WriteLine("Executing Interpreter: " + psi.FileName + " " + psi.Arguments, ConsoleColor.DarkGray);

            Process p = new Process();
            p.StartInfo = psi;
            p.ErrorDataReceived += HandleErrorDataReceived;
            p.OutputDataReceived += HandleOutputDataReceived;

            try
            {
                p.Start();
                p.BeginErrorReadLine();
                p.BeginOutputReadLine();

                //Pass remaining commands to pipe
                using (TextWriter tw = p.StandardInput)
                {
                    bool first = true;
                    foreach (var c in r.Commands)
                    {
                        if (first)
                        {
                            first = false;
                            continue;
                        }
                        ColorConsole.WriteLine(c.Command + " " + c.Arguments, ConsoleColor.DarkGray);
                        tw.WriteLine(c.Command + " " + c.Arguments);
                    }
                }

                p.WaitForExit();
                if (p.ExitCode == 0)
                    ColorConsole.WriteLine("Last succeeded", ConsoleColor.Green);
                else
                {
                    r.ExitCode = p.ExitCode;
                    ColorConsole.WriteLine("Failed: " + p.ExitCode, ConsoleColor.Red);
                }
            }
            catch (Exception e)
            {
                ColorConsole.WriteLine("Failed: " + e.Message, ConsoleColor.Red);
                r.ExitCode = -1;
            }
        }
Пример #7
0
        static void ParsePath(Rule r, string line, string rulesDir)
        {
            var filters = line.Split(' ');

            foreach (string f in filters)
            {
                var pf = new PathFilter();
                int lastsep = f.LastIndexOf(Path.DirectorySeparatorChar);
                if (lastsep < 0)
                {
                    pf.Path = rulesDir;
                    pf.Pattern = f;
                }
                else
                {
                    string path = f.Substring(0, lastsep);
                    if (Path.IsPathRooted(path) == false)
                        path = Path.Combine(rulesDir, path);
                    pf.Path = Path.GetFullPath(path);
                    pf.Pattern = f.Substring(lastsep + 1);

                    //If last "/" or "\" is double then include all subdirectories in search
                    if (lastsep > 0 && f[lastsep - 1] == Path.DirectorySeparatorChar)
                        pf.IncludeSubdirectories = true;
                }
                r.Files.Add(pf);
            }
        }