├── .gitignore ├── README.md ├── composer.json ├── resources └── views │ └── version-comment.blade.php └── src ├── Exception └── CouldNotGetVersionException.php ├── GitVersionHelper.php └── GitVersionServiceProvider.php /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | vendor 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Laravel/Lumen project git version getter 2 | ======================================== 3 | 4 | A helper class to get the current git version of the project. 5 | 6 | Expects either a `version` file to exist in the `base_path()` of your project 7 | containing a version string, or the `git` binary to be available. 8 | 9 | Framework version 10 | ----------------- 11 | 12 | This package works with both Laravel 4 and 5, and also Lumen. 13 | 14 | Installation 15 | ------------ 16 | 17 | Require it in your Laravel/Lumen project: 18 | 19 | composer require tremby/laravel-git-version 20 | 21 | ### Optional packages 22 | 23 | This module uses [Symfony's Process component][process] if available, 24 | or falls back to `shell_exec` otherwise. 25 | So if your deployment environment has `shell_exec` disabled, 26 | you can work around this by installing `symfony/process`. 27 | 28 | [process]: https://github.com/symfony/process 29 | 30 | Use 31 | --- 32 | 33 | You can get the git version string with 34 | 35 | \Tremby\LaravelGitVersion\GitVersionHelper::getVersion() 36 | 37 | Or you can get your app name and version number such as `my-project/1.0` with 38 | 39 | \Tremby\LaravelGitVersion\GitVersionHelper::getNameAndVersion() 40 | 41 | The app's name is taken from `Config::get('app.name', 'app')`, so you can 42 | configure it in your `config/app.php` file or leave it as the default of `app`. 43 | 44 | ### Recommended usage pattern 45 | 46 | Ensure your git tags are pushed to your servers 47 | so that the versions are described properly. 48 | 49 | During development and possibly in staging environments 50 | allow the version to be determined automatically 51 | (this is done via `git describe`). 52 | 53 | As part of your production deployment procedure, 54 | write a `version` file (perhaps via a command like 55 | `git describe --always --tags --dirty >version`, 56 | since this is the command this package would run otherwise). 57 | When this `version` file exists the package will use its contents 58 | rather than executing `git`, saving some processor and IO time. 59 | 60 | Add `/version` to your `.gitignore` file 61 | so your working tree stays clean and you don't accidentally commit it. 62 | 63 | View 64 | ---- 65 | 66 | A view is provided which just outputs an HTML comment with the return value of 67 | `getNameAndVersion()`. I like to include this in the main layout template of the 68 | project. 69 | 70 | To use this, install the service provider by adding it to your `config/app.php` 71 | file: 72 | 73 | 'providers' => [ 74 | ... 75 | Tremby\LaravelGitVersion\GitVersionServiceProvider::class, 76 | ], 77 | 78 | Then the view is available: 79 | 80 | @include('git-version::version-comment') 81 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tremby/laravel-git-version", 3 | "description": "A helper to get the current git version of the application", 4 | "require": { 5 | "illuminate/support": ">=4" 6 | }, 7 | "suggest": { 8 | "symfony/process": "Used rather than `shell_exec` if available (>=3.3)." 9 | }, 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Bart Nagel", 14 | "email": "bart@tremby.net" 15 | } 16 | ], 17 | "autoload": { 18 | "psr-4": { 19 | "Tremby\\LaravelGitVersion\\": "src/" 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /resources/views/version-comment.blade.php: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /src/Exception/CouldNotGetVersionException.php: -------------------------------------------------------------------------------- 1 | /version exists, its contents are trimmed and used. 23 | * Otherwise we get a suitable string from `git describe`. 24 | * 25 | * @throws Exception\CouldNotGetVersionException if there is no version file and `git 26 | * describe` fails 27 | * @return string Version string 28 | */ 29 | public static function getVersion() 30 | { 31 | // If we have a version file, just return its contents 32 | if (file_exists(self::versionFile())) { 33 | return trim(file_get_contents(self::versionFile())); 34 | } 35 | 36 | $path = base_path(); 37 | 38 | // Get version string from git 39 | $command = 'git describe --always --tags --dirty'; 40 | $fail = false; 41 | if (class_exists('\Symfony\Component\Process\Process')) { 42 | try { 43 | if (method_exists(Process::class, 'fromShellCommandline')) { 44 | $process = Process::fromShellCommandline($command, $path); 45 | } else { 46 | $process = new Process($command, $path); 47 | } 48 | 49 | $process->mustRun(); 50 | $output = $process->getOutput(); 51 | } catch (RuntimeException $e) { 52 | $fail = true; 53 | } 54 | } else { 55 | // Remember current directory 56 | $dir = getcwd(); 57 | 58 | // Change to base directory 59 | chdir($path); 60 | 61 | $output = shell_exec($command); 62 | 63 | // Change back 64 | chdir($dir); 65 | 66 | $fail = $output === null; 67 | } 68 | 69 | if ($fail) { 70 | throw new Exception\CouldNotGetVersionException; 71 | } 72 | 73 | return trim($output); 74 | } 75 | 76 | /** 77 | * Get a string identifying the app and version 78 | * 79 | * @see getVersion 80 | * @throws Exception\CouldNotGetVersionException if there is no version file and `git 81 | * describe` fails 82 | * @return string App name and version string 83 | */ 84 | public static function getNameAndVersion() 85 | { 86 | return self::appName() . '/' . self::getVersion(); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/GitVersionServiceProvider.php: -------------------------------------------------------------------------------- 1 | loadViewsFrom(__DIR__ . '/../resources/views', 'git-version'); 17 | } else { 18 | // Laravel 4 19 | $this->package('tremby/git-version', null, __DIR__ . '/../resources'); 20 | } 21 | } 22 | } 23 | --------------------------------------------------------------------------------