Locate() public static méthode

Locate a given file inside multiple base paths. Similar to what is done when looking up program names in $ENV{PATH}.
public static Locate ( IEnumerable bases, string files, bool expect ) : IEnumerable
bases IEnumerable
files string
expect bool Whether we expect a non-empty list
Résultat IEnumerable
Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="base_dir"></param>
        /// <param name="runner"></param>
        /// <param name="tool"></param>
        /// <param name="includes"></param>
        /// <param name="args"></param>
        public static int Execute(string base_dir, string runner, string tool, string[] includes, string[] args)
        {
            // Determine USE_XP path from either environment option or from xp.ini
            var env = System.Environment.GetEnvironmentVariable("USE_XP");
            IEnumerable <string> use_xp = null;

            if (null == env)
            {
                if (!File.Exists(base_dir + "xp.ini"))
                {
                    throw new FileNotFoundException("Cannot find xp.ini in " + base_dir);
                }

                foreach (var line in File.ReadAllLines(base_dir + "xp.ini"))
                {
                    var parsed = line.Split(KVAL_SEPARATOR, 2);
                    if (parsed[KEY] == "use")
                    {
                        use_xp = Paths.Translate(base_dir, parsed[VALUE].Split(PATH_SEPARATOR));
                    }
                }
            }
            else
            {
                use_xp = Paths.Translate(System.Environment.CurrentDirectory, env.Split(PATH_SEPARATOR));
            }

            // Search for tool
            var executor = "php";
            var argv     = String.Format(
                "-dinclude_path=\".;{0}\" -duser_dir=\"{1}\" -dmagic_quotes_gpc=0",
                String.Join(new string(PATH_SEPARATOR), includes),
                String.Join(new string(PATH_SEPARATOR), use_xp.ToArray())
                );

            foreach (var ini in Paths.Locate(use_xp, "php.ini", false))
            {
                foreach (var line in File.ReadAllLines(ini))
                {
                    var parsed = line.Split(KVAL_SEPARATOR, 2);
                    if (parsed[KEY] == "executor")
                    {
                        executor = parsed[VALUE];
                    }
                    else
                    {
                        argv += " -d" + parsed[KEY] + "=\"" + parsed[VALUE] + "\"";
                    }
                }
            }

            // Spawn runtime
            var proc = new System.Diagnostics.Process();

            proc.StartInfo.FileName  = executor;
            proc.StartInfo.Arguments = argv + " \"" + Paths.Locate(use_xp, "tools\\" + runner + ".php", true).First() + "\" " + tool;
            if (args.Length > 0)
            {
                proc.StartInfo.Arguments += " \"" + String.Join("\" \"", args) + "\"";
            }
            proc.StartInfo.UseShellExecute = false;
            try
            {
                proc.Start();
                proc.WaitForExit();
                return(proc.ExitCode);
            }
            finally
            {
                proc.Close();
            }
        }
Exemple #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="base_dir"></param>
        /// <param name="runner"></param>
        /// <param name="tool"></param>
        /// <param name="includes"></param>
        /// <param name="args"></param>
        public static Process Instance(string base_dir, string runner, string tool, string[] includes, string[] args)
        {
            string home = Environment.GetEnvironmentVariable("HOME");

            // Read configuration
            XpConfigSource configs = new CompositeConfigSource(
                new EnvironmentConfigSource(),
                new IniConfigSource(new Ini(Paths.Compose(".", "xp.ini"))),
                null != home ? new IniConfigSource(new Ini(Paths.Compose(home, ".xp", "xp.ini"))) : null,
                new IniConfigSource(new Ini(Paths.Compose(Environment.SpecialFolder.LocalApplicationData, "Xp", "xp.ini"))),
                new IniConfigSource(new Ini(Paths.Compose(base_dir, "xp.ini")))
                );

            IEnumerable <string> use_xp = configs.GetUse();
            string runtime  = configs.GetRuntime();
            string executor = configs.GetExecutable(runtime) ?? "php";

            if (null == use_xp)
            {
                throw new EntryPointNotFoundException("Cannot determine use_xp setting from " + configs);
            }

            // Pass "USE_XP" and includes inside include_path separated by two path
            // separators. Prepend "." for the oddity that if the first element does
            // not exist, PHP scraps all the others(!)
            //
            // E.g.: -dinclude_path=".;xp\5.7.0;..\dialog;;..\impl.xar;..\log.xar"
            //                       ^ ^^^^^^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^^^^^^
            //                       | |                   include_path
            //                       | USE_XP
            //                       Dot
            string argv = String.Format(
                "-C -q -dinclude_path=\".{1}{0}{1}{1}{2}\" -dmagic_quotes_gpc=0",
                String.Join(PATH_SEPARATOR, new List <string>(use_xp).ToArray()),
                PATH_SEPARATOR,
                String.Join(PATH_SEPARATOR, includes)
                );

            // If input or output encoding are not equal to default, also pass their
            // names inside an LC_CONSOLE environment variable. Only do this inside
            // real Windows console windows!
            //
            // See http://msdn.microsoft.com/en-us/library/system.text.encoding.headername.aspx
            // and http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx
            if (null == Environment.GetEnvironmentVariable("TERM"))
            {
                Encoding defaultEncoding = Encoding.Default;
                if (!defaultEncoding.Equals(Console.InputEncoding) || !defaultEncoding.Equals(Console.OutputEncoding))
                {
                    Environment.SetEnvironmentVariable("LC_CONSOLE", Console.InputEncoding.HeaderName + "," + Console.OutputEncoding.HeaderName);
                }
            }

            // Look for PHP configuration
            foreach (KeyValuePair <string, IEnumerable <string> > kv in configs.GetArgs(runtime))
            {
                foreach (string value in kv.Value)
                {
                    argv += " -d" + kv.Key + "=\"" + value + "\"";
                }
            }

            // Add extensions
            IEnumerable <string> extensions = configs.GetExtensions(runtime);

            if (null != extensions)
            {
                foreach (var ext in extensions)
                {
                    argv += " -dextension=" + ext;
                }
            }

            // Spawn runtime
            var proc = new Process();

            proc.StartInfo.FileName  = executor;
            proc.StartInfo.Arguments = argv + " \"" + new List <string>(Paths.Locate(use_xp, "tools\\" + runner + ".php", true))[0] + "\" " + tool;
            if (args.Length > 0)
            {
                foreach (string arg in args)
                {
                    proc.StartInfo.Arguments += " \"" + arg.Replace("\"", "\"\"\"") + "\"";
                }
            }

            // Catch Ctrl+C (only works in "real" consoles, not in a cygwin
            // shell, for example) and kill the spawned process, see also:
            // http://www.cygwin.com/ml/cygwin/2006-12/msg00151.html
            // http://www.mail-archive.com/[email protected]/msg74638.html
            Console.CancelKeyPress += delegate {
                proc.Kill();
                proc.WaitForExit();
            };

            proc.StartInfo.UseShellExecute = false;
            return(proc);
        }