in src/BuildScriptGenerator/DefaultBuildScriptGenerator.cs [263:362]
private string BuildScriptFromSnippets(
BuildScriptGeneratorContext context,
string installationScript,
IList<BuildScriptSnippet> buildScriptSnippets,
IDictionary<string, string> toolsToVersion,
List<string> directoriesToExcludeFromCopyToIntermediateDir,
List<string> directoriesToExcludeFromCopyToBuildOutputDir,
IEnumerable<PlatformDetectorResult> detectionResults)
{
string script;
string benvArgs = StringExtensions.JoinKeyValuePairs(toolsToVersion);
benvArgs = $"{benvArgs} {Constants.BenvDynamicInstallRootDirKey}=\"{_cliOptions.DynamicInstallRootDir}\"";
Dictionary<string, string> buildProperties = buildScriptSnippets
.Where(s => s.BuildProperties != null)
.SelectMany(s => s.BuildProperties)
.ToDictionary(p => p.Key, p => p.Value);
buildProperties[ManifestFilePropertyKeys.OperationId] = context.OperationId;
var sourceDirInBuildContainer = _cliOptions.SourceDir;
if (!string.IsNullOrEmpty(_cliOptions.IntermediateDir))
{
sourceDirInBuildContainer = _cliOptions.IntermediateDir;
}
buildProperties[ManifestFilePropertyKeys.SourceDirectoryInBuildContainer] = sourceDirInBuildContainer;
var allPlatformNames = detectionResults
.Where(s => s.Platform != null)
.Select(s => s.Platform)
.ToList();
foreach (var eachPlatformName in allPlatformNames)
{
_logger.LogInformation($"Build Property Key:{ManifestFilePropertyKeys.PlatformName} value: {eachPlatformName} is written into manifest");
if (buildProperties.ContainsKey(ManifestFilePropertyKeys.PlatformName))
{
var previousValue = buildProperties[ManifestFilePropertyKeys.PlatformName];
buildProperties[ManifestFilePropertyKeys.PlatformName]
= string.Join(
",",
previousValue,
eachPlatformName);
}
else
{
buildProperties[ManifestFilePropertyKeys.PlatformName] = eachPlatformName;
}
}
buildProperties[ManifestFilePropertyKeys.CompressDestinationDir] =
_cliOptions.CompressDestinationDir.ToString().ToLower();
(var preBuildCommand, var postBuildCommand) = PreAndPostBuildCommandHelper.GetPreAndPostBuildCommands(
context.SourceRepo,
_cliOptions);
var outputIsSubDirOfSourceDir = false;
if (!string.IsNullOrEmpty(_cliOptions.DestinationDir))
{
outputIsSubDirOfSourceDir = DirectoryHelper.IsSubDirectory(
_cliOptions.DestinationDir,
_cliOptions.SourceDir);
}
// Copy the source content to destination only if all the platforms involved in generating the build script
// say yes.
var copySourceDirectoryContentToDestinationDirectory = buildScriptSnippets.All(
snippet => snippet.CopySourceDirectoryContentToDestinationDirectory);
var buildScriptProps = new BaseBashBuildScriptProperties()
{
OsPackagesToInstall = _cliOptions.RequiredOsPackages ?? new string[0],
BuildScriptSnippets = buildScriptSnippets.Select(s => s.BashBuildScriptSnippet),
BenvArgs = benvArgs,
PreBuildCommand = preBuildCommand,
PostBuildCommand = postBuildCommand,
DirectoriesToExcludeFromCopyToIntermediateDir = directoriesToExcludeFromCopyToIntermediateDir,
DirectoriesToExcludeFromCopyToBuildOutputDir = directoriesToExcludeFromCopyToBuildOutputDir,
ManifestFileName = FilePaths.BuildManifestFileName,
ManifestDir = context.ManifestDir,
BuildCommandsFileName = context.BuildCommandsFileName,
BuildProperties = buildProperties,
BenvPath = FilePaths.Benv,
LoggerPath = FilePaths.Logger,
PlatformInstallationScript = installationScript,
OutputDirectoryIsNested = outputIsSubDirOfSourceDir,
CopySourceDirectoryContentToDestinationDirectory = copySourceDirectoryContentToDestinationDirectory,
CompressDestinationDir = _cliOptions.CompressDestinationDir,
};
LogScriptIfGiven("pre-build", buildScriptProps.PreBuildCommand);
LogScriptIfGiven("post-build", buildScriptProps.PostBuildCommand);
script = TemplateHelper.Render(
TemplateHelper.TemplateResource.BaseBashScript,
buildScriptProps,
_logger);
return script;
}