-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVersionEditorUtils.cs
More file actions
141 lines (126 loc) · 3.95 KB
/
VersionEditorUtils.cs
File metadata and controls
141 lines (126 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.IO;
using UnityEditor;
using UnityEngine;
using GameLovers.Services;
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;
private const string AssetsPath = "Assets";
private const string FilePath = "Configs/Resources";
/// <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);
}
/// <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;
}
/// <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.CommitHash = commitHash.Substring(0, ShortenedCommitLength);
}
}
}
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;
}
/// <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)
{
var absDirPath = Path.Combine(Application.dataPath, FilePath);
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(AssetsPath, FilePath,
Path.ChangeExtension(VersionServices.VersionDataFilename, assetExtension)));
}
// create new text file
const string textExtension = ".txt";
File.WriteAllText(Path.ChangeExtension(absFilePath, textExtension), serializedData);
AssetDatabase.ImportAsset(
Path.Combine(AssetsPath, FilePath,
Path.ChangeExtension(VersionServices.VersionDataFilename, textExtension)));
}
}
}