TeXStudio – How to Run Commands Sequentially in TeXStudio Script

macrostexstudio

I'm trying to set up a macro to set up git repo for version control, so that I don't have to resort to manual terminal commands every time. Commit & push scripts are ready and working, it's initialising a new git repo that gives me grief:

The idea is that a script will perform 4 functions:

  1. Initialise a git repo at the same directory your file is saved
  2. Clone a remote repo (I have a base repo with scripts/packages I want present into all tex projects I create)
  3. Create a new remote repo (using Node GH) and assign it as the origin (basically re-creating the ability to fork a repo from yourself via command line)
  4. Commit and push all files to the new remote repo.

Here's my code:

%SCRIPT
buildManager.runCommand("git init", editor.fileName());
buildManager.runCommand("gh re --new " + editor.fileName() + 
  " --type private", editor.fileName());
buildManager.runCommand("git clone https://github.com/example/example.git " 
  + editor.fileName(), editor.fileName());
buildManager.runCommand("git remote set-url origin https://github.com/example/" 
  + editor.fileName(), editor.fileName());
buildManager.runCommand("git push origin master", editor.fileName());
buildManager.runCommand("git push --all", editor.fileName())

The problem is I think TeXStudio doesn't perform the commands in order of appearance, which results in some commands failing (sample output at the bottom). Is there a way to define the order the commands are performed? Alternatively, can I do the same by calling a bash script (where I can definitely define the order) and pass the working dir through the macro?


Log output:

Process started: git push --all
fatal: Not a git repository (or any of the parent directories): .git
Process exited with error(s)    
fatal: Not a git repository (or any of the parent directories): .git
Process exited with error(s)
fatal: Not a git repository (or any of the parent directories): .git
Process exited with error(s)
fatal: could not create work tree dir '/NewProject': Permission denied
/.git: Permission denied
Process exited with error(s)
Process exited with error(s)
Process exited normally

Best Answer

buildmanager.runCommand() does not wait for the termination of the command. If you need that control, you should use system() instead. It returns a process object which you then can control (for details see the manual).

In your particular case, it would still be a bit tedious because you want to have the command executed in the directory of the file. As of now (TXS 2.10.8) the working directory of system() is the working directory of TeXstudio. So you either have to start TeXstudio from the shell in the desired directory or you have to wrap the git call in a shell script that changes the directory first.

Future releases will allow an optional second argument to system() for providing a working directory.

filename = editor.fileName()
workdir = filename.substring(0, filename.lastIndexOf("/"))
proc = system("git init", workdir)
proc.waitForFinished()
...