Using the bash script in our pipeline
Unity doesn't allow us to create text files that are not meant to be used as C# or JavaScript scripts, so access the folder Assets/Tools/Bash from outside and create a new text file called mac_githash.sh and add the following code to the file:
#! /usr/bin/env sh
SHORT_HASH="$( git log --pretty=format:'%h' -n 1 )"
echo $SHORT_HASH
This script grabs the current commit hash and returns a short version of it. So, instead of 40 characters, this will return something like 4cd9c3a.
Note
You must give execution permission to this script in order to make it work.
To use and call these kinds of scripts from Unity, we will create a utility method that receives the path from the script and parameters, if necessary.
Inside the Builder class, add the following method:
private static string ExecuteCommand (
string command, string arguments = "") {
System.Diagnostics.Process pProcess = new
System.Diagnostics.Process ();
pProcess.StartInfo.FileName = command;
pProcess.StartInfo.Arguments = arguments;
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.Start ();
string strOutput = pProcess.StandardOutput.ReadToEnd ();
pProcess.WaitForExit ();
return strOutput;
}
)
In this method we use the Process class, which is part of the .Net API. This provides access to local and remote processes and enables you to start and stop local system processes.
Basically, we are creating a Process instance here, setting its properties, and finally executing it. If there is any kind of output, that will be returned by the method as a string.
For each bash script we want to use, we must use this method to integrate it with our editor scripting code.
Still in the Builder class, we now create a new method to wrap the scripts we created in the folder Bash:
private static string batchPath = Application.dataPath + "/Tools/AppBuilder/Bash";
private static string GitHash () {
string command = batchPath + "/mac-githash.sh";
string output = ExecuteCommand (command);
// We trim the output to remove new lines at the end.
return output.Trim();
}