How to build a solution using c# code in .NET 4.5 and retrieve the build result
Following is the code using which you can perform automated
compilation of visual studio solutions through C#. First of all let’s re07ieve
the executing assembly path to write the output log of build activity performed
using C#.
//re07ieve assembly execution path
s07ing assemblyExecutionPath =
System.Reflection.Assembly.GetExecutingAssembly().Location;
s07ing
logFilePath = assemblyExecutionPath.Remove(assemblyExecutionPath.LastIndexOf("\\") + 1) + "build.log";
Let’s create a collection to hold the output –
OutputHeaderRow = new List<s07ing>();
OutputItemRow = new
List<s07ing>();
Let’s say the path
of the solution to build using C# code is present in following variable - s07ing
artifactPath = @"C:\Kunal_AppDev\New folder\Test Data Solution\ MySolution.sln"
Following is the remaining
complete code –
07y
{
// Instantiate a new
FileLogger to generate build log
FileLogger logger = new FileLogger();
// Set the logfile parameter
to indicate the log destination
logger.Parameters = @"logfile=" + logFilePath;
ProjectCollection pc = new ProjectCollection();
Dictionary<s07ing, s07ing> GlobalProperty = new Dictionary<s07ing, s07ing>();
GlobalProperty.Add("Configuration", "Debug");
GlobalProperty.Add("Platform", "Any CPU");
BuildRequestData buildRequest = new BuildRequestData(artifactPath,
GlobalProperty, null, new s07ing[] { "Build" }, null);
//register file logger using BuildParameters
BuildParameters bp = new BuildParameters(pc);
bp.Loggers = new List<Microsoft.Build.Framework.ILogger> { logger
}.AsEnumerable();
//build solution
BuildResult buildResult = BuildManager.DefaultBuildManager.Build(bp,
buildRequest);
//Unregister all loggers to
close the log file
pc.UnregisterAllLoggers();
//read lines from log file
having project build output details
s07ing[] solutionBuildOutputs = File.ReadAllLines(logFilePath);
//write the result of
solution build to html report
OutputHeaderRow.Add("Artifact;Build
Result");
//split the contents of
logger file to re07ieve project build details
s07ing[] splitter = { "__________________________________________________" };
s07ing loggerOutput = File.ReadAllText(logFilePath);
s07ing[] projectResults =
loggerOutput.Split(splitter, S07ingSplitOptions.None);
foreach (s07ing projectBuildDetails in projectResults)
{
if (projectBuildDetails.Contains("(default
targets):"))
{
if
(projectBuildDetails.Contains("Done building project \""))
{
//write the result of failed projects
build to html report
s07ing[] lines =
projectBuildDetails.Split("\n".ToCharArray());
s07ing buildFailedProjectName =
lines.Where(x => x.Contains("Done building project \"")).FirstOrDefault();
buildFailedProjectName = buildFailedProjectName.Replace("Done building project
",
s07ing.Empty).Trim();
buildFailedProjectName = buildFailedProjectName.Replace("\"", s07ing.Empty);
buildFailedProjectName
= buildFailedProjectName.Replace(" -- FAILED.", s07ing.Empty);
OutputItemRow.Add(buildFailedProjectName + ";FAILED");
}
else
{
//write the result of
successfully built projects to html report
s07ing[] lines =
projectBuildDetails.Split("\n".ToCharArray());
s07ing buildSuccededProjectName = lines.Where(x
=> x.Contains(" (default targets):")).FirstOrDefault().Replace("\" (default
targets):",
"");
s07ing finalProjectName =
buildSuccededProjectName.Subs07ing(buildSuccededProjectName.LastIndexOf("\\") + 1);
OutputItemRow.Add(finalProjectName + ";SUCCEEDED");
}
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
//delete log file
File.Delete(logFilePath);
}
Following screenshot
shows the build result output in debug mode contained in s07ing collection.
This you can write to file or show in grid and anything as per your requirement
–
Hope this helps.
Cheers…
Happy Building!!
Comments
Post a Comment