public LanguageDetectorResult Detect(RepositoryContext context)
        {
            var projectFile = _projectFileProvider.GetRelativePathToProjectFile(context);

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

            var sourceRepo             = context.SourceRepo;
            var projectFileDoc         = XDocument.Load(new StringReader(sourceRepo.ReadFile(projectFile)));
            var targetFrameworkElement = projectFileDoc.XPathSelectElement(
                DotNetCoreConstants.TargetFrameworkElementXPathExpression);
            var targetFramework = targetFrameworkElement?.Value;

            if (string.IsNullOrEmpty(targetFramework))
            {
                _logger.LogDebug(
                    $"Could not find 'TargetFramework' element in the project file.");
                return(null);
            }

            var version = GetVersion(context, targetFramework);

            version = GetMaxSatisfyingVersionAndVerify(version);

            return(new LanguageDetectorResult
            {
                Language = DotNetCoreConstants.LanguageName,
                LanguageVersion = version,
            });
        }
示例#2
0
        public PlatformDetectorResult Detect(RepositoryContext context)
        {
            var projectFile = _projectFileProvider.GetRelativePathToProjectFile(context);

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

            var sourceRepo             = context.SourceRepo;
            var projectFileDoc         = XDocument.Load(new StringReader(sourceRepo.ReadFile(projectFile)));
            var targetFrameworkElement = projectFileDoc.XPathSelectElement(
                DotNetCoreConstants.TargetFrameworkElementXPathExpression);
            var targetFramework = targetFrameworkElement?.Value;

            if (string.IsNullOrEmpty(targetFramework))
            {
                _logger.LogDebug(
                    $"Could not find 'TargetFramework' element in the project file.");
                return(null);
            }

            var version = DetermineRuntimeVersion(targetFramework);

            return(new PlatformDetectorResult
            {
                Platform = DotNetCoreConstants.PlatformName,
                PlatformVersion = version,
            });
        }
        public LanguageDetectorResult Detect(BuildScriptGeneratorContext context)
        {
            var projectFile = _projectFileProvider.GetRelativePathToProjectFile(context);

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

            var sourceRepo             = context.SourceRepo;
            var projectFileDoc         = XDocument.Load(new StringReader(sourceRepo.ReadFile(projectFile)));
            var targetFrameworkElement = projectFileDoc.XPathSelectElement(
                DotNetCoreConstants.TargetFrameworkElementXPathExpression);
            var targetFramework = targetFrameworkElement?.Value;

            if (string.IsNullOrEmpty(targetFramework))
            {
                _logger.LogDebug(
                    $"Could not find 'TargetFramework' element in the project file '{projectFile}'.");
                return(null);
            }

            // If a repo explicitly specifies an sdk version, then just use it as it is.
            string languageVersion = null;

            if (sourceRepo.FileExists(DotNetCoreConstants.GlobalJsonFileName))
            {
                var globalJson = GetGlobalJsonObject(sourceRepo);
                var sdkVersion = globalJson?.sdk?.version?.Value as string;
                if (sdkVersion != null)
                {
                    languageVersion = sdkVersion;
                }
            }

            if (string.IsNullOrEmpty(languageVersion))
            {
                languageVersion = DetermineSdkVersion(targetFramework);
            }

            if (languageVersion == null)
            {
                _logger.LogDebug(
                    $"Could not find a {DotNetCoreConstants.LanguageName} version corresponding to 'TargetFramework'" +
                    $" '{targetFramework}'.");
                return(null);
            }

            languageVersion = VerifyAndResolveVersion(languageVersion);

            return(new LanguageDetectorResult
            {
                Language = DotNetCoreConstants.LanguageName,
                LanguageVersion = languageVersion,
            });
        }
示例#4
0
        private (string projFile, string publishDir) GetProjectFileAndPublishDir(
            BuildScriptGeneratorContext context)
        {
            var projectFile = _projectFileProvider.GetRelativePathToProjectFile(context);

            if (!string.IsNullOrEmpty(projectFile))
            {
                var publishDir = Path.Combine(
                    context.SourceRepo.RootPath,
                    DotNetCoreConstants.OryxOutputPublishDirectory);
                return(projectFile, publishDir);
            }

            return(null, null);
        }
示例#5
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,
            });
        }
示例#6
0
        public BuildScriptSnippet GenerateBashBuildScriptSnippet(BuildScriptGeneratorContext context)
        {
            var buildProperties = new Dictionary <string, string>();

            buildProperties[ManifestFilePropertyKeys.OperationId] = context.OperationId;

            var projectFile = _projectFileProvider.GetRelativePathToProjectFile(context);

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

            _environmentSettingsProvider.TryGetAndLoadSettings(out var environmentSettings);

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

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

            var scriptBuilder = new StringBuilder();

            scriptBuilder
            .AppendLine("#!/bin/bash")
            .AppendLine("set -e")
            .AppendLine()
            .AddScriptToCopyToIntermediateDirectory(
                sourceDir: ref sourceDir,
                intermediateDir: intermediateDir,
                GetDirectoriesToExcludeFromCopyToIntermediateDir(context))
            .AppendFormatWithLine("cd \"{0}\"", sourceDir)
            .AppendLine();

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

            if (hasUserSuppliedDestinationDir)
            {
                if (zipAllOutput)
                {
                    scriptBuilder.AddScriptToZipAllOutput(
                        projectFile: projectFile,
                        buildConfiguration: buildConfiguration,
                        sourceDir: sourceDir,
                        temporaryDestinationDir: temporaryDestinationDir,
                        finalDestinationDir: destinationDir,
                        postBuildCommand: postBuildCommand,
                        buildProperties);
                }
                else
                {
                    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, buildProperties);

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

            return(new BuildScriptSnippet
            {
                BashBuildScriptSnippet = scriptBuilder.ToString(),
                IsFullScript = true,
            });
        }
        /// <inheritdoc/>
        public BuildScriptSnippet GenerateBashBuildScriptSnippet(
            BuildScriptGeneratorContext context,
            PlatformDetectorResult detectorResult)
        {
            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 = _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,

                // Setting this to false to avoid copying files like '.cs' to the destination
                CopySourceDirectoryContentToDestinationDirectory = false,
            });
        }
示例#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,
            });
        }