public void BranchBuildDefinition(BuildDefnDetails buildDetail)
        {
            IBuildDefinition defn = buildServer.GetBuildDefinition(buildDetail.TeamProjectName, buildDetail.Name);
            if(defn.GetDefaultSourceProvider().Name != "TFVC")
            {
                MessageBox.Show("Branch build definition is only supported for Team Foundation Version Controlled builds", "Microsoft Visual Studio", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            List<WorkspaceBranchMapping> originalMappings =
                (from mapping in defn.Workspace.Mappings select new WorkspaceBranchMapping(mapping.ServerItem, mapping.ServerItem)).ToList();

            VersionControlServer versionControlServer = (VersionControlServer)tfsConnection.ProjectCollection.GetService(typeof(VersionControlServer));
            WorkSpaceMappingDialog dlg = new WorkSpaceMappingDialog(originalMappings, versionControlServer);
            dlg.Text = string.Format("Branch Build - {0}", defn.Name);

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                IBuildDefinition branchedDefn = buildServer.CreateBuildDefinition(buildDetail.TeamProjectName);
                branchedDefn.CopyFrom(defn);
                branchedDefn.Name = string.Format("Branch of {0}", buildDetail.Name);

                // Update new build workspace
                foreach (IWorkspaceMapping mapping in branchedDefn.Workspace.Mappings)
                {
                    foreach(WorkspaceBranchMapping branchedMapping in dlg.BranchedMappings)
                    {
                        string pattern = branchedMapping.SourceServerPath.Replace("$", @"\$");
                        mapping.ServerItem = Regex.Replace(mapping.ServerItem, pattern, branchedMapping.TargetServerPath, RegexOptions.IgnoreCase);
                    }
                }

                // Update process parameters
                foreach (WorkspaceBranchMapping branchedMapping in dlg.BranchedMappings)
                {
                    string pattern = branchedMapping.SourceServerPath.Replace("$", @"\$");
                    branchedDefn.ProcessParameters = Utilities.ReplaceTFSServerPaths(branchedDefn.ProcessParameters, branchedMapping.SourceServerPath, branchedMapping.TargetServerPath);
                    //Regex.Replace(branchedDefn.ProcessParameters, pattern, branchedMapping.TargetServerPath, RegexOptions.IgnoreCase);
                    //branchedDefn.Process.ServerPath = Regex.Replace(branchedDefn.ProcessParameters, pattern, branchedMapping.TargetServerPath, RegexOptions.IgnoreCase);
                }

                try
                {
                    branchedDefn.Save();
                }
                catch (BuildDefinitionAlreadyExistsException ex)
                {
                    MessageBox.Show(string.Format("Team Foundation Error\n\r\n\r{0}", ex.Message), "Microsoft Visual Studio", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (vsTfsBuild != null)
                {
                    vsTfsBuild.DefinitionManager.OpenDefinition(branchedDefn.Uri);
                }

                teamExplorer.CurrentPage.Refresh();
            }
        }
        public override void Exec(object sender, EventArgs e)
        {
            BuildDefnDetails buildDefnDetail = new BuildDefnDetails() { Name = string.Empty, TeamProjectName = string.Empty };
            try
            {
                List<BuildDefnDetails> selectedItems = tfsBuildService.SelectedBuildDefinitions;
                if (selectedItems.Count == 1 && tfsBuildService.Connect())
                {
                    buildDefnDetail = selectedItems[0];
                    logger.Log(string.Format("Branch build defintion {0} in project {1}", buildDefnDetail.Name, buildDefnDetail.TeamProjectName), LogLevel.Verbose);

                    tfsBuildService.BranchBuildDefinition(buildDefnDetail);
                }
            }
            catch (Exception ex)
            {
                logger.LogError(string.Format("Error branching build {0} in project {1}", buildDefnDetail.Name, buildDefnDetail.TeamProjectName), ex);
            }
        }
Beispiel #3
0
        public void BranchBuildDefinition(BuildDefnDetails buildDetail)
        {
            IBuildDefinition defn = buildServer.GetBuildDefinition(buildDetail.TeamProjectName, buildDetail.Name);

            if (defn.GetDefaultSourceProvider().Name != "TFVC")
            {
                MessageBox.Show("Branch build definition is only supported for Team Foundation Version Controlled builds", "Microsoft Visual Studio", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            List <WorkspaceBranchMapping> originalMappings =
                (from mapping in defn.Workspace.Mappings select new WorkspaceBranchMapping(mapping.ServerItem, mapping.ServerItem)).ToList();

            VersionControlServer   versionControlServer = (VersionControlServer)tfsConnection.ProjectCollection.GetService(typeof(VersionControlServer));
            WorkSpaceMappingDialog dlg = new WorkSpaceMappingDialog(originalMappings, versionControlServer);

            dlg.Text = string.Format("Branch Build - {0}", defn.Name);

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                IBuildDefinition branchedDefn = buildServer.CreateBuildDefinition(buildDetail.TeamProjectName);
                branchedDefn.CopyFrom(defn);
                branchedDefn.Name = string.Format("Branch of {0}", buildDetail.Name);

                // Update new build workspace
                foreach (IWorkspaceMapping mapping in branchedDefn.Workspace.Mappings)
                {
                    foreach (WorkspaceBranchMapping branchedMapping in dlg.BranchedMappings)
                    {
                        string pattern = branchedMapping.SourceServerPath.Replace("$", @"\$");
                        mapping.ServerItem = Regex.Replace(mapping.ServerItem, pattern, branchedMapping.TargetServerPath, RegexOptions.IgnoreCase);
                    }
                }

                // Update process parameters
                foreach (WorkspaceBranchMapping branchedMapping in dlg.BranchedMappings)
                {
                    string pattern = branchedMapping.SourceServerPath.Replace("$", @"\$");
                    branchedDefn.ProcessParameters = Utilities.ReplaceTFSServerPaths(branchedDefn.ProcessParameters, branchedMapping.SourceServerPath, branchedMapping.TargetServerPath);
                    //Regex.Replace(branchedDefn.ProcessParameters, pattern, branchedMapping.TargetServerPath, RegexOptions.IgnoreCase);
                    //branchedDefn.Process.ServerPath = Regex.Replace(branchedDefn.ProcessParameters, pattern, branchedMapping.TargetServerPath, RegexOptions.IgnoreCase);
                }

                try
                {
                    branchedDefn.Save();
                }
                catch (BuildDefinitionAlreadyExistsException ex)
                {
                    MessageBox.Show(string.Format("Team Foundation Error\n\r\n\r{0}", ex.Message), "Microsoft Visual Studio", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (vsTfsBuild != null)
                {
                    vsTfsBuild.DefinitionManager.OpenDefinition(branchedDefn.Uri);
                }

                teamExplorer.CurrentPage.Refresh();
            }
        }