How to automatically pull the version number?

command-line-interface, composer-php, git, php, symfony

Solution

Pulling the version number from somewhere could be difficult, because you could run an older version of the application but wrongly pull the newest version number.

You could add a "release.version" file to your repo that only contains the version number and read this to pass it to the constructor.

Then you can use a git hook like pre-commit to update this file before you commit a tag.

This however is not recommended in distributed systems because it could lead to collisons, but for smaller teams or teams with a dedicated release manager it might be ok. Read here: https://wincent.com/blog/automatic-deployment-numbering-in-git and How do I enable ident string for Git repos?

It would maybe be enough to have a pre-commit hook that checks for a new tag and just reminds you to increment the version in the code ;).

Problem

I'm writing a Symfony Console PHP application which takes a version number in the constructor. ``` $app = new Application('myapp', '1.0'); ``` I've already found that it's easy to forget to bump the version number when making a new git tag release. How can I do this dynamically/automatically? Aside from here on SO, I searched packagist pretty deeply because I thought for sure this was a common thing, but wasn't able to turn up anything. I thought at first to write a function that would do something like this: ``` chdir(__DIR__); shell_exec('git describe --abbrev=0 --tags'); ``` But because I globally require this CLI app with composer, it doesn't have the git repository with the source code. My next idea is that I know I can at least get it from composer ``` composer global show myname/mypackage ``` But this spits out a ton of information and there's no option that I'm aware of to just show the version number. I feel like filtering through all this with something like regex might be overkill. Is there any better way?

Original source

Related problems