public
Last active

A Phing logger providing data for the buildhawk gem

  • Download Gist
BuildhawkLogger.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
<?php
/*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/
require_once 'phing/listener/DefaultLogger.php';
 
/**
* Writes a build event to the console and store the build time as a git notes in the
* project HEAD.
*
* @author Raphael Stolt <raphael.stolt@gmail.com>
* @see BuildEvent
* @link https://github.com/xaviershay/buildhawk Buildhawk on GitHub
* @package phing.listener
*/
class BuildhawkLogger extends DefaultLogger {
/**
* @var string
*/
private $_gitNotesCommandResponse = null;
 
/**
* Behaves like the original DefaultLogger, plus adds the total build time
* as a git note to current repository HEAD.
*
* @param BuildEvent $event
* @see BuildEvent::getException()
* @see DefaultLogger::buildFinished
* @link http://www.kernel.org/pub/software/scm/git/docs/git-notes.html
*/
public function buildFinished(BuildEvent $event) {
parent::buildFinished($event);
if ($this->_isProjectGitDriven($event)) {
$error = $event->getException();
if ($error === null) {
$buildtimeForBuildhawk = $this->_formatBuildhawkTime(
Phing::currentTimeMillis() - $this->startTime
);
if (!$this->_addBuildTimeAsGitNote($buildtimeForBuildhawk)) {
$message = sprintf(
"Failed to add git note due to '%s'",
$this->_gitNotesCommandResponse
);
$this->printMessage($message, $this->err, Project::MSG_ERR);
}
}
}
}
/**
* Checks (rudimentary) if the project is Git driven
*
* @param BuildEvent $event
* @return boolean
*/
private function _isProjectGitDriven(BuildEvent $event)
{
$project = $event->getProject();
$projectRelativeGitDir = sprintf(
'%s/.git', $project->getBasedir()->getPath()
);
return file_exists($projectRelativeGitDir) && is_dir($projectRelativeGitDir);
}
/**
* Formats a time micro integer to buildhawk readable format.
*
* @param integer The time stamp
*/
private function _formatBuildhawkTime($micros) {
return sprintf("%0.3f", $micros);
}
/**
* Adds the build time as a git note to the current repository HEAD
*
* @param string $buildTime The build time of the build
* @return mixed True on sucess otherwise the command failure response
*/
private function _addBuildTimeAsGitNote($buildTime) {
$gitNotesCommand = sprintf(
"git notes --ref=buildtime add -f -m '%s' HEAD 2>&1",
$buildTime
);
$gitNotesCommandResponse = exec($gitNotesCommand, $output, $return);
if ($return !== 0) {
$this->_gitNotesCommandResponse = $gitNotesCommandResponse;
return false;
}
return true;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.