Exemplo n.º 1
0
 public void SaveSingleScript(List<string> splist)
 {
     if (splist != null && splist.Count > 0)
     {
         string dirname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
         string path = Application.StartupPath + "\\GeneratedScripts\\" + dirname;
         Directory.CreateDirectory(path);
         string[] dirs = Directory.GetDirectories(path);
         int dirno = 0;
         if (dirs.Length > 0)
         {
             int maxno = 1;
             foreach (var dir in dirs)
             {
                 string no = Regex.Match(new DirectoryInfo(dir).Name, @"\d+").Value;
                 if (no.Trim().Length > 0)
                 {
                     if (Convert.ToInt32(no.Trim()) > maxno)
                         maxno = Convert.ToInt32(no.Trim());
                 }
             }
             if (dirno < maxno)
                 dirno = maxno;
         }
         dirno = dirno + 1;
         string grouppath = path + "\\" + dirno.ToString("00");
         Directory.CreateDirectory(grouppath);
         StoredProcedureBusiness spdata = new StoredProcedureBusiness();
         ScriptHelper spHelper = new ScriptHelper();
         foreach (var sp in splist)
         {
             string query = spHelper.GenerateIndividualScript(sp, spdata.GetScript(null, sp),false);
             System.IO.File.WriteAllText(grouppath + "\\" + sp + ".sql", query);
         }
         Process.Start(grouppath);
     }
 }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
0
 private void GenerateVersionScript()
 {
     ScriptHelper spHelper = new ScriptHelper();
     List<string> splist = new List<string>();
     foreach (DataGridViewRow rw in dgvSP.Rows)
     {
         if (Convert.ToBoolean(rw.Cells[2].Value))
             splist.Add(rw.Cells[3].Value.ToString());
     }
     string version_script = spHelper.GenerateVersionScript(splist);
     string filename = FileHelper.GenerateDateFile(dtpScriptDate.Value);
     frmScriptViewer frm = new frmScriptViewer(version_script, filename);
     frm.Show();
 }