using UnityEditor; using UnityEngine; using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; public partial class BuildScript { [MenuItem("Tools/Build/Build Andriod APK")] static public void BuildAndroid_ByMenu() // used by GitLab { BuildScript.pathTarget = "c:/temp/Chic.apk"; //"C:/srv/share/JenkinsBuilds/Chic/Android/Chic.apk"; isRelease = true; BuildAndroid(); } static public void BuildAndroid() // used by GitLab { SetCommonSettings(BuildTarget.Android); //IncrementVersionPart(incrementType); PlayerSettings.Android.keyaliasPass = KEY_STORE_PASS; PlayerSettings.Android.keystorePass = KEY_STORE_PASS; if(revisionAsInt == 0) { UnityEngine.Debug.LogError("Revision variable was not properly set, cannot make a build!"); throw new Exception("invalid revision"); } PlayerSettings.Android.bundleVersionCode = revisionAsInt; // //PlayerSettings.Android.bundleVersionCode = Convert.ToInt32(ObtainRevisionHeadNumber(BuildTarget.Android)); PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, CombineSymbols(standartDefinedSymbols, realserverSymbol, slackSymbol)); bool success = MakeBuild(pathTarget, BuildTarget.Android, isRelease ? RELEASE_MODE : DEVELOPMENT_MODE); OnBuildDone(success); } [MenuItem("Tools/Build/Build iOS XCode Project")] static public void BuildIOS_ByMenu() // used by GitLab { BuildScript.pathTarget = "c:/temp/XCodeProject"; // "C:/srv/share/JenkinsBuilds/Chic/iOS"; isRelease = true; //isUnstable = true; BuildIOS(); } static public void BuildIOS() // used by GitLab { SetCommonSettings(BuildTarget.iOS); //IncrementVersionPart(incrementType); PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.iOS, CombineSymbols(standartDefinedSymbols, realserverSymbol, slackSymbol)); PlayerSettings.iOS.buildNumber = revisionAsString; //PlayerSettings.iOS.buildNumber = ObtainRevisionHeadNumber(BuildTarget.iOS); bool success = MakeBuild(pathTarget, BuildTarget.iOS, isRelease ? RELEASE_MODE : DEVELOPMENT_MODE); OnBuildDone(success); } static bool MakeBuild(string locationPathName, BuildTarget _buildTarget, BuildOptions buildOptions) { buildTarget = _buildTarget; //ImportJenkinsDataToResources(); InitPlayerSettings(); string[] scenes = FindEnabledEditorScenes(); // UnityEngine.Debug.LogError("Current Directory: " + System.IO.Directory.GetCurrentDirectory()); var report = BuildPipeline.BuildPlayer(scenes, locationPathName, buildTarget, buildOptions); //if (!string.IsNullOrEmpty(report.summary.ToString())) if(report.summary.totalErrors > 0) //Unity 2018 version { string msg = "BUILD FAILED!! Build Report:\n" + report.ToString(); // todo, review out the build report is displayed in Unity 2018 as it might have a useful description of the build, maybe we only show a nice build report summary in gitlab UnityEngine.Debug.LogError(msg); return false; } return true; } /*--------------------------------------------------------------------------------*/ /* static public void GetCommitInfo() { ProcessStartInfo processStartInfo = new ProcessStartInfo(); processStartInfo.UseShellExecute = false; processStartInfo.RedirectStandardOutput = true; processStartInfo.WorkingDirectory = pathProject; processStartInfo.Arguments = "/c svn log -l1"; processStartInfo.FileName = "cmd.exe"; Process svnInstance = Process.Start(processStartInfo); svnInstance.WaitForExit(); svnInstance.StandardOutput.ReadLine(); string response = svnInstance.StandardOutput.ReadLine(); System.IO.File.WriteAllText(pathProject + "\\lastCommitInfo.txt", response); } */ /*--------------------------------------------------------------------------------*/ /* static void CommitVersionFiles(string _version, BuildTarget _t) { ProcessStartInfo processStartInfo = new ProcessStartInfo(); processStartInfo.UseShellExecute = false; processStartInfo.RedirectStandardOutput = true; StringBuilder sb = new StringBuilder("/c svn commit -m \"Version Snapshot: "); sb.Append(_version); sb.Append("\" "); sb.Append(_t == BuildTarget.Android ? GLOBAL_VERISON_FILE_android : GLOBAL_VERISON_FILE_ios); sb.Append(" "); sb.Append(_t == BuildTarget.Android ? DISPLAYED_VERISON_FILE_android : DISPLAYED_VERISON_FILE_ios); processStartInfo.Arguments = sb.ToString(); processStartInfo.FileName = "cmd.exe"; Process svnInstance = Process.Start(processStartInfo); svnInstance.WaitForExit(); string response = svnInstance.StandardOutput.ReadLine(); UnityEngine.Debug.Log(response); } */ /*--------------------------------------------------------------------------------*/ static void SetCommonSettings_ForAnyProject(BuildTarget _target) { buildTarget = _target; //UnityEngine.Application.logMessageReceivedThreaded += OnLogDuringBuild; //Facebook.Unity.Settings.FacebookSettings.SelectedAppIndex = _target == BuildTarget.iOS ? 1 : 0; PlayerSettings.SplashScreen.showUnityLogo = false; // to prevent accidentally enabling ProcessCommandLineValues(); //if(SVN_ARGS == "") // throw new FormatException("You should provide svn command to get revision number revision!"); string versionInfo = ""; string versionOriginal = ""; //string versionFilePath = incrementType == VersionPartName.None ? "\\Assets\\Resources\\versionDisplayed.txt" : "\\version.txt"; string versionFilePath = pathProject + "\\Assets\\Resources\\versionDisplayed.txt"; try { versionOriginal = System.IO.File.ReadAllText(versionFilePath); versionInfo = versionOriginal; int iRevisionLeft = versionInfo.LastIndexOf('('); int iRevisionRight = versionInfo.LastIndexOf(')'); revisionAsString = versionInfo.Substring(iRevisionLeft + 1, iRevisionRight - iRevisionLeft - 1); if(!int.TryParse(revisionAsString, out revisionAsInt)) // Will throw an error in case of "bad" revision received which is needed to stop GitLab { UnityEngine.Debug.Log("Failed to parse: revisionAsString=" + revisionAsString); throw new Exception("Failed to parse revision"); } int i = versionInfo.IndexOf(' '); versionInfo = versionInfo.Substring(0, i); PlayerSettings.bundleVersion = versionInfo; if (versionInfo.Trim() == "") throw new Exception("Version file is empty!"); } catch (Exception e) { UnityEngine.Debug.Log("versionFilePath = " + versionFilePath); UnityEngine.Debug.Log("version string text = " + versionOriginal); UnityEngine.Debug.LogException(e); UnityEngine.Debug.Log("Cannot make a build, see BuildScript.cs for details."); throw new Exception(e.Message); } //BuildScript.versionParts = versionInfo.Split('.'); //if (incrementType == VersionPartName.None) //{ // versionParts[versionParts.Length - 1] = versionParts[versionParts.Length - 1].Remove(versionParts[versionParts.Length - 1].IndexOf("(") - 1); //} PlayerSettings.applicationIdentifier = GetBundleId(); PlayerSettings.productName = nameProduct; PlayerSettings.runInBackground = false; } /* private static void OnLogDuringBuild(string text, string stackTrace, LogType type) { if (type == LogType.Log) { System.Console.WriteLine(text); } else { var color = System.Console.ForegroundColor; System.Console.ForegroundColor = type == LogType.Warning ? ConsoleColor.Yellow : ConsoleColor.Red; System.Console.WriteLine(text); System.Console.ForegroundColor = color; // restor } } */ static string GetSvnRevisionHeadNumber() { ProcessStartInfo processStartInfo = new ProcessStartInfo(); processStartInfo.WorkingDirectory = pathProject; processStartInfo.UseShellExecute = false; processStartInfo.RedirectStandardOutput = true; processStartInfo.Arguments = "/c svn info --show-item revision --non-interactive --trust-server-cert-failures=unknown-ca " + SVN_LOGIN_ARGS; processStartInfo.FileName = "cmd.exe"; Process svnInstance = Process.Start(processStartInfo); svnInstance.WaitForExit(); string response = svnInstance.StandardOutput.ReadLine(); response = (Convert.ToInt32(response)).ToString(); return response; } static string GetBundleId() { if(buildTarget == BuildTarget.iOS) { if (isUnstable) return bundleId_iOS_Unstable; return bundleId_iOS_Stable; } else if (buildTarget == BuildTarget.Android) { return bundleId_Android; } return "com.unknown.unknown"; } static void InitPlayerSettings() { PlayerSettings.productName = nameProduct; PlayerSettings.applicationIdentifier = GetBundleId(); switch (buildTarget) { case BuildTarget.Android: PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, standartDefinedSymbols); EditorUserBuildSettings.androidBuildSystem = AndroidBuildSystem.Gradle; break; case BuildTarget.StandaloneWindows64: PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, standartDefinedSymbols); break; case BuildTarget.iOS: PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.iOS, standartDefinedSymbols); break; default: break; } } static string[] FindEnabledEditorScenes() { List EditorScenes = new List(); foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes) { if (!scene.enabled) continue; EditorScenes.Add(scene.path); } return EditorScenes.ToArray(); } static void ProcessCommandLineValues() { commandLineArgs = Environment.GetCommandLineArgs(); UnityEngine.Debug.Log("Searching for command-line arguments..."); var len = commandLineArgs.Length; for(int i = 0; i < len; i++) { int j = i + 1; string next = j < len ? commandLineArgs[j] : "(unknown)"; string current = commandLineArgs[i]; ProcessCommandLineValue(current, next); } UnityEngine.Debug.Log("Done searching for command-line arguments."); ValidateCommandLine(); } private static void ValidateCommandLine() { if(string.IsNullOrEmpty(pathProject)) { // remove "Assets" from end of UnityEngine.Application.dataPath... pathProject = System.IO.Directory.GetParent(UnityEngine.Application.dataPath).ToString(); } if (string.IsNullOrEmpty(pathTarget)) { string msg = "-targetpath \"my path\" is missing from command-line."; UnityEngine.Debug.LogError(msg); throw new Exception(msg); } } static void ProcessCommandLineValue(string current, string next) { switch(current.ToLower()) { case "-targetpath": pathTarget = next; UnityEngine.Debug.Log(" -targetpath " + next); break; case "-projectpath": pathProject = next; UnityEngine.Debug.Log(" -projectpath " + next); break; case "-isrelease": isRelease = true; UnityEngine.Debug.Log(" -isRelease"); break; case "-isunstable": isUnstable = true; UnityEngine.Debug.Log(" -isUnstable"); break; } } static void ImportJenkinsDataToResources() { AssetDatabase.StartAssetEditing(); //AssetDatabase.ImportAsset("Resources" + System.IO.Path.DirectorySeparatorChar + JenkinsData.realServerVersionFileName); //AssetDatabase.ImportAsset("Resources" + System.IO.Path.DirectorySeparatorChar + JenkinsData.jenkinsDataFileName); AssetDatabase.StopAssetEditing(); System.Threading.Thread.Sleep(100); // wait a bit before refresh, in case system is slow AssetDatabase.Refresh(); } static string CombineSymbols(params string[] symbols) { string finalString = string.Empty; foreach (string s in symbols) { finalString += s + ";"; } return finalString; } /*--------------------------------------------------------------------------------*/ }