Skip to content

Commit ee8befd

Browse files
committed
v1.0.1.1
- Added: Update Current Build Version - Added: Validate -> version.txt - Added: Setup default config
1 parent b874c8c commit ee8befd

File tree

4 files changed

+112
-36
lines changed

4 files changed

+112
-36
lines changed

CustomBuildUpdater/Editor/BuildConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using UnityEngine;
23

34
namespace RimuruDev.Unity_CustomBuildUpdater.CustomBuildUpdater.Editor
@@ -7,6 +8,7 @@ public class BuildConfig : ScriptableObject
78
{
89
public string companyName = "AbyssMoth";
910
public string productName = "SuperGame";
11+
public string initialVersion = "1.0.0.0";
1012
public BuildPathType buildPathType = BuildPathType.Default;
1113
public string customBuildPath = "Builds";
1214
public bool archiveBuild = true;

CustomBuildUpdater/Editor/BuildConfigEditor.cs

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.IO;
12
using UnityEditor;
23
using UnityEngine;
34

@@ -6,12 +7,15 @@ namespace RimuruDev.Unity_CustomBuildUpdater.CustomBuildUpdater.Editor
67
[CustomEditor(typeof(BuildConfig))]
78
public class BuildConfigEditor : UnityEditor.Editor
89
{
10+
private const string PathToConfig = "Assets/Resources/Editor/BuildConfig.asset";
11+
912
public override void OnInspectorGUI()
1013
{
1114
var config = (BuildConfig)target;
1215

1316
config.companyName = EditorGUILayout.TextField("Company Name", config.companyName);
1417
config.productName = EditorGUILayout.TextField("Product Name", config.productName);
18+
config.initialVersion = EditorGUILayout.TextField("Initial Version", config.initialVersion);
1519
config.buildPathType = (BuildPathType)EditorGUILayout.EnumPopup("Build Path Type", config.buildPathType);
1620

1721
if (config.buildPathType == BuildPathType.Custom)
@@ -23,28 +27,38 @@ public override void OnInspectorGUI()
2327
config.versionType = (VersionType)EditorGUILayout.EnumPopup("Version Type", config.versionType);
2428
config.versionPattern = EditorGUILayout.TextField("Version Pattern", config.versionPattern);
2529

26-
if (GUILayout.Button("Initialize BuildConfig"))
30+
// if (GUILayout.Button("Initialize BuildConfig"))
31+
// {
32+
// CreateOrSelectBuildConfig();
33+
// }
34+
35+
if (GUILayout.Button("Update Current Version"))
2736
{
28-
CreateOrSelectBuildConfig();
37+
UpdateCurrentVersion();
2938
}
3039

3140
if (GUI.changed)
3241
{
3342
EditorUtility.SetDirty(config);
43+
AssetDatabase.SaveAssets();
3444
}
3545
}
3646

3747
[MenuItem("RimuruDev Tools/Initialize BuildConfig")]
3848
private static void CreateOrSelectBuildConfig()
3949
{
40-
var path = "Assets/Resources/Editor/BuildConfig.asset";
41-
var config = AssetDatabase.LoadAssetAtPath<BuildConfig>(path);
50+
var config = GetConfig();
4251

4352
if (config == null)
4453
{
4554
config = CreateInstance<BuildConfig>();
4655
config.companyName = PlayerSettings.companyName;
4756
config.productName = PlayerSettings.productName;
57+
config.initialVersion = "1.0.0.0";
58+
config.buildPathType = BuildPathType.Default;
59+
config.archiveBuild = true;
60+
config.versionType = VersionType.Build;
61+
config.versionPattern = "com.{company}.{product}.v{version}";
4862

4963
if (!AssetDatabase.IsValidFolder("Assets/Resources"))
5064
{
@@ -56,17 +70,46 @@ private static void CreateOrSelectBuildConfig()
5670
AssetDatabase.CreateFolder("Assets/Resources", "Editor");
5771
}
5872

59-
AssetDatabase.CreateAsset(config, path);
73+
AssetDatabase.CreateAsset(config, PathToConfig);
6074
AssetDatabase.SaveAssets();
61-
Debug.Log("BuildConfig created at " + path);
75+
AssetDatabase.Refresh();
76+
Debug.Log("BuildConfig created at " + PathToConfig);
6277
}
6378
else
6479
{
65-
Debug.Log("BuildConfig already exists at " + path);
80+
Debug.Log("BuildConfig already exists at " + PathToConfig);
81+
}
82+
83+
var versionFilePath = Path.Combine(Application.dataPath, "Resources/Editor/version.txt");
84+
if (!File.Exists(versionFilePath))
85+
{
86+
Directory.CreateDirectory(Path.GetDirectoryName(versionFilePath));
87+
File.WriteAllText(versionFilePath, config.initialVersion);
6688
}
6789

6890
EditorUtility.FocusProjectWindow();
6991
Selection.activeObject = config;
7092
}
93+
94+
private void UpdateCurrentVersion()
95+
{
96+
var config = GetConfig();
97+
98+
var versionFilePath = Path.Combine(Application.dataPath, "Resources/Editor/version.txt");
99+
if (!File.Exists(versionFilePath))
100+
{
101+
Directory.CreateDirectory(Path.GetDirectoryName(versionFilePath));
102+
File.WriteAllText(versionFilePath, config.initialVersion);
103+
}
104+
else
105+
{
106+
File.WriteAllText(versionFilePath, config.initialVersion);
107+
}
108+
109+
AssetDatabase.SaveAssets();
110+
AssetDatabase.Refresh();
111+
}
112+
113+
private static BuildConfig GetConfig() => AssetDatabase.LoadAssetAtPath<BuildConfig>(PathToConfig);
71114
}
72115
}

CustomBuildUpdater/Editor/BuildVersionUpdater.cs

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,23 @@
88

99
namespace RimuruDev.Unity_CustomBuildUpdater.CustomBuildUpdater.Editor
1010
{
11+
// TODO: Add set active debug log
1112
public class BuildVersionUpdater : IPreprocessBuildWithReport, IPostprocessBuildWithReport
1213
{
1314
public int callbackOrder => 0;
1415

1516
private static BuildConfig config;
17+
18+
// ReSharper disable once FieldCanBeMadeReadOnly.Local
19+
// ReSharper disable once ConvertToConstant.Local
1620
private static string defaultBuildPath = "Builds";
1721

18-
private void LoadConfig()
22+
private static void LoadConfig()
1923
{
2024
config = Resources.Load<BuildConfig>("Editor/BuildConfig");
2125
if (config == null)
2226
{
27+
// TODO: Add Auto create default config
2328
Debug.LogError("BuildConfig not found. Please create it in the Resources/Editor folder.");
2429
}
2530
}
@@ -73,7 +78,7 @@ private void EnsureVersionFileExists()
7378
if (!File.Exists(versionFilePath))
7479
{
7580
Directory.CreateDirectory(Path.GetDirectoryName(versionFilePath));
76-
File.WriteAllText(versionFilePath, "1.0.0.0");
81+
File.WriteAllText(versionFilePath, config.initialVersion);
7782
}
7883
}
7984

@@ -130,39 +135,28 @@ private string GetBuildExtension(BuildTarget platform)
130135

131136
private string GetFinalBuildPath(string buildPath, string extension)
132137
{
133-
string finalPath;
134-
if (config.buildPathType == BuildPathType.Default)
135-
{
136-
finalPath = Path.Combine(Application.dataPath, "..", defaultBuildPath,
137-
$"{config.companyName}.{config.productName}.v{PlayerSettings.bundleVersion}{extension}");
138-
}
139-
else
140-
{
141-
finalPath = Path.Combine(config.customBuildPath,
142-
$"{config.companyName}.{config.productName}.v{PlayerSettings.bundleVersion}{extension}");
143-
}
138+
var finalPath = config.buildPathType == BuildPathType.Default
139+
? Path.Combine(Application.dataPath, "..", defaultBuildPath, $"{config.companyName}.{config.productName}.v{PlayerSettings.bundleVersion}{extension}")
140+
: Path.Combine(config.customBuildPath, $"{config.companyName}.{config.productName}.v{PlayerSettings.bundleVersion}{extension}");
144141

145142
return finalPath;
146143
}
147144

148145
private void RenameBuildPath(string oldPath, string newPath)
149146
{
150-
try
147+
var newPathDirectory = Path.GetDirectoryName(newPath);
148+
if (!Directory.Exists(newPathDirectory))
151149
{
152-
if (Directory.Exists(oldPath))
153-
{
154-
Directory.Move(oldPath, newPath);
155-
}
156-
else if (File.Exists(oldPath))
157-
{
158-
File.Move(oldPath, newPath);
159-
}
150+
Directory.CreateDirectory(newPathDirectory);
160151
}
161-
catch (DirectoryNotFoundException directoryNotFoundException)
152+
153+
if (Directory.Exists(oldPath))
162154
{
163-
Debug.Log(
164-
"<color=red>You have collected the project in the wrong folder that you specified in the settings!!!</color>");
165-
Debug.LogException(directoryNotFoundException);
155+
Directory.Move(oldPath, newPath);
156+
}
157+
else if (File.Exists(oldPath))
158+
{
159+
File.Move(oldPath, newPath);
166160
}
167161
}
168162

@@ -192,11 +186,11 @@ private void ArchiveBuild(string buildPath)
192186

193187
private void CreateZipFromDirectory(string sourceDir, string zipFile)
194188
{
195-
using (var zip = ZipFile.Open(zipFile, ZipArchiveMode.Create))
189+
using (ZipArchive zip = ZipFile.Open(zipFile, ZipArchiveMode.Create))
196190
{
197191
var dirInfo = new DirectoryInfo(sourceDir);
198192

199-
foreach (var file in dirInfo.GetFiles())
193+
foreach (FileInfo file in dirInfo.GetFiles())
200194
{
201195
zip.CreateEntryFromFile(file.FullName, file.Name);
202196
}
@@ -220,5 +214,42 @@ private void AddDirectoryToZip(ZipArchive zip, DirectoryInfo dir, string entryNa
220214
AddDirectoryToZip(zip, subDir, Path.Combine(entryName, subDir.Name));
221215
}
222216
}
217+
218+
//[MenuItem("RimuruDev Tools/Build Project (DANGEROUS!!!)")]
219+
public static void BuildProject()
220+
{
221+
LoadConfig();
222+
223+
if (config == null)
224+
return;
225+
226+
var buildPath = config.buildPathType == BuildPathType.Default
227+
? Path.Combine(Application.dataPath, "..", defaultBuildPath)
228+
: config.customBuildPath;
229+
230+
if (!Directory.Exists(buildPath))
231+
{
232+
Directory.CreateDirectory(buildPath);
233+
}
234+
235+
var selectedPath = EditorUtility.SaveFolderPanel("Choose Build Location", buildPath, "");
236+
237+
if (!string.IsNullOrEmpty(selectedPath))
238+
{
239+
config.customBuildPath = selectedPath;
240+
EditorUtility.SetDirty(config);
241+
242+
// Trigger the actual build process (example for WebGL build) :D
243+
var buildPlayerOptions = new BuildPlayerOptions
244+
{
245+
// TODO: Write a config for the build according to the config.
246+
scenes = new[] { "Assets/Scenes/MainScene.unity" },
247+
locationPathName = selectedPath,
248+
target = BuildTarget.WebGL,
249+
options = BuildOptions.None
250+
};
251+
BuildPipeline.BuildPlayer(buildPlayerOptions);
252+
}
253+
}
223254
}
224255
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.rimurudev.unity-custom-build-updater",
33
"displayName": "Unity Custom Build Updater",
4-
"version": "1.0.0",
4+
"version": "1.0.1",
55
"unity": "2019.4",
66
"description": "Unity Custom Build Updater.",
77
"keywords": [

0 commit comments

Comments
 (0)