While running my msbuild instead of building the project , it skips the file and gives an out put as : Output: Skipping target because all output files are up-to-date with respect to the input files while running msbuild from console app Output: Running generator on: D:\git\abc\gen.xsl\MediaInputIdMap.cs.xsl What i have noticed is while execution it invokes default cmd.exe instead of 2017 cmd.exe even when i have changed the default to %comspec% /k "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsDevCmd.bat" in Tools > Options > Terminal , how do i invoke a 2017 command prompt while executing my console app?, it is still picking up cmd.exe
class Program
{
public static async Task Main(string[] args)
{
log4net.Config.BasicConfigurator.Configure();
log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
bool retvalue = false;
string LogPath = @"D:\Logs\";
string solutionpath = @"D:\git\abc\Build\Tools";
string msBuildPath = Path.Combine(Path.Combine(GetFolderPath(SpecialFolder.ProgramFilesX86)), @"Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin");
string solutionFilename = solutionpath;
Directory.SetCurrentDirectory(solutionFilename);
Console.WriteLine("Printing current working directory :{0} \n\n\n ", Environment.CurrentDirectory);
string RMPath = @"D:\git\abc\RMS";
string RMSolution = ResourceManagerPath;
try
{
if (Directory.Exists(LogPath))
{
log.Info("Log Directory Exists, Starting execution");
Console.WriteLine(LogPath, "Directory Exists {0}");
log.Debug("Checking for Log Directory, It Exists !!");
}
else
{
Console.WriteLine(LogPath, "Doesn't Exists, Creating the Folder");
DirectoryInfo LogPathfolder = Directory.CreateDirectory(LogPath);
log.Info("Creating LogPath Folder");
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
log.Error(e.Message);
}
try
{
log.Info("SETUP");
Console.WriteLine("Running Setup Commands: \n\n\n");
retvalue = RunMSBuild(msBuildPath, "/t:setup");
log.Debug(retvalue);
if (retvalue)
{
log.Info("Build");
Console.WriteLine("Running Build Commands: \n\n\n");
retvalue = RunMSBuild(msBuildPath, "/t:debug;pbuild");
log.Debug(retvalue);
if (retvalue)
{
log.Info("Build");
Console.WriteLine("Running Build Commands: \n\n\n");
retvalue = RunMSBuild(msBuildPath, "/t:debug;mpbuild /m");
log.Debug(retvalue);
if (retvalue)
{
log.Info("Build");
Console.WriteLine("Running Build Commands: \n\n\n");
retvalue = RunMSBuild(msBuildPath, "/t:debug;pbuild /m");
log.Debug(retvalue);
if (retvalue)
{
log.Info("Nuget Package Restore");
Console.WriteLine("Restoring Nuget Packages for abc.sln \n\n\n\n");
Directory.SetCurrentDirectory(RMSolution);
Console.WriteLine("Running Dotnet Restore");
var stdOutDR = new StringBuilder();
var stdErrDR = new StringBuilder();
var DotRest = await Cli.Wrap("dotnet")
.WithArguments("restore")
.WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutDR))
.WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrDR))
.ExecuteBufferedAsync();
var stdOut1 = stdOutDR.ToString();
var stdErr1 = stdErrDR.ToString();
Console.WriteLine("Dotnet Restore Nuget Package Completed : \n {0}", stdOut1);
Console.WriteLine("Dotnet Restore Nuget Package Error : \n {0}", stdErr1);
var stdOutNR = new StringBuilder();
var stdErrNR = new StringBuilder();
var NugRest = await Cli.Wrap("nuget")
.WithArguments("restore")
.WithStandardOutputPipe(PipeTarget.ToStringBuilder(stdOutNR))
.WithStandardErrorPipe(PipeTarget.ToStringBuilder(stdErrNR))
.ExecuteBufferedAsync();
var stdOut2 = stdOutNR.ToString();
var stdErr2 = stdErrNR.ToString();
Console.WriteLine("Nuget Restore Packages");
Console.WriteLine("Nuget Restore completed : \n {0}",stdOut2);
Console.WriteLine("Nuget Restore Error : \n {0}",stdErr2);
log.Info("ResourceManagerMS");
Console.WriteLine("Building ResourceManager.sln");
retvalue = RunMSBuild(msBuildPath, "abc.sln");
log.Debug(retvalue);
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
log.Error(ex.Message);
}
Console.WriteLine("Build completed");
Console.ReadKey();
}
private static bool RunMSBuild(string msBuildPath, string arguments = null)
{
try
{
//get tools path for newest Visual Studio version
string msBuildFilename = Path.Combine(msBuildPath, "MSBuild.exe");
if (!File.Exists(msBuildFilename))
throw new Exception($"Error: MSBuild.exe not found ({msBuildFilename})");
ProcessStartInfo startInfo = new ProcessStartInfo(msBuildFilename)
{
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Normal
};
using (Process p = new Process() { StartInfo = startInfo, EnableRaisingEvents = true })
{
//subscribe to event and add event handler code
p.ErrorDataReceived += (sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
//ToDo: add desired code
Console.WriteLine("Error: " + e.Data);
Debug.WriteLine("Error: " + e.Data);
}
};
//subscribe to event and add event handler code
p.OutputDataReceived += (sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
//ToDo: add desired code
Console.WriteLine("Output: " + e.Data);
Debug.WriteLine("Output: " + e.Data);
}
};
//start
p.Start();
p.BeginErrorReadLine(); //begin async reading for standard error
p.BeginOutputReadLine(); //begin async reading for standard output
//waits until the process is finished before continuing
p.WaitForExit();
p.CancelErrorRead(); //cancel async reading for standard error
p.CancelOutputRead(); //cancel async reading for standard output
return true;
}
}
catch(Exception ex)
{
Console.WriteLine("Error {0}",ex.Message);
return false;
}
}
}
}