using System; using System.Windows.Forms; using Microsoft.TeamFoundation.Build.Client; using Microsoft.TeamFoundation.Build.Common; using Microsoft.TeamFoundation.Client; using Microsoft.TeamFoundation.VersionControl.Client; using Microsoft.TeamFoundation.VersionControl.Common; namespace BuildManager { class Program { #region Constants const string TEAM_PROJECT = "TestAgile"; const string SERVER_URL = "tfs2008b2"; #endregion #region Helper methods private static T GetService() { return (T)GetTeamFoundationServer().GetService(typeof(T)); } private static TeamFoundationServer GetTeamFoundationServer() { TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(SERVER_URL, new UICredentialsProvider()); tfs.Authenticate(); return tfs; } #endregion static void Main(string[] args) { IBuildDefinition buildDefinition = CreateBuildDefinition(); buildDefinition.Save(); } private static IBuildDefinition CreateBuildDefinition() { IBuildDefinition buildDefinition = GetService().CreateBuildDefinition(TEAM_PROJECT); buildDefinition.Name = string.Format("{0}{1}", TEAM_PROJECT, Guid.NewGuid().ToString()); buildDefinition.ConfigurationFolderPath = string.Format("$/{0}/Testing", TEAM_PROJECT); buildDefinition.DefaultBuildAgent = GetSingleBuildAgent(TEAM_PROJECT); buildDefinition.DefaultDropLocation = string.Format(@"\\{0}\Drops", buildDefinition.DefaultBuildAgent.MachineName); if (!ProjectFileExists(buildDefinition.ConfigurationFolderPath)) { IProjectFile projectFile = buildDefinition.CreateProjectFile(); //Optionally set a few properties on the project file. //projectFile.RunCodeAnalysis = CodeAnalysisRunType.Always; //projectFile.RunTest = true; projectFile.Save(buildDefinition.ConfigurationFolderPath); } return buildDefinition; } /// /// Determine whether a TFSBuild.proj file exists in the specified build configuration path /// /// private static bool ProjectFileExists(string configurationFolderPath) { string projectFilePath = VersionControlPath.Combine(configurationFolderPath, BuildConstants.ProjectFileName); return GetService().ServerItemExists(projectFilePath, VersionSpec.Latest, DeletedState.NonDeleted, ItemType.File); } /// /// Gets the first build agent for specified team project. /// Creates and gets a dummy build agent if none are found. /// /// /// private static IBuildAgent GetSingleBuildAgent(string teamProject) { IBuildServer buildServer = GetService(); IBuildAgent[] buildAgents = buildServer.QueryBuildAgents(teamProject); if (buildAgents.Length == 0) { buildAgents = buildServer.SaveBuildAgents(new IBuildAgent[] { GetDummyBuildAgent(teamProject) }); } return buildAgents[0]; } /// /// Creates a build agent for the specified team project /// using the current computer name and the default build directory. /// /// /// The newly created build agent private static IBuildAgent GetDummyBuildAgent(string teamProject) { IBuildAgent buildAgent = GetService().CreateBuildAgent(teamProject); buildAgent.Name = SystemInformation.ComputerName; buildAgent.MachineName = SystemInformation.ComputerName; buildAgent.BuildDirectory = @"$(Temp)\$(BuildDefinitionPath)"; buildAgent.Description = "Dummy build agent - may not actually exist."; return buildAgent; } } }