Beispiel #1
0
 /// <summary>Creates the commandline representation from argv and a composer file</summary>
 public CommandLine(string[] argv, ComposerFile composer)
 {
     using (composer)
     {
         Parse(argv, composer);
     }
 }
Beispiel #2
0
        /// <summary>Returns the given module and a unique list of dependencies of a given module</summary>
        private IEnumerable <string> DependenciesAndSelf(string dir, string module, HashSet <string> loaded)
        {
            var path = Paths.Compose(dir, module.Replace('/', Path.DirectorySeparatorChar));

            yield return(path);

            loaded.Add(module);

            using (var composer = new ComposerFile(Paths.Compose(path, ComposerFile.NAME)))
            {
                foreach (var require in composer.Definitions.Require.Where(pair => pair.Key.Contains("/")))
                {
                    if (loaded.Contains(require.Key))
                    {
                        continue;
                    }

                    foreach (var transitive in DependenciesAndSelf(dir, require.Key, loaded))
                    {
                        yield return(transitive);
                    }
                }
            }
        }
Beispiel #3
0
        /// <summary>Parses command line</summary>
        private void Parse(string[] argv, ComposerFile composer)
        {
            var offset = 0;

            for (var i = 0; i < argv.Length; i++)
            {
                if (aliases.ContainsKey(argv[i]))
                {
                    command = Activator.CreateInstance(aliases[argv[i]]) as Command;
                    offset  = ++i;
                    break;
                }
                else if (options.ContainsKey(argv[i]))
                {
                    if (i >= argv.Length - 1)
                    {
                        throw new CannotExecute("Argument `" + argv[i] + "` requires a value");
                    }
                    options[argv[i]](this, argv[++i]);
                    offset = i + 1;
                }
                else if (flags.ContainsKey(argv[i]))
                {
                    flags[argv[i]](this);
                    offset = i + 1;
                }
                else if (IsOption(argv[i]))
                {
                    throw new CannotExecute("Unknown argument `" + argv[i] + "`");
                }
                else if (IsCommand(argv[i]))
                {
                    var name = argv[i];
                    offset = ++i;

                    // Check builtin commands
                    var type = Type.GetType("Xp.Runners.Commands." + name.UpperCaseFirst());
                    if (null != type)
                    {
                        command = Activator.CreateInstance(type) as Command;
                        break;
                    }

                    // Check composer.json for scripts
                    try
                    {
                        if (composer.Definitions.Scripts.ContainsKey(name))
                        {
                            command        = null;
                            executionModel = null;
                            config         = null;
                            Parse(ArgsOf(composer.Definitions.Scripts[name]).Skip(1).ToArray(), ComposerFile.Empty);
                            arguments = arguments.Concat(argv.Skip(offset));
                            return;
                        }
                    }
                    catch (FormatException e)
                    {
                        Console.Error.WriteLine("Warning: {0}", e.Message);
                    }

                    // Otherwise, it's a plugin defined via `bin/xp.{org}.{slug}.{name}`
                    command = new Plugin(name);
                    break;
                }
                else if (!argv[i].EndsWith(".class.php") && !argv[i].EndsWith(".xar") && File.Exists(argv[i]))
                {
                    command = new Script(argv[i]);
                    offset  = i + 1;
                    break;
                }
                else
                {
                    command = new Run();
                    offset  = i;
                    break;
                }
            }

            arguments = argv.Skip(offset);
        }