Exemple #1
0
        void TaskBuild(Job job)
        {
            // Handle switching build target when necessary
            var activeTargetMatches = (EditorUserBuildSettings.activeBuildTarget == job.target);

            if (continueTask == ContinueTask.BuildAfterSwitchingTarget && !activeTargetMatches)
            {
                throw new Exception($"Trimmer BuildRunner: Failed to switch active build target to {job.target}.");
            }
            else if (!activeTargetMatches)
            {
                token.Report(jobIndex, description: $"Switching active build target to {job.target}");

                var group = BuildPipeline.GetBuildTargetGroup(job.target);
                EditorUserBuildSettings.SwitchActiveBuildTarget(group, job.target);

                ContinueWith(ContinueTask.BuildAfterSwitchingTarget, afterDomainRelaod: true);
                return;
            }

            // Build
            token.Report(jobIndex, description: $"Building {job.target}");
            BuildReport report;

            try {
                var options = BuildManager.GetDefaultOptions(job.target);
                if (!string.IsNullOrEmpty(job.outputPath))
                {
                    options.locationPathName = job.outputPath;
                }

                report = BuildManager.BuildSync(job.profile, options);
                results[jobIndex].report = report;
            } catch (Exception e) {
                results[jobIndex] = ProfileBuildResult.Error(job.profile, e.Message);
                throw;
            }

            if (report.summary.result != BuildResult.Succeeded)
            {
                throw new Exception($"Trimmer BuildRunner: Build failed");
            }

            ContinueWith(ContinueTask.NextJob);
        }
Exemple #2
0
        /// <summary>
        /// Build a specific target of a profile with the default build options.
        /// </summary>
        /// <param name="profile">Profile to build</param>
        /// <param name="target">Target to build, needs to be part of profile</param>
        public static void Build(BuildProfile profile, BuildTarget target, IBuildsCompleteListener onComplete = null)
        {
            if (profile.BuildTargets != null && profile.BuildTargets.Any() && !profile.BuildTargets.Contains(target))
            {
                var err = $"Build target {target} is not part of the build profile {profile.name}";
                if (onComplete != null)
                {
                    onComplete.OnComplete(false, new[] { ProfileBuildResult.Error(profile, err) });
                }
                else
                {
                    Debug.LogError(err);
                }
                return;
            }

            var runner = ScriptableObject.CreateInstance <BuildRunner>();

            runner.Run(new[] { new BuildRunner.Job(profile, target) }, onComplete, TrimmerPrefs.RestoreActiveBuildTarget);
        }
Exemple #3
0
        public void Run(Job[] jobs, bool restoreActiveBuildTarget = true)
        {
            if (jobs == null || jobs.Length == 0)
            {
                throw new Exception($"Trimmer BuildRunner: No jobs given.");
            }

            EnsureNotRunning();

            var results = new ProfileBuildResult[jobs.Length];

            for (int i = 0; i < jobs.Length; i++)
            {
                var job = jobs[i];
                if (job.profile == null || job.target == 0 || job.target == BuildTarget.NoTarget)
                {
                    throw new Exception($"Trimmer BuildRunner: Invalid job at index {i}: Profile or target not set ({job.profile} / {job.target})");
                }
                results[i].profile = job.profile;
            }

            Current      = this;
            this.jobs    = jobs;
            this.results = results;
            this.restoreActiveTargetTo = EditorUserBuildSettings.activeBuildTarget;
            jobIndex         = 0;
            currentTask      = Task.Build;
            completeResult   = false;
            restoreSelection = null;

            if (Selection.activeObject is BuildProfile || Selection.activeObject is DistroBase)
            {
                restoreSelection = Selection.activeObject;
            }

            //Debug.Log($"Trimmer BuildRunner: Got jobs:\n{string.Join("\n", jobs.Select(j => $"- {j.profile?.name ?? "<none>"} {j.target}"))}");

            ContinueDebounced();
        }
Exemple #4
0
        public void Run(Job[] jobs, bool restoreActiveBuildTarget = true, UnityEngine.Object context = null)
        {
            if (jobs == null || jobs.Length == 0)
            {
                DestroyImmediate(this);
                throw new Exception($"Trimmer BuildRunner: No jobs given.");
            }

            EnsureNotRunning();

            var results = new ProfileBuildResult[jobs.Length];

            for (int i = 0; i < jobs.Length; i++)
            {
                var job = jobs[i];
                if ((job.profile == null || job.target == 0 || job.target == BuildTarget.NoTarget) && job.distro == null)
                {
                    DestroyImmediate(this);
                    throw new Exception($"Trimmer BuildRunner: Invalid job at index {i}: Profile or target or distro not set ({job.profile} / {job.target} / {job.distro})");
                }
                results[i].profile = job.profile;
            }

            Current               = this;
            this.jobs             = jobs;
            this.results          = results;
            restoreActiveTargetTo = EditorUserBuildSettings.activeBuildTarget;
            jobIndex              = -1;

            token         = TaskToken.Start(context?.name ?? "Trimmer", options: Progress.Options.Synchronous);
            token.context = context;
            Progress.SetPriority(token.taskId, Progress.Priority.High);
            token.Report(0, jobs.Length);

            //Debug.Log($"Trimmer BuildRunner: Got jobs:\n{string.Join("\n", jobs.Select(j => $"- {j.profile?.name ?? "<none>"} {j.target}"))}");

            ContinueWith(ContinueTask.NextJob);
        }
Exemple #5
0
        public static void Build(IBuildsCompleteListener onComplete = null)
        {
            string commandLineBuildPath = null;
            string profileName          = null;
            var    buildActiveTarget    = false;

            if (Application.isBatchMode)
            {
                string[] args = Environment.GetCommandLineArgs();
                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i].EqualsIgnoringCase("-profileName"))
                    {
                        if (i + 1 == args.Length || args[i + 1].StartsWith("-"))
                        {
                            throw new Exception("-profileName needs to be followed by a profile name.");
                        }
                        profileName = args[++i];
                    }
                    else if (args[i].EqualsIgnoringCase("-output"))
                    {
                        if (i + 1 == args.Length || args[i + 1].StartsWith("-"))
                        {
                            throw new Exception("-output needs to be followed by a path.");
                        }
                        commandLineBuildPath = args[++i];
                    }
                    else if (args[i].EqualsIgnoringCase("-buildTarget"))
                    {
                        // Unity will validate the value of the -buildTarget option
                        buildActiveTarget = true;
                    }
                }
            }

            BuildProfile profile = null;

            if (Application.isBatchMode && profileName != null)
            {
                profile = BuildProfile.Find(profileName);
                if (profile == null)
                {
                    var err = "Build profile named '" + profileName + "' cloud not be found.";
                    if (onComplete != null)
                    {
                        onComplete.OnComplete(false, new[] { ProfileBuildResult.Error(null, err) });
                    }
                    else
                    {
                        Debug.LogError(err);
                    }
                    return;
                }

                Debug.Log("Building " + profile.name + ", selected from command line.");
            }

            if (profile == null)
            {
                if (EditorProfile.Instance.ActiveProfile == null)
                {
                    var err = "No profile specified and not active profile set: Nothing to build";
                    if (onComplete != null)
                    {
                        onComplete.OnComplete(false, new[] { ProfileBuildResult.Error(null, err) });
                    }
                    else
                    {
                        Debug.LogError(err);
                    }
                    return;
                }
                profile = EditorProfile.Instance.ActiveProfile;
                Debug.Log("Building active profile.");
            }

            // Throw if command line build failed to cause non-zero exit code
            if (Application.isBatchMode && onComplete == null)
            {
                onComplete = ScriptableObject.CreateInstance <CommandLineBuildsCompleteListener>();
            }

            BuildRunner.Job[] jobs;
            if (buildActiveTarget)
            {
                jobs = new[] {
                    new BuildRunner.Job(profile, EditorUserBuildSettings.activeBuildTarget, commandLineBuildPath)
                };
            }
            else
            {
                var targets = profile.BuildTargets.ToArray();
                jobs = new BuildRunner.Job[targets.Length];
                for (int i = 0; i < targets.Length; i++)
                {
                    jobs[i] = new BuildRunner.Job()
                    {
                        profile    = profile,
                        target     = targets[i],
                        outputPath = commandLineBuildPath,
                    };
                }
            }

            var runner = ScriptableObject.CreateInstance <BuildRunner>();

            runner.Run(jobs, onComplete, TrimmerPrefs.RestoreActiveBuildTarget && !Application.isBatchMode);
        }