Introduction
A while ago, I was developing a project with a large number of components. The problem was that each time we need to compile the entire project, we need to download each individual component project from our source control tool, change the projects to release build configuration and compile them. It's an easy task, but it can become very time consuming if you have to compile about 10 components a day. The other problem I faced was that each project has its own target folder configuration, and I wanted to compile all the projects to the same target folder. To solve this problem, I created this handy little tool. It'll search all C# or VB.NET projects in a specified folder and rebuild them. There are some options available in the tool, that are described below.
The Project option
In the Project option, you'll be able to select the project type you want to search/rebuild. You can select C# or VB.NET projects. You'll also need to select the path where your project files are located (E.g.: \My Documents\Visual Studio Projects). The tool will recurse in to the subfolders of the specified path, looking for .csproj or .vbproj files. There's another option here, the Version option, which allows you to select the kind of compilation you want to use: Release or Debug.
The Destination options
In this section, you can configure the destination folder of your assemblies. If you check the first check box, the tool will update the configuration for the target folder of your project files with the folder specified (this change will be permanent, since we need to save the project file in order to compile it). You can also check the "Copy Content Option" checkbox, which is a very handy option for ASP.NET projects. This option will copy all the content files found in the project's folder to the specified folder (there is a fixed list of extensions, of files considered "content files").
Logging Options
In this section, you can specify the path to save the log files. Batch Rebuilder creates a log for each project it compiles, using the format: myprojectname_build.txt. By default, it will only save log files for projects that didn't compile successfully. You can change this option by unchecking the check box.
How does it work?
The project is very simple, it'll recurse in the subfolders of the specified project folder looking for .vbproj or .csproj files using the GetFiles
function of the System.IO.Directory
class. When it finds one, it will compile it using devenv.exe.
The devenv call is invoked using the Process
class that is available in the System.Diagnostics
namespace. The first thing we need to do is create a new instance of this class.
Process oProcess = new Process();
If the user checked the option of copying the assemblies to a different folder, we'll open the project file and change the corresponding compilation target folder to the desired folder. Since the Visual Studio project files are XML documents, we'll use the XmlDocument
class to update the project file with the new path.
XmlDocument oPrjFile = new XmlDocument(); oPrjFile.Load(sFile);
XmlElement oNode = (XmlElement)
oPrjFile.SelectSingleNode("/VisualStudioProject/" +
"VisualBasic/Build/Settings/Config[@Name='"
+ cboVersion.SelectedItem.ToString() + "']");
if(chkCopyAssemblys.Checked)
{
oNode.Attributes["OutputPath"].Value = txtAssemblysTargetPath.Text;
}
oPrjFile.Save(sFile);
Now that the project file is set, we'll set the Process
class properties to execute devenv.exe and compile the project. The first thing to do is to specify where devenv is. This is accomplished by reading a registry entry that indicates where Visual Studio is installed.
Next, we specify the arguments that need to be informed to devenv. The /out argument specifies the log file that will be created, and the /rebuild option indicates which version we want to rebuild (Release or Debug).
oProcess.StartInfo.FileName = sDevEnvPath;
if(cboVersion.SelectedIndex == 0)
oProcess.StartInfo.Arguments = "/rebuild RELEASE ";
else
oProcess.StartInfo.Arguments = "/rebuild DEBUG ";
oProcess.StartInfo.Arguments += " /out \"" + txtLogPath.Text +
sProjectName + "_build.txt\" \"" + sFile + "\"";
oProcess.StartInfo.RedirectStandardOutput = true;
oProcess.StartInfo.UseShellExecute = false;
oProcess.Start();
After the compilation finishes, we test the exit code of devenv. If anything goes wrong with the compilation, the return code will be different than zero. In this case, we'll maintain the log file for further analysis.
if(oProcess.ExitCode != 0)
txtOutput.AppendText("** Error compiling project!! ** " + "\n");
else {
if(chkLogOnlyOnFail.Checked)
System.IO.File.Delete(txtLogPath.Text + sProjectName + "_build.txt");
string sProjectPath = Path.GetDirectoryName(sFile);
}
Final words
This tool was created to solve a specific problem (my problem, in this case), and I know that its functionality is very limited. I think that this tool can do a lot more, so... I'm up to suggestions. If anyone has any suggestions, just drop me a note!