Skip to content

Commit 6b31c0c

Browse files
committed
fix(version): Corrected the path for version.txt file
1 parent 67c4523 commit 6b31c0c

File tree

1 file changed

+67
-65
lines changed

1 file changed

+67
-65
lines changed

Assets/Editor/SyncBundleVersion.cs

Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,105 +2,107 @@
22
using UnityEditor;
33
using UnityEditor.Build;
44
using UnityEditor.Build.Reporting;
5+
using UnityEngine;
56
using System.IO;
67
using System.Text.RegularExpressions;
78

89
/// <summary>
910
/// Keeps Unity's version fields in sync with files generated by CI:
1011
/// - version.txt -> PlayerSettings.bundleVersion (maps to Android versionName)
11-
/// - versionCode.txt (optional) -> PlayerSettings.Android.bundleVersionCode
12+
/// - versionCode.txt -> PlayerSettings.Android.bundleVersionCode
1213
///
1314
/// If versionCode.txt is missing, it will derive a versionCode from version.txt using:
1415
/// code = MAJOR*10000 + MINOR*100 + PATCH
1516
/// This ensures a monotonic integer for Google Play uploads.
1617
/// </summary>
17-
public class SyncBundleVersionPreprocess : IPreprocessBuildWithReport
18+
public class SyncBundleVersion : IPreprocessBuildWithReport
1819
{
1920
/// <summary>
2021
/// Determines the order in which build preprocessors are called. 0 means default/early execution.
2122
/// </summary>
2223
public int callbackOrder => 0;
2324

24-
/// <summary>
25-
/// Keeps Unity's internal application version (Application.version) in sync with CI/CD-managed versions.
26-
/// </summary>
27-
public void OnPreprocessBuild(BuildReport report)
25+
// Auto-run whenever scripts reload (after pulling a Release PR, switching branches, etc.)
26+
[InitializeOnLoadMethod]
27+
private static void AutoSyncOnReload()
2828
{
29-
var root = Directory.GetCurrentDirectory();
29+
// Delay a tick so Unity finishes the domain reload before touching PlayerSettings
30+
EditorApplication.delayCall += SyncNow;
31+
}
3032

31-
// 1) Sync human-readable version
32-
var versionPath = Path.Combine(root, "version.txt");
33-
if (File.Exists(versionPath))
34-
{
35-
var v = File.ReadAllText(versionPath).Trim();
33+
// Manual command: Tools > Versioning > Sync from Files
34+
[MenuItem("Tools/Versioning/Sync from Files")]
35+
public static void SyncNow()
36+
{
37+
string root = Directory.GetCurrentDirectory();
38+
string assets = Application.dataPath;
3639

37-
// Update if the file is not empty and differs from current bundleVersion
38-
if (!string.IsNullOrEmpty(v) && PlayerSettings.bundleVersion != v)
39-
{
40-
PlayerSettings.bundleVersion = v; // Update Unity's app version
41-
AssetDatabase.SaveAssets();
42-
UnityEngine.Debug.Log($"[Build] Synced bundleVersion -> {v}");
43-
}
40+
// Prefer Assets/version.txt; fallback to root/version.txt
41+
string versionPath = File.Exists(Path.Combine(assets, "version.txt"))
42+
? Path.Combine(assets, "version.txt")
43+
: Path.Combine(root, "version.txt");
4444

45-
// 2) Android versionCode: prefer explicit file; else derive from semver
45+
if (!File.Exists(versionPath))
46+
{
47+
Debug.LogWarning("[Versioning] version.txt not found in Assets/ or project root; bundleVersion not changed.");
48+
return;
49+
}
4650

51+
// Extract only the semver token (optionally prefixed with 'v'), ignore any trailing markers
52+
// Example line: "1.2.1 x-release-please-version"
53+
string raw = File.ReadAllText(versionPath);
54+
var mVer = Regex.Match(raw, @"^\s*v?(?<ver>\d+\.\d+\.\d+)", RegexOptions.Multiline);
55+
if (!mVer.Success)
56+
{
57+
Debug.LogWarning($"[Versioning] Could not find a semantic version in '{versionPath}'.");
58+
return;
59+
}
4760

48-
#if UNITY_ANDROID
61+
string v = mVer.Groups["ver"].Value; // e.g., "1.2.1"
4962

50-
// Local holder for the versionCode we want to apply to the Android build.
51-
int code; // Google Play requires this to be a monotonically increasing integer.
52-
var codePath = Path.Combine(root, "versionCode.txt");
63+
if (PlayerSettings.bundleVersion != v)
64+
{
65+
PlayerSettings.bundleVersion = v;
66+
Debug.Log($"[Versioning] Synced PlayerSettings.bundleVersion -> {v}");
67+
}
5368

54-
// Preferred path: if versionCode.txt exists and contains a valid integer, use it as-is.
55-
if (File.Exists(codePath) && int.TryParse(File.ReadAllText(codePath).Trim(), out code))
56-
{
57-
SetAndroidVersionCodeIfDifferent(code);
58-
}
59-
else
60-
{
61-
// Fallback path: derive an integer versionCode from the semver in version.txt (string 'v').
62-
var m = Regex.Match(v, @"^(?<maj>\d+)\.(?<min>\d+)\.(?<pat>\d+)");
63-
if (m.Success)
64-
{
65-
// This scheme reserves two digits for MINOR and two for PATCH (00–99 each).
66-
// If you expect MINOR or PATCH to exceed 99, switch to a wider encoding
67-
// (e.g., MAJOR*1_000_000 + MINOR*1_000 + PATCH).
68-
int maj = int.Parse(m.Groups["maj"].Value);
69-
int min = int.Parse(m.Groups["min"].Value);
70-
int pat = int.Parse(m.Groups["pat"].Value);
71-
code = maj * 10000 + min * 100 + pat;
72-
SetAndroidVersionCodeIfDifferent(code);
73-
}
74-
else
75-
{
76-
// If version.txt doesn't match MAJOR.MINOR.PATCH, we can't derive a versionCode.
77-
// Build will still proceed with the current PlayerSettings.Android.bundleVersionCode.
78-
UnityEngine.Debug.LogWarning($"[Build] Could not parse semver from '{v}' to derive Android versionCode.");
79-
}
80-
}
81-
#endif
69+
// Android versionCode: prefer explicit file; else derive from semver
70+
int code;
71+
string codePath = File.Exists(Path.Combine(assets, "versionCode.txt"))
72+
? Path.Combine(assets, "versionCode.txt")
73+
: Path.Combine(root, "versionCode.txt");
74+
75+
if (File.Exists(codePath) && int.TryParse(File.ReadAllText(codePath).Trim(), out code))
76+
{
77+
SetAndroidVersionCodeIfDifferent(code);
8278
}
8379
else
8480
{
85-
UnityEngine.Debug.LogWarning("[Build] version.txt not found; bundleVersion not changed.");
81+
// Derive from MAJOR.MINOR.PATCH (v already parsed as pure semver)
82+
var m = Regex.Match(v, @"^(?<maj>\d+)\.(?<min>\d+)\.(?<pat>\d+)$");
83+
if (m.Success)
84+
{
85+
int maj = int.Parse(m.Groups["maj"].Value);
86+
int min = int.Parse(m.Groups["min"].Value);
87+
int pat = int.Parse(m.Groups["pat"].Value);
88+
code = maj * 10000 + min * 100 + pat; // e.g., 2.3.4 -> 20304
89+
SetAndroidVersionCodeIfDifferent(code);
90+
}
8691
}
87-
}
8892

93+
AssetDatabase.SaveAssets();
94+
}
8995

90-
#if UNITY_ANDROID && UNITY_EDITOR
96+
// Safety net: also run right before every build
97+
public void OnPreprocessBuild(BuildReport report) => SyncNow();
9198

92-
/// <summary>
93-
/// Writes <see cref="PlayerSettings.Android.bundleVersionCode"/> if the value differ.
94-
/// </summary>
9599
private static void SetAndroidVersionCodeIfDifferent(int code)
100+
{
101+
if (PlayerSettings.Android.bundleVersionCode != code)
96102
{
97-
if (PlayerSettings.Android.bundleVersionCode != code)
98-
{
99-
PlayerSettings.Android.bundleVersionCode = code; // Updates ProjectSettings/ProjectSettings.asset
100-
AssetDatabase.SaveAssets(); // Keeps the change
101-
UnityEngine.Debug.Log($"[Build] Synced Android bundleVersionCode -> {code}");
102-
}
103+
PlayerSettings.Android.bundleVersionCode = code;
104+
Debug.Log($"[Versioning] Synced Android bundleVersionCode -> {code}");
103105
}
104-
#endif
106+
}
105107
}
106108
#endif

0 commit comments

Comments
 (0)