Automatic Versioning in Laravel 5

I started with this article http://afaraz.it/post/laravel-4-versioning-your-app-using-git

I had to make changes because the author is using a post-commit hook.  Every time I committed, I had a dirty repository since it was updating the version file after commit.  This solution is verified working in Laravel 5.

First, create your new command:

$ php artisan make:command Version

Here is my Version.php command file:

<?php namespace SalesMonkey\Console\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputArgument;

class Version extends Command
{

 /**
 * The console command name.
 *
 * @var string
 */
 protected $name = 'version:update';

 /**
 * The console command description.
 *
 * @var string
 */
 protected $description = 'Update the version number in app/config/version.php';

 /**
 * Create a new command instance.
 *
 */
 public function __construct()
 {
     parent::__construct();
 }

 /**
 * Execute the console command.
 *
 * @return mixed
 */
 public function fire()
 {
     // get file containing current version information
     $versionFile = 'config/version.php';

     // get the argument passed in the git command
     $version = $this->argument('app_version');

     // discard the commit hash
     $version = explode('-', $version);
     $realVersion = $version[0] . '-' . $version[1];

     // save the version array to a variable
     $array = var_export(array('app_version' => $realVersion), true);

     // construct file content
     $content = <<<CON
<?php

return $array;
CON;

     \File::put($versionFile, $content);
     $this->line('Setting version: ' . $realVersion);
 }

 /**
 * Get the console command arguments.
 *
 * @return array
 */
 protected function getArguments()
 {
     return [
         ['app_version', InputArgument::REQUIRED, 'version number is required'],
     ];
 }

 /**
 * Get the console command options.
 *
 * @return array
 */
 protected function getOptions()
 {
     return [];
 }

}

I didn’t want the commit has to be part of the version, so I explode on the hyphen and only use the first two array elements (the git command formats it’s output like this: tag-#commits_from_tag-commit_hash)

Register the command in Kernel.php:

protected $commands = [
    'SalesMonkey\Console\Commands\Version',
];

Create an empty version.php file in config/.

Create a pre-commit hook in your local repository. It should look like this:

#!/bin/sh
php `pwd`/artisan version:update `git describe --tags`
git add config/version.php
echo "updated version"
exit 0

You can test your results by running:

$ pa version:update 1.0-4
Setting version: 1.0-4

Open up config/version.php and you should see version 1.0-4 as the app_version!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>