tools/builder/ABuilder.cs
author Scott Ehlert <ds@alliedmods.net>
Sun Jul 15 08:00:26 2007 +0000 (2007-07-15)
changeset 1116 40cc5e9f0f1b
parent 984 c996eafa2d16
child 1329 8a8789881ec3
permissions -rw-r--r--
Removed strange debugging code
     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 abstract string GetPawnCompilerName();
    20 
    21 		public bool CompilePlugin(Package pkg, Plugin pl)
    22 		{
    23 			string local_dir = Config.PathFormat("{0}/{1}/addons/sourcemod/scripting", cfg.OutputBase, pkg.GetBaseFolder());
    24 
    25 			string filepath = null;
    26 			if (pl.Folder != null)
    27 			{
    28 				filepath = Config.PathFormat("{0}/{1}", pl.Folder, pl.Source);
    29 			} 
    30 			else 
    31 			{
    32 				filepath = pl.Source;
    33 			}
    34 
    35 			ProcessStartInfo info = new ProcessStartInfo();
    36 			info.WorkingDirectory = local_dir;
    37 			info.FileName = Config.PathFormat("{0}/{1}", local_dir, GetPawnCompilerName());
    38 			info.Arguments = filepath + ".sp";
    39 			info.UseShellExecute = false;
    40 			info.RedirectStandardOutput = true;
    41 			info.RedirectStandardError = true;
    42 
    43 			Process p = Process.Start(info);
    44 			string output = p.StandardOutput.ReadToEnd() + "\n";
    45 			output += p.StandardError.ReadToEnd();
    46 			p.WaitForExit();
    47 			p.Close();
    48 
    49 			Console.WriteLine(output);
    50 
    51 			string binary = Config.PathFormat("{0}/{1}/addons/sourcemod/scripting/{2}.smx", cfg.OutputBase, pkg.GetBaseFolder(), pl.Source);
    52 			if (!File.Exists(binary))
    53 			{
    54 				return false;
    55 			}
    56 			string new_loc = Config.PathFormat("{0}/{1}/addons/sourcemod/plugins/{2}.smx", cfg.OutputBase, pkg.GetBaseFolder(), pl.Source);
    57 
    58 			try
    59 			{
    60 				if (File.Exists(new_loc))
    61 				{
    62 					File.Delete(new_loc);
    63 				}
    64 				File.Move(binary, new_loc);
    65 			}
    66 			catch (System.Exception e)
    67 			{
    68 				Console.WriteLine(e.Message);
    69 				return false;
    70 			}
    71 
    72 			return true;
    73 		}
    74 
    75 		public string GetRevsionOfPath(string path)
    76 		{
    77 			ProcessStartInfo info = new ProcessStartInfo();
    78 
    79 			info.WorkingDirectory = path;
    80 			info.FileName = cfg.SVNVersion;
    81 			info.Arguments = "--committed \"" + path + "\"";
    82 			info.UseShellExecute = false;
    83 			info.RedirectStandardOutput = true;
    84 
    85 			Process p = Process.Start(info);
    86 			string output = p.StandardOutput.ReadToEnd();
    87 			p.WaitForExit();
    88 			p.Close();
    89 
    90 			string [] revs = output.Split(":".ToCharArray(), 2);
    91 			if (revs.Length < 1)
    92 			{
    93 				return null;
    94 			}
    95 
    96 			string rev = null;
    97 			if (revs.Length == 1)
    98 			{
    99 				rev = revs[0];
   100 			} 
   101 			else 
   102 			{
   103 				rev = revs[1];
   104 			}
   105 
   106 			rev = rev.Trim();
   107 			rev = rev.Replace("M", "");
   108 			rev = rev.Replace("S", "");
   109 
   110 			return rev;
   111 		}
   112 
   113 		public bool CopyFile(Package pkg, string source, string dest)
   114 		{
   115 			string from = Config.PathFormat("{0}/{1}", 
   116 				cfg.SourceBase,
   117 				source);
   118 			string to = Config.PathFormat("{0}/{1}/{2}",
   119 				cfg.OutputBase,
   120 				pkg.GetBaseFolder(),
   121 				dest);
   122 
   123 			File.Copy(from, to, true);
   124 
   125 			return true;
   126 		}
   127 
   128 		/** dest can be null to mean root base folder */
   129 		public void CopyFolder(Package pkg, string source, string dest, string [] omits)
   130 		{
   131 			string from_base = Config.PathFormat("{0}/{1}", cfg.SourceBase, source);
   132 			string to_base = null;
   133 			
   134 			if (dest == null)
   135 			{
   136 				to_base = Config.PathFormat("{0}/{1}", 
   137 					cfg.OutputBase, 
   138 					pkg.GetBaseFolder());
   139 			} 
   140 			else 
   141 			{
   142 				to_base = Config.PathFormat("{0}/{1}/{2}", 
   143 					cfg.OutputBase, 
   144 					pkg.GetBaseFolder(), 
   145 					dest);
   146 			}
   147 
   148 			string [] files = Directory.GetFiles(from_base);
   149 			string file;
   150 
   151 			for (int i=0; i<files.Length; i++)
   152 			{
   153 				file = Path.GetFileName(files[i]);
   154 
   155 				if (omits != null)
   156 				{
   157 					bool skip = false;
   158 					for (int j=0; j<omits.Length; j++)
   159 					{
   160 						if (file.CompareTo(omits[j]) == 0)
   161 						{
   162 							skip = true;
   163 							break;
   164 						}
   165 					}
   166 					if (skip)
   167 					{
   168 						continue;
   169 					}
   170 				}
   171 				dest = Config.PathFormat("{0}/{1}", to_base, file);
   172 				File.Copy(files[i], dest, true);
   173 			}
   174 		}
   175 
   176 		public string PackageBuildName(Package pkg)
   177 		{
   178 			return pkg.GetPackageName() 
   179 				+ "-r"
   180 				+ GetRevsionOfPath(cfg.SourceBase)
   181 				+ "-"
   182 				+ DateTime.Now.Year
   183 				+ DateTime.Now.Month.ToString("00")
   184 				+ DateTime.Now.Day.ToString("00");
   185 		}
   186 
   187 		public void BuildPackage(Package pkg)
   188 		{
   189 			string path = Config.PathFormat("{0}/{1}", cfg.OutputBase, pkg.GetBaseFolder());
   190 
   191 			if (!Directory.Exists(path))
   192 			{
   193 				Directory.CreateDirectory(path);
   194 			}
   195 
   196 			/* Create all dirs */
   197 			string [] paths = pkg.GetFolders();
   198 			for (int i=0; i<paths.GetLength(0); i++)
   199 			{
   200 				path = Config.PathFormat("{0}/{1}/{2}", cfg.OutputBase, pkg.GetBaseFolder(), paths[i]);
   201 				if (!Directory.Exists(path))
   202 				{
   203 					Directory.CreateDirectory(path);
   204 				}
   205 			}
   206 
   207 			/* Do primitive copies */
   208 			pkg.OnCopyFolders(this);
   209 			pkg.OnCopyFiles(this);
   210 
   211 			/* Do libraries */
   212 			Library [] libs = pkg.GetLibraries();
   213 			string bin = null, binpath = null;
   214 			for (int i=0; i<libs.Length; i++)
   215 			{
   216 				if (BuildLibrary(pkg, libs[i], ref bin, ref binpath))
   217 				{
   218 					path = Config.PathFormat("{0}/{1}/{2}/{3}",
   219 						cfg.OutputBase,
   220 						pkg.GetBaseFolder(),
   221 						libs[i].Destination,
   222 						bin);
   223 					File.Copy(binpath, path, true);
   224 				}
   225 				else 
   226 				{
   227 					throw new System.Exception("Failed to compile library: " + libs[i].Name);
   228 				}
   229 			}
   230 
   231 			/* Do plugins */
   232 			Plugin [] plugins = pkg.GetPlugins();
   233 			if (plugins != null)
   234 			{
   235 				for (int i=0; i<plugins.Length; i++)
   236 				{
   237 					if (!CompilePlugin(pkg, plugins[i]))
   238 					{
   239 						throw new System.Exception("Failed to compile plugin: " + plugins[i].Source);
   240 					}
   241 				}
   242 			}
   243 
   244 			string pkg_file = null;
   245 			if (cfg.Compressor != null)
   246 			{
   247 				if ((pkg_file=CompressPackage(pkg)) == null)
   248 				{
   249 					throw new System.Exception("Failed to compress package: " + pkg.GetPackageName());
   250 				}
   251 	
   252 				string lpath = null, ltarget = null;
   253 				pkg.GetCompressBases(ref lpath, ref ltarget);
   254 				lpath = Config.PathFormat("{0}/{1}/{2}",
   255 					cfg.OutputBase,
   256 					lpath,
   257 					pkg_file);
   258 				ltarget = Config.PathFormat("{0}/{1}", cfg.OutputBase, pkg_file);
   259 	
   260 				if (File.Exists(ltarget))
   261 				{
   262 					File.Delete(ltarget);
   263 				}
   264 
   265 				File.Move(lpath, ltarget);
   266 			}
   267 		}
   268 	}
   269 }