예제 #1
0
 /**
  * Setup/initialize Phing environment from commandline args.
  * @param array $args commandline args passed to phing shell.
  * @return void
  */
 public function execute($args)
 {
     self::$definedProps = new Properties();
     $this->searchForThis = null;
     // 1) First handle any options which should always
     // Note: The order in which these are executed is important (if multiple of these options are specified)
     if (in_array('-help', $args) || in_array('-h', $args)) {
         $this->printUsage();
         return;
     }
     if (in_array('-version', $args) || in_array('-v', $args)) {
         $this->printVersion();
         return;
     }
     // 2) Next pull out stand-alone args.
     // Note: The order in which these are executed is important (if multiple of these options are specified)
     if (false !== ($key = array_search('-quiet', $args, true))) {
         self::$msgOutputLevel = Project::MSG_WARN;
         unset($args[$key]);
     }
     if (false !== ($key = array_search('-verbose', $args, true))) {
         self::$msgOutputLevel = Project::MSG_VERBOSE;
         unset($args[$key]);
     }
     if (false !== ($key = array_search('-debug', $args, true))) {
         self::$msgOutputLevel = Project::MSG_DEBUG;
         unset($args[$key]);
     }
     // 3) Finally, cycle through to parse remaining args
     //
     $keys = array_keys($args);
     // Use keys and iterate to max(keys) since there may be some gaps
     $max = $keys ? max($keys) : -1;
     for ($i = 0; $i <= $max; $i++) {
         if (!array_key_exists($i, $args)) {
             // skip this argument, since it must have been removed above.
             continue;
         }
         $arg = $args[$i];
         if ($arg == "-logfile") {
             try {
                 // see: http://phing.info/trac/ticket/65
                 if (!isset($args[$i + 1])) {
                     $msg = "You must specify a log file when using the -logfile argument\n";
                     throw new ConfigurationException($msg);
                 } else {
                     $logFile = new PhingFile($args[++$i]);
                     $out = new FileOutputStream($logFile);
                     // overwrite
                     self::setOutputStream($out);
                     self::setErrorStream($out);
                     self::$isLogFileUsed = true;
                 }
             } catch (IOException $ioe) {
                 $msg = "Cannot write on the specified log file. Make sure the path exists and you have write permissions.";
                 throw new ConfigurationException($msg, $ioe);
             }
         } elseif ($arg == "-buildfile" || $arg == "-file" || $arg == "-f") {
             if (!isset($args[$i + 1])) {
                 $msg = "You must specify a buildfile when using the -buildfile argument.";
                 throw new ConfigurationException($msg);
             } else {
                 $this->buildFile = new PhingFile($args[++$i]);
             }
         } elseif ($arg == "-listener") {
             if (!isset($args[$i + 1])) {
                 $msg = "You must specify a listener class when using the -listener argument";
                 throw new ConfigurationException($msg);
             } else {
                 $this->listeners[] = $args[++$i];
             }
         } elseif (StringHelper::startsWith("-D", $arg)) {
             $name = substr($arg, 2);
             $value = null;
             $posEq = strpos($name, "=");
             if ($posEq !== false) {
                 $value = substr($name, $posEq + 1);
                 $name = substr($name, 0, $posEq);
             } elseif ($i < count($args) - 1 && !StringHelper::startsWith("-D", $arg)) {
                 $value = $args[++$i];
             }
             self::$definedProps->setProperty($name, $value);
         } elseif ($arg == "-logger") {
             if (!isset($args[$i + 1])) {
                 $msg = "You must specify a classname when using the -logger argument";
                 throw new ConfigurationException($msg);
             } else {
                 $this->loggerClassname = $args[++$i];
             }
         } elseif ($arg == "-inputhandler") {
             if ($this->inputHandlerClassname !== null) {
                 throw new ConfigurationException("Only one input handler class may be specified.");
             }
             if (!isset($args[$i + 1])) {
                 $msg = "You must specify a classname when using the -inputhandler argument";
                 throw new ConfigurationException($msg);
             } else {
                 $this->inputHandlerClassname = $args[++$i];
             }
         } elseif ($arg == "-longtargets") {
             self::$definedProps->setProperty('phing.showlongtargets', 1);
         } elseif ($arg == "-projecthelp" || $arg == "-targets" || $arg == "-list" || $arg == "-l" || $arg == "-p") {
             // set the flag to display the targets and quit
             $this->projectHelp = true;
         } elseif ($arg == "-find") {
             // eat up next arg if present, default to build.xml
             if ($i < count($args) - 1) {
                 $this->searchForThis = $args[++$i];
             } else {
                 $this->searchForThis = self::DEFAULT_BUILD_FILENAME;
             }
         } elseif (substr($arg, 0, 1) == "-") {
             // we don't have any more args
             self::$err->write("Unknown argument: {$arg}" . PHP_EOL);
             self::printUsage();
             return;
         } else {
             // if it's no other arg, it may be the target
             array_push($this->targets, $arg);
         }
     }
     // if buildFile was not specified on the command line,
     if ($this->buildFile === null) {
         // but -find then search for it
         if ($this->searchForThis !== null) {
             $this->buildFile = $this->_findBuildFile(self::getProperty("user.dir"), $this->searchForThis);
         } else {
             $this->buildFile = new PhingFile(self::DEFAULT_BUILD_FILENAME);
         }
     }
     // make sure buildfile exists
     if (!$this->buildFile->exists()) {
         throw new ConfigurationException("Buildfile: " . $this->buildFile->__toString() . " does not exist!");
     }
     // make sure it's not a directory
     if ($this->buildFile->isDirectory()) {
         throw new ConfigurationException("Buildfile: " . $this->buildFile->__toString() . " is a dir!");
     }
     $this->readyToRun = true;
 }
예제 #2
0
 /**
  * Setup/initialize Phing environment from commandline args.
  * @param array $args commandline args passed to phing shell.
  * @return void
  */
 public function execute($args)
 {
     self::$definedProps = new Properties();
     $this->searchForThis = null;
     // cycle through given args
     for ($i = 0, $argcount = count($args); $i < $argcount; ++$i) {
         // ++$i intentional here, as first param is script name
         $arg = $args[$i];
         if ($arg == "-help" || $arg == "-h") {
             $this->printUsage();
             return;
         } elseif ($arg == "-version" || $arg == "-v") {
             $this->printVersion();
             return;
         } elseif ($arg == "-quiet" || $arg == "-q") {
             self::$msgOutputLevel = PROJECT_MSG_WARN;
         } elseif ($arg == "-verbose") {
             $this->printVersion();
             self::$msgOutputLevel = PROJECT_MSG_VERBOSE;
         } elseif ($arg == "-debug") {
             $this->printVersion();
             self::$msgOutputLevel = PROJECT_MSG_DEBUG;
         } elseif ($arg == "-logfile") {
             try {
                 // try to set logfile
                 if (!isset($args[$i + 1])) {
                     print "You must specify a log file when using the -logfile argument\n";
                     return;
                 } else {
                     $logFile = new PhingFile($args[++$i]);
                     $this->loggerClassname = 'phing.listener.PearLogger';
                     $this->setDefinedProperty('pear.log.name', $logFile->getAbsolutePath());
                 }
             } catch (IOException $ioe) {
                 print "Cannot write on the specified log file. Make sure the path exists and you have write permissions.\n";
                 throw $ioe;
             }
         } elseif ($arg == "-buildfile" || $arg == "-file" || $arg == "-f") {
             if (!isset($args[$i + 1])) {
                 print "You must specify a buildfile when using the -buildfile argument\n";
                 return;
             } else {
                 $this->buildFile = new PhingFile($args[++$i]);
             }
         } elseif ($arg == "-listener") {
             if (!isset($args[$i + 1])) {
                 print "You must specify a listener class when using the -listener argument\n";
                 return;
             } else {
                 $this->listeners[] = $args[++$i];
             }
         } elseif (StringHelper::startsWith("-D", $arg)) {
             $name = substr($arg, 2);
             $value = null;
             $posEq = strpos($name, "=");
             if ($posEq !== false) {
                 $value = substr($name, $posEq + 1);
                 $name = substr($name, 0, $posEq);
             } elseif ($i < count($args) - 1) {
                 $value = $args[++$i];
             }
             self::$definedProps->setProperty($name, $value);
         } elseif ($arg == "-logger") {
             if (!isset($args[$i + 1])) {
                 print "You must specify a classname when using the -logger argument\n";
                 return;
             } else {
                 $this->loggerClassname = $args[++$i];
             }
         } elseif ($arg == "-inputhandler") {
             if ($this->inputHandlerClassname !== null) {
                 throw new BuildException("Only one input handler class may be specified.");
             }
             if (!isset($args[$i + 1])) {
                 print "You must specify a classname when using the -inputhandler argument\n";
                 return;
             } else {
                 $this->inputHandlerClassname = $args[++$i];
             }
         } elseif ($arg == "-projecthelp" || $arg == "-targets" || $arg == "-list" || $arg == "-l") {
             // set the flag to display the targets and quit
             $this->projectHelp = true;
         } elseif ($arg == "-find") {
             // eat up next arg if present, default to build.xml
             if ($i < count($args) - 1) {
                 $this->searchForThis = $args[++$i];
             } else {
                 $this->searchForThis = self::DEFAULT_BUILD_FILENAME;
             }
         } elseif (substr($arg, 0, 1) == "-") {
             // we don't have any more args
             print "Unknown argument: {$arg}\n";
             $this->printUsage();
             return;
         } else {
             // if it's no other arg, it may be the target
             array_push($this->targets, $arg);
         }
     }
     // if buildFile was not specified on the command line,
     if ($this->buildFile === null) {
         // but -find then search for it
         if ($this->searchForThis !== null) {
             $this->buildFile = $this->_findBuildFile(self::getProperty("user.dir"), $this->searchForThis);
         } else {
             $this->buildFile = new PhingFile(self::DEFAULT_BUILD_FILENAME);
         }
     }
     // make sure buildfile exists
     if (!$this->buildFile->exists()) {
         throw new BuildException("Buildfile: " . $this->buildFile->__toString() . " does not exist!");
     }
     // make sure it's not a directory
     if ($this->buildFile->isDirectory()) {
         throw new BuildException("Buildfile: " . $this->buildFile->__toString() . " is a dir!");
     }
     $this->readyToRun = true;
 }