-
Notifications
You must be signed in to change notification settings - Fork 2
Release 0.8.1 #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Release 0.8.1 #14
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| using System; | ||
| using System.Diagnostics; | ||
|
|
||
| namespace GameLovers.Services.Editor | ||
| { | ||
| /// <summary> | ||
| /// Run git commands processes that would otherwise be used in the terminal. | ||
| /// </summary> | ||
| /// <author> | ||
| /// https://blog.somewhatabstract.com/2015/06/22/getting-information-about-your-git-repository-with-c/ | ||
| /// </author> | ||
| public class GitEditorProcess : IDisposable | ||
|
Comment on lines
+6
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| { | ||
| private const string DefaultPathToGitBinary = "git"; | ||
|
|
||
|
Comment on lines
+14
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| private readonly Process Process; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The - private readonly Process Process;
+ private readonly Process _process; |
||
|
|
||
| /// <summary> | ||
| /// <inheritdoc cref="Process.ExitCode"/> | ||
| /// </summary> | ||
| public int ExitCode => Process.ExitCode; | ||
|
|
||
| public GitEditorProcess(string workingDir, string pathToGitBinary = DefaultPathToGitBinary) | ||
| { | ||
| var startInfo = new ProcessStartInfo | ||
| { | ||
| UseShellExecute = false, | ||
| RedirectStandardOutput = true, | ||
| FileName = pathToGitBinary, | ||
| CreateNoWindow = true, | ||
| WorkingDirectory = workingDir | ||
| }; | ||
|
Comment on lines
+23
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| Process = new Process { StartInfo = startInfo }; | ||
| } | ||
|
Comment on lines
+23
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The constructor of |
||
|
|
||
| /// <summary> | ||
| /// Is this unity project a git repo? | ||
| /// </summary> | ||
| public bool IsValidRepo() | ||
| { | ||
| return ExecuteCommand("rev-parse --is-inside-work-tree") == "true"; | ||
| } | ||
|
|
||
|
Comment on lines
+37
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| /// <summary> | ||
| /// Get the git branch name of the unity project. | ||
| /// </summary> | ||
| public string GetBranch() | ||
| { | ||
| return ExecuteCommand("rev-parse --abbrev-ref HEAD"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Get the git commit hash of the unity project. | ||
| /// </summary> | ||
| public string GetCommitHash() | ||
| { | ||
| return ExecuteCommand($"rev-parse HEAD"); | ||
| } | ||
|
Comment on lines
+48
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| /// <summary> | ||
| /// Get the diff of the working directory in its current state from the state it was at at | ||
| /// a given commit. | ||
| /// </summary> | ||
| public string GetDiffFromCommit(string commitHash) | ||
| { | ||
| return ExecuteCommand($"diff --word-diff=porcelain {commitHash} -- {Process.StartInfo.WorkingDirectory}"); | ||
| } | ||
|
Comment on lines
+61
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| /// <inheritdoc/> | ||
| public void Dispose() | ||
| { | ||
| Process?.Dispose(); | ||
| } | ||
|
Comment on lines
+71
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| /// <summary> | ||
| /// Execute a command eg. "status --verbose" | ||
| /// </summary> | ||
| private string ExecuteCommand(string args) | ||
| { | ||
| Process.StartInfo.Arguments = args; | ||
| Process.Start(); | ||
| var output = Process.StandardOutput.ReadToEnd().Trim(); | ||
| Process.WaitForExit(); | ||
| return output; | ||
|
Comment on lines
+79
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using the Here's an updated version of the code with the suggested changes: using System;
using System.Diagnostics;
namespace GameLovers.Utils
{
/// <summary>
/// Run git commands processes that would otherwise be used in the terminal.
/// </summary>
/// <author>
/// https://blog.somewhatabstract.com/2015/06/22/getting-information-about-your-git-repository-with-c/
/// </author>
public class GitEditorProcess : IDisposable
{
private readonly Process _process;
/// <summary>
/// <inheritdoc cref="Process.ExitCode"/>
/// </summary>
public int ExitCode => _process.ExitCode;
public GitEditorProcess(string workingDir, string pathToGitBinary = "git")
{
var startInfo = new ProcessStartInfo
{
UseShellExecute = false,
RedirectStandardOutput = true,
FileName = pathToGitBinary,
CreateNoWindow = true,
WorkingDirectory = workingDir
};
_process = new Process { StartInfo = startInfo };
}
/// <summary>
/// Is this unity project a git repo?
/// </summary>
public bool IsValidRepo()
{
return ExecuteCommand("rev-parse --is-inside-work-tree") == "true";
}
/// <summary>
/// Get the git branch name of the unity project.
/// </summary>
public string GetBranch()
{
return ExecuteCommand("rev-parse --abbrev-ref HEAD");
}
/// <summary>
/// Get the git commit hash of the unity project.
/// </summary>
public string GetCommitHash()
{
return ExecuteCommand("rev-parse HEAD");
}
/// <summary>
/// Get the diff of the working directory in its current state from the state it was at at
/// a given commit.
/// </summary>
public string GetDiffFromCommit(string commitHash)
{
return ExecuteCommand($"diff --word-diff=porcelain --relative {commitHash}");
}
/// <inheritdoc/>
void IDisposable.Dispose()
{
_process?.Dispose();
}
/// <summary>
/// Execute a command eg. "status --verbose"
/// </summary>
private string ExecuteCommand(string args)
{
_process.StartInfo.Arguments = args;
_process.Start();
var output = _process.StandardOutput.ReadToEnd().Trim();
_process.WaitForExit();
return output;
}
}
}Please review the changes and let me know if you have any questions or concerns.
Comment on lines
+81
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The - Process.Start();
- var output = Process.StandardOutput.ReadToEnd().Trim();
- Process.WaitForExit();
+ _process.StartInfo.Arguments = args;
+ _process.Start();
+ var output = _process.StandardOutput.ReadToEnd().Trim();
+ _process.WaitForExit();
+ if (_process.ExitCode != 0)
+ {
+ throw new InvalidOperationException($"Git command '{args}' failed with exit code {_process.ExitCode}");
+ } |
||
| } | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| using System; | ||
| using System.IO; | ||
| using UnityEditor; | ||
| using UnityEngine; | ||
|
|
||
| namespace GameLovers.Services.Editor | ||
| { | ||
| /// <summary> | ||
| /// Set the internal version in any VersionService instances in the project before building | ||
| /// and whenever the project loads. | ||
| /// </summary> | ||
| public static class VersionEditorUtils | ||
| { | ||
| private const int ShortenedCommitLength = 8; | ||
|
|
||
|
Comment on lines
+13
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| /// <summary> | ||
| /// Set the internal version before building the app. | ||
| /// </summary> | ||
| public static void SetAndSaveInternalVersion(bool isStoreBuild) | ||
| { | ||
| var newVersionData = GenerateInternalVersionSuffix(isStoreBuild); | ||
| var newVersionDataSerialized = JsonUtility.ToJson(newVersionData); | ||
| var oldVersionDataSerialized = LoadVersionDataSerializedSync(); | ||
| if (newVersionDataSerialized.Equals(oldVersionDataSerialized, StringComparison.Ordinal)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| Debug.Log($"Saving new version data: {newVersionDataSerialized}"); | ||
| SaveVersionData(newVersionDataSerialized); | ||
| } | ||
|
Comment on lines
+19
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Here's an example of how the refactored method could look like: public static void SetAndSaveInternalVersion(bool isStoreBuild)
{
var newVersionData = GenerateInternalVersionSuffix(isStoreBuild);
var newVersionDataSerialized = JsonUtility.ToJson(newVersionData);
var oldVersionDataSerialized = LoadVersionDataSerializedSync();
if (IsVersionDataEqual(newVersionDataSerialized, oldVersionDataSerialized))
{
return;
}
Debug.Log($"Saving new version data: {newVersionDataSerialized}");
SaveVersionData(newVersionDataSerialized);
}
private static bool IsVersionDataEqual(string versionData1, string versionData2)
{
return versionData1.Equals(versionData2, StringComparison.Ordinal);
}
Comment on lines
+19
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method - if (newVersionDataSerialized.Equals(oldVersionDataSerialized, StringComparison.Ordinal))
- {
- return;
- } |
||
|
|
||
| /// <summary> | ||
| /// Loads the game version saved in disk into string format | ||
| /// </summary> | ||
| public static string LoadVersionDataSerializedSync() | ||
| { | ||
| var textAsset = Resources.Load<TextAsset>(VersionServices.VersionDataFilename); | ||
| if (!textAsset) | ||
| { | ||
| Debug.LogError("Could not load internal version from Resources."); | ||
| return string.Empty; | ||
| } | ||
|
|
||
| var serialized = textAsset.text; | ||
| Resources.UnloadAsset(textAsset); | ||
| return serialized; | ||
|
Comment on lines
+36
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Here's an example of how the method could be modified: public static string LoadVersionDataSerializedSync()
{
try
{
var textAsset = Resources.Load<TextAsset>(VersionServices.VersionDataFilename);
if (textAsset != null)
{
var serialized = textAsset.text;
Resources.UnloadAsset(textAsset);
return serialized;
}
}
catch (Exception e)
{
Debug.LogException(e);
}
Debug.LogError("Could not load internal version from Resources.");
return string.Empty;
} |
||
| } | ||
|
Comment on lines
+36
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method |
||
|
|
||
| /// <summary> | ||
| /// Set the internal version for when the app plays in editor. | ||
| /// </summary> | ||
| [InitializeOnLoadMethod] | ||
| private static void OnEditorLoad() | ||
| { | ||
| SetAndSaveInternalVersion(false); | ||
| } | ||
|
|
||
| private static VersionServices.VersionData GenerateInternalVersionSuffix(bool isStoreBuild) | ||
| { | ||
| var data = new VersionServices.VersionData(); | ||
|
|
||
| using (var repo = new GitEditorProcess(Application.dataPath)) | ||
| { | ||
| try | ||
| { | ||
| if (!repo.IsValidRepo()) | ||
| { | ||
| Debug.LogWarning("Project is not a git repo. Internal version not set."); | ||
| } | ||
| else | ||
| { | ||
| var branch = repo.GetBranch(); | ||
| if (string.IsNullOrEmpty(branch)) | ||
| { | ||
| Debug.LogWarning("Could not get git branch for internal version"); | ||
| } | ||
| else | ||
| { | ||
| data.BranchName = branch; | ||
| } | ||
|
|
||
| var commitHash = repo.GetCommitHash(); | ||
| if (string.IsNullOrEmpty(commitHash)) | ||
| { | ||
| Debug.LogWarning("Could not get git commit for internal version"); | ||
| } | ||
| else | ||
| { | ||
| data.Commit = commitHash.Substring(0, ShortenedCommitLength); | ||
| } | ||
| } | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| Debug.LogException(e); | ||
| Debug.LogWarning("Could not execute git commands. Internal version not set."); | ||
| } | ||
|
Comment on lines
+65
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the - Debug.LogException(e);
- Debug.LogWarning("Could not execute git commands. Internal version not set.");
+ throw; |
||
| } | ||
|
Comment on lines
+59
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Here's an example of how the refactored method could look like: private static VersionServices.VersionData GenerateInternalVersionSuffix(bool isStoreBuild)
{
var data = new VersionServices.VersionData();
using (var repo = new GitEditorProcess(Application.dataPath))
{
try
{
if (!repo.IsValidRepo())
{
Debug.LogWarning("Project is not a git repo. Internal version not set.");
return data;
}
var branch = GetGitBranch(repo);
if (string.IsNullOrEmpty(branch))
{
Debug.LogWarning("Could not get git branch for internal version");
}
else
{
data.BranchName = branch;
}
var commitHash = GetGitCommitHash(repo);
if (string.IsNullOrEmpty(commitHash))
{
Debug.LogWarning("Could not get git commit for internal version");
}
else
{
data.Commit = commitHash.Substring(0, 8);
}
}
catch (Exception e)
{
Debug.LogException(e);
Debug.LogWarning("Could not execute git commands. Internal version not set.");
}
}
data.BuildNumber = PlayerSettings.iOS.buildNumber;
data.BuildType = isStoreBuild ? "prod" : "dev";
return data;
}
private static string GetGitBranch(GitEditorProcess repo)
{
return repo.GetBranch();
}
private static string GetGitCommitHash(GitEditorProcess repo)
{
return repo.GetCommitHash();
} |
||
|
|
||
| data.BuildNumber = PlayerSettings.iOS.buildNumber; | ||
| data.BuildType = isStoreBuild ? "prod" : "dev"; | ||
|
|
||
| return data; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Set the internal version of this application and save it in resources. This should be | ||
| /// called at edit/build time. | ||
| /// </summary> | ||
| private static void SaveVersionData(string serializedData) | ||
| { | ||
| const string assets = "Assets"; | ||
| const string resources = "Build/Resources"; | ||
|
|
||
| var absDirPath = Path.Combine(Application.dataPath, resources); | ||
| if (!Directory.Exists(absDirPath)) | ||
| { | ||
| Directory.CreateDirectory(absDirPath); | ||
| } | ||
|
|
||
| // delete old file with incorrect extension | ||
| const string assetExtension = ".asset"; | ||
| var absFilePath = Path.Combine(absDirPath, VersionServices.VersionDataFilename); | ||
| if (File.Exists(Path.ChangeExtension(absFilePath, assetExtension))) | ||
| { | ||
| AssetDatabase.DeleteAsset( | ||
| Path.Combine(assets, resources, | ||
| Path.ChangeExtension(VersionServices.VersionDataFilename, assetExtension))); | ||
| } | ||
|
|
||
| // create new text file | ||
| const string textExtension = ".txt"; | ||
| File.WriteAllText(Path.ChangeExtension(absFilePath, textExtension), serializedData); | ||
|
|
||
| AssetDatabase.ImportAsset( | ||
| Path.Combine(assets, resources, | ||
| Path.ChangeExtension(VersionServices.VersionDataFilename, textExtension))); | ||
| } | ||
|
Comment on lines
+101
to
+139
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Here's an example of how the refactored method could look like: private static void SaveVersionData(string serializedData)
{
const string assets = "Assets";
const string resources = "Build/Resources";
const string versionDataFilename = VersionServices.VersionDataFilename;
var absDirPath = Path.Combine(Application.dataPath, resources);
if (!Directory.Exists(absDirPath))
{
Directory.CreateDirectory(absDirPath);
}
// Delete old file with incorrect extension
const string assetExtension = ".asset";
var absFilePath = Path.Combine(absDirPath, versionDataFilename);
if (File.Exists(Path.ChangeExtension(absFilePath, assetExtension)))
{
AssetDatabase.DeleteAsset(Path.Combine(assets, resources, Path.ChangeExtension(versionDataFilename, assetExtension)));
}
// Create new text file
const string textExtension = ".txt";
File.WriteAllText(Path.ChangeExtension(absFilePath, textExtension), serializedData);
AssetDatabase.ImportAsset(Path.Combine(assets, resources, Path.ChangeExtension(versionDataFilename, textExtension)));
}
Comment on lines
+113
to
+139
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
GameLovers.Services.Editornamespace seems to be unrelated to the purpose of this class. Consider moving theGitEditorProcessclass to a more appropriate namespace, such asGameLovers.UtilsorGameLovers.VersionControl.