Skip to content

Commit

Permalink
Commit upon developer output
Browse files Browse the repository at this point in the history
  • Loading branch information
ticktackk committed Feb 12, 2018
1 parent 7c9756b commit a447abc
Show file tree
Hide file tree
Showing 137 changed files with 1,806 additions and 2 deletions.
87 changes: 87 additions & 0 deletions upload/src/addons/TickTackk/DeveloperTools/Job/Commit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace TickTackk\DeveloperTools\Job;

use \XF\Job\AbstractJob;
use \XF\Mvc\Entity\Entity;
use \TickTackk\DeveloperTools\Git\GitRepository;

class Commit extends AbstractJob
{
public function run($maxRunTime)
{
$actionType = $this->data['actionType'];
$typeDir = $this->data['typeDir'];
$repoDir = $this->data['repoDir'];
$entityClone = $this->data['entityClone'];
$delayedJob = $this->data['delayedJob'];
$isUpdate = $this->data['isUpdate'];

if ($isUpdate && $actionType == 'export')
{
$actionType = 'change';
}

if ($delayedJob)
{
foreach ($entityClone as $key => $value)
{
if (strpos($value, '\__phrase') || (strcmp(substr($value, strlen($value) - strlen('\__phrase')), '\__phrase') === 0))
{
$entityClone[$key] = \XF::phrase(str_replace('\__phrase', '', $value))->render();
}
}
}

if (is_dir($repoDir))
{
$git = new GitRepository($repoDir);

if ($git->isInitialized())
{
$comment = \XF::phrase('developerTools_' . $actionType . '_' . $typeDir . '_commit_template', $entityClone)->render();

$options = \XF::options();
$gitUsername = $options->developerTools_git_username;
$gitEmail = $options->developerTools_git_email;

if (!empty($git->config()->get('user.name')))
{
$git->config()->add('user.name', $gitUsername)->execute();
}
if (!empty($git->config()->get('user.email')))
{
$git->config()->add('user.email', $gitEmail)->execute();
}

if (empty($git->config()->get('user.name')) || empty($git->config()->get('user.name')))
{
return $this->complete();
}

$git->add()->execute('*');

$git->commit()
->message($comment)
->execute();
}
}

return $this->complete();
}

public function getStatusMessage()
{
return '';
}

public function canCancel()
{
return false;
}

public function canTriggerByChoice()
{
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace TickTackk\DeveloperTools\XF\Behavior;

use XF\Mvc\Entity\Entity;

class DevOutputWritable extends XFCP_DevOutputWritable
{
public function postSave()
{
$this->cloneEntity($this->entity);
parent::postSave();
}

public function preDelete()
{
$this->cloneEntity($this->entity);
parent::preDelete();
}

protected function cloneEntity(Entity $entity)
{
\XF::app()->developmentOutput()->cloneEntity($entity);
}
}
181 changes: 181 additions & 0 deletions upload/src/addons/TickTackk/DeveloperTools/XF/DevelopmentOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<?php

namespace TickTackk\DeveloperTools\XF;

use XF\Mvc\Entity\Entity;
use XF\Util\Json;

class DevelopmentOutput extends XFCP_DevelopmentOutput
{
protected $repoSuffix = '_repo';

public function export(Entity $entity)
{
$response = parent::export($entity);

$this->commitOutput('export', $entity);

return $response;
}

public function delete(Entity $entity, $new = true)
{
$response = parent::delete($entity, $new);

$this->commitOutput('delete', $entity);

return $response;
}

public function writeFileToRepo($typeDir, $addOnId, $fileName, $fileContents, array $metadata = [], $verifyChange = true)
{
if (!$this->enabled || $this->isAddOnSkipped($addOnId))
{
return false;
}

$fullPathForRepo = $this->getFilePathForRepo($typeDir, $addOnId, $fileName);

if ($verifyChange)
{
if (!file_exists($fullPathForRepo))
{
$write = true;
}
else
{
$write = file_get_contents($fullPathForRepo) != $fileContents;
}
}
else
{
$write = true;
}

if ($write)
{
\XF\Util\File::writeFile($fullPathForRepo, $fileContents, false);
}

$metadata['hash'] = $this->hashContents($fileContents);
$this->updateMetadata($typeDir, $addOnId, $fileName, $metadata);

return true;
}

public function writeFile($typeDir, $addOnId, $fileName, $fileContents, array $metadata = [], $verifyChange = true)
{
$response = parent::writeFile($typeDir, $addOnId, $fileName, $fileContents, $metadata, $verifyChange);

$this->writeFileToRepo($typeDir, $addOnId, $fileName, $fileContents, $metadata, $verifyChange);

return $response;
}

public function deleteFileFromRepo($typeDir, $addOnId, $fileName)
{
if (!$this->enabled || $this->isAddOnSkipped($addOnId))
{
return false;
}

$fullPath = $this->getFilePathForRepo($typeDir, $addOnId, $fileName);
if (file_exists($fullPath))
{
unlink($fullPath);
}

$this->removeMetadata($typeDir, $addOnId, $fileName);

return true;
}

public function deleteFile($typeDir, $addOnId, $fileName)
{
$reponse = parent::deleteFile($typeDir, $addOnId, $fileName);

$this->deleteFileFromRepo($typeDir, $addOnId, $fileName);

return $reponse;
}

public function getFilePathForRepo($typeDir, $addOnId, $fileName)
{
$ds = \DIRECTORY_SEPARATOR;
$addOnIdDir = $this->prepareAddOnIdForPath($addOnId);
return "{$this->basePath}{$ds}{$addOnIdDir}{$ds}{$this->repoSuffix}{$ds}upload{$ds}src{$ds}addons{$ds}{$addOnIdDir}{$ds}_output{$ds}{$typeDir}{$ds}{$fileName}";
}

protected function getMetadataFileNameForRepo($typeDir, $addOnId)
{
$ds = DIRECTORY_SEPARATOR;
$addOnIdDir = $this->prepareAddOnIdForPath($addOnId);
return "{$this->basePath}{$ds}{$addOnIdDir}{$ds}{$this->repoSuffix}{$ds}upload{$ds}src{$ds}addons{$ds}{$addOnIdDir}{$ds}_output{$ds}{$typeDir}{$ds}{$this->metadataFilename}";
}

protected function writeTypeMetadataForRepo($typeDir, $addOnId, array $typeMetadata)
{
if ($this->isAddOnSkipped($addOnId))
{
return;
}

ksort($typeMetadata);

$this->metadataCache[$typeDir][$addOnId] = $typeMetadata;

if ($this->batchMode)
{
$this->batchesPending[$typeDir][$addOnId] = true;
}
else
{
$metadataPath = $this->getMetadataFileNameForRepo($typeDir, $addOnId);
file_put_contents($metadataPath, Json::jsonEncodePretty($typeMetadata));
}
}

protected function writeTypeMetadata($typeDir, $addOnId, array $typeMetadata)
{
parent::writeTypeMetadata($typeDir, $addOnId, $typeMetadata);
$this->writeTypeMetadataForRepo($typeDir, $addOnId, $typeMetadata);
}

public function cloneEntity(Entity $entity)
{
/** @var \TickTackk\DeveloperTools\XF\DevelopmentOutput\CommitTrait $handler */
$handler = $this->getHandler($entity->structure()->shortName);

if (method_exists($handler, 'commitRepo') &&
method_exists($handler, 'getOutputCommitData'))
{
$outputDataKeys = $handler->getOutputCommitData($entity);
if (empty($outputDataKeys))
{
return;
}

$handler->cloneEntity($entity, $outputDataKeys);
}
}

protected function commitOutput($actionType, Entity $entity)
{
if (!$this->batchMode)
{
/** @var \TickTackk\DeveloperTools\XF\DevelopmentOutput\CommitTrait $handler */
$handler = $this->getHandler($entity->structure()->shortName);

if (method_exists($handler, 'commitRepo') &&
method_exists($handler, 'getOutputCommitData'))
{
$ds = DIRECTORY_SEPARATOR;
$repoDir = $this->basePath . $ds . $this->prepareAddOnIdForPath($entity->addon_id) . $ds . '_repo';

$jobId = $entity->structure()->shortName . '_' . $actionType . '_' . $entity->getEntityId();

$handler->commitRepo($jobId, $repoDir, $actionType, $entity->isUpdate());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace TickTackk\DeveloperTools\XF\DevelopmentOutput;

use TickTackk\DeveloperTools\XF\DevelopmentOutput\CommitTrait;
use XF\Mvc\Entity\Entity;

class AdminNavigation extends XFCP_AdminNavigation
{
use CommitTrait;

/**
* @param \XF\Entity\AdminNavigation $entity
*
* @return array
*/
public function getOutputCommitData(Entity $entity)
{
return [
'navigation_title' => ['getTitle', '\__phrase'],
'navigation_id' => $entity->getEntityId()
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace TickTackk\DeveloperTools\XF\DevelopmentOutput;

use TickTackk\DeveloperTools\XF\DevelopmentOutput\CommitTrait;
use XF\Mvc\Entity\Entity;

class AdminPermission extends XFCP_AdminPermission
{
use CommitTrait;

/**
* @param \XF\Entity\AdminPermission $entity
*
* @return array
*/
public function getOutputCommitData(Entity $entity)
{
return [
'permission_title' => ['getTitle', '\__phrase'],
'permission_id'
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace TickTackk\DeveloperTools\XF\DevelopmentOutput;

use TickTackk\DeveloperTools\XF\DevelopmentOutput\CommitTrait;
use XF\Mvc\Entity\Entity;

class AdvertisingPosition extends XFCP_AdvertisingPosition
{
use CommitTrait;

/**
* @param \XF\Entity\AdvertisingPosition $entity
*
* @return array
*/
public function getOutputCommitData(Entity $entity)
{
return [
'position_title' => ['getTitle', '\__phrase'],
'position_id'
];
}
}
Loading

0 comments on commit a447abc

Please sign in to comment.