Skip to content

Instantly share code, notes, and snippets.

@raphaelstolt
Created November 20, 2010 14:42
Show Gist options
  • Save raphaelstolt/707868 to your computer and use it in GitHub Desktop.
Save raphaelstolt/707868 to your computer and use it in GitHub Desktop.
A Phing logger providing data for the buildhawk gem
<?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 <[email protected]>
* @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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment