tools/builder/ABuilder.cs
author David Anderson <dvander@alliedmods.net>
Fri Feb 16 03:47:10 2007 +0000 (2007-02-16)
changeset 501 e209a7296936
parent 424 f9bcc06b52eb
child 538 f2440cef1a52
permissions -rw-r--r--
removed this potential feature, not useful enough
     1 using System;
     2 using System.IO;
     3 using System.Diagnostics;
     4 
     5 namespace builder
     6 {
     7 	public abstract class ABuilder
     8 	{
     9 		public Config cfg;
    10 
    11 		public ABuilder()
    12 		{
    13 		}
    14 
    15 		public abstract string CompressPackage(Package pkg);
    16 
    17 		public abstract bool BuildLibrary(Package pkg, Library lib, ref string _binName, ref string _binPath);
    18 
    19 		public string GetRevsionOfPath(string path)
    20 		{
    21 			ProcessStartInfo info = new ProcessStartInfo();
    22 
    23 			info.WorkingDirectory = path;
    24 			info.FileName = cfg.SVNVersion;
    25 			info.Arguments = "--committed \"" + path + "\"";
    26 			info.UseShellExecute = false;
    27 			info.RedirectStandardOutput = true;
    28 
    29 			Process p = Process.Start(info);
    30 			string output = p.StandardOutput.ReadToEnd();
    31 			p.WaitForExit();
    32 			p.Close();
    33 
    34 			string [] revs = output.Split(":".ToCharArray(), 2);
    35 			if (revs.Length < 1)
    36 			{
    37 				return null;
    38 			}
    39 
    40 			string rev = null;
    41 			if (revs.Length == 1)
    42 			{
    43 				rev = revs[0];
    44 			} 
    45 			else 
    46 			{
    47 				rev = revs[1];
    48 			}
    49 
    50 			rev = rev.Trim();
    51 			rev = rev.Replace("M", "");
    52 			rev = rev.Replace("S", "");
    53 
    54 			return rev;
    55 		}
    56 
    57 		public void UpdateRevisionInfo(Package pkg, Library lib)
    58 		{
    59 			string path = Config.PathFormat("{0}/{1}", cfg.SourceBase, lib.LocalPath);
    60 			string file = Config.PathFormat("{0}/svn_version.h", path);
    61 
    62 			if (File.Exists(file))
    63 			{
    64 				UpdateRevisionInfo(path, file);
    65 			}
    66 		}
    67 
    68 		public void UpdateRevisionInfo(string path, string file)
    69 		{
    70 			string vers = cfg.ProductVersion.Replace(".", ",");
    71 			string rev = GetRevsionOfPath(path);
    72 
    73 			File.Delete(file);
    74 			StreamWriter sw = File.CreateText(file);
    75 
    76 			sw.WriteLine("/** This file is autogenerated by build scripts */");
    77 			sw.WriteLine("");
    78 			sw.WriteLine("#ifndef _INCLUDE_SVN_VERSION_H_");
    79 			sw.WriteLine("#define _INCLUDE_SVN_VERSION_H_");
    80 			sw.WriteLine("");
    81 			sw.WriteLine("#define SVN_REVISION			{0}", rev);
    82 			sw.WriteLine("#define SVN_REVISION_STRING	\"{0}\"", rev);
    83 			sw.WriteLine("#define SVN_FILE_VERSION		{0},{1}", vers, rev);
    84 			sw.WriteLine("");
    85 			sw.WriteLine("#endif //_INCLUDE_SVN_VERSION_H_");
    86 			sw.WriteLine("");
    87 
    88 			sw.Close();
    89 		}
    90 
    91 		public bool CopyFile(Package pkg, string source, string dest)
    92 		{
    93 			string from = Config.PathFormat("{0}/{1}", 
    94 				cfg.SourceBase,
    95 				source);
    96 			string to = Config.PathFormat("{0}/{1}/{2}",
    97 				cfg.OutputBase,
    98 				pkg.GetBaseFolder(),
    99 				dest);
   100 
   101 			File.Copy(from, to, true);
   102 
   103 			return true;
   104 		}
   105 
   106 		/** dest can be null to mean root base folder */
   107 		public void CopyFolder(Package pkg, string source, string dest, string [] omits)
   108 		{
   109 			string from_base = Config.PathFormat("{0}/{1}", cfg.SourceBase, source);
   110 			string to_base = null;
   111 			
   112 			if (dest == null)
   113 			{
   114 				to_base = Config.PathFormat("{0}/{1}", 
   115 					cfg.OutputBase, 
   116 					pkg.GetBaseFolder());
   117 			} 
   118 			else 
   119 			{
   120 				to_base = Config.PathFormat("{0}/{1}/{2}", 
   121 					cfg.OutputBase, 
   122 					pkg.GetBaseFolder(), 
   123 					dest);
   124 			}
   125 
   126 			string [] files = Directory.GetFiles(from_base);
   127 			string file;
   128 
   129 			for (int i=0; i<files.Length; i++)
   130 			{
   131 				file = Path.GetFileName(files[i]);
   132 
   133 				if (omits != null)
   134 				{
   135 					bool skip = false;
   136 					for (int j=0; j<omits.Length; j++)
   137 					{
   138 						if (file.CompareTo(omits[j]) == 0)
   139 						{
   140 							skip = true;
   141 							break;
   142 						}
   143 					}
   144 					if (skip)
   145 					{
   146 						continue;
   147 					}
   148 				}
   149 				dest = Config.PathFormat("{0}/{1}", to_base, file);
   150 				File.Copy(files[i], dest, true);
   151 			}
   152 		}
   153 
   154 		public string PackageBuildName(Package pkg)
   155 		{
   156 			return pkg.GetPackageName() 
   157 				+ "-"
   158 				+ DateTime.Now.Day.ToString("00")
   159 				+ DateTime.Now.Month.ToString("00")
   160 				+ DateTime.Now.Year
   161 				+ "-r"
   162 				+ GetRevsionOfPath(cfg.SourceBase);
   163 		}
   164 
   165 		public void BuildPackage(Package pkg)
   166 		{
   167 			string path = Config.PathFormat("{0}/{1}", cfg.OutputBase, pkg.GetBaseFolder());
   168 
   169 			if (!Directory.Exists(path))
   170 			{
   171 				Directory.CreateDirectory(path);
   172 			}
   173 
   174 			/* Create all dirs */
   175 			string [] paths = pkg.GetFolders();
   176 			for (int i=0; i<paths.GetLength(0); i++)
   177 			{
   178 				path = Config.PathFormat("{0}/{1}/{2}", cfg.OutputBase, pkg.GetBaseFolder(), paths[i]);
   179 				if (!Directory.Exists(path))
   180 				{
   181 					Directory.CreateDirectory(path);
   182 				}
   183 			}
   184 
   185 			/* Do primitive copies */
   186 			pkg.OnCopyFolders(this);
   187 			pkg.OnCopyFiles(this);
   188 
   189 			/* Do libraries */
   190 			Library [] libs = pkg.GetLibraries();
   191 			string bin = null, binpath = null;
   192 			for (int i=0; i<libs.Length; i++)
   193 			{
   194 				UpdateRevisionInfo(pkg, libs[i]);
   195 				if (BuildLibrary(pkg, libs[i], ref bin, ref binpath))
   196 				{
   197 					path = Config.PathFormat("{0}/{1}/{2}/{3}",
   198 						cfg.OutputBase,
   199 						pkg.GetBaseFolder(),
   200 						libs[i].Destination,
   201 						bin);
   202 					File.Copy(binpath, path, true);
   203 				}
   204 				else 
   205 				{
   206 					throw new System.Exception("Failed to compile library: " + libs[i].Name);
   207 				}
   208 			}
   209 
   210 			string pkg_file = null;
   211 			if ((pkg_file=CompressPackage(pkg)) == null)
   212 			{
   213 				throw new System.Exception("Failed to compress package: " + pkg.GetPackageName());
   214 			}
   215 
   216 			string lpath = null, ltarget = null;
   217 			pkg.GetCompressBases(ref lpath, ref ltarget);
   218 			lpath = Config.PathFormat("{0}/{1}/{2}",
   219 				cfg.OutputBase,
   220 				lpath,
   221 				pkg_file);
   222 			ltarget = Config.PathFormat("{0}/{1}", cfg.OutputBase, pkg_file);
   223 
   224 			if (File.Exists(ltarget))
   225 			{
   226 				File.Delete(ltarget);
   227 			}
   228 
   229 			File.Move(lpath, ltarget);
   230 		}
   231 	}
   232 }