Exemple #1
0
 /**
  * Creates a new instance of this builder element, with default values.
  */
 public static function create()
 {
     $o = new self();
     $fileset = new Build_BuilderElement_Type_Fileset();
     $fileset->setType(Build_BuilderElement_Type_Fileset::FILE);
     $fileset->setDir('${sourcesDir}');
     $fileset->addInclude('**/*.pl');
     $o->setFilesets(array($fileset));
     return $o;
 }
Exemple #2
0
 /**
  * Creates a new instance of this builder element, with default values.
  */
 public static function create()
 {
     $o = new self();
     $o->setIncludeEmptyDirs(true);
     $fileset = new Build_BuilderElement_Type_Fileset();
     $fileset->setType(Build_BuilderElement_Type_Fileset::BOTH);
     $fileset->setDefaultExcludes(false);
     $o->setFilesets(array($fileset));
     return $o;
 }
Exemple #3
0
 /**
  * Creates a new instance of this builder element, with default values.
  */
 public static function create()
 {
     $o = new self();
     $fileset = new Build_BuilderElement_Type_Fileset();
     $fileset->setType(Build_BuilderElement_Type_Fileset::FILE);
     $fileset->setDir('${sourcesDir}');
     $fileset->addInclude(CINTIENT_TEMP_UNIT_TESTS_DEFAULT_INCLUDE_MATCH);
     $o->setFilesets(array($fileset));
     $o->setLogJunitXmlFile($GLOBALS['project']->getReportsWorkingDir() . CINTIENT_JUNIT_REPORT_FILENAME);
     $o->setCodeCoverageXmlFile($GLOBALS['project']->getReportsWorkingDir() . CINTIENT_CODECOVERAGE_XML_REPORT_FILENAME);
     $o->setCodeCoverageHtmlDir($GLOBALS['project']->getReportsWorkingDir() . CINTIENT_CODECOVERAGE_HTML_DIR);
     return $o;
 }
Exemple #4
0
 public function toPhp(array &$context = array())
 {
     if (!$this->isActive()) {
         return '';
     }
     $php = '';
     if (!$this->getFile() && !$this->getFilesets()) {
         SystemEvent::raise(SystemEvent::ERROR, 'No source files set for task copy.', __METHOD__);
         return false;
     }
     if (!$this->getToFile() && !$this->getToDir()) {
         SystemEvent::raise(SystemEvent::ERROR, 'No destination set for task copy.', __METHOD__);
         return false;
     }
     $php .= "\n\$GLOBALS['result']['task'] = 'copy';\n\$baseToFilename = '';\n";
     if ($this->getToFile()) {
         $php .= "\n\$path = pathinfo(expandStr('{$this->getToFile()}'));\n\$baseToDir = \$path['dirname'];\n\$baseToFilename = '/' . \$path['basename']; // pathinfo's dirname *always* returns the dirname without the trailing slash.\n";
     } elseif ($this->getToDir()) {
         $php .= "\n\$baseToDir = expandStr('{$this->getToDir()}');\n";
     }
     //
     // TODO: Potential bug here. If the following generated mkdir does
     // indeed fail and failOnError == true, the execution will continue
     // because we are not returning true... A return true here would
     // halt the generated script execution (that's what return false does
     // in case of error...)
     //
     // Wrapping this whole element into a generated auto-executing closure
     // a la Javascript would be awesome, because that way we could just
     // force a return and not risk shutting down the whole builder script
     //
     $php .= "\nif (!file_exists(\$baseToDir) && !@mkdir(\$baseToDir, 0755, true)) {\n  output(\"Failed creating dir \$baseToDir.\");\n\tif ({$this->getFailOnError()}) {\n    \$GLOBALS['result']['ok'] = false;\n    return false;\n  } else {\n\t  \$GLOBALS['result']['ok'] = \$GLOBALS['result']['ok'] & true;\n  }\n}";
     //
     // Internally treat $this->getFile() as a fileset.
     //
     $filesets = array();
     if ($this->getFile()) {
         $getFile = self::_expandStr($this->getFile(), $context);
         $pathFrom = pathinfo($getFile);
         $fileset = new Build_BuilderElement_Type_Fileset();
         if (!file_exists($getFile)) {
             $php .= "\noutput(\"No such file or directory {$getFile}.\");\nif ({$this->getFailOnError()}) { // failonerror\n  \$GLOBALS['result']['ok'] = false;\n  return false;\n} else {\n  \$GLOBALS['result']['ok'] = \$GLOBALS['result']['ok'] & true;\n}\n";
         } elseif (is_file($getFile)) {
             $fileset->addInclude($pathFrom['basename']);
             $fileset->setDir($pathFrom['dirname']);
             $fileset->setType(Build_BuilderElement_Type_Fileset::FILE);
             $php .= "\n\$baseFromDir = '{$pathFrom['dirname']}';\n";
         } else {
             // It's a directory
             $fileset->addInclude('**/*');
             $fileset->setDir($getFile);
             $fileset->setType(Build_BuilderElement_Type_Fileset::BOTH);
             // Very important default!!!
             $php .= "\n\$baseFromDir = '{$getFile}';\n";
         }
         $filesets[] = $fileset;
     } elseif ($this->getFilesets()) {
         // If file exists, it takes precedence over filesets
         $realFilesets = $this->getFilesets();
         // Not to be overwritten
         if (!$realFilesets[0]->getDir() || !$realFilesets[0]->getInclude()) {
             SystemEvent::raise(SystemEvent::ERROR, 'No source files set for task copy.', __METHOD__);
             return false;
         }
         // Iterator mode for copy() must enforce parent dirs before their children,
         // so that we can mkdir the parent without first trying to copy in the children
         // on a non-existing dir.
         $fileset = new Build_BuilderElement_Type_Fileset();
         $fileset->setDir(self::_expandStr($realFilesets[0]->getDir(), $context));
         $fileset->setInclude(explode(' ', self::_expandStr(implode(' ', $realFilesets[0]->getInclude()), $context)));
         $fileset->setExclude(explode(' ', self::_expandStr(implode(' ', $realFilesets[0]->getExclude()), $context)));
         $filesets[] = $fileset;
         $php .= "\n\$baseFromDir = '{$fileset->getDir()}';\n";
     }
     $php .= "\n\$callback = function (\$entry) use (\$baseToDir, \$baseFromDir, \$baseToFilename) {\n  \$dest = \$baseToDir . (!empty(\$baseToFilename)?\$baseToFilename:substr(\$entry, strlen(\$baseFromDir)));\n  if (is_file(\$entry)) {\n    \$ret = @copy(\$entry, \$dest);\n  } elseif (is_dir(\$entry)) {\n  \tif (!file_exists(\$dest) && !@mkdir(\$dest, 0755, true)) {\n  \t  \$ret = false;\n  \t} else {\n  \t  \$ret = true;\n    }\n  } else {\n    \$ret = false;\n  }\n  if (!\$ret) {\n    output(\"Failed copy of \$entry to \$dest.\");\n  } else {\n    output(\"Copied \$entry to \$dest.\");\n  }\n  return \$ret;\n};\n";
     $context['iteratorMode'] = RecursiveIteratorIterator::SELF_FIRST;
     // Make sure dirs come before their children, in order to be created first
     foreach ($filesets as $fileset) {
         $php .= "\n" . $fileset->toPhp($context) . "\nif (!fileset{$fileset->getId()}_{$context['id']}(\$callback) && {$this->getFailOnError()}) {\n  \$GLOBALS['result']['ok'] = false;\n  return false;\n} else {\n  \$GLOBALS['result']['ok'] = \$GLOBALS['result']['ok'] & true;\n}\n";
     }
     return $php;
 }
Exemple #5
0
 /**
  * Saves one builder element at a time on project edit.
  */
 public static function project_integrationBuilderSaveElement()
 {
     /*
     matamouros 2011.05.18:
     In order to simpflify a great deal of things, I've opted for the following:
         . filesets in HTML are abstracted as seamlessly part of the respective
           tasks where they are used. In truth, filesets should be completely
           independent editable forms, and tasks should then reference them
           (perhaps on a select dropdown).
         . The above means that, for now, only one fileset is allowed for
           each task that uses it.
     Alas, this simplification accounts for slightly bloatier code in
         this method, namely because we now have to special case the processing
         of seamless embedded filesets and make sure that backstage they are
         still properly updating the corresponding Build_BuilderElement_Fileset object.
     */
     SystemEvent::raise(SystemEvent::DEBUG, "Called.", __METHOD__);
     if (empty($GLOBALS['project']) || !$GLOBALS['project'] instanceof Project || empty($_REQUEST['internalId'])) {
         $msg = 'Invalid request';
         SystemEvent::raise(SystemEvent::INFO, $msg, __METHOD__);
         echo json_encode(array('success' => false, 'error' => $msg));
         exit;
     }
     if (!$GLOBALS['project']->userHasAccessLevel($GLOBALS['user'], Access::WRITE) && !$GLOBALS['user']->hasCos(UserCos::ROOT)) {
         $msg = 'Not authorized';
         SystemEvent::raise(SystemEvent::INFO, $msg, __METHOD__);
         echo json_encode(array('success' => false, 'error' => $msg));
         exit;
     }
     $o = $GLOBALS['project']->getIntegrationBuilder()->getElement($_REQUEST['internalId']['value']);
     if (!$o instanceof Build_BuilderElement) {
         $msg = 'Unknown task specified';
         SystemEvent::raise(SystemEvent::INFO, $msg, __METHOD__);
         echo json_encode(array('success' => false, 'error' => $msg));
         exit;
     }
     if (!$o->isEditable()) {
         $msg = 'Builder element not editable';
         SystemEvent::raise(SystemEvent::INFO, $msg, __METHOD__);
         echo json_encode(array('success' => false, 'error' => $msg));
         exit;
     }
     foreach ($_REQUEST as $attributeName => $attributeValue) {
         $method = 'set' . ucfirst($attributeName);
         if (!isset($attributeValue['value'])) {
             // Unselected radio buttons cause the value attribute to not be sent
             $attributeValue['value'] = null;
         }
         $value = filter_var($attributeValue['value'], FILTER_SANITIZE_STRING);
         if ($attributeValue['type'] == 'checkbox') {
             $value = !empty($attributeValue['value']) ? true : false;
         }
         //
         // Specific handling for filesets
         //
         if (($attributeName == 'include' || $attributeName == 'exclude' || $attributeName == 'defaultExcludes' || $attributeName == 'dir' || $attributeName == 'type') && ($o instanceof Build_BuilderElement_Task_Perl_PerlSyntax || $o instanceof Build_BuilderElement_Task_Php_PhpUnit || $o instanceof Build_BuilderElement_Task_Php_PhpLint || $o instanceof Build_BuilderElement_Task_Filesystem_Chmod || $o instanceof Build_BuilderElement_Task_Filesystem_Chown || $o instanceof Build_BuilderElement_Task_Filesystem_Delete || $o instanceof Build_BuilderElement_Task_Filesystem_Copy || $o instanceof Build_BuilderElement_Task_ReplaceRegexp)) {
             $filesets = $o->getFilesets();
             // Only one is expected, for now
             if ($attributeName == 'include' || $attributeName == 'exclude') {
                 $value = array($value);
             } elseif ($attributeName == 'type' && $value != Build_BuilderElement_Type_Fileset::BOTH && $value != Build_BuilderElement_Type_Fileset::FILE && $value != Build_BuilderElement_Type_Fileset::DIR) {
                 $value = Build_BuilderElement_Type_Fileset::getDefaultType();
             }
             $filesets[0]->{$method}($value);
         } else {
             $o->{$method}($value);
         }
     }
     $GLOBALS['project']->log("Integration builder changed.", $GLOBALS['user']->getUsername());
     SystemEvent::raise(SystemEvent::DEBUG, "Builder element properly edited.", __METHOD__);
     echo json_encode(array('success' => true));
     exit;
 }
 public static function tests_tasks()
 {
     $exec = new Build_BuilderElement_Task_Exec();
     $exec->setExecutable('ls');
     $exec->setArgs(array('-la'));
     $exec->setBaseDir('/tmp/');
     //$exec->setOutputProperty('xpto');
     //echo $exec->toString('ant');
     $delete = new Build_BuilderElement_Task_Filesystem_Delete();
     $delete->setIncludeEmptyDirs(true);
     $delete->setFailOnError(true);
     $fileset = new Build_BuilderElement_Type_Fileset();
     $fileset->setDir('/tmp/apache');
     //$fileset->setDefaultExcludes(false);
     $fileset->setInclude(array('extra/**/*.conf'));
     $delete->setFilesets(array($fileset));
     //echo $delete->toString('ant');
     $echo = new Build_BuilderElement_Task_Echo();
     $echo->setMessage('Ol�');
     $echo->setFile('${workDir}/ixo.txt');
     $echo2 = new Build_BuilderElement_Task_Echo();
     $echo2->setMessage('About to do an exec2!');
     $echo2->setFile('/tmp/test.log');
     $echo2->setAppend(true);
     $mkdir = new Build_BuilderElement_Task_Filesystem_Mkdir();
     //$mkdir->setDir('/tmp/tmp2/tmp3');
     $mkdir->setDir('${dir}');
     $lint = new Build_BuilderElement_Task_Php_PhpLint();
     $lint->setFilesets(array($fileset));
     $chmod = new Build_BuilderElement_Task_Filesystem_Chmod();
     $chmod->setMode('${perms}');
     $chmod->setFile('${file}');
     //$chmod->setFilesets(array($fileset));
     $chown = new Build_BuilderElement_Task_Filesystem_Chown();
     $chown->setFile('/tmp/lixo1.php');
     $chown->setUser('www-data');
     $copy = new Build_BuilderElement_Task_Filesystem_Copy();
     $copy->setFile('/tmp/src/config/cintient.conf.php');
     $copy->setToDir('${toDir}');
     $perl = new Build_BuilderElement_Task_Perl_PerlSyntax();
     $fs2 = new Build_BuilderElement_Type_Fileset();
     $fs2->setDir('/tmp/');
     $fs2->setInclude(array('**/*pl'));
     $perl->setFilesets(array($fs2));
     /*$fileset = new Build_BuilderElement_Type_Fileset();
         $fileset->setDir('${dir}');
         $fileset->setInclude(array('${include}'));
     
         $fileset->setType(Build_BuilderElement_Type_Fileset::BOTH);
         $copy->setFilesets(array($fileset));
     		*/
     $properties = new Build_BuilderElement_Type_Properties();
     $properties->setText("workDir = /tmp/\ntitle = Cintient\nexecutable = ls\nargs = -la\ndir = /tmp/src/\nfile = /tmp/lixo1.php\nperms = 755\ninclude = **/*\ntoDir = /tmp/src2/\n");
     $rr = new Build_BuilderElement_Task_ReplaceRegexp();
     $rr->setFile('/tmp/whatevs.txt');
     //$rr->setFilesets(array($fileset));
     $rr->setFlags('gmi');
     $rr->setReplace('asd$1');
     $rr->setMatch('/^ola"asd(.*)$/');
     $target = new Build_BuilderElement_Target();
     $target->setName('tests');
     $target->setTasks(array($rr));
     //echo $target->toString('php');
     $target2 = new Build_BuilderElement_Target();
     $target2->setName('tests2');
     //$target->setTasks(array($delete, $exec));
     $target2->setTasks(array($echo, $rr));
     //echo $target->toString('php');
     $project = new Build_BuilderElement_Project();
     $project->addTarget($target);
     $project->setBaseDir('/tmp/');
     //$project->addTarget($target2);
     $project->setDefaultTarget($target->getName());
     $code = $project->toPhp();
     echo $code;
     //var_dump(BuilderConnector_Php::execute($code));
     exit;
 }
 public function toPhp(array &$context = array())
 {
     if (!$this->isActive()) {
         return true;
     }
     $php = "\n\$GLOBALS['result']['task'] = 'replaceregexp';\n";
     if (empty($this->_filesets) && empty($this->_file)) {
         $msg = 'No files set for task replaceregexp.';
         SystemEvent::raise(SystemEvent::INFO, $msg, __METHOD__);
         $php .= "\noutput('{$msg}');\n\$GLOBALS['result']['ok'] = false;\nreturn false;\n";
         return $php;
     }
     if (empty($this->_match)) {
         $msg = 'Match attribute is mandatory for task replaceregexp.';
         SystemEvent::raise(SystemEvent::INFO, $msg, __METHOD__);
         $php .= "\noutput('{$msg}');\n\$GLOBALS['result']['ok'] = false;\nreturn false;\n";
         return $php;
     }
     if (empty($this->_replace)) {
         $msg = 'Replace attribute is mandatory for task replaceregexp.';
         SystemEvent::raise(SystemEvent::INFO, $msg, __METHOD__);
         $php .= "\noutput('{$msg}');\n\$GLOBALS['result']['ok'] = false;\nreturn false;\n";
         return $php;
     }
     $php .= "\n\$callback = function (\$entry) {\n  \$ret = true;\n  if (is_file(\$entry)) { // ignore anything other than files\n    if (\$fileContent = file_get_contents(\$entry)) {\n      \$replaces = 0;\n    \t\$newContent = preg_replace('/" . preg_replace("(')", "\\'", self::_expandStr(html_entity_decode($this->getMatch(), ENT_QUOTES), $context)) . "/', '" . preg_replace("(')", "\\'", self::_expandStr(html_entity_decode($this->getReplace(), ENT_QUOTES), $context)) . "', \$fileContent, -1, \$replaces); // Let the user know that he must escape singlequotes in the match textfield\n    \tif (\$replaces > 0) {\n    \t\t\$replaces = (\$replaces == 1 ? '' : 'es'); // Ugly code for nice output messages\n    \t\tif (!file_put_contents(\$entry, \$newContent)) {\n    \t\t\toutput(\"Found replaceable match\$replaces, but couldn't update file \$entry.\");\n    \t\t\t\$ret = false;\n    \t  } else {\n    \t    output(\"Replaced match\$replaces in \$entry.\");\n  \t\t\t}\n      } else {\n      \toutput(\"No matches in \$entry.\");\n      }\n    }\n  }\n  return \$ret;\n};\n";
     if ($this->getFile()) {
         $getFile = self::_expandStr($this->getFile(), $context);
         $pathFrom = pathinfo($getFile);
         $fileset = new Build_BuilderElement_Type_Fileset();
         $fileset->addInclude($pathFrom['basename']);
         $fileset->setDir($pathFrom['dirname']);
         $fileset->setType(Build_BuilderElement_Type_Fileset::FILE);
         // If File is set, it takes precedence over any set filesets,
         // for simplification purposes
         $this->setFilesets(array($fileset));
     }
     $filesets = $this->getFilesets();
     foreach ($filesets as $fileset) {
         $php .= "\n" . $fileset->toPhp($context) . "\nif (!fileset{$fileset->getId()}_{$context['id']}(\$callback) && {$this->getFailOnError()}) {\n  \$GLOBALS['result']['ok'] = false;\n  return false;\n} else {\n  \$GLOBALS['result']['ok'] = \$GLOBALS['result']['ok'] & true;\n}\n";
     }
     return $php;
 }