|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThe weak link in many software projects is the final build. Too often we believe we're done when the last line of code compiles on our machine. But there's still work to be done, tying up all the loose ends so that someone else can extract the code from version control and build it. And just because someone else can extract and build the project doesn't mean it is pleasant or easy. The chore of mapping your Team Foundation Server (TFS) workspace to a location on your disk further adds to the pain of building your code in a clean test environment. The process of extracting and building the code should be automated to ensure that the process is reproducible. Automation also makes the process faster and easier, though that is secondary. In this article, we describe a PowerShell script that we have used to automate extracting application source code from Visual Studio Team System and building a Visual Studio .NET solution. Why not MSBuild/TeamBuild?So why didn't we just use the MSBuild/TeamBuild tools instead of writing a PowerShell script? We wanted a build tool that was lightweight and easy for developers to use. This script can be used on any machine that has Visual Studio .NET installed, and it doesn't require a Team Build Server to be set up. The developer can watch the build happen in real time. We also wanted to experiment with PowerShell. We have used this script as part of our deployment process for ASP.NET applications for over a year and it has served us well. PowerShell BackgroundIf you do not have PowerShell installed, download it from Microsoft. Before running any PowerShell script, you must set the execution policy. For example, entering You might also enjoy the PowerShell tools from the PowerShell Community Extensions (PSCX) available here. This library adds many worthwhile tools to your PowerShell installation. One simple but useful addition is the ability to right-click on a folder in Windows Explorer and select "Open PowerShell Here." PowerShell requires an explicit path to scripts. This security feature prevents someone from running a malicious script with the same name as a familiar script but with a different location. This means that you cannot run a script foo.ps1 from your current directory simply by typing its name. You must specify .\foo.ps1. Using the CodeWhat the Script DoesThe PowerShell script BuildMe.ps1 takes a small XML configuration file as input. We will call this file BuildMe.xml here, but it can be named anything you like. The PowerShell script:
How to Run the ScriptThe input file can be specified to the script in three ways:
To run the script, do the following:
The output from the Visual Studio .NET compiler is written to BuildMe.xml.SolutionBuild.log if the script runs with the default input file in the working directory. If a different input file name is specified, the log file name will reflect the name of the input file with the SolutionBuild.log NB: You can only have one TFS workspace at a time mapped to a directory location. If you try to build a second project in a location mapped to the workspace of another project, the script will print out a warning saying that Preparing the Input FileThe project(s) to build are specified in the input file as follows. The example project below is for a fictitious TFS project called BuildMe.xml<Solution>
<SlnToBuild> .\BEETLE\BEETLE_sln\BEETLE.sln </SlnToBuild>
<ConfigToBuild> Release </ConfigToBuild>
<BuildBitsOutputFolder>
.\BEETLE\BEETLE_sln\projects\BEETLE_Setup\($ConfigToBuild)
</BuildBitsOutputFolder>
<MappedDrive>
<Drive> M: </Drive>
<MapTo> \\myServer\myShare\myDir </MapTo>
</MappedDrive>
<TFSproject >
<ProjName> $/BEETLE </ProjName>
<ProjLocalDir> .\BEETLE\code </ProjLocalDir>
<ProjTreeToGet> $/BEETLE </ProjTreeToGet>
<Label> v0.6 </Label>
</TFSproject>
</Solution>
The
The The ExamplesPSH> set-executionPolicy unrestricted
This relaxes the default security setting in PowerShell, allowing your code to run. You need to do this only once per machine. It will remain in effect thereafter. PSH> .\BuildMe.ps1
Runs the PowerShell script. It will read the default input file (BuildMe.xml) in the current directory. The TFS project(s) described in the input file will be extracted from TFS and built at the location described in the input file. PSH> .\BuildMe.ps1 MyProject.xml
Runs the PowerShell script. It will read the specified input file ( PSH> .\BuildMe.ps1 $/BETTLE/BuildMe.xml
Runs the PowerShell script. It will get the input file, BuildMe.xml, from the $/BEETLE TFS project and place it in the current directory. The input file will then be read and the TFS project(s) described in the input file will be extracted from TFS. The project(s) will then be built at the location described in the input file. NOTE: The PSH> .\BuildMe.ps1 $/BETTLE/BuildMe.xml v1.2
Same as above except the input file having label "v2.1" will be extracted from TFS. The label(s) in the input file will be used to extract the other source code. Points of InterestWe found the TFS .NET class libraries to be very thorough, but also rather undocumented and difficult to use. The PowerShell code shown here illustrates the use of the TFS API for several interesting operations. Special thanks goes to James Manning whose blog helped us with the TFS API. History
|
|||||||||||||||||||||||||||||||||||