SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
SourceSafe.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.IO;
5 using System.Diagnostics;
6 using Seci.Definitions;
7 
8 namespace Seci.Helpers
9 {
13  static class SourceSafe
14  {
15  private static String _filename;
16  private static String _SSexe;
17  private static String _SSlocation;
18  private static String _SSuser = "seci";
19  private static String _SSserver = "$/labview/seci/seciconfigs/";
20  private static String _SSbat;
21 
29  internal static void saveToSourceSafe(String fileName)
30  {
31  //Set up SourceSafe
32  _SSexe = Status.SSexe;
33  _SSlocation = Status.SSlocation;
34  _SSuser = Status.SSuser;
35  _SSserver = Status.SSserver;
36 
37  _SSbat = Status.ExecutionDir + "\\SS.bat";
38 
39  _filename = fileName;
40 
41  //First check whether it currently exists in SS, if not add it
42  if (projectExists())
43  {
44  //Check it out
45  checkOutSourceSafe();
46 
47  //Then check it back in
48  checkInSourceSafe();
49  }
50  else
51  {
52  //Create project, does not matter if it already exists
53  createSourceSafeProject();
54 
55  //Add it
56  addFile();
57  }
58 
59  //Then label it
60  labelInSourceSafe();
61  }
62 
67  private static Boolean projectExists()
68  {
69  String fileList = Status.ExecutionDir + "\\SSfilelist.txt";
70 
71  //Creates a batch file which is run
72  using (StreamWriter SW = new StreamWriter(_SSbat, false))
73  {
74  //Set up path and user
75  SW.WriteLine("set ssdir=" + _SSserver);
76  SW.WriteLine("set ssuser=" + _SSuser);
77 
78  //Ask for list of files
79  SW.WriteLine("\"" + _SSexe + "\" directory \"" + _SSlocation + Status.Instrument + "\" -NL > \"" + fileList + "\"" );
80  }
81 
82  //Run bat then delete bat
83  runBatchFile();
84 
85  //Get data from SSfilelist.txt, then delete that
86  FileInfo fi = new FileInfo(fileList);
87 
88  if (fi.Exists)
89  {
90  using (StreamReader SR = new StreamReader(fileList))
91  {
92  while (SR.Peek() >= 0)
93  {
94  String line = SR.ReadLine();
95 
96  if (line == _filename.Substring(_filename.LastIndexOf("\\") + 1))
97  {
98  //Found it in list of files
99  SR.Close();
100  fi.Delete();
101  return true;
102  }
103  }
104  }
105  fi.Delete();
106  }
107  return false;
108  }
109 
114  private static void checkOutSourceSafe()
115  {
116  //Creates a batch file which is run
117  using (StreamWriter SW = new StreamWriter(_SSbat, false))
118  {
119  //Set up path and user
120  SW.WriteLine("set ssdir=" + _SSserver);
121  SW.WriteLine("set ssuser=" + _SSuser);
122 
123  //Set working folder
124  SW.WriteLine("\"" + _SSexe + "\" workfold " + "\"" + _SSlocation + Status.Instrument + "/\" \"" + _filename.Substring(0, _filename.LastIndexOf('\\')) + "\"");
125 
126  //Change directory to working folder
127  SW.WriteLine("chdir \"" + _filename.Substring(0, _filename.LastIndexOf("\\")) + "\"");
128 
129  //Then checkout
130  SW.WriteLine("\"" + _SSexe + "\" checkout \"" + _SSlocation + Status.Instrument + "/" + _filename.Substring(_filename.LastIndexOf("\\") + 1) + "\" -GWR -G- -I-");
131  }
132 
133  //Run bat then delete bat
134  runBatchFile();
135  }
136 
140  private static void checkInSourceSafe()
141  {
142  //Creates a batch file which is run
143  using (StreamWriter SW = new StreamWriter(_SSbat, false))
144  {
145  //Set up path and user
146  SW.WriteLine("set ssdir=" + _SSserver);
147  SW.WriteLine("set ssuser=" + _SSuser);
148 
149  //Set working folder
150  SW.WriteLine("\"" + _SSexe + "\" workfold " + "\"" + _SSlocation + Status.Instrument
151  + "/\" \"" + _filename.Substring(0, _filename.LastIndexOf('\\')) + "\"");
152 
153  //Change directory to working folder
154  SW.WriteLine("chdir \"" + _filename.Substring(0, _filename.LastIndexOf("\\")) + "\"");
155 
156  //Then check in
157  SW.WriteLine("\"" + _SSexe + "\" checkin \"" + _SSlocation + Status.Instrument
158  + "/" + _filename.Substring(_filename.LastIndexOf("\\") + 1) + "\" -I-");
159  }
160 
161  //run bat then delete bat
162  runBatchFile();
163  }
164 
168  private static void addFile()
169  {
170  //Adds a file only if it is not already in SS
171  //Creates a batch file which is run
172  using (StreamWriter SW = new StreamWriter(_SSbat, false))
173  {
174  //Set up path and user
175  SW.WriteLine("set ssdir=" + _SSserver);
176  SW.WriteLine("set ssuser=" + _SSuser);
177 
178  //Copy to sourcesafe
179  SW.WriteLine("\"" + _SSexe + "\" CP " + "\"" + _SSlocation + Status.Instrument + "/\"");
180 
181  //Then add
182  SW.WriteLine("\"" + _SSexe + "\" add \"" + _filename + "\" -I-");
183  }
184 
185  //run bat then delete bat
186  runBatchFile();
187  }
188 
193  private static void createSourceSafeProject()
194  {
195  //Creates a batch file which is run
196  using (StreamWriter SW = new StreamWriter(_SSbat, false))
197  {
198  //Set up path and user
199  SW.WriteLine("set ssdir=" + _SSserver);
200  SW.WriteLine("set ssuser=" + _SSuser);
201 
202  //Create project in SS
203  SW.WriteLine("\"" + _SSexe + "\" create \"" + _SSlocation + Status.Instrument + "\" -I-");
204  }
205 
206  //run bat then delete bat
207  runBatchFile();
208  }
209 
213  private static void labelInSourceSafe()
214  {
215  //Creates a batch file which is run
216  using (StreamWriter SW = new StreamWriter(_SSbat, false))
217  {
218  //Set up path and user
219  SW.WriteLine("set ssdir=" + _SSserver);
220  SW.WriteLine("set ssuser=" + _SSuser);
221 
222  //Label it with date and time
223  String time = DateTime.Now.ToString();
224  time = time.Replace(' ', '_');
225 
226  SW.WriteLine("\"" + _SSexe + "\" label \"" + _SSlocation + Status.Instrument + "/" + _filename.Substring(_filename.LastIndexOf("\\") + 1) + "\" -I- -L" + time);
227  }
228 
229  //run bat then delete bat
230  runBatchFile();
231  }
232 
236  private static void runBatchFile()
237  {
238  Process proc = new Process();
239  proc.StartInfo.FileName = "C:\\windows\\system32\\cmd.exe";
240  proc.StartInfo.Arguments = " /c \"" + _SSbat + "\"";
241  proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
242  proc.StartInfo.UseShellExecute = true;
243 
244  //Start batch file
245  proc.Start();
246  //Have to wait for process to finish, otherwise it may not work properly
247  proc.WaitForExit();
248 
249  FileInfo bat = new FileInfo(_SSbat);
250  if (bat.Exists)
251  {
252  bat.Delete();
253  }
254  }
255 
256  }
257 }
static void runBatchFile()
Runs a created batch file. Spawns a hidden process.
Definition: SourceSafe.cs:236
static void addFile()
Add the new configuration file to the project.
Definition: SourceSafe.cs:168
Class for saving configurations into SourceSafe. Works by creating batch files then running them...
Definition: SourceSafe.cs:13
static Boolean projectExists()
Checks to see if the project exists in SourceSafe.
Definition: SourceSafe.cs:67
static void labelInSourceSafe()
Labels the saved configuration with the date and time of saving.
Definition: SourceSafe.cs:213
static void checkOutSourceSafe()
Check-out the existing version in SourceSafe. Does not overwrite the local copy.
Definition: SourceSafe.cs:114
static String _SSexe
Definition: SourceSafe.cs:16
static String _filename
Definition: SourceSafe.cs:15
static void checkInSourceSafe()
Check-in the updated project.
Definition: SourceSafe.cs:140
static void createSourceSafeProject()
Create a new project in SourceSafe - this happens the first ever time a configuration is saved on the...
Definition: SourceSafe.cs:193
static String _SSbat
Definition: SourceSafe.cs:20
static String _SSlocation
Definition: SourceSafe.cs:17