public void Execute()
        {
            CLITools.PrintBanner("Build");

            // TODO: Find a way to extract this into a build-script plugin.
            ScenesMenu.GenerateMapData();
            string staticDataPath = Path.Combine(buildPath, ScenesMenu.MAP_PATH);

            new FileInfo(staticDataPath).Directory?.Create();
            File.Copy(Path.Combine(Application.persistentDataPath, ScenesMenu.MAP_PATH), staticDataPath, true);


            if (target == BuildTarget.Android)
            {
                HandleAndroidAppBundle();
                HandleAndroidBundleVersionCode();
                HandleAndroidKeystore();
            }

            SetScriptingBackendFromEnv(target);

            string[]    scenes = CLITools.GetEnabledScenes();
            BuildReport report = BuildPipeline.BuildPlayer(scenes, fixedBuildPath, target, buildOpts);

            if (report.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded)
            {
                throw new Exception($"Build ended with {report.summary.result} status");
            }
            else
            {
                PrintReport(report);
            }
        }
        public void RunFinished(ITestResultAdaptor results)
        {
            if (silent && results.FailCount == 0)
            {
                return;
            }

            if (verbose || results.FailCount > 0)
            {
                CLITools.PrintBanner("Tests");
                PrintTest(results, 0);
            }

            Console.WriteLine();
            CLITools.PrintInlineBanner("Test Summary");
            Console.WriteLine(" Passed: " + results.PassCount);
            Console.WriteLine(" Failed: " + results.FailCount);
            Console.WriteLine(" Skipped/Inconclusive: " + (results.SkipCount + results.InconclusiveCount));
            CLITools.PrintBannerBar();
            Console.WriteLine();

            if (results.FailCount > 0)
            {
                throw new Exception("Fix your tests, dag nabbit!");
            }
        }
        private BuildOptions GetBuildOptions()
        {
            string options = CLITools.GetArgument("opts");

            string[]     allOptionVars = !string.IsNullOrEmpty(options) ? options.Split(',') : new string[] {};
            BuildOptions allOptions    = BuildOptions.None;
            BuildOptions option;
            string       optionVar;
            int          length = allOptionVars.Length;

            for (int i = 0; i < length; i++)
            {
                optionVar = allOptionVars[i];

                if (optionVar.TryConvertToEnum(out option))
                {
                    allOptions |= option;
                }
                else
                {
                    Console.WriteLine($":: Cannot convert {optionVar} to {nameof(BuildOptions)} enum, skipping it.");
                }
            }

            return(allOptions);
        }
 //-------------------------------------------------------------------------
 // Public Interface
 //-------------------------------------------------------------------------
 public void ParseCommandLine()
 {
     testMode    = GetTestMode();
     synchronous = CLITools.GetFlag("runSynchronously") || CLITools.GetFlag("sync") || CLITools.GetFlag("synchronous");
     silent      = CLITools.GetFlag("silent");
     verbose     = !silent && (CLITools.GetFlag("verbose") || CLITools.GetFlag("v"));
     runner      = ScriptableObject.CreateInstance <TestRunnerApi>();
 }
        private string GetBuildPath()
        {
            string buildPath = CLITools.GetArgument("dir");

            if (buildPath == "")
            {
                throw new Exception("dir argument is missing");
            }
            return(buildPath);
        }
        private BuildTarget GetBuildTarget()
        {
            targetName = CLITools.GetArgument("target");
            if (targetName.TryConvertToEnum(out BuildTarget target))
            {
                return(target);
            }

            return(BuildTarget.NoTarget);
        }
        private string GetBuildName()
        {
            string buildName = CLITools.GetArgument("name");

            Console.WriteLine(":: Received name " + buildName);
            if (buildName == "")
            {
                throw new Exception("name argument is missing");
            }
            return(buildName);
        }
        //-------------------------------------------------------------------------
        // Helper Methods
        //-------------------------------------------------------------------------
        private TestMode?GetTestMode()
        {
            if (CLITools.GetFlag("playMode"))
            {
                return(TestMode.PlayMode);
            }
            else if (CLITools.GetFlag("editMode"))
            {
                return(TestMode.EditMode);
            }

            return(null);
        }
 private static void HandleAndroidBundleVersionCode()
 {
     if (CLITools.TryGetEnv(ANDROID_BUNDLE_VERSION_CODE, out string value))
     {
         if (int.TryParse(value, out int version))
         {
             PlayerSettings.Android.bundleVersionCode = version;
             Console.WriteLine($":: {ANDROID_BUNDLE_VERSION_CODE} env var detected, set the bundle version code to {value}.");
         }
         else
         {
             Console.WriteLine($":: {ANDROID_BUNDLE_VERSION_CODE} env var detected but the version value \"{value}\" is not an integer.");
         }
     }
 }
 //-------------------------------------------------------------------------
 // Android Stuff
 //-------------------------------------------------------------------------
 private static void HandleAndroidAppBundle()
 {
     if (CLITools.TryGetEnv(ANDROID_APP_BUNDLE, out string value))
     {
         if (bool.TryParse(value, out bool buildAppBundle))
         {
             EditorUserBuildSettings.buildAppBundle = buildAppBundle;
             Console.WriteLine($":: {ANDROID_APP_BUNDLE} env var detected, set buildAppBundle to {value}.");
         }
         else
         {
             Console.WriteLine($":: {ANDROID_APP_BUNDLE} env var detected but the value \"{value}\" is not a boolean.");
         }
     }
 }
        private static void HandleAndroidKeystore()
        {
            PlayerSettings.Android.useCustomKeystore = false;

            if (!File.Exists(KEYSTORE))
            {
                Console.WriteLine($":: {KEYSTORE} not found, skipping setup, using Unity's default keystore");
                return;
            }

            PlayerSettings.Android.keystoreName = KEYSTORE;

            string keystorePass;
            string keystoreAliasPass;

            if (CLITools.TryGetEnv(KEY_ALIAS_NAME, out string keyaliasName))
            {
                PlayerSettings.Android.keyaliasName = keyaliasName;
                Console.WriteLine($":: using ${KEY_ALIAS_NAME} env var on PlayerSettings");
            }
            else
            {
                Console.WriteLine($":: ${KEY_ALIAS_NAME} env var not set, using Project's PlayerSettings");
            }

            if (!CLITools.TryGetEnv(KEYSTORE_PASS, out keystorePass))
            {
                Console.WriteLine($":: ${KEYSTORE_PASS} env var not set, skipping setup, using Unity's default keystore");
                return;
            }

            if (!CLITools.TryGetEnv(KEY_ALIAS_PASS, out keystoreAliasPass))
            {
                Console.WriteLine($":: ${KEY_ALIAS_PASS} env var not set, skipping setup, using Unity's default keystore");
                return;
            }

            PlayerSettings.Android.useCustomKeystore = true;
            PlayerSettings.Android.keystorePass      = keystorePass;
            PlayerSettings.Android.keyaliasPass      = keystoreAliasPass;
        }
        //-------------------------------------------------------------------------
        // Helper Methods
        //-------------------------------------------------------------------------
        private void PrintReport(BuildReport report)
        {
            CLITools.PrintInlineBanner("Steps");

            foreach (BuildStep step in report.steps)
            {
                int    depth   = step.depth;
                string message = "";
                if (depth == 0)
                {
                    message += "Step: ";
                }
                else
                {
                    message += new String(' ', (depth - 1) * 3);
                    message += " - ";
                }
                message += step.name + " - " + step.duration.TotalMilliseconds + "ms";
                Console.WriteLine(message);
            }

            CLITools.PrintInlineBanner("Build Complete");
        }
        private void SetScriptingBackendFromEnv(BuildTarget platform)
        {
            var targetGroup = BuildPipeline.GetBuildTargetGroup(platform);

            if (CLITools.TryGetEnv(SCRIPTING_BACKEND_ENV_VAR, out string scriptingBackend))
            {
                if (scriptingBackend.TryConvertToEnum(out ScriptingImplementation backend))
                {
                    Console.WriteLine($":: Setting ScriptingBackend to {backend}");
                    PlayerSettings.SetScriptingBackend(targetGroup, backend);
                }
                else
                {
                    string possibleValues = string.Join(", ", Enum.GetValues(typeof(ScriptingImplementation)).Cast <ScriptingImplementation>());
                    throw new Exception($"Could not find '{scriptingBackend}' in ScriptingImplementation enum. Possible values are: {possibleValues}");
                }
            }
            else
            {
                var defaultBackend = PlayerSettings.GetDefaultScriptingBackend(targetGroup);
                Console.WriteLine($":: Using project's configured ScriptingBackend (should be {defaultBackend} for tagetGroup {targetGroup}");
            }
        }