Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Git hash creator
<?php namespace Elevio\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
class GitVersion extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'venturecraft:gitversion';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Work out the version number to display and store in local file.';
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$version_file = app_path() . '/../elevio_version.txt';
$git_hash_file = app_path() . '/../git_hash.txt';
$short = exec('git rev-parse --short HEAD');
$full = exec('git describe --tags');
$parts = explode('-', $full);
$structured = 'N/A';
if (strlen($parts[0])) {
$structured = str_replace('v', '', $parts[0]);
if (isset($parts[1])) {
$structured .= '.' . $parts[1];
} else {
$structured .= '.0';
}
if ($this->option('include_hash') && isset($parts[2])) {
$structured .= ' ' . $parts[2];
}
}
file_put_contents($git_hash_file, $short);
file_put_contents($version_file, $structured);
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
array('include_hash', null, InputOption::VALUE_NONE, 'Include commit hash', null),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment