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); }
/// <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); }
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); }