private void SwitchToSelfContainedBuildIfNeeded()

in src/AWS.Deploy.Orchestration/DeploymentBundleHandler.cs [127:193]


        private void SwitchToSelfContainedBuildIfNeeded(Recommendation recommendation)
        {
            if (recommendation.Recipe.TargetService == RecipeIdentifier.TARGET_SERVICE_ELASTIC_BEANSTALK)
            {
                if (recommendation.DeploymentBundle.DotnetPublishSelfContainedBuild)
                    return;

                var targetFramework = recommendation.ProjectDefinition.TargetFramework ?? string.Empty;
                if (string.IsNullOrEmpty(targetFramework))
                    return;

                // Elastic Beanstalk currently has .NET 8 and 9 supported on their platforms.
                var supportedFrameworks = new List<string> { "net8.0", "net9.0" };
                var retiredFrameworks = new List<string> { "netcoreapp3.1", "net5.0", "net6.0", "net7.0" };
                if (!supportedFrameworks.Contains(targetFramework))
                {
                    if (retiredFrameworks.Contains(targetFramework))
                    {
                        _interactiveService.LogErrorMessage($"The version of .NET that you are targeting has reached its end-of-support and has been retired by Elastic Beanstalk");
                        _interactiveService.LogInfoMessage($"Using self-contained publish to include the out of support version of .NET used by this application with the deployment bundle");
                    }
                    else
                    {
                        _interactiveService.LogInfoMessage($"Using self-contained publish since AWS Elastic Beanstalk does not currently have {targetFramework} preinstalled");
                    }
                    recommendation.DeploymentBundle.DotnetPublishSelfContainedBuild = true;
                    return;
                }

                var beanstalkPlatformSetting = recommendation.Recipe.OptionSettings.FirstOrDefault(x => x.Id.Equals("ElasticBeanstalkPlatformArn"));
                if (beanstalkPlatformSetting != null)
                {
                    var beanstalkPlatformSettingValue = _optionSettingHandler.GetOptionSettingValue<string>(recommendation, beanstalkPlatformSetting);
                    var beanstalkPlatformSettingValueSplit = beanstalkPlatformSettingValue?.Split("/");
                    if (beanstalkPlatformSettingValueSplit?.Length != 3)
                        // If the platform is not in the expected format, we will proceed normally to allow users to manually set the self-contained build to true.
                        return;
                    var beanstalkPlatformName = beanstalkPlatformSettingValueSplit[1];
                    if (!Version.TryParse(beanstalkPlatformSettingValueSplit[2], out var beanstalkPlatformVersion))
                        // If the platform is not in the expected format, we will proceed normally to allow users to manually set the self-contained build to true.
                        return;

                    // Elastic Beanstalk recently added .NET8 support in
                    // platform '.NET 8 on AL2023 version 3.1.1' and '.NET Core on AL2 version 2.8.0'.
                    // If users are using platform versions other than the above or older than '2.8.0' for '.NET Core'
                    // we need to perform a self-contained publish.
                    if (targetFramework.Equals("net8.0"))
                    {
                        if (beanstalkPlatformName.Contains(".NET Core"))
                        {
                            if (beanstalkPlatformVersion < new Version(2, 8, 0))
                            {
                                _interactiveService.LogInfoMessage($"Using self-contained publish since AWS Elastic Beanstalk does not currently have .NET 8 preinstalled on {beanstalkPlatformName} ({beanstalkPlatformVersion.ToString()})");
                                recommendation.DeploymentBundle.DotnetPublishSelfContainedBuild = true;
                                return;
                            }
                        }
                        else if (!beanstalkPlatformName.Contains(".NET 8"))
                        {
                            _interactiveService.LogInfoMessage($"Using self-contained publish since AWS Elastic Beanstalk does not currently have .NET 8 preinstalled on {beanstalkPlatformName} ({beanstalkPlatformVersion.ToString()})");
                            recommendation.DeploymentBundle.DotnetPublishSelfContainedBuild = true;
                            return;
                        }
                    }
                }
            }
        }