Matthew Fritz Matthew Fritz - 1 year ago 87
PHP Question

PhpStorm update version text in .env file whenever a file is saved

I'm using PhpStorm to manage many of my client Laravel PHP projects, however as these projects have become larger it's become important to maintain and also show version information inside the system I'm developing.

To do this I'm using the

.env
file to hold a config called
APP_VERSION
.

What I would like to do is whenever a file is saved inside PhpStorm - any file - I want to automatically update the value (by incrementing) for this configuration variable.

However I can't seem to find a way to do this, or if it's even possible.

I have looked at File Watchers but this doesn't seem to solve the problem I'm having in that it only allows certain file types and even then it wants to run a compiler (such as a Sass or Less compiler, etc.) which is certainly not what I'm after.

Is there any way to do this, without me having to manually increment the minor version number with each file I update?

Update

So by using an adaptation of the answer by @LazyOne I created a small script which executes the necessary text update to the file in question.

I used a custom File Watcher which was setup against changes to any file type with a custom scope created to exclude certain files and folders that I didn't want watched.

End result, when I save a file that is within a valid scope, the script I wrote executes to update my file with the necessary change.

Answer Source

PhpStorm does not have such functionality (including other IDEA-based IDEs). There are some tickets asking for such functionality .. but it will definitely not work for your scenario (the use case there is different).

You will have to do it "manually". By this I mean:

  • either actual manually editing the file (as in your question)...
  • or automate it by writing some script (e.g. in PHP) that would do it for you and then just execute it either on demand (e.g. External Tools or manually) or via File Watcher (File Watcher is basically (simply speaking) External Tools that is run on file modification -- you can run any program there, even your own shell/batch script).

Such script will open your .env file, find the right line and edit the value there -- little bit of file parsing job -- nothing super heavy.

Hint: Laravel has artisan .. and one of the commands is key:generate (which alters .env file -- at very least it is run at very least once when you create new Laravel app). You may do it in similar fashion -- do your script as artisan command (so that you have full Laravel power behind it) and just call it when needed (File Watcher or whatever).


P.S. Instead of editing .env file (which is more environment specific rather than global setting, which version info is) .. why not store this in custom config file (e.g. config/version.php) which will contain that info only and therefore will be much easier to alter or even generate from scratch (template) as it has much simple structure and therefore no need to maintain other existing info there (which you have to do with .env files)?

At the end of the day -- there is no big difference for you/programmers between calling env('APP_VERSION') and config('version.app_version') to get that info in your app.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download