Permalink
Browse files

refactoring

  • Loading branch information...
1 parent accbb95 commit fd22cb101b39bfc374a9e9b82b60966fafa557cf @juzna committed Mar 8, 2012
Showing with 118 additions and 91 deletions.
  1. +2 −0 app/config/config.neon
  2. +115 −0 app/models/Generator.php
  3. +1 −91 app/presenters/GeneratorPresenter.php
View
@@ -36,6 +36,8 @@ common:
authenticator: Authenticator( @database::table(users) )
+ generator: Generator
+
factories:
View
@@ -0,0 +1,115 @@
+<?php
+
+/**
+ * Generator - all the hard work
+ *
+ * @author Jan Dolecek <juzna.cz@gmail.com>
+ */
+class Generator extends \Nette\Object {
+ /** @var \Nette\Database\Connection */
+ private $db;
+
+ /** @var int Item being processed */
+ private $itemId;
+
+
+ public function __construct(\Nette\Database\Connection $db) {
+ $this->db = $db;
+ }
+
+ /**
+ * make it all
+ * @param \Nette\Database\Row $item
+ * @param bool $force
+ */
+ public function make($item, $force) {
+ $pwd = getcwd(); // current working directory
+ $this->itemId = $item->id;
+ $repoDir = REPOS_DIR . '/' . $item->dir; $repoDirE = escapeshellarg($repoDir);
+ $gitDir = "$repoDir/.git";
+
+ // Clone repo
+ if(!file_exists($gitDir)) {
+ $cloned = true;
+ echo "Cloning '$item->url' to '$repoDir'\n";
+ $this->git("clone '$item->url' $repoDirE");
+ $this->git("--git-dir='$gitDir' submodule init");
+ $this->git("--git-dir='$gitDir' submodule update");
+ } else $cloned = false;
+
+ // Pull
+ $headBefore = $this->getHead($gitDir);
+ $branch = $item->branch ?: 'origin/master'; // branch to be checked out
+ $this->git("--git-dir='$gitDir' fetch");
+ chdir($repoDir); $this->git("--git-dir='$gitDir' reset --hard $branch"); chdir($pwd);
+ $this->db->query("update repo set lastPull=now() where id=$item->id");
+ $headAfter = $this->getHead($gitDir);
+ if(!$force && !$cloned && $headBefore == $headAfter) { // no need to generate
+ echo " This repo is upto date ($headBefore, $headAfter)\n";
+ return;
+ }
+
+ // Generate API
+ $timeStarted = microtime(true);
+ $rootDir = APP_DIR . '/../';
+ $sourceDir = "$repoDir/$item->subdir";
+ $docDir = DOC_PROCESSING_DIR . "/$item->dir";
+ $docFinalDir = DOC_FINAL_DIR . "/$item->dir";
+ $generatedWell = $this->exec("php -dmemory_limit=1024M $rootDir/apigen/apigen.php -s " . escapeshellarg($sourceDir) . " -d " . escapeshellarg($docDir) . " --charset=auto --download --debug --colors=no --progressbar=no --title=" . escapeshellarg($item->name), $result);
+ $this->db->table('repo')->where('id', $item->id)->update(array(
+ 'apigenResultId' => $result->id,
+ 'apigenTime' => microtime(true) - $timeStarted,
+ 'sizeDoc' => (int) exec("du -sb " . escapeshellarg($docDir)),
+ ));
+
+ // check
+ if(!file_exists("$docDir/index.html") || !$generatedWell) {
+ echo "Failed to generate\n";
+ $this->db->query("update repo set error=1 where id=$item->id");
+ return;
+ }
+
+ // Move to final dir
+ @mkdir(dirname($docFinalDir));
+ $this->exec("rm -rf " . escapeshellarg($docFinalDir)); // remove contemporary doc
+ rename($docDir, $docFinalDir);
+
+ // Mark as generated
+ $this->db->query("update repo set lastGenerated=now(), error=0 where id=$item->id");
+ }
+
+
+ /**
+ * Get HEAD revision of given repo
+ * @param string $gitDir
+ * @return string
+ */
+ private function getHead($gitDir) {
+ return trim(file_get_contents("$gitDir/refs/heads/master"));
+ }
+
+ private function git($cmd) {
+ $this->exec("/usr/bin/git $cmd");
+ }
+
+ /**
+ * Execute external command
+ * @param string $cmd
+ * @param ActiveRow $result Database row store into result table
+ * @return bool Finished sucessfully?
+ */
+ private function exec($cmd, &$result = null) {
+ echo "$cmd\n";
+ exec($cmd, $output, $retval);
+
+ // Store results
+ $result = $this->db->table('result')->insert(array(
+ 'repo_id' => $this->itemId,
+ 'cmd' => $cmd,
+ 'ok' => $retval == 0,
+ 'output' => implode("\n", $output),
+ ));
+
+ return $retval == 0;
+ }
+}
@@ -8,9 +8,6 @@
* @author Jan Dolecek <juzna.cz@gmail.com>
*/
class GeneratorPresenter extends BasePresenter {
- /** @var int Item being processed */
- private $itemId;
-
protected function startup() {
parent::startup();
@@ -79,93 +76,6 @@ protected function beginRawOutput() {
* @param \Nette\Database\Row $item
*/
protected function make($item) {
- $pwd = getcwd(); // current working directory
- $this->itemId = $item->id;
- $repoDir = REPOS_DIR . '/' . $item->dir; $repoDirE = escapeshellarg($repoDir);
- $gitDir = "$repoDir/.git";
-
- // Clone repo
- if(!file_exists($gitDir)) {
- $cloned = true;
- echo "Cloning '$item->url' to '$repoDir'\n";
- $this->git("clone '$item->url' $repoDirE");
- $this->git("--git-dir='$gitDir' submodule init");
- $this->git("--git-dir='$gitDir' submodule update");
- } else $cloned = false;
-
- // Pull
- $headBefore = $this->getHead($gitDir);
- $branch = $item->branch ?: 'origin/master'; // branch to be checked out
- $this->git("--git-dir='$gitDir' fetch");
- chdir($repoDir); $this->git("--git-dir='$gitDir' reset --hard $branch"); chdir($pwd);
- $this->db->query("update repo set lastPull=now() where id=$item->id");
- $headAfter = $this->getHead($gitDir);
- if(!$this->getParameter('force') && !$cloned && $headBefore == $headAfter) { // no need to generate
- echo " This repo is upto date ($headBefore, $headAfter)\n";
- return;
- }
-
- // Generate API
- $timeStarted = microtime(true);
- $rootDir = APP_DIR . '/../';
- $sourceDir = "$repoDir/$item->subdir";
- $docDir = DOC_PROCESSING_DIR . "/$item->dir";
- $docFinalDir = DOC_FINAL_DIR . "/$item->dir";
- $generatedWell = $this->exec("php -dmemory_limit=1024M $rootDir/apigen/apigen.php -s " . escapeshellarg($sourceDir) . " -d " . escapeshellarg($docDir) . " --charset=auto --download --debug --colors=no --progressbar=no --title=" . escapeshellarg($item->name), $result);
- $this->db->table('repo')->where('id', $item->id)->update(array(
- 'apigenResultId' => $result->id,
- 'apigenTime' => microtime(true) - $timeStarted,
- 'sizeDoc' => (int) exec("du -sb " . escapeshellarg($docDir)),
- ));
-
- // check
- if(!file_exists("$docDir/index.html") || !$generatedWell) {
- echo "Failed to generate\n";
- $this->db->query("update repo set error=1 where id=$item->id");
- return;
- }
-
- // Move to final dir
- @mkdir(dirname($docFinalDir));
- $this->exec("rm -rf " . escapeshellarg($docFinalDir)); // remove contemporary doc
- rename($docDir, $docFinalDir);
-
- // Mark as generated
- $this->db->query("update repo set lastGenerated=now(), error=0 where id=$item->id");
- }
-
-
- /**
- * Get HEAD revision of given repo
- * @param string $gitDir
- * @return string
- */
- private function getHead($gitDir) {
- return trim(file_get_contents("$gitDir/refs/heads/master"));
- }
-
- private function git($cmd) {
- $this->exec("/usr/bin/git $cmd");
- }
-
- /**
- * Execute external command
- * @param string $cmd
- * @param ActiveRow $result Database row store into result table
- * @return bool Finished sucessfully?
- */
- private function exec($cmd, &$result = null) {
- echo "$cmd\n";
- exec($cmd, $output, $retval);
-
- // Store results
- $result = $this->db->table('result')->insert(array(
- 'repo_id' => $this->itemId,
- 'cmd' => $cmd,
- 'ok' => $retval == 0,
- 'output' => implode("\n", $output),
- ));
-
- return $retval == 0;
+ $this->context->generator->make($item, (bool) $this->getParameter('force'));
}
}

0 comments on commit fd22cb1

Please sign in to comment.