Hi,

I want to run a python script before i build and run my project. I noticed i can create my own run.sh inside the project directory. I can call the python script from there, but how do i then build and run the project as would normally happen when clicking on the build and run button.

Thanks!

The IDE calls make -C /root/Bela all PROJECT=ProjectName to build the project, followed by make -C /root/Bela runide PROJECT=ProjectName CL="..." to run it, where "..." are the command line options built from the project settings in the IDE. The Makefile looks for a file RUN_FILE?=$(PROJECT_DIR)/run.sh. If it finds it, it doesn't build anything and it just executes the file.

From your own run.sh file you can call make RUN_FILE=/non/existing/path PROJECT=ProjectName run CL="...". This will override RUN_FILE so that it points to a non-existing path, then make will not find it and therefore build and run the project as if run.sh was not there.

Thanks! I got it working. Not sure if this is the best way to do it since i had to write a small script to parse the IDE settings from the settings.json file to command line parameters for make.

Is there a better way to do that? This is my run.sh

echo  "Building project from custom build script"
python create_config.py
echo "Succesfully run pre-build script"

CL=$(python parse_cl_args.py)
echo "$CL"
make -C /root/Bela RUN_FILE=/non/exisiting/path PROJECT=AudioPlayer run CL="$CL"

    That's the sort of thing you need to do it "properly" (please share parse_cl_args.py). I tend to be more low-tech: I just run the program once via the IDE, then look in journalctl -fu bela_ide for what actually gets run and copy that and hardcode it in my script.

    Ok cool, good to know. Here is the python script for parsing the cl args:

    import json
    import re
    
    with open("settings.json") as f:
        settings = json.load(f)
    
    clargs = settings.get("CLArgs", {})
    cmd_parts = []
    
    for key, value in clargs.iteritems(): 
        if not value:
            continue
        
        # Doesn't seem like a valid argument
        if key == "audioExpander":
            continue
    	
        # Channel gains need a comma, rest don't
        m = re.match(r"(-[HI])(\d+),?$", key)
        if not m:
    	key = key.replace(",", "")
        
        # Add extra space for long arguments
        m = re.match(r"^--", key)
        space = ""
        if m:
            space = " "
            
        cmd_parts.append(key + space + str(value))
    
    print(" ".join(cmd_parts))

    roel python create_config.py

    Another alternative is to add a custom target to the Makefile and make that a prerequisite of the all target. For this purpose, you could create a CustomMakefileTop.in (which is automatically included by the Makefile). In that file, you'd put:

    all: prebuild
    
    prebuild:
    	@echo prebuild target for project $(PROJECT)
    	python create_config.py 

    though this would apply to all projects on the board.

    Even more fun, I don't think we are doing a lot of escaping/sanitisation of the content of the User Command Line Arguments textbox: you could inject some bash in there. For instance, instead of typing --help in there, you could type

    `echo --help`

    then running the program you will get its help printout.

    In your case, you could add in User Command Line Arguments:

    `cd projects/MYPROJECT && python create_config.py`

    Don't rely on this "feature" to stay there forever ;-).