Version
Take control over your Laravel app version
, (*1)
, (*2)
, (*3)
Description
This package is a Laravel (5.5+) utility which helps you keep and manage your application version, increment version numbers (major, minor, patch, commit), and can also use your last commit hash., (*4)
The end results of this package are:
- Print a version on a page.
- Print it in the console, via an Artisan command.
Full SemVer compatibility
This package is able to parse a SemVer version:, (*5)
v2.0.1-alpha.1227
And translate it to be used as:, (*6)
label: v
major: 2
minor: 0
patch: 1
prerelease: alpha
buildmetadata: 1227
commit: 49ffe2
You can use the format function to rewrite and show it in your app, for instance, as:, (*7)
MyApp version 2.0.1 - alpha 1227 (commit 49ffe2)
Some use cases for those results could be:
- Make sure a rollback was successful.
- Know if an update reached all servers.
- Check if a user is looking at the last version of your app.
- Verify if is Travis CI testing the version it is supposed to be testing.
- You simple love to version your stuff, and you like to see them in all your pages? That's cool too. :)
- What's your use case? Tell us!
Features
Easily control you app version using a YAML config file
``` yaml
version:
current:
major: 1
minor: 0
patch: 0
format: '{$major}.{$minor}.{$patch}'
commit:
mode: number
number: 701036, (*8)
### Use your git commit as your app commit hash/number
Configure it
``` yaml
commit:
mode: git-local
And you may have an output like this, (*9)
MyApp version 1.0.0 (commit a9c03f)
Or just use an incremental commit hash/number:, (*10)
``` yaml
commit:
mode: number
number: 701036, (*11)
To get
MyApp version 1.0.0 (commit 701036), (*12)
### Easily increment your version numbers, using Artisan commands
``` bash
php artisan version:commit
Which should print the new version number, (*13)
``` bash
New commit: 701037
MyApp version 1.0.0 (commit 701037), (*14)
Available for all of them:
``` bash
$ php artisan version:major
$ php artisan version:minor
$ php artisan version:patch
$ php artisan version:build
You can configure the :, (*15)
``` yaml
format:
version: "{$major}.{$minor}.{$patch}"
full: "version {{'format.version'}} (commit {$commit})"
compact: "v{{'format.version'}}-{$commit}", (*16)
Those are the results for `full` and `compact` formats
MyApp version 1.0.0 (commit 701037)
MyApp v1.0.0-701037, (*17)
It gives you access to dynamic methods:
``` php
Version::compact()
And should you create a new one:, (*18)
``` yaml
format:
awesome: "awesome version {$major}.{$minor}.{$patch}", (*19)
It will also become callable:
``` php
Version::awesome()
A Facade is available
``` php
Version::version() // 1.2.25, (*20)
Version::commit() // 703110, (*21)
Version::major() // 1, (*22)
Version::minor() // 2, (*23)
Version::patch() // 25, (*24)
Version::format('full') // version 1.0.0 (commit 703110), (*25)
Version::full() // version 1.0.0 (commit 703110) -- dynamic method, (*26)
Version::format('compact') // v.1.0.0-703110, (*27)
Version::compact() // v.1.0.0-703110 -- dynamic method, (*28)
### Instantiating it
If you prefer not to use the Façade:
``` php
dd(
Version::format()
);
The best ways to instantiate it are:, (*29)
A simple PHP object instantiation:, (*30)
``` php
$version = new \PragmaRX\Version\Package\Version();, (*31)
dd(
$version->format()
);, (*32)
Or to get an already instantiated Version object from the container:
``` php
dd(
app(\PragmaRX\Version\Package\Version::class)->format()
);
But you have to make sure you published the config file, (*33)
A Blade directive is also ready to be used in your views
You can use this directive to render a full version format:, (*34)
``` php
@version, (*35)
Or choose the format:
``` php
@version('full')
@version('compact')
You can configure the directive name:, (*36)
``` yaml
blade_directive: printversion, (*37)
Then
``` php
@printversion('compact')
You can use your git tags as application versions, all you need is to set the version source to "git":, (*38)
``` yaml
version_source: git, (*39)
And if you add a commit hash/number to your tags:
``` bash
$ git tag -a -f v0.1.1.3128
Version will use it as your app commit hash/number, (*40)
You probably only need to change the git version matcher, (*41)
``` yaml
git:
...
version:
matcher: "/[V|v][ersion]\s\.(\d+)\.(\d+)\.(\d+)\.(\w)/", (*42)
So let's say you tag your releases as
``` text
2017120299
YYYYMMDD##
You can change your matcher to, (*43)
``` yaml
git:
version:
matcher: "/(\d{4})(\d{2})(\d{2})(?:\d{2})/", (*44)
And remove dots from your formats:
``` yaml
format:
compact: "v{$major}{$minor}{$patch}-{$commit}"
Using the current application version in your code
Here's a community example on how to send the app version number when logging an exception to Bugsnag:, (*45)
<?php
namespace App\Exceptions;
use PragmaRX\Version\Package\Version;
use Bugsnag\BugsnagLaravel\Facades\Bugsnag;
class Handler extends ExceptionHandler
{
public function report(Exception $exception)
{
if ($this->shouldReport($exception)) {
Bugsnag::setAppVersion((new Version())->format('version'));
Bugsnag::notifyException($exception);
}
}
}
Commit Timestamp
This package also lets you absorb the last commit timestamp or store the current date to the version.yml file. This is the format in the config file:, (*46)
timestamp:
year:
month:
day:
hour:
minute:
second:
timezone:
To absorb you only need to configure mode: absorb
then execute:, (*47)
php artisan version:absorb, (*48)
But you can also set mode: increment
then execute:, (*49)
php artisan version:timestamp
To store the current date and time to the config file:, (*50)
``` text
$ php artisan version:minor
New timestamp: 2019-09-16 18:23:03
MyApp version 2.3.2 (commit 49ffe2), (*51)
And you can then use it to show in your app:
Version::format('timestamp-full'), (*52)
### Artisan commands
Those are the commands you have at your disposal:
#### version:show
Show the current app version:
``` text
$ php artisan version:show
PragmaRX version 1.0.0 (build 701031)
$ php artisan version:show --format=compact
PragmaRX v1.0.0-701031
$ php artisan version:show --format=compact --suppress-app-name
v1.0.0-701031
version:absorb
You need to set mode: absorb
., (*53)
Version can absorb git version and commit to the config file, so you can delete the .git folder and still keep your version and commit for fast access. You have to configure git_absorb
in your config file:, (*54)
``` yaml
commit:
#...
git_absorb: git-local # "false", "git-local" or "git-remote", (*55)
And run it
``` bash
$ php artisan version:absorb
The usual configuration setup to implement absorb is:, (*56)
``` yaml
version_source: config ## must be set as config
current:
major: 1 ## |
minor: 0 ## | --> will be changed by absorb
patch: 0 ## |
git_absorb: git-local ## configure to get from local or remote
commit:
mode: number ## must be set as number
number: f477c8 ## will be changed by absorb
git_absorb: git-local ## configure to get from local or remote, (*57)
#### version:(major|minor|patch|commit)
You need to set `mode: increment`.
Increment the version item:
``` text
$ php artisan version:minor
New minor version: 5
MyApp version 1.5.0 (commit 701045)
Regex Matcher
This is the current regex used to break a version string:, (*58)
^(?P<label>[v|V]*[er]*[sion]*)[\.|\s]*(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$
You can test it online: https://regex101.com/r/Ly7O1x/42, (*59)
Install
Via Composer, (*60)
``` bash
$ composer require pragmarx/version, (*61)
Then publish the configuration file you'll have to:
``` bash
$ php artisan vendor:publish --provider="PragmaRX\Version\Package\ServiceProvider"
And you should be good to use it in your views:, (*62)
``` php
@version, (*63)
As git versions are cached, you can tell composer to refresh your version numbers every time an update or install occur, by adding the refresh command to `post-autoload-dump`:
``` json
"post-autoload-dump": [
...
"@php artisan version:refresh"
]
[Optional] You may also can automated this process by set inside your .git/hooks/post-commit
. It will automatic run the command once you have make a commit., (*64)
``` bash, (*65)
!/bin/sh
php artisan version:refresh, (*66)
If you are using Git commits on your commit numbers, you may have to add the git repository to your .env file
``` text
VERSION_GIT_REMOTE_REPOSITORY=https://github.com/antonioribeiro/version.git
If you are using git-local
make sure the current folder is a git repository, (*67)
Minimum requirements
Testing
``` bash
$ composer test, (*68)
## Troubleshooting
- If you are having trouble to install because of symfony/router (3.3/3.4) or symfony/yaml (3.3/3.4), you can try to:
rm -rf vendor
rm composer.lock
composer install
```, (*69)
Author
Antonio Carlos Ribeiro, (*70)
License
This package is licensed under the MIT License - see the LICENSE
file for details, (*71)
Contributing
Pull requests and issues are welcome., (*72)