private BuildResultCode BuildMaster() { try { PackageSessionPublicHelper.FindAndSetMSBuildVersion(); } catch (Exception e) { var message = "Could not find a compatible version of MSBuild.\r\n\r\n" + "Check that you have a valid installation with the required workloads, or go to [www.visualstudio.com/downloads](https://www.visualstudio.com/downloads) to install a new one.\r\n\r\n" + e; builderOptions.Logger.Error(message); return(BuildResultCode.BuildError); } AssetCompilerContext context = null; PackageSession projectSession = null; try { var sessionLoadParameters = new PackageLoadParameters { AutoCompileProjects = !builderOptions.DisableAutoCompileProjects, ExtraCompileProperties = builderOptions.ExtraCompileProperties, RemoveUnloadableObjects = true, BuildConfiguration = builderOptions.ProjectConfiguration, }; // Loads the root Package var projectSessionResult = PackageSession.Load(builderOptions.PackageFile, sessionLoadParameters); projectSessionResult.CopyTo(builderOptions.Logger); if (projectSessionResult.HasErrors) { return(BuildResultCode.BuildError); } projectSession = projectSessionResult.Session; // Find loaded package (either xkpkg or csproj) -- otherwise fallback to first one var packageFile = (UFile)builderOptions.PackageFile; var package = projectSession.LocalPackages.FirstOrDefault(x => x.FullPath == packageFile || (x.Container is SolutionProject project && project.FullPath == packageFile)) ?? projectSession.LocalPackages.First(); // Setup variables var buildDirectory = builderOptions.BuildDirectory; var outputDirectory = builderOptions.OutputDirectory; // Process game settings asset var gameSettingsAsset = package.GetGameSettingsAsset(); if (gameSettingsAsset == null) { builderOptions.Logger.Warning($"Could not find game settings asset at location [{GameSettingsAsset.GameSettingsLocation}]. Use a Default One"); gameSettingsAsset = GameSettingsFactory.Create(); } // Create context context = new AssetCompilerContext { Platform = builderOptions.Platform, CompilationContext = typeof(AssetCompilationContext), BuildConfiguration = builderOptions.ProjectConfiguration, Package = package, }; // Command line properties foreach (var property in builderOptions.Properties) { context.OptionProperties.Add(property.Key, property.Value); } // Set current game settings context.SetGameSettingsAsset(gameSettingsAsset); // Builds the project var assetBuilder = new PackageCompiler(new RootPackageAssetEnumerator(package)); assetBuilder.AssetCompiled += RegisterBuildStepProcessedHandler; context.Properties.Set(BuildAssetNode.VisitRuntimeTypes, true); var assetBuildResult = assetBuilder.Prepare(context); assetBuildResult.CopyTo(builderOptions.Logger); if (assetBuildResult.HasErrors) { return(BuildResultCode.BuildError); } // Setup the remote process build var remoteBuilderHelper = new PackageBuilderRemoteHelper(projectSession.AssemblyContainer, builderOptions); var indexName = "index." + package.Meta.Name; // Add runtime identifier (if any) to avoid clash when building multiple at the same time (this happens when using ExtrasBuildEachRuntimeIdentifier feature of MSBuild.Sdk.Extras) if (builderOptions.Properties.TryGetValue("RuntimeIdentifier", out var runtimeIdentifier)) { indexName += $".{runtimeIdentifier}"; } // Create the builder builder = new Builder(builderOptions.Logger, buildDirectory, indexName) { ThreadCount = builderOptions.ThreadCount, TryExecuteRemote = remoteBuilderHelper.TryExecuteRemote }; builder.MonitorPipeNames.AddRange(builderOptions.MonitorPipeNames); // Add build steps generated by AssetBuilder builder.Root.Add(assetBuildResult.BuildSteps); // Run builder var result = builder.Run(Builder.Mode.Build); builder.WriteIndexFile(false); // Fill list of bundles var bundlePacker = new BundlePacker(); bundlePacker.Build(builderOptions.Logger, projectSession, indexName, outputDirectory, builder.DisableCompressionIds, context.GetCompilationMode() != CompilationMode.AppStore); return(result); } finally { builder?.Dispose(); // Dispose the session (in order to unload assemblies) projectSession?.Dispose(); context?.Dispose(); // Make sure that MSBuild doesn't hold anything else VSProjectHelper.Reset(); } }
private BuildResultCode BuildMaster() { // Only querying graphics platform, let's load package, print it and exit if (builderOptions.GetGraphicsPlatform) { return(BuildGetGraphicsPlatform()); } AssetCompilerContext context = null; PackageSession projectSession = null; try { if (!PackageSessionPublicHelper.FindAndSetMSBuildVersion()) { var message = "Could not find a compatible version of MSBuild.\r\n\r\n" + "Check that you have a valid installation with the required workloads, or go to [www.visualstudio.com/downloads](https://www.visualstudio.com/downloads) to install a new one."; builderOptions.Logger.Error(message); return(BuildResultCode.BuildError); } var sessionLoadParameters = new PackageLoadParameters { AutoCompileProjects = !builderOptions.DisableAutoCompileProjects, ExtraCompileProperties = builderOptions.ExtraCompileProperties, RemoveUnloadableObjects = true, }; // Loads the root Package var projectSessionResult = PackageSession.Load(builderOptions.PackageFile, sessionLoadParameters); projectSessionResult.CopyTo(builderOptions.Logger); if (projectSessionResult.HasErrors) { return(BuildResultCode.BuildError); } projectSession = projectSessionResult.Session; // Check build configuration var package = projectSession.LocalPackages.Last(); // Check build profile var buildProfile = package.Profiles.FirstOrDefault(pair => pair.Name == builderOptions.BuildProfile); if (buildProfile == null) { builderOptions.Logger.Error($"Unable to find profile [{builderOptions.BuildProfile}] in package [{package.FullPath}]"); return(BuildResultCode.BuildError); } // Setup variables var buildDirectory = builderOptions.BuildDirectory; var outputDirectory = builderOptions.OutputDirectory; // Process game settings asset var gameSettingsAsset = package.GetGameSettingsAsset(); if (gameSettingsAsset == null) { builderOptions.Logger.Warning($"Could not find game settings asset at location [{GameSettingsAsset.GameSettingsLocation}]. Use a Default One"); gameSettingsAsset = GameSettingsFactory.Create(); } // Create context context = new AssetCompilerContext { Profile = builderOptions.BuildProfile, Platform = builderOptions.Platform, CompilationContext = typeof(AssetCompilationContext), BuildConfiguration = builderOptions.ProjectConfiguration }; // Command line properties foreach (var property in builderOptions.Properties) { context.OptionProperties.Add(property.Key, property.Value); } // Set current game settings context.SetGameSettingsAsset(gameSettingsAsset); // Builds the project var assetBuilder = new PackageCompiler(new RootPackageAssetEnumerator(package)); assetBuilder.AssetCompiled += RegisterBuildStepProcessedHandler; context.Properties.Set(BuildAssetNode.VisitRuntimeTypes, true); var assetBuildResult = assetBuilder.Prepare(context); assetBuildResult.CopyTo(builderOptions.Logger); if (assetBuildResult.HasErrors) { return(BuildResultCode.BuildError); } // Setup the remote process build var remoteBuilderHelper = new PackageBuilderRemoteHelper(projectSession.AssemblyContainer, builderOptions); // Create the builder var indexName = "index." + builderOptions.BuildProfile; builder = new Builder(builderOptions.Logger, buildDirectory, builderOptions.BuildProfile, indexName) { ThreadCount = builderOptions.ThreadCount, TryExecuteRemote = remoteBuilderHelper.TryExecuteRemote }; builder.MonitorPipeNames.AddRange(builderOptions.MonitorPipeNames); // Add build steps generated by AssetBuilder builder.Root.Add(assetBuildResult.BuildSteps); // Run builder var result = builder.Run(Builder.Mode.Build); builder.WriteIndexFile(false); // Fill list of bundles var bundlePacker = new BundlePacker(); bundlePacker.Build(builderOptions.Logger, projectSession, buildProfile, indexName, outputDirectory, builder.DisableCompressionIds, context.GetCompilationMode() != CompilationMode.AppStore); return(result); } finally { builder?.Dispose(); // Dispose the session (in order to unload assemblies) projectSession?.Dispose(); context?.Dispose(); // Make sure that MSBuild doesn't hold anything else VSProjectHelper.Reset(); } }