Esempio n. 1
0
        public override string GetInstallerScriptSnippet(string runtimeVersion)
        {
            var versionMap   = _versionProvider.GetSupportedVersions();
            var sdkVersion   = versionMap[runtimeVersion];
            var dirToInstall =
                $"{Constants.TemporaryInstallationDirectoryRoot}/{DotNetCoreConstants.LanguageName}/sdks/{sdkVersion}";
            var sentinelFileDir =
                $"{Constants.TemporaryInstallationDirectoryRoot}/{DotNetCoreConstants.LanguageName}/runtimes/{runtimeVersion}";
            var sdkInstallerScript = GetInstallerScriptSnippet(
                DotNetCoreConstants.LanguageName,
                sdkVersion,
                dirToInstall);
            var dotnetDir = $"{Constants.TemporaryInstallationDirectoryRoot}/{DotNetCoreConstants.LanguageName}";

            // Create the following structure so that 'benv' tool can understand it as it already does.
            var scriptBuilder = new StringBuilder();

            scriptBuilder
            .AppendLine(sdkInstallerScript)
            .AppendLine($"mkdir -p {dotnetDir}/runtimes/{runtimeVersion}")
            .AppendLine($"echo '{sdkVersion}' > {dotnetDir}/runtimes/{runtimeVersion}/sdkVersion.txt")
            // Write out a sentinel file to indicate downlaod and extraction was successful
            .AppendLine($"echo > {sentinelFileDir}/{SdkStorageConstants.SdkDownloadSentinelFileName}");
            return(scriptBuilder.ToString());
        }
Esempio n. 2
0
        /// <inheritdoc/>
        public void ResolveVersions(RepositoryContext context, PlatformDetectorResult detectorResult)
        {
            var dotNetCorePlatformDetectorResult = detectorResult as DotNetCorePlatformDetectorResult;

            if (dotNetCorePlatformDetectorResult == null)
            {
                throw new ArgumentException(
                          $"Expected '{nameof(detectorResult)}' argument to be of type " +
                          $"'{typeof(DotNetCorePlatformDetectorResult)}' but got '{detectorResult.GetType()}'.");
            }

            // Get runtime version
            var resolvedRuntimeVersion = GetRuntimeVersionUsingHierarchicalRules(
                dotNetCorePlatformDetectorResult.PlatformVersion);

            resolvedRuntimeVersion = GetMaxSatisfyingRuntimeVersionAndVerify(resolvedRuntimeVersion);
            dotNetCorePlatformDetectorResult.PlatformVersion = resolvedRuntimeVersion;

            var versionMap = _versionProvider.GetSupportedVersions();
            var sdkVersion = GetSdkVersion(context, dotNetCorePlatformDetectorResult.PlatformVersion, versionMap);

            dotNetCorePlatformDetectorResult.SdkVersion = sdkVersion;
        }
        public virtual string GetInstallerScriptSnippet(string runtimeVersion, string globalJsonSdkVersion)
        {
            string sdkVersion;

            if (string.IsNullOrEmpty(globalJsonSdkVersion))
            {
                var versionMap = _versionProvider.GetSupportedVersions();
                sdkVersion = versionMap[runtimeVersion];
                _logger.LogDebug(
                    "Generating installation script for sdk version {sdkVersion} based on " +
                    "runtime version {runtimeVersion}",
                    sdkVersion,
                    runtimeVersion);
            }
            else
            {
                sdkVersion = globalJsonSdkVersion;
                _logger.LogDebug(
                    "Generating installation script for sdk version {sdkVersion} based on global.json file.",
                    sdkVersion);
            }

            var dirToInstall =
                $"{DotNetCoreConstants.DynamicDotNetCoreSdkVersionsInstallDir}/{sdkVersion}";
            var sentinelFileDir =
                $"{DotNetCoreConstants.DynamicDotNetCoreRuntimeVersionsInstallDir}/{runtimeVersion}";

            var sdkInstallerScript = GetInstallerScriptSnippet(
                DotNetCoreConstants.PlatformName,
                sdkVersion,
                dirToInstall);

            // Create the following structure so that 'benv' tool can understand it as it already does.
            var scriptBuilder = new StringBuilder();

            scriptBuilder
            .AppendLine(sdkInstallerScript)
            .AppendLine($"mkdir -p {DotNetCoreConstants.DynamicDotNetCoreRuntimeVersionsInstallDir}/{runtimeVersion}")
            .AppendLine($"echo '{sdkVersion}' > {DotNetCoreConstants.DynamicDotNetCoreRuntimeVersionsInstallDir}/{runtimeVersion}/sdkVersion.txt")

            // Write out a sentinel file to indicate downlaod and extraction was successful
            .AppendLine($"echo > {sentinelFileDir}/{SdkStorageConstants.SdkDownloadSentinelFileName}");
            return(scriptBuilder.ToString());
        }
Esempio n. 4
0
        public string GetMaxSatisfyingVersionAndVerify(string runtimeVersion)
        {
            var versionMap = _versionProvider.GetSupportedVersions();

            // Since our semantic versioning library does not work with .NET Core preview version format, here
            // we do some trivial way of finding the latest version which matches a given runtime version
            // Runtime versions are usually like: 1.0, 2.1, 3.1, 5.0 etc.
            // (these are constructed from netcoreapp21, netcoreapp31 etc.)
            // Preview version of sdks also have preview versions of runtime versions and hence they
            // have '-' in their names.
            var nonPreviewRuntimeVersions = versionMap.Keys.Where(version => version.IndexOf("-") < 0);
            var maxSatisfyingVersion      = SemanticVersionResolver.GetMaxSatisfyingVersion(
                runtimeVersion,
                nonPreviewRuntimeVersions);

            // Check if a preview version is available
            if (string.IsNullOrEmpty(maxSatisfyingVersion))
            {
                // NOTE:
                // Preview versions: 5.0.0-preview.3.20214.6, 5.0.0-preview.2.20160.6, 5.0.0-preview.1.20120.5
                var previewRuntimeVersions = versionMap.Keys
                                             .Where(version => version.IndexOf("-") >= 0)
                                             .Where(version => version.StartsWith(runtimeVersion))
                                             .OrderByDescending(version => version);
                if (previewRuntimeVersions.Any())
                {
                    maxSatisfyingVersion = previewRuntimeVersions.First();
                }
            }

            if (string.IsNullOrEmpty(maxSatisfyingVersion))
            {
                var exception = new UnsupportedVersionException(
                    DotNetCoreConstants.PlatformName,
                    runtimeVersion,
                    versionMap.Keys);
                _logger.LogError(
                    exception,
                    $"Exception caught, the version '{runtimeVersion}' is not supported for the .NET Core platform.");
                throw exception;
            }

            return(maxSatisfyingVersion);
        }
        private string GetMaxSatisfyingVersionAndVerify(string runtimeVersion)
        {
            var versionMap           = _versionProvider.GetSupportedVersions();
            var maxSatisfyingVersion = SemanticVersionResolver.GetMaxSatisfyingVersion(
                runtimeVersion,
                versionMap.Keys);

            if (string.IsNullOrEmpty(maxSatisfyingVersion))
            {
                var exception = new UnsupportedVersionException(
                    DotNetCoreConstants.LanguageName,
                    runtimeVersion,
                    versionMap.Keys);
                _logger.LogError(
                    exception,
                    $"Exception caught, the version '{runtimeVersion}' is not supported for the .NET Core platform.");
                throw exception;
            }

            return(maxSatisfyingVersion);
        }
Esempio n. 6
0
        /// <inheritdoc/>
        public BuildScriptSnippet GenerateBashBuildScriptSnippet(
            BuildScriptGeneratorContext context,
            PlatformDetectorResult detectorResult)
        {
            var dotNetCorePlatformDetectorResult = detectorResult as DotNetCorePlatformDetectorResult;

            if (dotNetCorePlatformDetectorResult == null)
            {
                throw new ArgumentException(
                          $"Expected '{nameof(detectorResult)}' argument to be of type " +
                          $"'{typeof(DotNetCorePlatformDetectorResult)}' but got '{detectorResult.GetType()}'.");
            }

            var versionMap = _versionProvider.GetSupportedVersions();

            string globalJsonSdkVersion = null;

            if (_commonOptions.EnableDynamicInstall)
            {
                var availableSdks = versionMap.Values;
                globalJsonSdkVersion = _globalJsonSdkResolver.GetSatisfyingSdkVersion(
                    context.SourceRepo,
                    detectorResult.PlatformVersion,
                    availableSdks);
            }

            var manifestFileProperties = new Dictionary <string, string>();

            manifestFileProperties[ManifestFilePropertyKeys.OperationId] = context.OperationId;
            manifestFileProperties[ManifestFilePropertyKeys.DotNetCoreRuntimeVersion]
                = detectorResult.PlatformVersion;

            if (string.IsNullOrEmpty(globalJsonSdkVersion))
            {
                manifestFileProperties[ManifestFilePropertyKeys.DotNetCoreSdkVersion]
                    = versionMap[detectorResult.PlatformVersion];
            }
            else
            {
                manifestFileProperties[ManifestFilePropertyKeys.DotNetCoreSdkVersion] = globalJsonSdkVersion;
            }

            var projectFile = dotNetCorePlatformDetectorResult.ProjectFile;

            if (string.IsNullOrEmpty(projectFile))
            {
                return(null);
            }

            var templateProperties = new DotNetCoreBashBuildSnippetProperties
            {
                ProjectFile   = projectFile,
                Configuration = GetBuildConfiguration(),
            };

            var script = TemplateHelper.Render(
                TemplateHelper.TemplateResource.DotNetCoreSnippet,
                templateProperties,
                _logger);

            SetStartupFileNameInfoInManifestFile(context, projectFile, manifestFileProperties);

            return(new BuildScriptSnippet
            {
                BashBuildScriptSnippet = script,
                BuildProperties = manifestFileProperties,

                // Setting this to false to avoid copying files like '.cs' to the destination
                CopySourceDirectoryContentToDestinationDirectory = false,
            });
        }
Esempio n. 7
0
        /// <inheritdoc/>
        public BuildScriptSnippet GenerateBashBuildScriptSnippet(BuildScriptGeneratorContext context)
        {
            var versionMap = _versionProvider.GetSupportedVersions();

            string installationScriptSnippet = null;
            string globalJsonSdkVersion      = null;

            if (_cliOptions.EnableDynamicInstall)
            {
                _logger.LogDebug("Dynamic install is enabled.");

                var availableSdks = versionMap.Values;
                globalJsonSdkVersion = _globalJsonSdkResolver.GetSatisfyingSdkVersion(
                    context.SourceRepo,
                    context.ResolvedDotNetCoreRuntimeVersion,
                    availableSdks);

                if (_platformInstaller.IsVersionAlreadyInstalled(
                        context.ResolvedDotNetCoreRuntimeVersion,
                        globalJsonSdkVersion))
                {
                    _logger.LogDebug(
                        "DotNetCore runtime version {runtimeVersion} is already installed. " +
                        "So skipping installing it again.",
                        context.ResolvedDotNetCoreRuntimeVersion);
                }
                else
                {
                    _logger.LogDebug(
                        "DotNetCore runtime version {runtimeVersion} is not installed. " +
                        "So generating an installation script snippet for it.",
                        context.ResolvedDotNetCoreRuntimeVersion);

                    installationScriptSnippet = _platformInstaller.GetInstallerScriptSnippet(
                        context.ResolvedDotNetCoreRuntimeVersion,
                        globalJsonSdkVersion);
                }
            }
            else
            {
                _logger.LogDebug("Dynamic install is not enabled.");
            }

            var manifestFileProperties = new Dictionary <string, string>();

            manifestFileProperties[ManifestFilePropertyKeys.OperationId] = context.OperationId;
            manifestFileProperties[ManifestFilePropertyKeys.DotNetCoreRuntimeVersion]
                = context.ResolvedDotNetCoreRuntimeVersion;

            if (string.IsNullOrEmpty(globalJsonSdkVersion))
            {
                manifestFileProperties[ManifestFilePropertyKeys.DotNetCoreSdkVersion]
                    = versionMap[context.ResolvedDotNetCoreRuntimeVersion];
            }
            else
            {
                manifestFileProperties[ManifestFilePropertyKeys.DotNetCoreSdkVersion] = globalJsonSdkVersion;
            }


            var projectFile = _projectFileProvider.GetRelativePathToProjectFile(context);

            if (string.IsNullOrEmpty(projectFile))
            {
                return(null);
            }

            (var preBuildCommand, var postBuildCommand) = PreAndPostBuildCommandHelper.GetPreAndPostBuildCommands(
                context.SourceRepo,
                _cliOptions);

            var sourceDir = _cliOptions.SourceDir;
            var temporaryDestinationDir       = "/tmp/puboutput";
            var destinationDir                = _cliOptions.DestinationDir;
            var intermediateDir               = _cliOptions.IntermediateDir;
            var hasUserSuppliedDestinationDir = !string.IsNullOrEmpty(_cliOptions.DestinationDir);
            var buildConfiguration            = GetBuildConfiguration();

            // Since destination directory is optional for .NET Core builds, check
            var outputIsSubDirOfSourceDir = false;

            if (!string.IsNullOrEmpty(_cliOptions.DestinationDir))
            {
                outputIsSubDirOfSourceDir = DirectoryHelper.IsSubDirectory(
                    _cliOptions.DestinationDir,
                    _cliOptions.SourceDir);
            }

            var scriptBuilder = new StringBuilder();

            scriptBuilder
            .AppendLine("#!/bin/bash")
            .AppendLine("set -e")
            .AppendLine();

            // For 1st build this is not a problem, but for subsequent builds we want the source directory to be
            // in a clean state to avoid considering earlier build's state and potentially yielding incorrect results.
            if (outputIsSubDirOfSourceDir)
            {
                scriptBuilder.AppendLine($"rm -rf {_cliOptions.DestinationDir}");
            }

            scriptBuilder.AddScriptToCopyToIntermediateDirectory(
                sourceDir: ref sourceDir,
                intermediateDir: intermediateDir,
                GetDirectoriesToExcludeFromCopyToIntermediateDir(context))
            .AppendFormatWithLine("cd \"{0}\"", sourceDir)
            .AppendLine();

            if (!string.IsNullOrEmpty(installationScriptSnippet))
            {
                scriptBuilder.AppendLine(installationScriptSnippet);
            }

            scriptBuilder.AddScriptToCopyToIntermediateDirectory(
                sourceDir: ref sourceDir,
                intermediateDir: intermediateDir,
                GetDirectoriesToExcludeFromCopyToIntermediateDir(context))
            .AppendFormatWithLine("cd \"{0}\"", sourceDir)
            .AppendLine();

            scriptBuilder
            .AddScriptToSetupSourceAndDestinationDirectories(
                sourceDir: sourceDir,
                temporaryDestinationDir: temporaryDestinationDir,
                destinationDir: destinationDir,
                hasUserSuppliedDestinationDir: hasUserSuppliedDestinationDir)
            .AppendBenvCommand($"dotnet={context.ResolvedDotNetCoreRuntimeVersion}")
            .AddScriptToRunPreBuildCommand(sourceDir: sourceDir, preBuildCommand: preBuildCommand)
            .AppendLine("echo")
            .AppendLine("dotnetCoreVersion=$(dotnet --version)")
            .AppendLine("echo \"Using .NET Core SDK Version: $dotnetCoreVersion\"")
            .AppendLine()
            .AddScriptToRestorePackages(projectFile);

            if (hasUserSuppliedDestinationDir)
            {
                scriptBuilder
                .AddScriptToPublishOutput(
                    projectFile: projectFile,
                    buildConfiguration: buildConfiguration,
                    finalDestinationDir: destinationDir)
                .AddScriptToRunPostBuildCommand(
                    sourceDir: sourceDir,
                    postBuildCommand: postBuildCommand);
            }
            else
            {
                scriptBuilder
                .AddScriptToBuildProject(projectFile)
                .AddScriptToRunPostBuildCommand(
                    sourceDir: sourceDir,
                    postBuildCommand: postBuildCommand);
            }

            SetStartupFileNameInfoInManifestFile(context, projectFile, manifestFileProperties);

            scriptBuilder
            .AddScriptToCreateManifestFile(
                manifestFileProperties,
                manifestDir: _cliOptions.ManifestDir,
                finalDestinationDir: destinationDir)
            .AppendLine("echo Done.");

            return(new BuildScriptSnippet
            {
                BashBuildScriptSnippet = scriptBuilder.ToString(),
                IsFullScript = true,
            });
        }
Esempio n. 8
0
        /// <inheritdoc/>
        public BuildScriptSnippet GenerateBashBuildScriptSnippet(BuildScriptGeneratorContext context)
        {
            var versionMap = _versionProvider.GetSupportedVersions();

            string installationScriptSnippet = null;
            string globalJsonSdkVersion      = null;

            if (_cliOptions.EnableDynamicInstall)
            {
                _logger.LogDebug("Dynamic install is enabled.");

                var availableSdks = versionMap.Values;
                globalJsonSdkVersion = _globalJsonSdkResolver.GetSatisfyingSdkVersion(
                    context.SourceRepo,
                    context.ResolvedDotNetCoreRuntimeVersion,
                    availableSdks);

                if (_platformInstaller.IsVersionAlreadyInstalled(
                        context.ResolvedDotNetCoreRuntimeVersion,
                        globalJsonSdkVersion))
                {
                    _logger.LogDebug(
                        "DotNetCore runtime version {runtimeVersion} is already installed. " +
                        "So skipping installing it again.",
                        context.ResolvedDotNetCoreRuntimeVersion);
                }
                else
                {
                    _logger.LogDebug(
                        "DotNetCore runtime version {runtimeVersion} is not installed. " +
                        "So generating an installation script snippet for it.",
                        context.ResolvedDotNetCoreRuntimeVersion);

                    installationScriptSnippet = _platformInstaller.GetInstallerScriptSnippet(
                        context.ResolvedDotNetCoreRuntimeVersion,
                        globalJsonSdkVersion);
                }
            }
            else
            {
                _logger.LogDebug("Dynamic install is not enabled.");
            }

            var manifestFileProperties = new Dictionary <string, string>();

            manifestFileProperties[ManifestFilePropertyKeys.OperationId] = context.OperationId;
            manifestFileProperties[ManifestFilePropertyKeys.DotNetCoreRuntimeVersion]
                = context.ResolvedDotNetCoreRuntimeVersion;

            if (string.IsNullOrEmpty(globalJsonSdkVersion))
            {
                manifestFileProperties[ManifestFilePropertyKeys.DotNetCoreSdkVersion]
                    = versionMap[context.ResolvedDotNetCoreRuntimeVersion];
            }
            else
            {
                manifestFileProperties[ManifestFilePropertyKeys.DotNetCoreSdkVersion] = globalJsonSdkVersion;
            }

            var projectFile = _projectFileProvider.GetRelativePathToProjectFile(context);

            if (string.IsNullOrEmpty(projectFile))
            {
                return(null);
            }

            var templateProperties = new DotNetCoreBashBuildSnippetProperties
            {
                ProjectFile   = projectFile,
                Configuration = GetBuildConfiguration(),
            };

            var script = TemplateHelper.Render(
                TemplateHelper.TemplateResource.DotNetCoreSnippet,
                templateProperties,
                _logger);

            SetStartupFileNameInfoInManifestFile(context, projectFile, manifestFileProperties);

            return(new BuildScriptSnippet
            {
                BashBuildScriptSnippet = script,
                BuildProperties = manifestFileProperties,
                PlatformInstallationScriptSnippet = installationScriptSnippet,

                // Setting this to false to avoid copying files like '.cs' to the destination
                CopySourceDirectoryContentToDestinationDirectory = false,
            });
        }