123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 |
- <?php
- require_once 'phing/Task.php';
- include_once 'phing/tasks/system/ExecTask.php';
- include_once 'phing/types/Commandline.php';
- class CvsTask extends Task {
-
- const DEFAULT_COMPRESSION_LEVEL = 3;
- private $cmd;
-
- private $commandlines = array();
-
- private $cvsRoot;
-
- private $cvsRsh;
-
- private $cvsModule;
-
- private static $default_command = "checkout";
-
-
- private $command = null;
-
- private $quiet = false;
-
- private $compression = 0;
-
- private $noexec = false;
-
- private $port = 0;
-
- private $passFile = null;
-
- private $dest;
-
- private $error;
-
- private $output;
-
-
- private $failOnError = false;
-
- public function init() {
- $this->cmd = new Commandline();
- }
-
-
- protected function runCommand(Commandline $toExecute) {
-
-
-
-
-
- if ($this->port > 0) {
- putenv("CVS_CLIENT_PORT=".$this->port);
- }
-
-
-
- if ($this->passFile === null) {
- $defaultPassFile = new PhingFile(Phing::getProperty("cygwin.user.home", Phing::getProperty("user.home"))
- . DIRECTORY_SEPARATOR . ".cvspass");
- if($defaultPassFile->exists()) {
- $this->setPassfile($defaultPassFile);
- }
- }
- if ($this->passFile !== null) {
- if ($this->passFile->isFile() && $this->passFile->canRead()) {
- putenv("CVS_PASSFILE=" . $this->passFile->__toString());
- $this->log("Using cvs passfile: " . $this->passFile->__toString(), Project::MSG_INFO);
- } elseif (!$this->passFile->canRead()) {
- $this->log("cvs passfile: " . $this->passFile->__toString()
- . " ignored as it is not readable", Project::MSG_WARN);
- } else {
- $this->log("cvs passfile: " . $this->passFile->__toString()
- . " ignored as it is not a file",
- Project::MSG_WARN);
- }
- }
- if ($this->cvsRsh !== null) {
- putenv("CVS_RSH=".$this->cvsRsh);
- }
-
- $exe = new ExecTask($this->project);
- $exe->setProject($this->project);
-
-
- if ($this->dest === null) {
- $this->dest = $this->project->getBaseDir();
- }
- if (!$this->dest->exists()) {
- $this->dest->mkdirs();
- }
-
- if ($this->output !== null) {
- $exe->setOutput($this->output);
- }
- if ($this->error !== null) {
- $exe->setError($this->error);
- }
-
- $exe->setDir($this->dest);
-
- if (is_object($toExecute)) {
- $toExecuteStr = $toExecute->__toString();
- }
-
- $exe->setCommand($toExecuteStr);
-
- try {
- $actualCommandLine = $toExecuteStr;
- $this->log($actualCommandLine, Project::MSG_INFO);
- $retCode = $exe->execute();
- $this->log("retCode=" . $retCode, Project::MSG_DEBUG);
-
- if ($this->failOnError && $retCode !== 0) {
- throw new BuildException("cvs exited with error code "
- . $retCode
- . PHP_EOL
- . "Command line was ["
- . $toExecute->describeCommand() . "]", $this->getLocation());
- }
- } catch (IOException $e) {
- if ($this->failOnError) {
- throw new BuildException($e, $this->getLocation());
- } else {
- $this->log("Caught exception: " . $e, Project::MSG_WARN);
- }
- } catch (BuildException $e) {
- if ($this->failOnError) {
- throw $e;
- } else {
- $t = $e->getCause();
- if ($t === null) {
- $t = $e;
- }
- $this->log("Caught exception: " . $t, Project::MSG_WARN);
- }
- } catch (Exception $e) {
- if ($this->failOnError) {
- throw new BuildException($e, $this->getLocation());
- } else {
- $this->log("Caught exception: " . $e, Project::MSG_WARN);
- }
- }
- }
-
- public function main() {
- $savedCommand = $this->getCommand();
- if ($this->getCommand() === null && empty($this->commandlines)) {
-
- $this->setCommand(self::$default_command);
- }
- $c = $this->getCommand();
- $cloned = null;
- if ($c !== null) {
- $cloned = $this->cmd->__copy();
- $cloned->createArgument(true)->setLine($c);
- $this->addConfiguredCommandline($cloned, true);
- }
- try {
- for ($i = 0, $vecsize=count($this->commandlines); $i < $vecsize; $i++) {
- $this->runCommand($this->commandlines[$i]);
- }
-
-
- if ($cloned !== null) {
- $this->removeCommandline($cloned);
- }
- $this->setCommand($savedCommand);
-
- } catch (Exception $e) {
-
- if ($cloned !== null) {
- $this->removeCommandline($cloned);
- }
- $this->setCommand($savedCommand);
- throw $e;
- }
- }
-
- public function setCvsRoot($root) {
-
- if ($root !== null) {
- if (trim($root) == "") {
- $root = null;
- }
- }
- $this->cvsRoot = $root;
- }
- public function getCvsRoot() {
- return $this->cvsRoot;
- }
-
- public function setCvsRsh($rsh) {
-
- if ($rsh !== null) {
- if (trim($rsh) == "") {
- $rsh = null;
- }
- }
- $this->cvsRsh = $rsh;
- }
- public function getCvsRsh() {
- return $this->cvsRsh;
- }
-
- public function setPort($port){
- $this->port = $port;
- }
-
- public function getPort() {
- return $this->port;
- }
-
- public function setPassfile(PhingFile $passFile) {
- $this->passFile = $passFile;
- }
-
-
- public function getPassFile() {
- return $this->passFile;
- }
-
- public function setDest(PhingFile $dest) {
- $this->dest = $dest;
- }
- public function getDest() {
- return $this->dest;
- }
-
- public function setModule($m) {
- $this->cvsModule = $m;
- }
- public function getModule(){
- return $this->cvsModule;
- }
-
- public function setTag($p) {
-
- if ($p !== null && trim($p) !== "") {
- $this->appendCommandArgument("-r");
- $this->appendCommandArgument($p);
- }
- }
-
- public function appendCommandArgument($arg) {
- $this->cmd->createArgument()->setValue($arg);
- }
-
- public function setDate($p) {
- if ($p !== null && trim($p) !== "") {
- $this->appendCommandArgument("-D");
- $this->appendCommandArgument($p);
- }
- }
-
- public function setCommand($c) {
- $this->command = $c;
- }
-
- public function getCommand() {
- return $this->command;
- }
-
- public function setQuiet($q) {
- $this->quiet = $q;
- }
-
- public function setNoexec($ne) {
- $this->noexec = (boolean) $ne;
- }
-
- public function setFailOnError($failOnError) {
- $this->failOnError = (boolean) $failOnError;
- }
-
- protected function configureCommandline($c) {
- if ($c === null) {
- return;
- }
- $c->setExecutable("cvs");
-
- if ($this->cvsModule !== null) {
- $c->createArgument()->setLine($this->cvsModule);
- }
- if ($this->compression > 0 && $this->compression < 10) {
- $c->createArgument(true)->setValue("-z" . $this->compression);
- }
- if ($this->quiet) {
- $c->createArgument(true)->setValue("-q");
- }
- if ($this->noexec) {
- $c->createArgument(true)->setValue("-n");
- }
- if ($this->cvsRoot !== null) {
- $c->createArgument(true)->setLine("-d" . $this->cvsRoot);
- }
- }
- protected function removeCommandline(Commandline $c) {
- $idx = array_search($c, $this->commandlines, true);
- if ($idx === false) {
- return false;
- }
- $this->commandlines = array_splice($this->commandlines, $idx, 1);
- return true;
- }
-
- public function addConfiguredCommandline(Commandline $c, $insertAtStart = false) {
- if ($c === null) {
- return;
- }
- $this->configureCommandline($c);
- if ($insertAtStart) {
- array_unshift($this->commandlines, $c);
- } else {
- array_push($this->commandlines, $c);
- }
- }
-
- public function setCompressionLevel($level) {
- $this->compression = $level;
- }
-
- public function setCompression($usecomp) {
- $this->setCompressionLevel($usecomp ?
- self::DEFAULT_COMPRESSION_LEVEL : 0);
- }
-
- function setOutput(PhingFile $f) {
- $this->output = $f;
- }
-
-
- function setError(PhingFile $f) {
- $this->error = $f;
- }
- }
|