Running a script after building

Hi,

I’m trying to run a .bat file after building my project. I want to do this to create a file and add it to .git so when people pull the project the file is detected and a build is automatically made (via a post pull script). However, I have no luck in running the batch file from my javascript code. Is there any way of doing this?

Things I have tried:

studio.project.buildEnded.connect(function(success) {
var projectPath = studio.project.filePath.replace(‘project.fspro’, ‘’);
//WshShell = new ActiveXObject(“WScript.Shell”);
//WshShell.Run(‘file://’ + projectPath + ‘postbuild.bat’,1,true);
var shell = WScript.CreateObject(“WScript.Shell”);
shell.Run("start ‘’ /D “+projectPath+ " /W postbuild.bat”);
});

Thanks

Solution is using system.start

studio.project.buildEnded.connect(function(success) {
var projectPath = studio.project.filePath.replace(‘project.fspro’, ‘’);
try {
studio.system.start(projectPath+“postbuild.bat”, { timeout: 200000 });
}
catch(err) {
studio.system.message(“Failed to run”);
var textFile = studio.system.getFile(outputPath);
}
});

So I thought I solved this, but after developing the script I noticed it wasn’t running. Or at least not completing, but without any errors thrown.

This code here runs a script. I’ve added a while loop that checks if the script is running, but that freezes FMOD and doesn’t unfreeze after the 20000ms timeout. If you remove the while loop no freeze occurs, but it nothing happens either way.

function runAScript() {
    var projectPath = studio.project.filePath.replace('project.fspro', '');
    try 
    {
        console.log("Running "+projectPath+"script.bat");
        var z = studio.system.startAsync(projectPath+"script.bat", { timeout: 20000 });
        while(z.isRunning()){
            console.log(z.readAllStandardOutput());
            console.log(z.readAllStandardError());
        }
    }
    catch(err) 
    {
        studio.system.message("Failed to run "+ projectPath+"script.bat");
    }
}

The script itself is really simple. It creates an empty file.

copy /y NUL thisisafile >NUL

Hope to see a response from a developer, because the documentation is not making me any wiser at this moment. And searching the forums I only found one thread that uses the system.start command.

Thanks!

1 Like

Bump. This one is quite vital for my workflow.

Hi sorry for the late reply. Could you please post what your bat file is trying to run? It might be that the FMOD startAsync() function is running correctly but the build script is either not being called correctly or has some error.

Edit: Oh, sorry I just saw the bat file copy /y NUL thisisafile >NUL command.

I have been able to get FMOD Studio to find out the issue is to do with projectPath variable. Please use this at the top of your script instead of var projectPath

var projectPath = studio.project.filePath;
projectPath = projectPath.substr(0, projectPath.lastIndexOf("/"));

Hi. Thanks for the response! I’ve tried your solution but it gives me the same result. Just to be sure we are on the same page. The function below gives you a file in the root folder of your project?

var projectPath = studio.project.filePath;
projectPath = projectPath.substr(0, projectPath.lastIndexOf("/") +1);

function runAScript() {
    try 
    {
        console.log("Running "+projectPath+"script.bat");
        var z = studio.system.startAsync(projectPath+"script.bat", { timeout: 20000 });
    }
    catch(err) 
    {
        studio.system.message("Failed to run "+ projectPath+"script.bat");
    }
}


var test = {
    name: "test",
    execute: function(){ runAScript(); }
};

var automationMissing = true;
var menuItems = studio.menu.menuItems();
for (var i = 0; i < menuItems.length; ++i) {
    if (menuItems[i].name == test.name) {
        automationMissing = false;
        break;
    }
}

if (automationMissing) {
    studio.menu.addMenuItem(test);
}

Edit: I’ve added +1 to the following row.

projectPath = projectPath.substr(0, projectPath.lastIndexOf("/") +1);

Now my console says the following:

Running  pathToMyProject/FMOD/script.bat 

Which seems correct. But I do not get a file in a my project root directory.

The issue seems to be that running a bat script that does not explicitly state its directory does not work as expected. The bat script by itself will run and create this “thisisafile” file but when run by an external program (such as FMOD Studio) it will create it in a temporary folder somewhere not expected by the user.

Instead, please make the following adjustments:

  1. In the bat script, make these adjustments to allow it to take a path as an argument and create the file in that directory:
echo off
set arg1=%1
cd %arg1%
copy /y NUL thisisafile >NUL
  1. Please change line 8 of your script to provide the project path. Ensure the directory provided exists, the bat script does not automatically create directories if they don’t exist:
var z = studio.system.startAsync(projectPath+"myScript.bat", { timeout: 20000, args: [projectPath + "/script_export"] }); // Ensure this path exists or it will silently fail

Yes! So simple. Thanks a bunch Richard that really helps me a lot. To tackle this issue a little simpler, you can add the following line to your script which will set the directory of the script to the location of the script.

cd "%~dp"

So you get

cd /D "%~dp0"
copy /y NUL thisisafile >NUL

And it will work as well. Thanks again.