Exemplo n.º 1
0
        /// <summary>
        /// 签出编辑
        /// </summary>
        private bool Checkout(TFSHelper tfsHelper)
        {
            decimal progressAddValue = 80M / this._updateFiles.Count;

            //签出编辑
            foreach (var updateFile in this._updateFiles)
            {
                progressValue += progressAddValue;
                this.worker.ReportProgress((int)progressValue, "开始签出编辑文件" + updateFile.Name + ",请稍后.....");
                var directoryName = updateFile.DirectoryName.Replace(rootProductPath + "\\", "");
                if (directoryName.Contains(rootProductPath))
                {
                    directoryName = updateFile.DirectoryName.Replace(rootProductPath, "");
                    var fileName = Path.Combine(customizePath, directoryName, updateFile.Name);
                    tfsHelper.GetLatest(fileName, JoeyRecursionType.OneLevel);
                    tfsHelper.CheckOut(fileName);
                }
                else
                {
                    var fileName = Path.Combine(customizePath, directoryName, updateFile.Name);
                    //获取文件所在目录的最新版本
                    tfsHelper.GetLatest(fileName);
                    tfsHelper.CheckOut(fileName);
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            string sln =
                File.ReadAllText(
                    "E:\\mysoft\\tfs_new\\10.5.10.70\\星河二开项目\\总部星河\\明源云ERPv1.0SP5星河孵化\\源代码\\分支1\\材料供应链二开整体解决方案.sln");

            string regex = @"SccTeamFoundationServer = (.+)\r";

            var math2 = Regex.Matches(sln, regex, RegexOptions.IgnoreCase);

            ////连接TFS
            string tpcURL = "http://10.5.10.70:8080/tfs";

            //登录服务器
            TfsConfigurationServer tfs = TfsConfigurationServerFactory.GetConfigurationServer(new Uri(tpcURL),
                                                                                              new UICredentialsProvider());

            //登录服务前,如果没有登录过会弹出提示框登录,登录过会直接跳过
            tfs.EnsureAuthenticated();

            #region 隐藏
            ////获取tfs服务器上所有项目
            //CatalogNode configurationServerNode = tfs.CatalogNode;

            //ReadOnlyCollection<CatalogNode> tpcNodes = configurationServerNode.QueryChildren(
            //      new Guid[] { CatalogResourceTypes.ProjectCollection },
            //      false,
            //      CatalogQueryOptions.None);

            //List<TfsTeamProjectCollection> lst = new List<TfsTeamProjectCollection>();

            ////遍历每一个TeamProjectCollection 节点
            //foreach (CatalogNode tpcNode in tpcNodes)
            //{
            //    //获取 当前 team project collection 名称.
            //    String displayName = tpcNode.Resource.DisplayName;

            //    // 获得 当前 team project collection 描述.
            //    String description = tpcNode.Resource.Description;

            //    //获取当前 team project collection 的描述.
            //    ServiceDefinition tpcServiceDefinition = tpcNode.Resource.ServiceReferences["Location"];
            //    ILocationService configLocationService = tfs.GetService<ILocationService>();
            //    Uri tpcUri = new Uri(configLocationService.LocationForCurrentConnection(tpcServiceDefinition));

            //    // 真正的连接到team project collection
            //    TfsTeamProjectCollection tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(tpcUri);
            //    lst.Add(tpc);
            //}
            #endregion

            //获取tfs权限
            TeamFoundationIdentity tfi;
            tfs.GetAuthenticatedIdentity(out tfi);

            //登录服务器指定tfs项目
            TfsTeamProjectCollection pjc     = new TfsTeamProjectCollection(new Uri(tpcURL + "/szzb"), tfi.Descriptor);
            VersionControlServer     version = pjc.GetService <VersionControlServer>();

            //获取文件夹目录 ItemSet
            //ItemSet its = version.GetItems("$/", RecursionType.OneLevel);

            //获取工作区
            Workspace[] wss = version.QueryWorkspaces(Environment.MachineName, version.AuthenticatedUser, Environment.MachineName);//查询工作区

            Workspace ws = wss.FirstOrDefault();

            #region 获取最新版本信息
            ItemSet itemSet = version.GetItems("$\\总部星河\\明源云ERPv1.0SP5星河孵化\\源代码\\分支1\\00-ERP站点\\Clgyl\\OrderMng\\M02210303", RecursionType.Full);

            //string filename =
            //    @"E:\mysoft\tfs_new\10.5.10.70\星河二开项目\总部星河\明源云ERPv1.0SP5星河孵化\源代码\分支1\00-ERP站点\Clgyl\OrderMng\M02210303\ApplyEdit.js";

            //foreach (var item in itemSet.Items)
            //{
            //    if (item.ItemType == ItemType.File)
            //    {
            //        if (Path.GetFileName(item.ServerItem) == Path.GetFileName(filename))
            //        {
            //            item.DownloadFile(filename);
            //        }
            //    }
            //}
            #endregion

            #region TFS获取最新版本

            TFSHelper tfsHelper = new TFSHelper("E:\\mysoft\\tfs_new\\10.5.10.70\\星河二开项目\\总部星河\\明源云ERPv1.0SP5星河孵化\\源代码\\分支1", "材料供应链二开整体解决方案.sln");

            ItemSet its = version.GetItems("$/总部星河/明源云ERPv1.0SP5星河孵化/源代码/分支1/00-ERP站点/Clgyl/OrderMng/M02210303", RecursionType.Full);

            tfsHelper.GetLatest("E:\\mysoft\\tfs_new\\10.5.10.70\\星河二开项目\\总部星河\\明源云ERPv1.0SP5星河孵化\\源代码\\分支1\\00-ERP站点\\Clgyl\\OrderMng\\M02210303\\ApplyMng.js");

            #endregion

            //添加Item
            //int pend = ws.PendAdd(localPath);

            //删除Item
            //int pend = ws.PendDelete(localPath);

            //编辑
            //int pend = wss.FirstOrDefault().PendEdit();

            //签入
            //ItemSpec[] itemSpecs = new ItemSpec[1];
            //itemSpecs[0] = new ItemSpec(localDir, RecursionType.Full);
            //WorkspaceCheckInParameters wscip = new WorkspaceCheckInParameters(itemSpecs, "注释内容");
            //int changeSetId = ws.CheckIn(wscip);//如果签入失败changeSetId==-1;反之,返回变更集,大于0的整数
        }
Exemplo n.º 3
0
        void CheckInScripts()
        {
            List <CheckInModel> errorlist   = new List <CheckInModel>();
            List <CheckInModel> successlist = new List <CheckInModel>();

            OpResult = new List <LogText>();
            bool vsucess = true;

            successlist = StoredProcedures;

            var bVersion = cbVersion.Checked;
            var bSingle  = cbSP.Checked;

            try
            {
                if (_username.Length > 0 && _password.Length > 0)
                {
                    TFSAuthModel auth = new TFSAuthModel()
                    {
                        Username = _username, Password = _password
                    };

                    if (successlist.Count > 0)
                    {
                        if (bVersion || bSingle)
                        {
                            // get latest project files
                            if (bVersion)
                            {
                                var res = TFSHelper.GetLatest(_path_version_tfs, auth);
                                AddConsoleText(res);
                                if (res.IsError)
                                {
                                    OpResult.Add(new LogText()
                                    {
                                        IsError = true,
                                        Output  = "Operation failed - Error occured while updating version scripts."
                                    });
                                    vsucess = false;
                                    MessageBox.Show("Error occured while updating version scripts", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    return;
                                }
                            }

                            if (bSingle)
                            {
                                var res = TFSHelper.GetLatest(_path_sp_tfs, auth);
                                AddConsoleText(res);
                                if (res.IsError)
                                {
                                    OpResult.Add(new LogText()
                                    {
                                        IsError = true,
                                        Output  = "Operation failed - Error occured while updating Stored Procedures."
                                    });
                                    MessageBox.Show("Error occured while updating Stored Procedures", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    return;
                                }
                            }



                            AddManualConsoleTest("Getting latest scripts successful....");

                            // end getting latest

                            //create files if not exists - version
                            bool vcreated = false;
                            if (bVersion)
                            {
                                string path = _path_version_local + "\\" + FileHelper.GenerateDateFile(ScriptDate);
                                if (!File.Exists(path))
                                {
                                    FileHelper.CreateFile(path);
                                    vcreated = true;
                                }
                            }

                            //create files if not exists - single
                            if (bSingle)
                            {
                                foreach (var sp in successlist)
                                {
                                    string path = _path_sp_local + "\\" + sp.Name + ".sql";
                                    if (!File.Exists(path))
                                    {
                                        FileHelper.CreateFile(path);
                                        sp.fCreated = true;
                                    }
                                }
                            }

                            //checkout files for edit
                            if (bVersion)
                            {
                                if (!vcreated)
                                {
                                    string path = _path_version_tfs + "\\" + FileHelper.GenerateDateFile(ScriptDate);
                                    var    res  = TFSHelper.Checkout(path, auth);
                                    AddConsoleText(res);
                                    if (res != null && res.IsError)
                                    {
                                        OpResult.Add(new LogText()
                                        {
                                            IsError = true,
                                            Output  = "Version Script(" + FileHelper.GenerateDateFile(ScriptDate) + ") - Failed."
                                        });
                                        vsucess = false;
                                    }
                                }
                            }

                            if (bSingle)
                            {
                                foreach (var sp in successlist)
                                {
                                    string path = _path_sp_tfs + "\\" + sp.Name + ".sql";
                                    if (!sp.fCreated)
                                    {
                                        var res = TFSHelper.Checkout(path, auth);
                                        AddConsoleText(res);
                                        if (res.IsError)
                                        {
                                            sp.IsError = true;
                                            errorlist.Add(sp);
                                        }
                                    }
                                }

                                foreach (var ersp in errorlist)
                                {
                                    var sp = successlist.Where(x => x.ID == ersp.ID).SingleOrDefault();
                                    if (sp != null)
                                    {
                                        successlist.Remove(sp);
                                    }
                                }
                            }


                            //add new files to tfs
                            //version script
                            if (bVersion)
                            {
                                if (vcreated)
                                {
                                    var res = TFSHelper.AddFile(_path_version_local, FileHelper.GenerateDateFile(ScriptDate), auth);
                                    AddConsoleText(res);
                                }
                            }

                            if (bSingle)
                            {
                                foreach (var sp in successlist)
                                {
                                    string name = sp.Name + ".sql";
                                    if (sp.fCreated)
                                    {
                                        var res = TFSHelper.AddFile(_path_sp_local, name, auth);
                                        AddConsoleText(res);
                                    }
                                }
                            }


                            //update scripts text
                            if (bVersion)
                            {
                                if (vcreated)
                                {
                                    string path = _path_version_local + "\\" + FileHelper.GenerateDateFile(ScriptDate);
                                    FileHelper.UpdateFileText(path, new ScriptHelper().GenerateVersionScript(successlist.Select(x => x.Name).ToList()));
                                }
                                else
                                {
                                    string path    = _path_version_local + "\\" + FileHelper.GenerateDateFile(ScriptDate);
                                    string vscript = FileHelper.ReadFile(path);
                                    StoredProcedureBusiness spData = new StoredProcedureBusiness();
                                    foreach (var s in successlist)
                                    {
                                        string script = new ScriptHelper().GenerateIndividualScript(s.Name, spData.GetScript(null, s.Name).Trim(), true);
                                        vscript = ReplaceScript(vscript, script, s.Name);
                                    }
                                    FileHelper.UpdateFileText(path, vscript);
                                }
                            }

                            //update single scripts
                            if (bSingle)
                            {
                                StoredProcedureBusiness spData = new StoredProcedureBusiness();
                                foreach (var sp in successlist)
                                {
                                    string script = new ScriptHelper().GenerateIndividualScript(sp.Name, spData.GetScript(null, sp.Name).Trim(), false);
                                    string path   = _path_sp_local + "\\" + sp.Name + ".sql";
                                    FileHelper.UpdateFileText(path, script);
                                }
                            }


                            //checkin pending changes
                            string comment = "";
                            if (txtComment.Text.Trim().Length > 0)
                            {
                                comment = txtComment.Text.Trim();
                            }
                            else
                            {
                                comment = "Script Changes";
                            }
                            var checkinres = TFSHelper.CheckIn(comment, auth);
                            AddConsoleText(checkinres);
                            AddManualConsoleTest("Operation completed successfuly");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                GenerateLog(successlist, errorlist, vsucess, bVersion);
            }
        }