tools/builder/ABuilder.cs
author David Anderson <dvander@alliedmods.net>
Sun Aug 19 15:11:49 2007 +0000 (2007-08-19)
changeset 1356 4688fae59bcf
parent 1329 8a8789881ec3
child 1548 e759eedf6c9b
permissions -rw-r--r--
- fixed a series of corruption bugs in the admin cache (oh man, i suck sometimes)
- fixed per-group immunity being implemented completely wrong
- fixed GetAdminGroup being implemented wrong
- fixed a crash bug in the native GetAdminGroup
     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 
    57 			string new_loc;
    58 			if (pl.disabled)
    59 			{
    60 				new_loc = Config.PathFormat("{0}/{1}/addons/sourcemod/plugins/disabled/{2}.smx", cfg.OutputBase, pkg.GetBaseFolder(), pl.Source);
    61 			} 
    62 			else 
    63 			{
    64 				new_loc = Config.PathFormat("{0}/{1}/addons/sourcemod/plugins/{2}.smx", cfg.OutputBase, pkg.GetBaseFolder(), pl.Source);
    65 			}
    66 
    67 			try
    68 			{
    69 				if (File.Exists(new_loc))
    70 				{
    71 					File.Delete(new_loc);
    72 				}
    73 				File.Move(binary, new_loc);
    74 			}
    75 			catch (System.Exception e)
    76 			{
    77 				Console.WriteLine(e.Message);
    78 				return false;
    79 			}
    80 
    81 			return true;
    82 		}
    83 
    84 		public string GetRevsionOfPath(string path)
    85 		{
    86 			ProcessStartInfo info = new ProcessStartInfo();
    87 
    88 			info.WorkingDirectory = path;
    89 			info.FileName = cfg.SVNVersion;
    90 			info.Arguments = "--committed \"" + path + "\"";
    91 			info.UseShellExecute = false;
    92 			info.RedirectStandardOutput = true;
    93 
    94 			Process p = Process.Start(info);
    95 			string output = p.StandardOutput.ReadToEnd();
    96 			p.WaitForExit();
    97 			p.Close();
    98 
    99 			string [] revs = output.Split(":".ToCharArray(), 2);
   100 			if (revs.Length < 1)
   101 			{
   102 				return null;
   103 			}
   104 
   105 			string rev = null;
   106 			if (revs.Length == 1)
   107 			{
   108 				rev = revs[0];
   109 			} 
   110 			else 
   111 			{
   112 				rev = revs[1];
   113 			}
   114 
   115 			rev = rev.Trim();
   116 			rev = rev.Replace("M", "");
   117 			rev = rev.Replace("S", "");
   118 
   119 			return rev;
   120 		}
   121 
   122 		public bool CopyFile(Package pkg, string source, string dest)
   123 		{
   124 			string from = Config.PathFormat("{0}/{1}", 
   125 				cfg.SourceBase,
   126 				source);
   127 			string to = Config.PathFormat("{0}/{1}/{2}",
   128 				cfg.OutputBase,
   129 				pkg.GetBaseFolder(),
   130 				dest);
   131 
   132 			File.Copy(from, to, true);
   133 
   134 			return true;
   135 		}
   136 
   137 		/** dest can be null to mean root base folder */
   138 		public void CopyFolder(Package pkg, string source, string dest, string [] omits)
   139 		{
   140 			string from_base = Config.PathFormat("{0}/{1}", cfg.SourceBase, source);
   141 			string to_base = null;
   142 			
   143 			if (dest == null)
   144 			{
   145 				to_base = Config.PathFormat("{0}/{1}", 
   146 					cfg.OutputBase, 
   147 					pkg.GetBaseFolder());
   148 			} 
   149 			else 
   150 			{
   151 				to_base = Config.PathFormat("{0}/{1}/{2}", 
   152 					cfg.OutputBase, 
   153 					pkg.GetBaseFolder(), 
   154 					dest);
   155 			}
   156 
   157 			string [] files = Directory.GetFiles(from_base);
   158 			string file;
   159 
   160 			for (int i=0; i<files.Length; i++)
   161 			{
   162 				file = Path.GetFileName(files[i]);
   163 
   164 				if (omits != null)
   165 				{
   166 					bool skip = false;
   167 					for (int j=0; j<omits.Length; j++)
   168 					{
   169 						if (file.CompareTo(omits[j]) == 0)
   170 						{
   171 							skip = true;
   172 							break;
   173 						}
   174 					}
   175 					if (skip)
   176 					{
   177 						continue;
   178 					}
   179 				}
   180 				dest = Config.PathFormat("{0}/{1}", to_base, file);
   181 				File.Copy(files[i], dest, true);
   182 			}
   183 		}
   184 
   185 		public string PackageBuildName(Package pkg)
   186 		{
   187 			return pkg.GetPackageName() 
   188 				+ "-r"
   189 				+ GetRevsionOfPath(cfg.SourceBase)
   190 				+ "-"
   191 				+ DateTime.Now.Year
   192 				+ DateTime.Now.Month.ToString("00")
   193 				+ DateTime.Now.Day.ToString("00");
   194 		}
   195 
   196 		public void BuildPackage(Package pkg)
   197 		{
   198 			string path = Config.PathFormat("{0}/{1}", cfg.OutputBase, pkg.GetBaseFolder());
   199 
   200 			if (!Directory.Exists(path))
   201 			{
   202 				Directory.CreateDirectory(path);
   203 			}
   204 
   205 			/* Create all dirs */
   206 			string [] paths = pkg.GetFolders();
   207 			for (int i=0; i<paths.GetLength(0); i++)
   208 			{
   209 				path = Config.PathFormat("{0}/{1}/{2}", cfg.OutputBase, pkg.GetBaseFolder(), paths[i]);
   210 				if (!Directory.Exists(path))
   211 				{
   212 					Directory.CreateDirectory(path);
   213 				}
   214 			}
   215 
   216 			/* Do primitive copies */
   217 			pkg.OnCopyFolders(this);
   218 			pkg.OnCopyFiles(this);
   219 
   220 			/* Do libraries */
   221 			Library [] libs = pkg.GetLibraries();
   222 			string bin = null, binpath = null;
   223 			for (int i=0; i<libs.Length; i++)
   224 			{
   225 				if (BuildLibrary(pkg, libs[i], ref bin, ref binpath))
   226 				{
   227 					path = Config.PathFormat("{0}/{1}/{2}/{3}",
   228 						cfg.OutputBase,
   229 						pkg.GetBaseFolder(),
   230 						libs[i].Destination,
   231 						bin);
   232 					File.Copy(binpath, path, true);
   233 				}
   234 				else 
   235 				{
   236 					throw new System.Exception("Failed to compile library: " + libs[i].Name);
   237 				}
   238 			}
   239 
   240 			/* Do plugins */
   241 			Plugin [] plugins = pkg.GetPlugins();
   242 			if (plugins != null)
   243 			{
   244 				for (int i=0; i<plugins.Length; i++)
   245 				{
   246 					if (!CompilePlugin(pkg, plugins[i]))
   247 					{
   248 						throw new System.Exception("Failed to compile plugin: " + plugins[i].Source);
   249 					}
   250 				}
   251 			}
   252 
   253 			string pkg_file = null;
   254 			if (cfg.Compressor != null)
   255 			{
   256 				if ((pkg_file=CompressPackage(pkg)) == null)
   257 				{
   258 					throw new System.Exception("Failed to compress package: " + pkg.GetPackageName());
   259 				}
   260 	
   261 				string lpath = null, ltarget = null;
   262 				pkg.GetCompressBases(ref lpath, ref ltarget);
   263 				lpath = Config.PathFormat("{0}/{1}/{2}",
   264 					cfg.OutputBase,
   265 					lpath,
   266 					pkg_file);
   267 				ltarget = Config.PathFormat("{0}/{1}", cfg.OutputBase, pkg_file);
   268 	
   269 				if (File.Exists(ltarget))
   270 				{
   271 					File.Delete(ltarget);
   272 				}
   273 
   274 				File.Move(lpath, ltarget);
   275 			}
   276 		}
   277 	}
   278 }