Changeset 334

Show
Ignore:
Timestamp:
01/04/08 14:25:20 (7 months ago)
Author:
hans
Message:

Refs #203 - Added returnProperty to exec task.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/2.3/classes/phing/tasks/system/ExecTask.php

    r215 r334  
    3333class ExecTask extends Task { 
    3434 
    35     /** 
    36     * Command to execute. 
    37     * @var string 
    38     */ 
    39     protected $command; 
    40      
    41     /** 
    42     * Working directory. 
    43     * @var File 
    44     */ 
    45     protected $dir; 
    46      
    47     /** 
    48     * Operating system. 
    49     * @var string 
    50     */ 
    51     protected $os; 
    52      
    53     /** 
    54     * Whether to escape shell command using escapeshellcmd(). 
    55     * @var boolean 
    56     */ 
    57     protected $escape = false; 
    58      
    59     /** 
    60     * Where to direct output. 
    61     * @var File 
    62     */ 
    63     protected $output; 
    64      
    65     /** 
    66     * Whether to passthru the output 
    67     * @var boolean 
    68     */ 
    69     protected $passthru = false; 
    70      
    71     /** 
    72     * Where to direct error output. 
    73     * @var File 
    74     */ 
    75     protected $error; 
    76      
     35       /** 
     36        * Command to execute. 
     37        * @var string 
     38        */ 
     39       protected $command; 
     40 
     41       /** 
     42        * Working directory. 
     43        * @var File 
     44        */ 
     45       protected $dir; 
     46 
     47       /** 
     48        * Operating system. 
     49        * @var string 
     50        */ 
     51       protected $os; 
     52 
     53       /** 
     54        * Whether to escape shell command using escapeshellcmd(). 
     55        * @var boolean 
     56        */ 
     57       protected $escape = false; 
     58 
     59       /** 
     60        * Where to direct output. 
     61        * @var File 
     62        */ 
     63       protected $output; 
     64 
     65       /** 
     66        * Whether to passthru the output 
     67        * @var boolean 
     68        */ 
     69       protected $passthru = false; 
     70 
     71       /** 
     72        * Where to direct error output. 
     73        * @var File 
     74        */ 
     75       protected $error; 
     76 
    7777        /** 
    7878         * If spawn is set then [unix] programs will redirect stdout and add '&'. 
     
    8080         */ 
    8181        protected $spawn = false; 
    82          
    83     /** 
    84      * Whether to check the return code. 
    85      * @var boolean 
    86      */ 
    87     protected $checkreturn = false; 
    88      
    89     /** 
    90      * Main method: wraps execute() command. 
    91      * @return void 
    92      */ 
    93     public function main() { 
    94         $this->execute(); 
    95     } 
    96      
    97     /** 
    98      * Executes a program and returns the return code. 
    99      * Output from command is logged at INFO level. 
    100      * @return int Return code from execution. 
    101      */ 
    102     public function execute() { 
    103      
    104          // test if os match 
    105         $myos = Phing::getProperty("os.name"); 
    106         $this->log("Myos = " . $myos, Project::MSG_VERBOSE); 
    107         if (($this->os !== null) && (strpos($this->os, $myos) === false)) { 
    108             // this command will be executed only on the specified OS 
    109             $this->log("Not found in " . $this->os, Project::MSG_VERBOSE); 
    110             return 0; 
    111         } 
    112          
    113          if ($this->dir !== null) { 
    114             if ($this->dir->isDirectory()) { 
    115                 $currdir = getcwd(); 
    116                 @chdir($this->dir->getPath()); 
    117             } else { 
    118                 throw new BuildException("Can't chdir to:" . $this->dir->__toString()); 
    119             } 
    120         } 
    121  
    122  
    123         if ($this->escape == true) { 
    124             // FIXME - figure out whether this is correct behavior 
    125             $this->command = escapeshellcmd($this->command); 
    126         } 
    127          
    128         if ($this->error !== null) { 
    129             $this->command .= ' 2> ' . $this->error->getPath(); 
    130             $this->log("Writing error output to: " . $this->error->getPath()); 
    131         } 
    132          
    133         if ($this->output !== null) { 
    134             $this->command .= ' 1> ' . $this->output->getPath(); 
    135             $this->log("Writing standard output to: " . $this->output->getPath()); 
    136         } elseif ($this->spawn) { 
     82 
     83        /** 
     84         * Property name to set with return value from exec call. 
     85         * 
     86         * @var string 
     87         */ 
     88        protected $returnProperty; 
     89 
     90        /** 
     91         * Whether to check the return code. 
     92         * @var boolean 
     93         */ 
     94        protected $checkreturn = false; 
     95 
     96        /** 
     97         * Main method: wraps execute() command. 
     98         * @return void 
     99         */ 
     100        public function main() { 
     101                $this->execute(); 
     102        } 
     103 
     104        /** 
     105         * Executes a program and returns the return code. 
     106         * Output from command is logged at INFO level. 
     107         * @return int Return code from execution. 
     108         */ 
     109        public function execute() { 
     110 
     111                // test if os match 
     112                $myos = Phing::getProperty("os.name"); 
     113                $this->log("Myos = " . $myos, Project::MSG_VERBOSE); 
     114                if (($this->os !== null) && (strpos($this->os, $myos) === false)) { 
     115                        // this command will be executed only on the specified OS 
     116                        $this->log("Not found in " . $this->os, Project::MSG_VERBOSE); 
     117                        return 0; 
     118                } 
     119 
     120                if ($this->dir !== null) { 
     121                        if ($this->dir->isDirectory()) { 
     122                                $currdir = getcwd(); 
     123                                @chdir($this->dir->getPath()); 
     124                        } else { 
     125                                throw new BuildException("Can't chdir to:" . $this->dir->__toString()); 
     126                        } 
     127                } 
     128 
     129 
     130                if ($this->escape == true) { 
     131                        // FIXME - figure out whether this is correct behavior 
     132                        $this->command = escapeshellcmd($this->command); 
     133                } 
     134 
     135                if ($this->error !== null) { 
     136                        $this->command .= ' 2> ' . $this->error->getPath(); 
     137                        $this->log("Writing error output to: " . $this->error->getPath()); 
     138                } 
     139 
     140                if ($this->output !== null) { 
     141                        $this->command .= ' 1> ' . $this->output->getPath(); 
     142                        $this->log("Writing standard output to: " . $this->output->getPath()); 
     143                } elseif ($this->spawn) { 
    137144                        $this->command .= ' 1>/dev/null'; 
    138145                        $this->log("Sending ouptut to /dev/null"); 
    139146                } 
    140          
    141         // If neither output nor error are being written to file 
    142         // then we'll redirect error to stdout so that we can dump 
    143         // it to screen below. 
    144  
    145         if ($this->output === null && $this->error === null) { 
    146             $this->command .= ' 2>&1'; 
    147        
    148                          
     147 
     148               // If neither output nor error are being written to file 
     149               // then we'll redirect error to stdout so that we can dump 
     150               // it to screen below. 
     151 
     152               if ($this->output === null && $this->error === null) { 
     153                       $this->command .= ' 2>&1'; 
     154               
     155 
    149156                // we ignore the spawn boolean for windows 
    150157                if ($this->spawn) { 
    151                     $this->command .= ' &'; 
     158                       $this->command .= ' &'; 
    152159                } 
    153160 
    154161                $this->log("Executing command: " . $this->command); 
    155                                  
    156         $output = array(); 
    157         $return = null; 
    158         exec($this->command, $output, $return); 
    159  
    160         if ($this->dir !== null) { 
    161             @chdir($currdir); 
    162         } 
    163  
    164         foreach($output as $line) { 
    165             $this->log($line,  ($this->passthru ? Project::MSG_INFO : Project::MSG_VERBOSE)); 
    166         } 
    167          
    168         if($return != 0 && $this->checkreturn) 
    169         { 
    170           throw new BuildException("Task exited with code $return"); 
    171         } 
    172          
    173         return $return; 
    174     } 
    175  
    176     /** 
    177      * The command to use. 
    178      * @param mixed $command String or string-compatible (e.g. w/ __toString()). 
    179      */ 
    180     function setCommand($command) { 
    181         $this->command = "" . $command; 
    182     } 
    183      
    184     /** 
    185      * Whether to use escapeshellcmd() to escape command. 
    186      * @param boolean $escape 
    187      */ 
    188     function setEscape($escape) { 
    189         $this->escape = (bool) $escape; 
    190     } 
    191      
    192     /** 
    193      * Specify the working directory for executing this command. 
    194      * @param PhingFile $dir 
    195      */ 
    196     function setDir(PhingFile $dir) { 
    197         $this->dir = $dir; 
    198     } 
    199      
    200     /** 
    201      * Specify OS (or muliple OS) that must match in order to execute this command. 
    202      * @param string $os 
    203      */ 
    204     function setOs($os) { 
    205         $this->os = (string) $os; 
    206     } 
    207      
    208     /** 
    209      * File to which output should be written. 
    210      * @param PhingFile $output 
    211      */ 
    212     function setOutput(PhingFile $f) { 
    213         $this->output = $f; 
    214     } 
    215      
    216     /** 
    217      * File to which error output should be written. 
    218      * @param PhingFile $output 
    219      */ 
    220     function setError(PhingFile $f) { 
    221         $this->error = $f; 
    222     } 
    223      
    224     /** 
    225      * Whether to use passthru the output. 
    226      * @param boolean $passthru 
    227      */ 
    228     function setPassthru($passthru) { 
    229         $this->passthru = (bool) $passthru; 
    230     } 
    231      
     162 
     163                $output = array(); 
     164                $return = null; 
     165                exec($this->command, $output, $return); 
     166 
     167                if ($this->dir !== null) { 
     168                        @chdir($currdir); 
     169                } 
     170 
     171                foreach($output as $line) { 
     172                        $this->log($line,  ($this->passthru ? Project::MSG_INFO : Project::MSG_VERBOSE)); 
     173                } 
     174 
     175                if ($this->returnProperty) { 
     176                        $this->project->setProperty($this->returnProperty, $return); 
     177                } 
     178 
     179                if($return != 0 && $this->checkreturn) { 
     180                        throw new BuildException("Task exited with code $return"); 
     181                } 
     182 
     183                return $return; 
     184        } 
     185 
     186        /** 
     187         * The command to use. 
     188         * @param mixed $command String or string-compatible (e.g. w/ __toString()). 
     189         */ 
     190        function setCommand($command) { 
     191                $this->command = "" . $command; 
     192        } 
     193 
     194        /** 
     195         * Whether to use escapeshellcmd() to escape command. 
     196         * @param boolean $escape 
     197         */ 
     198        function setEscape($escape) { 
     199                $this->escape = (bool) $escape; 
     200        } 
     201 
     202        /** 
     203         * Specify the working directory for executing this command. 
     204         * @param PhingFile $dir 
     205         */ 
     206        function setDir(PhingFile $dir) { 
     207                $this->dir = $dir; 
     208        } 
     209 
     210        /** 
     211         * Specify OS (or muliple OS) that must match in order to execute this command. 
     212         * @param string $os 
     213         */ 
     214        function setOs($os) { 
     215                $this->os = (string) $os; 
     216        } 
     217 
     218        /** 
     219         * File to which output should be written. 
     220         * @param PhingFile $output 
     221         */ 
     222        function setOutput(PhingFile $f) { 
     223                $this->output = $f; 
     224        } 
     225 
     226        /** 
     227         * File to which error output should be written. 
     228         * @param PhingFile $output 
     229         */ 
     230        function setError(PhingFile $f) { 
     231                $this->error = $f; 
     232        } 
     233 
     234        /** 
     235         * Whether to use passthru the output. 
     236         * @param boolean $passthru 
     237         */ 
     238        function setPassthru($passthru) { 
     239                $this->passthru = (bool) $passthru; 
     240        } 
     241 
    232242        /** 
    233243         * Whether to suppress all output and run in the background. 
     
    238248        } 
    239249 
    240     /** 
    241      * Whether to check the return code. 
    242      * @param boolean $checkreturn 
    243      */ 
    244     function setCheckreturn($checkreturn) { 
    245       $this->checkreturn = (bool) $checkreturn; 
    246     } 
     250        /** 
     251         * Whether to check the return code. 
     252         * @param boolean $checkreturn 
     253         */ 
     254        function setCheckreturn($checkreturn) { 
     255                $this->checkreturn = (bool) $checkreturn; 
     256        } 
     257         
     258        /** 
     259         * The name of property to set to return value from exec() call. 
     260         * @param string $prop 
     261         */ 
     262        function setReturnProperty($prop) { 
     263                $this->returnProperty = $prop; 
     264        } 
    247265} 
    248266 
  • branches/2.3/docs/phing_guide/book/chapters/appendixes/AppendixB-CoreTasks.html

    r278 r334  
    714714      <td> Wether to spawn unix programs to the background, redirecting stdout.</td> 
    715715      <td>FALSE</td> 
     716      <td>No</td> 
     717    </tr> 
     718    <tr> 
     719      <td>returnProperty</td> 
     720      <td>String</td> 
     721      <td>Property name to set return value to from exec() call.</td> 
     722      <td>n/a</td> 
    716723      <td>No</td> 
    717724    </tr>