Last active
October 12, 2020 18:16
-
-
Save guidoschmidt/33db7876642c2641ed64c299c58645d3 to your computer and use it in GitHub Desktop.
Unity create Preset + git autocommit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Diagnostics; | |
using NaughtyAttributes; | |
using UnityEngine; | |
using UnityEditor; | |
using UnityEditor.Presets; | |
public class GitCommitAndSave : MonoBehaviour | |
{ | |
public bool _dryRun; | |
[SerializeField] | |
public MonoBehaviour _script; | |
[SerializeField] | |
public ComputeShader _shader; | |
[SerializeField] | |
public DefaultAsset _presetDir; | |
[SerializeField] | |
[InfoBox("Set your git command location here. Usually sth. like:\n\n/usr/bin/git", EInfoBoxType.Normal)] | |
public string _gitMacLocation; | |
[SerializeField] | |
[InfoBox("Set your git command location here [WINDOWS]. Usually sth. like:\n\nC:/Program Files/git/cmd/git.exe", EInfoBoxType.Normal)] | |
public string _gitWindowsLocation; | |
[Button("Commit & Save")] | |
public void CommitAndSave() | |
{ | |
// Diagnostic log output | |
UnityEngine.Debug.Log($"Running on: {Application.platform}"); | |
UnityEngine.Debug.Log($"Project directory: {System.IO.Directory.GetCurrentDirectory()}"); | |
// Determine git version depending on OS | |
var gitLocation = _gitMacLocation; | |
if (Application.platform == RuntimePlatform.WindowsEditor) { | |
gitLocation = _gitWindowsLocation; | |
} | |
ProcessStartInfo gitInfo = new ProcessStartInfo(); | |
gitInfo.CreateNoWindow = true; | |
gitInfo.RedirectStandardError = true; | |
gitInfo.RedirectStandardOutput = true; | |
gitInfo.UseShellExecute = false; | |
gitInfo.WorkingDirectory = System.IO.Directory.GetCurrentDirectory(); | |
gitInfo.FileName = gitLocation; | |
Process gitProcess = new Process(); | |
gitInfo.Arguments = "status"; | |
var scriptInst = MonoScript.FromMonoBehaviour(_script); | |
string scriptFilepath = AssetDatabase.GetAssetPath(scriptInst); | |
string shaderFilepath = AssetDatabase.GetAssetPath(_shader); | |
string presetsFilePath = AssetDatabase.GetAssetPath(_presetDir); | |
gitProcess.StartInfo = gitInfo; | |
gitProcess.Start(); | |
string stderr_str = gitProcess.StandardError.ReadToEnd(); // pick up STDERR | |
string stdout_str = gitProcess.StandardOutput.ReadToEnd(); // pick up STDOUT | |
gitProcess.WaitForExit(); | |
gitProcess.Close(); | |
string dateTime = DateTime.Now.ToString("yyyy.MM.dd-HH.mm.ss"); | |
string presetFileName = $"AUTOCOMMIT_{dateTime}.preset"; | |
// Git arguments | |
string gitAddArgument = $"add {shaderFilepath} {scriptFilepath} ./{presetsFilePath}/{presetFileName}"; | |
string gitCommitArgument = $"commit -m 'AUTOCOMMIT: {dateTime}'"; | |
if (_dryRun) | |
{ | |
UnityEngine.Debug.Log($"[DRY RUN] CREATE: ./{presetsFilePath}/{presetFileName}"); | |
UnityEngine.Debug.Log($"[DRY RUN] GIT ADD: {gitAddArgument}"); | |
UnityEngine.Debug.Log($"[DRY RUN] GIT COMMIT: {gitCommitArgument}"); | |
} | |
else | |
{ | |
// Create a preset | |
Preset preset = new Preset(_script); | |
AssetDatabase.CreateAsset(preset, $"{presetsFilePath}/{presetFileName}"); | |
string presetAssetPath = AssetDatabase.GetAssetPath(preset); | |
// Git arguments | |
gitAddArgument = $"add {shaderFilepath} {scriptFilepath} {presetAssetPath}"; | |
gitCommitArgument = $"commit -m 'AUTOCOMMIT: {dateTime}'"; | |
// Git add | |
Process p = new Process(); | |
p.StartInfo.FileName = gitLocation; | |
p.StartInfo.Arguments = gitAddArgument; | |
p.StartInfo.UseShellExecute = false; | |
p.StartInfo.RedirectStandardError = true; | |
p.StartInfo.RedirectStandardOutput = true; | |
p.Start(); | |
stderr_str = p.StandardError.ReadToEnd(); | |
stdout_str = p.StandardOutput.ReadToEnd(); | |
p.WaitForExit(); | |
p.Close(); | |
UnityEngine.Debug.Log("--- GIT OUTPUT ---------"); | |
UnityEngine.Debug.Log(stdout_str); | |
UnityEngine.Debug.Log("--- GIT ERROR OUTPUT ---"); | |
UnityEngine.Debug.Log(stderr_str); | |
UnityEngine.Debug.Log("------------------------"); | |
// Git commit | |
p = new Process(); | |
p.StartInfo.FileName = gitLocation; | |
p.StartInfo.Arguments = gitCommitArgument; | |
p.StartInfo.UseShellExecute = false; | |
p.StartInfo.RedirectStandardError = true; | |
p.StartInfo.RedirectStandardOutput = true; | |
p.Start(); | |
stderr_str = p.StandardError.ReadToEnd(); | |
stdout_str = p.StandardOutput.ReadToEnd(); | |
p.WaitForExit(); | |
p.Close(); | |
UnityEngine.Debug.Log("--- GIT OUTPUT ---------"); | |
UnityEngine.Debug.Log(stdout_str); | |
UnityEngine.Debug.Log("--- GIT ERROR OUTPUT ---"); | |
UnityEngine.Debug.Log(stderr_str); | |
UnityEngine.Debug.Log("------------------------"); | |
UnityEngine.Debug.Log(stdout_str); | |
UnityEngine.Debug.Log(stderr_str); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
C:\Users\gs\.babun\cygwin\bin\git.exe
)