[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/vendor/pake/ -> pakeApp.class.php (source)

   1  <?php
   2  
   3  /**
   4   * @package    pake
   5   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
   6   * @copyright  2004-2005 Fabien Potencier <fabien.potencier@symfony-project.com>
   7   * @license    see the LICENSE file included in the distribution
   8   * @version    SVN: $Id: pakeApp.class.php 2574 2006-10-31 06:44:28Z fabien $
   9   */
  10  
  11  /**
  12   *
  13   * main pake class.
  14   *
  15   * This class is a singleton.
  16   *
  17   * @package    pake
  18   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  19   * @copyright  2004-2005 Fabien Potencier <fabien.potencier@symfony-project.com>
  20   * @license    see the LICENSE file included in the distribution
  21   * @version    SVN: $Id: pakeApp.class.php 2574 2006-10-31 06:44:28Z fabien $
  22   */
  23  class pakeApp
  24  {
  25    const VERSION = '1.1.DEV';
  26  
  27    private static $MAX_LINE_SIZE = 65;
  28    private static $PROPERTIES = array();
  29    private static $PAKEFILES = array('pakefile', 'Pakefile', 'pakefile.php', 'Pakefile.php');
  30    private static $PLUGINDIRS = array();
  31    private static $OPTIONS = array(
  32      array('--dry-run',  '-n', pakeGetopt::NO_ARGUMENT,       "Do a dry run without executing actions."),
  33      array('--help',     '-H', pakeGetopt::NO_ARGUMENT,       "Display this help message."),
  34      array('--libdir',   '-I', pakeGetopt::REQUIRED_ARGUMENT, "Include LIBDIR in the search path for required modules."),
  35      array('--nosearch', '-N', pakeGetopt::NO_ARGUMENT,       "Do not search parent directories for the pakefile."),
  36      array('--prereqs',  '-P', pakeGetopt::NO_ARGUMENT,       "Display the tasks and dependencies, then exit."),
  37      array('--quiet',    '-q', pakeGetopt::NO_ARGUMENT,       "Do not log messages to standard output."),
  38      array('--pakefile', '-f', pakeGetopt::REQUIRED_ARGUMENT, "Use FILE as the pakefile."),
  39      array('--require',  '-r', pakeGetopt::REQUIRED_ARGUMENT, "Require MODULE before executing pakefile."),
  40      array('--tasks',    '-T', pakeGetopt::NO_ARGUMENT,       "Display the tasks and dependencies, then exit."),
  41      array('--trace',    '-t', pakeGetopt::NO_ARGUMENT,       "Turn on invoke/execute tracing, enable full backtrace."),
  42      array('--usage',    '-h', pakeGetopt::NO_ARGUMENT,       "Display usage."),
  43      array('--verbose',  '-v', pakeGetopt::NO_ARGUMENT,       "Log message to standard output (default)."),
  44      array('--version',  '-V', pakeGetopt::NO_ARGUMENT,       "Display the program version."),
  45    );
  46  
  47    private $opt = null;
  48    private $nosearch = false;
  49    private $trace = false;
  50    private $verbose = true;
  51    private $dryrun = false;
  52    private $nowrite = false;
  53    private $show_tasks = false;
  54    private $show_prereqs = false;
  55    private $pakefile = '';
  56    private static $instance = null;
  57  
  58    private function __construct()
  59    {
  60      self::$PLUGINDIRS[] = dirname(__FILE__).'/tasks';
  61    }
  62  
  63    public static function get_plugin_dirs()
  64    {
  65      return self::$PLUGINDIRS;
  66    }
  67  
  68    public function get_properties()
  69    {
  70      return self::$PROPERTIES;
  71    }
  72  
  73    public function set_properties($properties)
  74    {
  75      self::$PROPERTIES = $properties;
  76    }
  77  
  78    public static function get_instance()
  79    {
  80      if (!self::$instance) self::$instance = new pakeApp();
  81  
  82      return self::$instance;
  83    }
  84  
  85    public function get_verbose()
  86    {
  87      return $this->verbose;
  88    }
  89  
  90    public function get_trace()
  91    {
  92      return $this->trace;
  93    }
  94  
  95    public function get_dryrun()
  96    {
  97      return $this->dryrun;
  98    }
  99  
 100    public function run($pakefile = null, $options = null, $load_pakefile = true)
 101    {
 102      if ($pakefile)
 103      {
 104        pakeApp::$PAKEFILES = array($pakefile);
 105      }
 106  
 107      $this->handle_options($options);
 108      if ($load_pakefile)
 109      {
 110        $this->load_pakefile();
 111      }
 112  
 113      if ($this->show_tasks)
 114      {
 115        $this->display_tasks_and_comments();
 116      }
 117      else if ($this->show_prereqs)
 118      {
 119        $this->display_prerequisites();
 120      }
 121      else
 122      {
 123        $args = $this->opt->get_arguments();
 124        $task = array_shift($args);
 125  
 126        $abbrev_options = $this->abbrev(array_keys(pakeTask::get_tasks()));
 127        $task = pakeTask::get_full_task_name($task);
 128        if (!$task)
 129        {
 130          $task = 'default';
 131        }
 132  
 133        if (!array_key_exists($task, $abbrev_options))
 134        {
 135          throw new pakeException(sprintf('Task "%s" is not defined.', $task));
 136        }
 137        else if (count($abbrev_options[$task]) > 1)
 138        {
 139          throw new pakeException(sprintf('Task "%s" is ambiguous (%s).', $task, implode(', ', $abbrev_options[$task])));
 140        }
 141        else
 142        {
 143          return pakeTask::get($abbrev_options[$task][0])->invoke($args);
 144        }
 145      }
 146    }
 147  
 148    // Read and handle the command line options.
 149    public function handle_options($options = null)
 150    {
 151      $this->opt = new pakeGetopt(pakeApp::$OPTIONS);
 152      $this->opt->parse($options);
 153      foreach ($this->opt->get_options() as $opt => $value)
 154      {
 155        $this->do_option($opt, $value);
 156      }
 157    }
 158  
 159    // True if one of the files in RAKEFILES is in the current directory.
 160    // If a match is found, it is copied into @pakefile.
 161    public function have_pakefile()
 162    {
 163      foreach (pakeApp::$PAKEFILES as $file)
 164      {
 165        if (file_exists($file))
 166        {
 167          $this->pakefile = $file;
 168          return true;
 169        }
 170      }
 171  
 172      return false;
 173    }
 174  
 175    public function load_pakefile()
 176    {
 177      $here = getcwd();
 178      while (!$this->have_pakefile())
 179      {
 180        chdir('..');
 181        if (getcwd() == $here || $this->nosearch)
 182        {
 183          throw new pakeException(sprintf('No pakefile found (looking for: %s)', join(', ', pakeApp::$PAKEFILES))."\n");
 184        }
 185  
 186        $here = getcwd();
 187      }
 188  
 189      require_once($this->pakefile);
 190    }
 191  
 192    // Do the option defined by +opt+ and +value+.
 193    public function do_option($opt, $value)
 194    {
 195      switch ($opt)
 196      {
 197        case 'dry-run':
 198          $this->verbose = true;
 199          $this->nowrite = true;
 200          $this->dryrun = true;
 201          $this->trace = true;
 202          break;
 203        case 'help':
 204          $this->help();
 205          exit();
 206        case 'libdir':
 207          set_include_path($value.PATH_SEPARATOR.get_include_path());
 208          break;
 209        case 'nosearch':
 210          $this->nosearch = true;
 211          break;
 212        case 'prereqs':
 213          $this->show_prereqs = true;
 214          break;
 215        case 'quiet':
 216          $this->verbose = false;
 217          break;
 218        case 'pakefile':
 219          pakeApp::$PAKEFILES = array($value);
 220          break;
 221        case 'require':
 222          require $value;
 223          break;
 224        case 'tasks':
 225          $this->show_tasks = true;
 226          break;
 227        case 'trace':
 228          $this->trace = true;
 229          $this->verbose = true;
 230          break;
 231        case 'usage':
 232          $this->usage();
 233          exit();
 234        case 'verbose':
 235          $this->verbose = true;
 236          break;
 237        case 'version':
 238          echo sprintf('pake version %s', pakeColor::colorize(pakeApp::VERSION, 'INFO'))."\n";
 239          exit();
 240        default:
 241          throw new pakeException(sprintf("Unknown option: %s", $opt));
 242      }
 243    }
 244  
 245    // Display the program usage line.
 246    public function usage()
 247    {
 248      echo "pake [-f pakefile] {options} targets...\n".pakeColor::colorize("Try pake -H for more information", 'INFO')."\n";
 249    }
 250  
 251    // Display the rake command line help.
 252    public function help()
 253    {
 254      $this->usage();
 255      echo "\n";
 256      echo "available options:";
 257      echo "\n";
 258  
 259      foreach (pakeApp::$OPTIONS as $option)
 260      {
 261        list($long, $short, $mode, $comment) = $option;
 262        if ($mode == pakeGetopt::REQUIRED_ARGUMENT)
 263        {
 264          if (preg_match('/\b([A-Z]{2,})\b/', $comment, $match))
 265            $long .= '='.$match[1];
 266        }
 267        printf("  %-20s (%s)\n", pakeColor::colorize($long, 'INFO'), pakeColor::colorize($short, 'INFO'));
 268        printf("      %s\n", $comment);
 269      }
 270    }
 271  
 272    // Display the tasks and dependencies.
 273    public function display_tasks_and_comments()
 274    {
 275      $width = 0;
 276      $tasks = pakeTask::get_tasks();
 277      foreach ($tasks as $name => $task)
 278      {
 279        $w = strlen(pakeTask::get_mini_task_name($name));
 280        if ($w > $width) $width = $w;
 281      }
 282      $width += strlen(pakeColor::colorize(' ', 'INFO'));
 283  
 284      echo "available pake tasks:\n";
 285  
 286      // display tasks
 287      $has_alias = false;
 288      ksort($tasks);
 289      foreach ($tasks as $name => $task)
 290      {
 291        if ($task->get_alias())
 292        {
 293          $has_alias = true;
 294        }
 295  
 296        if (!$task->get_alias() && $task->get_comment())
 297        {
 298          $mini_name = pakeTask::get_mini_task_name($name);
 299          printf('  %-'.$width.'s > %s'."\n", pakeColor::colorize($mini_name, 'INFO'), $task->get_comment().($mini_name != $name ? ' ['.$name.']' : ''));
 300        }
 301      }
 302  
 303      if ($has_alias)
 304      {
 305        print("\ntask aliases:\n");
 306  
 307        // display aliases
 308        foreach ($tasks as $name => $task)
 309        {
 310          if ($task->get_alias())
 311          {
 312            $mini_name = pakeTask::get_mini_task_name($name);
 313            printf('  %-'.$width.'s = pake %s'."\n", pakeColor::colorize(pakeTask::get_mini_task_name($name), 'INFO'), $task->get_alias().($mini_name != $name ? ' ['.$name.']' : ''));
 314          }
 315        }
 316      }
 317    }
 318  
 319    // Display the tasks and prerequisites
 320    public function display_prerequisites()
 321    {
 322      foreach (pakeTask::get_tasks() as $name => $task)
 323      {
 324        echo "pake ".pakeTask::get_mini_task_name($name)."\n";
 325        foreach ($task->get_prerequisites() as $prerequisite)
 326        {
 327          echo "    $prerequisite\n";
 328        }
 329      }
 330    }
 331  
 332    public static function get_files_from_argument($arg, $target_dir = '', $relative = false)
 333    {
 334      $files = array();
 335      if (is_array($arg))
 336      {
 337        $files = $arg;
 338      }
 339      else if (is_string($arg))
 340      {
 341        $files[] = $arg;
 342      }
 343      else if ($arg instanceof pakeFinder)
 344      {
 345        $files = $arg->in($target_dir);
 346      }
 347      else
 348      {
 349        throw new pakeException('Wrong argument type (must be a list, a string or a pakeFinder object).');
 350      }
 351  
 352      if ($relative && $target_dir)
 353      {
 354        $files = preg_replace('/^'.preg_quote(realpath($target_dir), '/').'/', '', $files);
 355  
 356        // remove leading /
 357        $files = array_map(create_function('$f', 'return 0 === strpos($f, DIRECTORY_SEPARATOR) ? substr($f, 1) : $f;'), $files);
 358      }
 359  
 360      return $files;
 361    }
 362  
 363    public static function excerpt($text, $size = null)
 364    {
 365      if (!$size)
 366      {
 367        $size = self::$MAX_LINE_SIZE;
 368      }
 369  
 370      if (strlen($text) < $size)
 371      {
 372        return $text;
 373      }
 374  
 375      $subsize = floor(($size - 3) / 2);
 376  
 377      return substr($text, 0, $subsize).pakeColor::colorize('...', 'INFO').substr($text, -$subsize);
 378    }
 379  
 380    /* see perl Text::Abbrev module */
 381    private function abbrev($options)
 382    {
 383      $abbrevs = array();
 384      $table = array();
 385  
 386      foreach ($options as $option)
 387      {
 388        $option = pakeTask::get_mini_task_name($option);
 389  
 390        for ($len = (strlen($option)) - 1; $len > 0; --$len)
 391        {
 392          $abbrev = substr($option, 0, $len);
 393          if (!array_key_exists($abbrev, $table))
 394            $table[$abbrev] = 1;
 395          else
 396            ++$table[$abbrev];
 397  
 398          $seen = $table[$abbrev];
 399          if ($seen == 1)
 400          { 
 401            // we're the first word so far to have this abbreviation.
 402            $abbrevs[$abbrev] = array($option);
 403          }
 404          else if ($seen == 2)
 405          { 
 406            // we're the second word to have this abbreviation, so we can't use it.
 407            //unset($abbrevs[$abbrev]);
 408            $abbrevs[$abbrev][] = $option;
 409          }
 410          else
 411          { 
 412            // we're the third word to have this abbreviation, so skip to the next word.
 413            continue;
 414          }
 415        }
 416      }
 417  
 418      // Non-abbreviations always get entered, even if they aren't unique
 419      foreach ($options as $option)
 420      {
 421        $abbrevs[$option] = array($option);
 422      }
 423  
 424      return $abbrevs;
 425    }
 426  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7