/// <summary>
        /// Gets the installation script snippet which contains snippets for all detected platforms.
        /// </summary>
        /// <param name="context">The <see cref="RepositoryContext"/>.</param>
        /// <param name="detectionResults">Already detected platforms results.</param>
        /// <returns>A snippet having logic to install all detected platforms.</returns>
        public string GetBashScriptSnippet(
            BuildScriptGeneratorContext context,
            IEnumerable <PlatformDetectorResult> detectionResults = null)
        {
            var scriptBuilder = new StringBuilder();

            // Avoid detecting again if detection was already run.
            if (detectionResults == null)
            {
                var platformInfos = _platformDetector.GetPlatformsInfo(context);
                if (platformInfos != null)
                {
                    detectionResults = platformInfos.Select(pi => pi.DetectorResult);
                }
            }

            var snippets = GetInstallationScriptSnippets(detectionResults, context);

            foreach (var snippet in snippets)
            {
                scriptBuilder.AppendLine(snippet);
                scriptBuilder.AppendLine();
            }

            return(scriptBuilder.ToString());
        }
Exemplo n.º 2
0
        public void GenerateBashScript(
            BuildScriptGeneratorContext context,
            out string script,
            List <ICheckerMessage> checkerMessageSink = null)
        {
            script = null;

            IList <BuildScriptSnippet> buildScriptSnippets;
            var directoriesToExcludeFromCopyToIntermediateDir = new List <string>();
            var directoriesToExcludeFromCopyToBuildOutputDir  = new List <string>();

            // Try detecting ALL platforms since in some scenarios this is required.
            // For example, in case of a multi-platform app like ASP.NET Core + NodeJs, we might need to dynamically
            // install both these platforms' sdks before actually using any of their commands. So even though a user
            // of Oryx might explicitly supply the platform of the app as .NET Core, we still need to make sure the
            // build environment is setup with detected platforms' sdks.
            var platformInfos      = _platformsInformationProvider.GetPlatformsInfo(context);
            var detectionResults   = platformInfos.Select(pi => pi.DetectorResult);
            var installationScript = _environmentSetupScriptProvider.GetBashScriptSnippet(
                context,
                detectionResults);

            // Get list of tools to be set on benv
            var toolsToVersion = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            foreach (var toolsToBeSetInPath in platformInfos
                     .Where(pi => pi.RequiredToolsInPath != null)
                     .Select(pi => pi.RequiredToolsInPath))
            {
                foreach (var toolNameAndVersion in toolsToBeSetInPath)
                {
                    if (!string.IsNullOrEmpty(
                            Environment.GetEnvironmentVariable(toolNameAndVersion.Key)))
                    {
                        _logger.LogInformation($"If {toolNameAndVersion.Key} is set as environment, it'll be not be set via benv");
                    }
                    else
                    {
                        _logger.LogInformation($"If {toolNameAndVersion.Key} is not set as environment, it'll be set to {toolNameAndVersion.Value} via benv");
                        toolsToVersion[toolNameAndVersion.Key] = toolNameAndVersion.Value;
                    }
                }
            }

            using (var timedEvent = _logger.LogTimedEvent("GetBuildSnippets"))
            {
                buildScriptSnippets = GetBuildSnippets(
                    context,
                    detectionResults,
                    runDetection: false,
                    directoriesToExcludeFromCopyToIntermediateDir,
                    directoriesToExcludeFromCopyToBuildOutputDir);
                timedEvent.SetProperties(toolsToVersion);
            }

            if (_checkers != null && checkerMessageSink != null && _cliOptions.EnableCheckers)
            {
                try
                {
                    _logger.LogDebug("Running checkers");
                    RunCheckers(context, toolsToVersion, checkerMessageSink);
                }
                catch (Exception exc)
                {
                    _logger.LogError(exc, "Exception caught while running checkers");
                }
            }
            else
            {
                _logger.LogInformation("Not running checkers - condition evaluates to " +
                                       "({checkersNotNull} && {sinkNotNull} && {enableCheckers})",
                                       _checkers != null, checkerMessageSink != null, _cliOptions.EnableCheckers);
            }

            if (buildScriptSnippets != null)
            {
                foreach (var snippet in buildScriptSnippets)
                {
                    if (snippet.IsFullScript)
                    {
                        script = snippet.BashBuildScriptSnippet;
                        return;
                    }
                }
            }

            if (buildScriptSnippets.Any())
            {
                // By default exclude these irrespective of platform
                directoriesToExcludeFromCopyToIntermediateDir.Add(".git");
                directoriesToExcludeFromCopyToBuildOutputDir.Add(".git");

                script = BuildScriptFromSnippets(
                    context,
                    installationScript,
                    buildScriptSnippets,
                    new ReadOnlyDictionary <string, string>(toolsToVersion),
                    directoriesToExcludeFromCopyToIntermediateDir,
                    directoriesToExcludeFromCopyToBuildOutputDir,
                    detectionResults);
            }
            else
            {
                // TODO: Should an UnsupportedPlatformException be thrown here?
                // Seeing as the issue was that platforms were IDENTIFIED, but no build snippets were emitted from them
                throw new UnsupportedPlatformException(Labels.UnableToDetectPlatformMessage);
            }
        }