Skip to content

Commit 5c0c804

Browse files
committed
Add the possibility to bind multiple interfaces in one call
Add the auto execute process to fill up the version service data based on the git information
1 parent b8efa18 commit 5c0c804

7 files changed

Lines changed: 388 additions & 4 deletions

File tree

Editor.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/GitEditorProcess.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
using System.Diagnostics;
3+
4+
namespace GameLovers.Services.Editor
5+
{
6+
/// <summary>
7+
/// Run git commands processes that would otherwise be used in the terminal.
8+
/// </summary>
9+
/// <author>
10+
/// https://blog.somewhatabstract.com/2015/06/22/getting-information-about-your-git-repository-with-c/
11+
/// </author>
12+
public class GitEditorProcess : IDisposable
13+
{
14+
private const string DefaultPathToGitBinary = "git";
15+
16+
private readonly Process Process;
17+
18+
/// <summary>
19+
/// <inheritdoc cref="Process.ExitCode"/>
20+
/// </summary>
21+
public int ExitCode => Process.ExitCode;
22+
23+
public GitEditorProcess(string workingDir, string pathToGitBinary = DefaultPathToGitBinary)
24+
{
25+
var startInfo = new ProcessStartInfo
26+
{
27+
UseShellExecute = false,
28+
RedirectStandardOutput = true,
29+
FileName = pathToGitBinary,
30+
CreateNoWindow = true,
31+
WorkingDirectory = workingDir
32+
};
33+
34+
Process = new Process { StartInfo = startInfo };
35+
}
36+
37+
/// <summary>
38+
/// Is this unity project a git repo?
39+
/// </summary>
40+
public bool IsValidRepo()
41+
{
42+
return ExecuteCommand("rev-parse --is-inside-work-tree") == "true";
43+
}
44+
45+
/// <summary>
46+
/// Get the git branch name of the unity project.
47+
/// </summary>
48+
public string GetBranch()
49+
{
50+
return ExecuteCommand("rev-parse --abbrev-ref HEAD");
51+
}
52+
53+
/// <summary>
54+
/// Get the git commit hash of the unity project.
55+
/// </summary>
56+
public string GetCommitHash()
57+
{
58+
return ExecuteCommand($"rev-parse HEAD");
59+
}
60+
61+
/// <summary>
62+
/// Get the diff of the working directory in its current state from the state it was at at
63+
/// a given commit.
64+
/// </summary>
65+
public string GetDiffFromCommit(string commitHash)
66+
{
67+
return ExecuteCommand($"diff --word-diff=porcelain {commitHash} -- {Process.StartInfo.WorkingDirectory}");
68+
}
69+
70+
/// <inheritdoc/>
71+
public void Dispose()
72+
{
73+
Process?.Dispose();
74+
}
75+
76+
/// <summary>
77+
/// Execute a command eg. "status --verbose"
78+
/// </summary>
79+
private string ExecuteCommand(string args)
80+
{
81+
Process.StartInfo.Arguments = args;
82+
Process.Start();
83+
var output = Process.StandardOutput.ReadToEnd().Trim();
84+
Process.WaitForExit();
85+
return output;
86+
}
87+
}
88+
}

Editor/GitEditorProcess.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/VersionEditorUtils.cs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
using System;
2+
using System.IO;
3+
using UnityEditor;
4+
using UnityEngine;
5+
6+
namespace GameLovers.Services.Editor
7+
{
8+
/// <summary>
9+
/// Set the internal version in any VersionService instances in the project before building
10+
/// and whenever the project loads.
11+
/// </summary>
12+
public static class VersionEditorUtils
13+
{
14+
private const int ShortenedCommitLength = 8;
15+
16+
/// <summary>
17+
/// Set the internal version before building the app.
18+
/// </summary>
19+
public static void SetAndSaveInternalVersion(bool isStoreBuild)
20+
{
21+
var newVersionData = GenerateInternalVersionSuffix(isStoreBuild);
22+
var newVersionDataSerialized = JsonUtility.ToJson(newVersionData);
23+
var oldVersionDataSerialized = LoadVersionDataSerializedSync();
24+
if (newVersionDataSerialized.Equals(oldVersionDataSerialized, StringComparison.Ordinal))
25+
{
26+
return;
27+
}
28+
29+
Debug.Log($"Saving new version data: {newVersionDataSerialized}");
30+
SaveVersionData(newVersionDataSerialized);
31+
}
32+
33+
/// <summary>
34+
/// Loads the game version saved in disk into string format
35+
/// </summary>
36+
public static string LoadVersionDataSerializedSync()
37+
{
38+
var textAsset = Resources.Load<TextAsset>(VersionServices.VersionDataFilename);
39+
if (!textAsset)
40+
{
41+
Debug.LogError("Could not load internal version from Resources.");
42+
return string.Empty;
43+
}
44+
45+
var serialized = textAsset.text;
46+
Resources.UnloadAsset(textAsset);
47+
return serialized;
48+
}
49+
50+
/// <summary>
51+
/// Set the internal version for when the app plays in editor.
52+
/// </summary>
53+
[InitializeOnLoadMethod]
54+
private static void OnEditorLoad()
55+
{
56+
SetAndSaveInternalVersion(false);
57+
}
58+
59+
private static VersionServices.VersionData GenerateInternalVersionSuffix(bool isStoreBuild)
60+
{
61+
var data = new VersionServices.VersionData();
62+
63+
using (var repo = new GitEditorProcess(Application.dataPath))
64+
{
65+
try
66+
{
67+
if (!repo.IsValidRepo())
68+
{
69+
Debug.LogWarning("Project is not a git repo. Internal version not set.");
70+
}
71+
else
72+
{
73+
var branch = repo.GetBranch();
74+
if (string.IsNullOrEmpty(branch))
75+
{
76+
Debug.LogWarning("Could not get git branch for internal version");
77+
}
78+
else
79+
{
80+
data.BranchName = branch;
81+
}
82+
83+
var commitHash = repo.GetCommitHash();
84+
if (string.IsNullOrEmpty(commitHash))
85+
{
86+
Debug.LogWarning("Could not get git commit for internal version");
87+
}
88+
else
89+
{
90+
data.Commit = commitHash.Substring(0, ShortenedCommitLength);
91+
}
92+
}
93+
}
94+
catch (Exception e)
95+
{
96+
Debug.LogException(e);
97+
Debug.LogWarning("Could not execute git commands. Internal version not set.");
98+
}
99+
}
100+
101+
data.BuildNumber = PlayerSettings.iOS.buildNumber;
102+
data.BuildType = isStoreBuild ? "prod" : "dev";
103+
104+
return data;
105+
}
106+
107+
/// <summary>
108+
/// Set the internal version of this application and save it in resources. This should be
109+
/// called at edit/build time.
110+
/// </summary>
111+
private static void SaveVersionData(string serializedData)
112+
{
113+
const string assets = "Assets";
114+
const string resources = "Build/Resources";
115+
116+
var absDirPath = Path.Combine(Application.dataPath, resources);
117+
if (!Directory.Exists(absDirPath))
118+
{
119+
Directory.CreateDirectory(absDirPath);
120+
}
121+
122+
// delete old file with incorrect extension
123+
const string assetExtension = ".asset";
124+
var absFilePath = Path.Combine(absDirPath, VersionServices.VersionDataFilename);
125+
if (File.Exists(Path.ChangeExtension(absFilePath, assetExtension)))
126+
{
127+
AssetDatabase.DeleteAsset(
128+
Path.Combine(assets, resources,
129+
Path.ChangeExtension(VersionServices.VersionDataFilename, assetExtension)));
130+
}
131+
132+
// create new text file
133+
const string textExtension = ".txt";
134+
File.WriteAllText(Path.ChangeExtension(absFilePath, textExtension), serializedData);
135+
136+
AssetDatabase.ImportAsset(
137+
Path.Combine(assets, resources,
138+
Path.ChangeExtension(VersionServices.VersionDataFilename, textExtension)));
139+
}
140+
}
141+
}

Editor/VersionEditorUtils.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)