• Stars
    star
    565
  • Rank 76,311 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Laravel App versioning

Version

Take control over your Laravel app version

Latest Stable Version License Code Quality Build

Coverage StyleCI Downloads

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.

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:

v2.0.1-alpha.1227

And translate it to be used as:

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:

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

version: 
    current:
        major: 1
        minor: 0
        patch: 0
        format: '{$major}.{$minor}.{$patch}'
    commit:
        mode: number
        number: 701036

Use your git commit as your app commit hash/number

Configure it

commit:
    mode: git-local

And you may have an output like this

MyApp version 1.0.0 (commit a9c03f)

Or just use an incremental commit hash/number:

commit:
    mode: number
    number: 701036

To get

MyApp version 1.0.0 (commit 701036)

Easily increment your version numbers, using Artisan commands

php artisan version:commit

Which should print the new version number

New commit: 701037
MyApp version 1.0.0 (commit 701037) 

Available for all of them:

$ php artisan version:major   
$ php artisan version:minor   
$ php artisan version:patch   
$ php artisan version:build   

The output format is highly configurable

You can configure the :

format:
  version: "{$major}.{$minor}.{$patch}"
  full: "version {{'format.version'}} (commit {$commit})"
  compact: "v{{'format.version'}}-{$commit}"

Those are the results for full and compact formats

MyApp version 1.0.0 (commit 701037)
MyApp v1.0.0-701037

It gives you access to dynamic methods:

Version::compact()

And should you create a new one:

format:
  awesome: "awesome version {$major}.{$minor}.{$patch}"

It will also become callable:

Version::awesome()

A Facade is available

Version::version() // 1.2.25

Version::commit() // 703110

Version::major() // 1

Version::minor() // 2

Version::patch() // 25

Version::format('full') // version 1.0.0 (commit 703110)

Version::full() // version 1.0.0 (commit 703110) -- dynamic method

Version::format('compact') // v.1.0.0-703110

Version::compact() // v.1.0.0-703110 -- dynamic method

Instantiating it

If you prefer not to use the Façade:

dd(
    Version::format()
);

The best ways to instantiate it are:

A simple PHP object instantiation:

$version = new \PragmaRX\Version\Package\Version();

dd(
    $version->format()
);

Or to get an already instantiated Version object from the container:

dd(
    app(\PragmaRX\Version\Package\Version::class)->format()
);

But you have to make sure you published the config file

A Blade directive is also ready to be used in your views

You can use this directive to render a full version format:

@version

Or choose the format:

@version('full')
@version('compact')

You can configure the directive name:

blade_directive: printversion

Then

@printversion('compact')

Git tags

You can use your git tags as application versions, all you need is to set the version source to "git":

version_source: git

And if you add a commit hash/number to your tags:

$ git tag -a -f v0.1.1.3128

Version will use it as your app commit hash/number

Matching other version (git tags) formats

You probably only need to change the git version matcher

git:
  ...
  version:
    matcher: "/[V|v]*[ersion]*\\s*\\.*(\\d+)\\.(\\d+)\\.(\\d+)\\.*(\\w*)/"

So let's say you tag your releases as

2017120299
YYYYMMDD##

You can change your matcher to

git:
  version:
    matcher: "/(\d{4})(\d{2})(\d{2})(?:\d{2})/"

And remove dots from your formats:

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:

<?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:

timestamp:
  year:
  month:
  day:
  hour:
  minute:
  second:
  timezone:

To absorb you only need to configure mode: absorb then execute:

php artisan version:absorb

But you can also set mode: increment then execute:

php artisan version:timestamp

To store the current date and time to the config file:

$ php artisan version:minor
New timestamp: 2019-09-16 18:23:03
MyApp version 2.3.2 (commit 49ffe2)

And you can then use it to show in your app:

Version::format('timestamp-full')

Artisan commands

Those are the commands you have at your disposal:

version:show

Show the current app version:

$ 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.

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:

commit:
  #...  
  git_absorb: git-local # "false", "git-local" or "git-remote"

And run it

$ php artisan version:absorb

The usual configuration setup to implement absorb is:

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 

version:(major|minor|patch|commit)

You need to set mode: increment.

Increment the version item:

$ 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:

^(?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

Install

Via Composer

$ composer require pragmarx/version

Then publish the configuration file you'll have to:

$ php artisan vendor:publish --provider="PragmaRX\Version\Package\ServiceProvider"

And you should be good to use it in your views:

@version

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:

"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.

#!/bin/sh

php artisan version:refresh

If you are using Git commits on your commit numbers, you may have to add the git repository to your .env file

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

Minimum requirements

  • Laravel 5.5
  • PHP 7.0

Testing

$ composer test

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

Author

Antonio Carlos Ribeiro

License

This package is licensed under the MIT License - see the LICENSE file for details

Contributing

Pull requests and issues are welcome.

More Repositories

1

tracker

Laravel Stats Tracker
PHP
2,824
star
2

health

Laravel Health Panel
PHP
1,879
star
3

countries

Laravel countries and currencies
PHP
1,741
star
4

google2fa

A One Time Password Authentication package, compatible with Google Authenticator.
PHP
1,684
star
5

firewall

Firewall package for Laravel applications
PHP
1,362
star
6

google2fa-laravel

A One Time Password Authentication package, compatible with Google Authenticator for Laravel
PHP
825
star
7

tddd

A Laravel Continuous Integration Package
Vue
727
star
8

laravelcs

Laravel PHP_CodeSniffer
PHP
236
star
9

deeployer

Deploy your Laravel applications via Github or Bitbucket Hooks
PHP
146
star
10

countries-laravel

Countries for Laravel
PHP
141
star
11

yaml

A Laravel YAML parser and config loader
PHP
110
star
12

zipcode

Zip code searcher
PHP
97
star
13

steroids

Laravel 4 Blade on Steroids
PHP
95
star
14

coollection

Laravel Collection Objectified
PHP
87
star
15

google2fa-qrcode

QRCode for Google2FA
PHP
84
star
16

recovery

Create recovery/backup codes for 2FA
PHP
72
star
17

random

Generate random strings or numeric values
PHP
70
star
18

glottos

A PHP 5.3+ Translation/Localization System
PHP
69
star
19

sqli

A Laravel Artisan SQL Interactive Interface
PHP
60
star
20

support

Support Classes
PHP
58
star
21

ia-arr

Laravel Illuminate Agnostic Arr
PHP
49
star
22

ia-str

Laravel Illuminate Agnostic Str
PHP
46
star
23

ia-collection

Laravel Illuminate Agnostic Collection
PHP
43
star
24

artisan-anywhere

Execute Artisan from anywhere in your Laravel project tree
43
star
25

dev-box

Development Box Provisioning in Ansible
Shell
41
star
26

artisan-tool

Nova Artisan Tool
PHP
35
star
27

glottosAdmin

Glottos Admin Panel and Starter
JavaScript
27
star
28

laravel-installer

Laravel Framework Installer Script for Unlix-Like Systems
Shell
26
star
29

google2fa-starter

Google2FA Starter App
PHP
26
star
30

tddd-starter

Laravel TDDD Starter App
PHP
23
star
31

nova-boolean-datetime-field

A Laravel Nova Boolean DateTime field
PHP
20
star
32

skel

A PHP Package Creator & Skeleton
Shell
12
star
33

health-docker

App Health Panel for Docker Environments
PHP
8
star
34

google2fa-php

A One Time Password Authentication PHP class, compatible with Google Authenticator
PHP
6
star
35

lumen-image-processor

Lumen Image Processor
PHP
5
star
36

http-basic-auth

HTTP Basic Auth middleware for Laravel
Shell
3
star
37

trivia

Trivia database
PHP
3
star
38

vanhack-agentbot

Vanhack Agent Bot
PHP
3
star
39

pragmarx.com

Source of pragmarx.com
PHP
3
star
40

zsh

zsh
Shell
3
star
41

backup-server

backup-server
PHP
3
star
42

googleforms

Post to Google Form Spreadsheets
PHP
2
star
43

sdk

sdk
PHP
1
star
44

fluxbb-style

The FluxBB style for the Laravel forums.
1
star
45

core-libraries

core libraries
PHP
1
star
46

veveystore

Vevey Store
HTML
1
star
47

a17ex

Area 17 Exercice
PHP
1
star
48

church.api

Church API
PHP
1
star