I don’t think there’s a way of doing this directly from the Projucer.
The simplest way is probably to use a pre-build script that generates a .cpp file containing a constant string (const char* currentGitBranchName = ...) representing the branch name, and to declare extern const char* currentGitBranchName; in one of your headers.
To avoid unnecessary rebuilds/links, your pre-build script might check the current contents of the file and avoid modifying it if the git branch hasn’t changed.
Yes I’ve searched whole world internet
And I found many advices exactly as yours, to create with script the file containing string with branch name.
But the problem is I can’t find exactly what command should I use and exactly where in Projucer should I paste it? I can’t even find any tutorial how to write such scripts. So I don’t even know what syntax should be used.
I tired to use everything was shown here:
I pasted code to Projucer. But after all I always have errors. I’ve already spent about 4 hours to solve that, but still with no success.
Are you building on multiple platforms? If so, there may not be a single answer. On Windows, you’d probably need a Batch script to do it, and you might use sh or bash on macOS/Linux. Alternatively, you could ensure that your build machine has Python installed, and then use that.
This sort of thing is one reason that developers use tools like CMake. CMake is a scripting language as well as a build system, so it can be used to implement these sorts of custom behaviours.
In one project, I used the following setup to add a git hash to my builds (branch name could be achieved similarly):
Hello reuk,
great great great thanks for your detailed help.
Unfortunately it doesn’t help me. At the moment I have only VS available to test. Later I will try on my Mac in my home.
By the way it still looks very complicated for me.
While I just need to call in command line:
git branch --show-current
And then the result I need to assign to some macro like #define CURRENT_BRANCH or anything like that. So do I really need so much line of code to do do that?
I’ve already found my own script, but due to fact I don’t know the syntax, my script is much longer but probably do the same as your one line. My looks like that:
git branch --show-current >> temp_file.h
set /p branchname=<temp_file.h
set branchmacro=#define BRANCH_NAME %branchname%
echo #pragma once >> git_data.h
echo %branchmacro% >> git_data.h
del temp_file.h
But the problem is I still have no idea how to use it in Projucer, to force Projucer to launch those lines while opening project in VS from Projucer?
Szemek, unfortunately your line of code doesn’t work. It produces branch.h file, but inside of that file I get line: "#define CURRENT_BRANCH \"$(git branch --show-current)\""
So there is not my current branch name, there is only git command printed.
I tried to remove quotation marks, but it desn’t help.
But actually it doesn’t matter while I have my own lines which work fine. But I have no idea how to use that from Projucer.
And the second question is, why do you say it’s very naive approach? What’s wrong with that?
OK finally I’ve done that.
I created myScript.bat file.
And inside that file I added those lines:
git branch --show-current >> temp_file.h
set /p branchname=<temp_file.h
set branchmacro=#define BRANCH_NAME %branchname%
echo #pragma once >> git_data.h
echo %branchmacro% >> git_data.h
del temp_file.h
And then in Projucer project settings in field “Post-Export Shell Command (Windows)” I added line: %%1%%\myScript.bat
And finally it works as I expect. But I spent whole day to do such small thing. And still somebody say it’s naive approach. So I am really sad, but still happy I found the solution
I tested my one-liner in zsh shell on macOS, but command prompt or PowerShell on Windows have different interpolations - that’s why my one-liner didn’t work for you.
Answering your second question - nothing is wrong with naive approach. If it works for you, then it works for you. I’m glad that you found the solution
Well, I wrote something different I was describing only my idea, not yours.