88
99namespace 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}
0 commit comments