Dashboard > CruiseControl.NET Community > ... > Tools for VB6 > vbp.build
vbp.build Log In | Sign Up   View a printable version of the current page.

Added by John Cole , last edited by John Cole on Oct 18, 2005  (view change)
Labels: 
(None)

<project name="vbpfunctions">
	<script language="C#" prefix="vbp">
		<code><![CDATA[
			[Function("maxver")]
			public static string GetMaxVer( string pVer1, string pVer2  ) 
			{
				//Console.WriteLine(pVer1 + " " + pVer2);
				string lMax = pVer1;	// use if they are equal
				string[] a = pVer1.Split('.');
				string[] b = pVer2.Split('.');
				
				for (int i = 0; i < 3; i++)
				{
					//Console.WriteLine(string.Format("{0} {1} {2}", i, a[i], b[i]));
					if (Convert.ToInt32(a[i]) != Convert.ToInt32(b[i]))
					{
						if (Convert.ToInt32(a[i]) < Convert.ToInt32(b[i]))
						{
							lMax = pVer2;
							break;
						}
					}
				}

				return lMax;
			}
		

			[Function("version")]
			public static string GetVersion( string pFileName  ) 
			{
				string lMaj = "", lMin = "", lRev = "";
				lMaj = GetVBPValue(pFileName, "^MajorVer=.*");
				lMin = GetVBPValue(pFileName, "^MinorVer=.*");
				lRev = GetVBPValue(pFileName, "^RevisionVer=.*");
				
				return lMaj + "." + lMin + "." + lRev;
			}
			
			[Function("getvalue")]
			public static string GetVBPValue( string pFileName, string pSearch  ) 
			{
				string s = "";
				using (StreamReader sr = new StreamReader(pFileName)) 
				{
					String line;
					Regex re = new Regex(pSearch);
					// Read and display lines from the file until the end of 
					// the file is reached.
					while ((line = sr.ReadLine()) != null) 
					{
						if(re.IsMatch(line))
						{
							string [] a = line.Split('=');
							s = a[1];
						}
					}
				}
				return s;
			}
			
			[Function("setvalue")]
			public static string SetVBPValue( string pFileName, string pSearch, string pValue ) 
			{
				string s = "";
	
	
				if (File.Exists(pFileName + ".bak")) File.Delete(pFileName + ".bak");
				File.Copy(pFileName, pFileName + ".bak");
				
				StreamReader sr = new StreamReader(pFileName + ".bak");
				StreamWriter sw = new StreamWriter(pFileName);
				String line;
				Regex re = new Regex(pSearch);
	
				while ((line = sr.ReadLine()) != null) 
				{
					if(re.IsMatch(line))
					{
						string [] a = line.Split('=');
						if (a[1] != pValue) {
							s += pSearch + " from " + a[1] + " to " + pValue + "\n";
						}
						sw.WriteLine(a[0] + "=" + pValue);
					}
					else 
					{
						sw.WriteLine(line);
					}
				}
				
	
				sw.Close();
				sr.Close();
	
				return s;
			}
			
			[Function("setvalue2")]
			public static string SetVBPValue2( string pFileName, string pSearch, string pValue, string pSearchBefore, string pName ) 
			{
				string s = "";
	
	
				if (File.Exists(pFileName + ".bak")) File.Delete(pFileName + ".bak");
				File.Copy(pFileName, pFileName + ".bak");
				
				StreamReader sr = new StreamReader(pFileName + ".bak");
				StreamWriter sw = new StreamWriter(pFileName);
				String line;
				Regex re = new Regex(pSearch);
				Regex re2 = new Regex(pSearchBefore);
				
				while ((line = sr.ReadLine()) != null) 
				{
					if(re.IsMatch(line))
					{
						string [] a = line.Split('=');
						if (a[1] != pValue) {
							s += pSearch + " from " + a[1] + " to " + pValue + "\n";
						}
						sw.WriteLine(a[0] + "=" + pValue);
					}
					else if (re2.IsMatch(line))
					{
						sw.WriteLine(line);
						s += "added " + pValue + "\n";
						sw.WriteLine(pName + pValue);
					}
					else
					{
						sw.WriteLine(line);
					}
				}
				
	
				sw.Close();
				sr.Close();
	
				return s;
			}
			
			[Function("addvalue")]
			public static string AddVBPValue( string pFileName, string pSearch, string pValue ) 
			{
				string s = "";
	
	
				if (File.Exists(pFileName + ".bak")) File.Delete(pFileName + ".bak");
				File.Copy(pFileName, pFileName + ".bak");
				
				StreamReader sr = new StreamReader(pFileName + ".bak");
				StreamWriter sw = new StreamWriter(pFileName);
				String line;
				Regex re = new Regex(pSearch);
	
				while ((line = sr.ReadLine()) != null) 
				{
					if(re.IsMatch(line))
					{
						sw.WriteLine(line);	// write the before line
						s += "added " + pValue + "\n";
						sw.WriteLine(pValue); // write the new line
					}
					else 
					{
						sw.WriteLine(line);
					}
				}
				
	
				sw.Close();
				sr.Close();
	
				return s;
			}

			[Function("delvalue")]
			public static string DelVBPValue( string pFileName, string pSearch ) 
			{
				string s = "";
	
	
				if (File.Exists(pFileName + ".bak")) File.Delete(pFileName + ".bak");
				File.Copy(pFileName, pFileName + ".bak");
				
				StreamReader sr = new StreamReader(pFileName + ".bak");
				StreamWriter sw = new StreamWriter(pFileName);
				String line;
				Regex re = new Regex(pSearch);
	
				while ((line = sr.ReadLine()) != null) 
				{
					if(re.IsMatch(line))
					{
						//sw.WriteLine(line);	// write the before line
						s += "deleted " + line + "\n";
						//sw.WriteLine(pValue); // write the new line
					}
					else 
					{
						sw.WriteLine(line);
					}
				}
				
	
				sw.Close();
				sr.Close();
	
				return s;
			}

			public static string GetCommonPath(string pFile1, string pFile2) 
			{
				string [] a = pFile1.Split(@"\".ToCharArray());
				string [] b = pFile2.Split(@"\".ToCharArray());
				int i = 0;
				int lMinElements = a.Length < b.Length ? a.Length : b.Length;
				string lCommonPath = "";
				for (i = 0; i < lMinElements; i++)
				{
					if (String.Compare(a[i], b[i], true) == 0) 
					{
						if (lCommonPath.Length > 0) lCommonPath += @"\";
						lCommonPath += a[i];
					} else {
						break;
					}
				
				}
				
				return lCommonPath;
			}
			
			public static string GetRelativePath(string pFrom, string pTo)
			{
				string lCommonPath = GetCommonPath(pFrom, pTo);
				string lRelFrom = pFrom.Substring(lCommonPath.Length + 1);
				string [] a = lRelFrom.Split(@"\".ToCharArray());
				string lUpDir = "";
				for (int i = 0; i < a.Length - 1; i++) 
				{
					lUpDir += @"..\";
				}
				
				return lUpDir + pTo.Substring(lCommonPath.Length + 1);
			}
			
			[Function("get-relative-target")]
			public static string GetRelativeTarget(string pVBPfrom, string pVBPto)
			{
				string lFrom = Path.Combine(Path.GetDirectoryName(pVBPfrom), GetVBPValue(pVBPfrom, "ExeName32").Trim("\"".ToCharArray()));
				string lTo = Path.Combine(Path.GetDirectoryName(pVBPto), GetVBPValue(pVBPto, "ExeName32").Trim("\"".ToCharArray()));
				
				return GetRelativePath(lFrom, lTo);
			}

			[Function("replace-line")]
			public static string ReplaceLine( string pFileName, string pLineSearch, string pStrSearch, string pValue ) 
			{
				string s = "";
	
	
				if (File.Exists(pFileName + ".bak")) File.Delete(pFileName + ".bak");
				File.Copy(pFileName, pFileName + ".bak");
				
				StreamReader sr = new StreamReader(pFileName + ".bak");
				StreamWriter sw = new StreamWriter(pFileName);
				String line;
				Regex re = new Regex(pLineSearch);
				Regex re2 = new Regex(pStrSearch);
				while ((line = sr.ReadLine()) != null) 
				{
					if(re.IsMatch(line))
					{
						string line2 = re2.Replace(line, pValue);
						if (line != line2) {
							s += "-" + line + "\n";
							s += "+" + line2 + "\n";
						}
						sw.WriteLine(line2);
					}
					else 
					{
						sw.WriteLine(line);
					}
				}
				
	
				sw.Close();
				sr.Close();
	
				return s;
			}
			

			[Function("exe32path")]
			public static string GetExe32NamePath(string pFileName) {
				string lExe32Name = GetVBPValue(pFileName, "^ExeName32=.*").Trim('"');
				string lPath = Path.GetDirectoryName(pFileName);
				
							
				return Path.Combine(lPath, lExe32Name);
			}
			
			[Function("new-base-address")]
			public static string GetBaseAddress()
			{
				const uint MIN_ADDRESS = 0x11000000;
				uint lng;
				System.Random rnd = new Random(); 
	
	
				lng = (uint) rnd.Next(0, 16000);
				lng *= 65536;
				lng += MIN_ADDRESS;
				
				return("&H"+ lng.ToString("x"));
			}
			
			[Function("get-references")]
			public static string GetReferences(string pVBPFile, string pProjectsFile) 
			{
				System.Collections.Hashtable lRefHash = new System.Collections.Hashtable();
				System.Collections.Hashtable lSearched = new System.Collections.Hashtable();
				string lRefs = "";
	
				GetRef(pVBPFile, ref lRefHash, ref lSearched, 0, pProjectsFile);
	
				foreach (System.Collections.DictionaryEntry s in lRefHash) 
				{
					lRefs += s.Value + "\n";
				}
	
				return lRefs;
			}
	
			public static void GetRef(string pVBPFile, 
				ref System.Collections.Hashtable pRefs,
				ref System.Collections.Hashtable pSearched,
				int pDepth,
				string pProjectsFile)
			{
				if (pDepth > 30) 
				{
					Console.WriteLine("max depth exceeded");
					return;
				}
	
				if (pSearched.Contains(Path.GetFileName(pVBPFile)))
				{
					return;
				}
				
				pSearched.Add(Path.GetFileName(pVBPFile), pVBPFile);
	
				//Console.WriteLine(string.Format("{1} Depth {0}", pDepth, Path.GetFileName(pVBPFile)));
	
				System.Collections.Hashtable lLocalRefs = new System.Collections.Hashtable();
				Regex re = new Regex("^Reference=.*");
				Regex re2 = new Regex("^Object=.*");
				StreamReader sr = new StreamReader(pVBPFile);
				string line;
				while ((line = sr.ReadLine()) != null) 
				{
					// check for refernce match
					if(re.IsMatch(line))
					{
						if (!lLocalRefs.Contains(RefGuid(line))) lLocalRefs.Add(RefGuid(line), line + "#" + GetFileVersionInfo(RefGuid(line)));
					}
					
					// check for object match
					if(re2.IsMatch(line))
					{
						if (!lLocalRefs.Contains(ObjGuid(line))) lLocalRefs.Add(ObjGuid(line), line + "#" + GetFileVersionInfo(ObjGuid(line)));
					}
				}
				
				foreach (System.Collections.DictionaryEntry s in lLocalRefs) 
				{
					if (!pRefs.Contains(s.Key)) 
					{
						pRefs.Add(s.Key, s.Value);
					}
				}
	
				// recurse to get remaining dependencies
				foreach (System.Collections.DictionaryEntry s in lLocalRefs) 
				{
					// check for refernce match
					if(re.IsMatch(s.Value.ToString()))
					{
						string lVBP = FindVBP(pProjectsFile, RefFile(s.Value.ToString()));
						if (lVBP.Length > 0) GetRef(lVBP, ref pRefs, ref pSearched, pDepth++, pProjectsFile);
					}
					
					// check for object match
					if(re2.IsMatch(s.Value.ToString()))
					{
						string lVBP = FindVBP(pProjectsFile, ObjFile(s.Value.ToString()));
						if (lVBP.Length > 0) GetRef(lVBP, ref pRefs, ref pSearched, pDepth++, pProjectsFile);
					}
	
				}
	
				sr.Close();
			}
	
			public static string FindVBP(string pProjectsFile, string pEXEname)
			{
				StreamReader sr = new StreamReader(pProjectsFile);
				string line;
				string lVBP = "";
				while ((line = sr.ReadLine()) != null) 
				{
					string [] a = line.Split(',');
					lVBP = a[0];
					string lVBPExeName = GetVBPValue(lVBP, "^ExeName32=.*").Trim("\"".ToCharArray());
					//Console.WriteLine(string.Format("++ {0} {1}", lVBPExeName, pEXEname));
	
					if (lVBPExeName.ToLower() == pEXEname.ToLower()) 
					{
						break;
					}
					lVBP = "";
				}
				sr.Close();	
				return lVBP;
	
			}
	
			public static string RefGuid(string pRefLine) 
			{
				string [] a = pRefLine.Split('#');
				return a[0].Substring(13);
			}
	
			public static string ObjGuid(string pObjLine) 
			{
				string [] a = pObjLine.Split('#');
				return a[0].Substring(7);
			}
	
			public static string RefFile(string pRefLine) 
			{
				string [] a = pRefLine.Split('#');
				return Path.GetFileName(a[3]);
			}
	
			public static string ObjFile(string pRefLine) 
			{
				string [] a = pRefLine.Split('#');
				return Path.GetFileName(a[2].Substring(3));
			}

			public static string GetFileVersionInfo(string pCLSID)
			{
				string lMax = "";
				string lFileVersion = "";
	
				Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("TypeLib\\" + pCLSID);
				if (key != null) 
				{
					foreach (string s in key.GetSubKeyNames()) 
					{
						lMax = s;
						
						try 
						{
							Microsoft.Win32.RegistryKey kpath = key.OpenSubKey(s).OpenSubKey("0").OpenSubKey("win32");
							string filename = GetFileName(kpath.GetValue("").ToString(), true);
							if (File.Exists(filename)) 
							{
								System.Diagnostics.FileVersionInfo fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(filename);
								lFileVersion = fvi.FileVersion;
							} 
							else 
							{
								lFileVersion = "File not found.";
							}
						} 
						catch (Exception e)
						{
							System.Diagnostics.Debug.WriteLine(e.Message);
							System.Diagnostics.Debug.WriteLine(e.StackTrace);
						}
					}
				}
	
				return lFileVersion;
			}
			
			private static string GetFileName(string sPath, bool FullPath) 
			{
				System.IO.DirectoryInfo diFile ;
				string sFilePath = sPath ;
				
				try
				{
					while(!System.IO.Path.HasExtension(sFilePath))
					{
						//Console.WriteLine("FileName to get from : " + sFilePath + " - " + System.IO.Path.IsPathRooted(sFilePath).ToString()) ;
						diFile = System.IO.Directory.GetParent(sFilePath) ;
						
						sFilePath = diFile.FullName ;
					}
					
					if (FullPath) {
						return sFilePath;
					} else {
						return System.IO.Path.GetFileName(sFilePath);
					}
				}
				catch(Exception ex)
				{
					Console.WriteLine ("Failed to return filename for: [" + sPath + "]. Error: " + ex.Message ) ;
					return "" ;
				}
					
			}

			

			[Function("add-line")]
			public static string AddLine( string pFileName, string pLineSearch, string pNewLineSearch, string pNewLine ) 
			{
				string s = "";
				Regex re;
				System.IO.StreamReader sr;
				string VBP = "";
				using (sr = new System.IO.StreamReader(pFileName)) 
				{
					VBP = sr.ReadToEnd();
				}
				
				re = new Regex(pNewLineSearch, System.Text.RegularExpressions.RegexOptions.Singleline);
				if (re.IsMatch(VBP)) {
					return s;	
				}
				
				if (File.Exists(pFileName + ".bak")) File.Delete(pFileName + ".bak");
				File.Copy(pFileName, pFileName + ".bak");
				
				sr = new StreamReader(pFileName + ".bak");
				StreamWriter sw = new StreamWriter(pFileName);
				String line;
				re = new Regex(pLineSearch);
				while ((line = sr.ReadLine()) != null) 
				{
					if(re.IsMatch(line))
					{
						s += "+" + pNewLine + "\n";
						sw.WriteLine(line);
						sw.WriteLine(pNewLine);
					}
					else 
					{
						sw.WriteLine(line);
					}
				}
				
	
				sw.Close();
				sr.Close();
	
				return s;
			}


		]]></code>
	</script>
	
</project>
Powered by a free Atlassian Confluence Open Source Project License granted to ThoughtWorks, Inc.. Evaluate Confluence today.
Powered by Atlassian Confluence 2.7.1, the Enterprise Wiki. Bug/feature request - Atlassian news - Contact administrators