Advanced Scripting and Code Generation Tools and Unit Testing

I am working on a project where I wrote a tool that generates tool based on an .xml configuration file. Basically from that file I generate a folder under /lib/, so that it can be used as a library. So far I manually run the tool whenever I changed the .xml file.

I basically have two questions but maybe there is also the general question if my approach is correct/makes sense at all or if I should approach the problem differently.

The first question is how can I automatically invoke the code generation tool?
I tried to add a python script in the extra_scripts option with pre: and that does work if that scripts directly invokes the tool. However I was not able to find out how I could run the tool before the build stage with using env.AddPreAction("buildprog", callback) in the script.
Ideally the tool would only be used when the .xml file did change. Is there a way to do that?

The second questions is regarding unit tests:
For several tests I need a specific .xml file to be used for the code generation, so that before compiling testA I need to generate code based upon testA.xml and before compiling testB I need to generate code based upon testB.xml.
The closest I came to solving this problem was to create a section in the platformio.ini for each different .xml file that I need to use and add the extra_script option using different scripts (which just directly invoke the generator). This does not really work when I run all the unit test however.
Is there a way to register callbacks to be executed prior to compiling the code for a specific test, so that I could call my tool with the .xml file inside the test_ folder?

Currently my .ini file looks something like this:

[env]
test_framework = unity
check_tool = 
    clangtidy
    cppcheck
check_flags = 
    clangtidy: --config-file=.clang-tidy

[env:native]
platform = native
test_ignore = embedded/*

[env:uC]
# ... some platform info etc.
test_ignore = *
extra_scripts = pre:generate.py

[env:uCTestA]
extends = env:uC
test_ignore =
test_filter = embedded/peripheral/test_A
extra_scripts = pre:generateTestA.py 

[env:uCTestB]
extends = env:uC
test_ignore = 
test_filter = embedded/peripheral/test_B
extra_scripts = pre:generateTestB.py

Is it possible to use a code generation tool in such a way?
My alternative solution would be to write a script that runs the generator and than uses the cli of platformio to run the specific test, but I would prefer to manage everything in platformio if possible.
Any help and suggestions are appreciated:)