Skip to content
This repository has been archived by the owner on Dec 18, 2017. It is now read-only.

Commit

Permalink
Enabling running scripts per configuration per framework
Browse files Browse the repository at this point in the history
Required to fix #887
  • Loading branch information
moozzyk committed Sep 9, 2015
1 parent 3e39a01 commit 6864fc6
Show file tree
Hide file tree
Showing 2 changed files with 234 additions and 140 deletions.
302 changes: 163 additions & 139 deletions src/Microsoft.Dnx.Tooling/Building/BuildManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
using System.Linq;
using Microsoft.Dnx.Compilation;
using Microsoft.Dnx.Compilation.Caching;
using Microsoft.Dnx.Compilation.FileSystem;
using Microsoft.Dnx.Runtime;
using Microsoft.Dnx.Runtime.Compilation;
using Microsoft.Dnx.Tooling.SourceControl;
using Microsoft.Dnx.Tooling.Utils;
using Microsoft.Framework.FileSystemGlobbing;
Expand Down Expand Up @@ -127,192 +125,208 @@ private bool BuildInternal(string projectPath)
return false;
}

if (_buildOptions.GeneratePackages &&
!ScriptExecutor.Execute(_currentProject, "prepack", GetScriptVariable))
var success = true;

var allDiagnostics = new List<DiagnosticMessage>();

// Build all specified configurations
foreach (var configuration in configurations)
{
LogError(ScriptExecutor.ErrorMessage);
return false;
success &= BuildConfiguration(baseOutputPath, frameworks, allDiagnostics, configuration);
}

if (!ScriptExecutor.Execute(_currentProject, "prebuild", GetScriptVariable))
sw.Stop();

if (projectDiagnostics.Any())
{
LogError(ScriptExecutor.ErrorMessage);
return false;
}
// Add a new line to separate the project diagnostics information from compilation diagnostics
_buildOptions.Reports.Information.WriteLine();

var success = true;
projectDiagnostics.ForEach(d => LogWarning(d.FormattedMessage));
}

var allDiagnostics = new List<DiagnosticMessage>();
allDiagnostics.AddRange(projectDiagnostics);
WriteSummary(allDiagnostics);

_buildOptions.Reports.Information.WriteLine("Time elapsed {0}", sw.Elapsed);
return success;
}

private bool BuildConfiguration(string baseOutputPath, IEnumerable<System.Runtime.Versioning.FrameworkName> frameworks, List<DiagnosticMessage> allDiagnostics, string configuration)
{
PackageBuilder packageBuilder = null;
PackageBuilder symbolPackageBuilder = null;
InstallBuilder installBuilder = null;
SourceBuilder sourceBuilder = null;

// Build all specified configurations
foreach (var configuration in configurations)
if (_buildOptions.GeneratePackages)
{
if (_buildOptions.GeneratePackages)
{
// Create a new builder per configuration
packageBuilder = new PackageBuilder();
symbolPackageBuilder = new PackageBuilder();
InitializeBuilder(_currentProject, packageBuilder);
InitializeBuilder(_currentProject, symbolPackageBuilder);
installBuilder = new InstallBuilder(_currentProject, packageBuilder, _buildOptions.Reports);
sourceBuilder = new SourceBuilder(_currentProject, packageBuilder, _buildOptions.Reports);
}
// Create a new builder per configuration
packageBuilder = new PackageBuilder();
symbolPackageBuilder = new PackageBuilder();
InitializeBuilder(_currentProject, packageBuilder);
InitializeBuilder(_currentProject, symbolPackageBuilder);
installBuilder = new InstallBuilder(_currentProject, packageBuilder, _buildOptions.Reports);
sourceBuilder = new SourceBuilder(_currentProject, packageBuilder, _buildOptions.Reports);
}

var configurationSuccess = true;
var success = true;

var outputPath = Path.Combine(baseOutputPath, configuration);
var outputPath = Path.Combine(baseOutputPath, configuration);

// Build all target frameworks a project supports
foreach (var targetFramework in frameworks)
{
_buildOptions.Reports.Information.WriteLine();
_buildOptions.Reports.Information.WriteLine("Building {0} for {1}",
_currentProject.Name, targetFramework.ToString().Yellow().Bold());
// Build all target frameworks a project supports
foreach (var targetFramework in frameworks)
{
_buildOptions.Reports.Information.WriteLine();
_buildOptions.Reports.Information.WriteLine("Building {0} for {1}",
_currentProject.Name, targetFramework.ToString().Yellow().Bold());

var diagnostics = new List<DiagnosticMessage>();
if (!RunPreBuildScripts())
{
success = false;
continue;
}

var context = new BuildContext(_compilationEngine,
_currentProject,
targetFramework,
configuration,
outputPath);
var diagnostics = new List<DiagnosticMessage>();
var context = new BuildContext(_compilationEngine,
_currentProject,
targetFramework,
configuration,
outputPath);

context.Initialize(_buildOptions.Reports.Quiet);
context.Initialize(_buildOptions.Reports.Quiet);

if (context.Build(diagnostics))
if (context.Build(diagnostics))
{
if (_buildOptions.GeneratePackages)
{
if (_buildOptions.GeneratePackages)
{
context.PopulateDependencies(packageBuilder);
context.AddLibs(packageBuilder, "*.dll");
context.AddLibs(packageBuilder, "*.xml");
context.PopulateDependencies(packageBuilder);
context.AddLibs(packageBuilder, "*.dll");
context.AddLibs(packageBuilder, "*.xml");

context.PopulateDependencies(symbolPackageBuilder);
context.AddLibs(symbolPackageBuilder, "*.*");
context.PopulateDependencies(symbolPackageBuilder);
context.AddLibs(symbolPackageBuilder, "*.*");

context.AddLibs(packageBuilder, "*.resources.dll", recursiveSearch: true);
}
context.AddLibs(packageBuilder, "*.resources.dll", recursiveSearch: true);
}
else
{
configurationSuccess = false;
}

allDiagnostics.AddRange(diagnostics);

WriteDiagnostics(diagnostics);
// Run post-build steps
success &= RunPostBuildScripts();
}
else
{
success = false;
}

success = success && configurationSuccess;
allDiagnostics.AddRange(diagnostics);

WriteDiagnostics(diagnostics);
}

if (_buildOptions.GeneratePackages)
{
success = success &&
// Generates the application package only if this is an application packages
installBuilder.Build(outputPath) &&
sourceBuilder.Build(outputPath);

if (_buildOptions.GeneratePackages)
if (success)
{
// Create a package per configuration
string nupkg = GetPackagePath(_currentProject, outputPath);
string symbolsNupkg = GetPackagePath(_currentProject, outputPath, symbols: true);
var nupkg = GetPackagePath(_currentProject, outputPath);
var symbolsNupkg = GetPackagePath(_currentProject, outputPath, symbols: true);

if (configurationSuccess)
{
// Generates the application package only if this is an application packages
configurationSuccess = installBuilder.Build(outputPath);
success = success && configurationSuccess;
}
return GeneratePackage(success, allDiagnostics, packageBuilder, symbolPackageBuilder, nupkg, symbolsNupkg);
}
}

if (configurationSuccess)
{
configurationSuccess = sourceBuilder.Build(outputPath);
success = success && configurationSuccess;
}
return success;
}

if (configurationSuccess)
{
var packDiagnostics = new List<DiagnosticMessage>();
foreach (var sharedFile in _currentProject.Files.SharedFiles)
{
var file = new PhysicalPackageFile();
file.SourcePath = sharedFile;
file.TargetPath = String.Format(@"shared\{0}", Path.GetFileName(sharedFile));
packageBuilder.Files.Add(file);
}
private bool RunPreBuildScripts()
{
if (_buildOptions.GeneratePackages &&
!ScriptExecutor.Execute(_currentProject, "prepack", GetScriptVariable))
{
LogError(ScriptExecutor.ErrorMessage);
return false;
}

var root = _currentProject.ProjectDirectory;
if (!ScriptExecutor.Execute(_currentProject, "prebuild", GetScriptVariable))
{
LogError(ScriptExecutor.ErrorMessage);
return false;
}

if (_currentProject.Files.PackInclude != null && _currentProject.Files.PackInclude.Any())
{
AddPackageFiles(_currentProject.ProjectDirectory, _currentProject.Files.PackInclude, packageBuilder, packDiagnostics);
}
success &= !packDiagnostics.HasErrors();
allDiagnostics.AddRange(packDiagnostics);
return true;
}

foreach (var path in _currentProject.Files.SourceFiles)
{
var srcFile = new PhysicalPackageFile();
srcFile.SourcePath = path;
srcFile.TargetPath = Path.Combine("src", PathUtility.GetRelativePath(root, path));
symbolPackageBuilder.Files.Add(srcFile);
}

// Write the packages as long as we're still in a success state.
if (success)
{
using (var fs = File.Create(nupkg))
{
packageBuilder.Save(fs);
_buildOptions.Reports.Quiet.WriteLine("{0} -> {1}", _currentProject.Name, Path.GetFullPath(nupkg));
}

if (symbolPackageBuilder.Files.Any())
{
using (var fs = File.Create(symbolsNupkg))
{
symbolPackageBuilder.Save(fs);
_buildOptions.Reports.Quiet.WriteLine("{0} -> {1}", _currentProject.Name, Path.GetFullPath(symbolsNupkg));
}
}
}

WriteDiagnostics(packDiagnostics);
}
}
private bool RunPostBuildScripts()
{
if (!ScriptExecutor.Execute(_currentProject, "postbuild", GetScriptVariable))
{
LogError(ScriptExecutor.ErrorMessage);
return false;
}

// Run post-build steps
if (success)
if (_buildOptions.GeneratePackages &&
!ScriptExecutor.Execute(_currentProject, "postpack", GetScriptVariable))
{
if (!ScriptExecutor.Execute(_currentProject, "postbuild", GetScriptVariable))
{
LogError(ScriptExecutor.ErrorMessage);
success = false;
}
LogError(ScriptExecutor.ErrorMessage);
return false;
}

if (_buildOptions.GeneratePackages &&
!ScriptExecutor.Execute(_currentProject, "postpack", GetScriptVariable))
{
LogError(ScriptExecutor.ErrorMessage);
success = false;
}
return true;
}

private bool GeneratePackage(bool success, List<DiagnosticMessage> allDiagnostics, PackageBuilder packageBuilder, PackageBuilder symbolPackageBuilder, string nupkg, string symbolsNupkg)
{
var packDiagnostics = new List<DiagnosticMessage>();
foreach (var sharedFile in _currentProject.Files.SharedFiles)
{
var file = new PhysicalPackageFile();
file.SourcePath = sharedFile;
file.TargetPath = String.Format(@"shared\{0}", Path.GetFileName(sharedFile));
packageBuilder.Files.Add(file);
}

sw.Stop();
var root = _currentProject.ProjectDirectory;

if (projectDiagnostics.Any())
if (_currentProject.Files.PackInclude != null && _currentProject.Files.PackInclude.Any())
{
// Add a new line to separate the project diagnostics information from compilation diagnostics
_buildOptions.Reports.Information.WriteLine();
AddPackageFiles(_currentProject.ProjectDirectory, _currentProject.Files.PackInclude, packageBuilder, packDiagnostics);
}
success &= !packDiagnostics.HasErrors();
allDiagnostics.AddRange(packDiagnostics);

projectDiagnostics.ForEach(d => LogWarning(d.FormattedMessage));
foreach (var path in _currentProject.Files.SourceFiles)
{
var srcFile = new PhysicalPackageFile();
srcFile.SourcePath = path;
srcFile.TargetPath = Path.Combine("src", PathUtility.GetRelativePath(root, path));
symbolPackageBuilder.Files.Add(srcFile);
}

allDiagnostics.AddRange(projectDiagnostics);
WriteSummary(allDiagnostics);
// Write the packages as long as we're still in a success state.
if (success)
{
using (var fs = File.Create(nupkg))
{
packageBuilder.Save(fs);
_buildOptions.Reports.Quiet.WriteLine("{0} -> {1}", _currentProject.Name, Path.GetFullPath(nupkg));
}

_buildOptions.Reports.Information.WriteLine("Time elapsed {0}", sw.Elapsed);
if (symbolPackageBuilder.Files.Any())
{
using (var fs = File.Create(symbolsNupkg))
{
symbolPackageBuilder.Save(fs);
_buildOptions.Reports.Quiet.WriteLine("{0} -> {1}", _currentProject.Name, Path.GetFullPath(symbolsNupkg));
}
}
}

WriteDiagnostics(packDiagnostics);
return success;
}

Expand Down Expand Up @@ -414,6 +428,16 @@ private string GetScriptVariable(string key)
return GetBuildOutputDir(_currentProject);
}

//if (string.Equals("Configuration", key, StringComparison.OrdinalIgnoreCase))
//{
// return configuration;
//}

//if (string.Equals("Framework", key, StringComparison.OrdinalIgnoreCase))
//{
// return targetFramework;
//}

return null;
}

Expand Down
Loading

0 comments on commit 6864fc6

Please sign in to comment.