4

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?

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • What's the objective? To detect the latest version (by tag) of the repository in the current working directory that the app is running in? – scrowler Oct 25 '16 at 03:44
  • Yes, that would be good, but like I mentioned in my question, there's no git repository when installed via composer. – Jeff Puckett Oct 25 '16 at 04:49
  • 1
    The objective is when I tag a new release, I don't want to have to remember to first update that hard coded string with the version number. – Jeff Puckett Oct 25 '16 at 04:57
  • Right. Can you retrieve it dynamically every time from an API, or it that overkill? – scrowler Oct 25 '16 at 05:06

1 Answers1

3

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 ;).

ivoba
  • 5,780
  • 5
  • 48
  • 55