tools/builder/ABuilder.cs
author David Anderson <dvander@alliedmods.net>
Fri Dec 07 21:43:28 2007 +0000 (2007-12-07)
changeset 1774 b94704497073
parent 1586 e6c0abbec4ce
child 1817 debf84ea93df
permissions -rw-r--r--
added debugging to build tool
     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 bool BuildLibrary(Package pkg, Library lib);
    16 
    17 		public abstract string GetPawnCompilerName();
    18 
    19 		public bool CompilePlugin(Package pkg, Plugin pl)
    20 		{
    21 			string local_dir = Config.PathFormat("{0}/{1}/addons/sourcemod/scripting", cfg.pkg_path, pkg.GetBaseFolder());
    22 
    23 			string filepath = null;
    24 			if (pl.Folder != null)
    25 			{
    26 				filepath = Config.PathFormat("{0}/{1}", pl.Folder, pl.Source);
    27 			} 
    28 			else 
    29 			{
    30 				filepath = pl.Source;
    31 			}
    32 
    33 			ProcessStartInfo info = new ProcessStartInfo();
    34 			info.WorkingDirectory = local_dir;
    35 			info.FileName = Config.PathFormat("{0}/{1}", local_dir, GetPawnCompilerName());
    36 			info.Arguments = filepath + ".sp";
    37 			info.UseShellExecute = false;
    38 			info.RedirectStandardOutput = true;
    39 			info.RedirectStandardError = true;
    40 
    41 			Process p = Process.Start(info);
    42 			string output = p.StandardOutput.ReadToEnd() + "\n";
    43 			output += p.StandardError.ReadToEnd();
    44 			p.WaitForExit();
    45 			p.Close();
    46 
    47 			Console.WriteLine("Debug: wd = " + info.WorkingDirectory + " fn = " + info.FileName + " arg = " + info.Arguments);
    48 			Console.WriteLine(output);
    49 
    50 			string binary = Config.PathFormat("{0}/{1}/addons/sourcemod/scripting/{2}.smx", cfg.pkg_path, pkg.GetBaseFolder(), pl.Source);
    51 			if (!File.Exists(binary))
    52 			{
    53 				Console.WriteLine("Could not find binary: " + binary);
    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.pkg_path, pkg.GetBaseFolder(), pl.Source);
    61 			} 
    62 			else 
    63 			{
    64 				new_loc = Config.PathFormat("{0}/{1}/addons/sourcemod/plugins/{2}.smx", cfg.pkg_path, 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 bool CopyFile(Package pkg, string source, string dest)
    85 		{
    86 			string from = Config.PathFormat("{0}/{1}", 
    87 				cfg.source_path,
    88 				source);
    89 			string to = Config.PathFormat("{0}/{1}/{2}",
    90 				cfg.pkg_path,
    91 				pkg.GetBaseFolder(),
    92 				dest);
    93 
    94 			File.Copy(from, to, true);
    95 
    96 			return true;
    97 		}
    98 
    99 		/** dest can be null to mean root base folder */
   100 		public void CopyFolder(Package pkg, string source, string dest, string [] omits)
   101 		{
   102 			string from_base = Config.PathFormat("{0}/{1}", cfg.source_path, source);
   103 			string to_base = null;
   104 			
   105 			if (dest == null)
   106 			{
   107 				to_base = Config.PathFormat("{0}/{1}", 
   108 					cfg.pkg_path, 
   109 					pkg.GetBaseFolder());
   110 			} 
   111 			else 
   112 			{
   113 				to_base = Config.PathFormat("{0}/{1}/{2}", 
   114 					cfg.pkg_path, 
   115 					pkg.GetBaseFolder(), 
   116 					dest);
   117 			}
   118 
   119 			string [] files = Directory.GetFiles(from_base);
   120 			string file;
   121 
   122 			for (int i=0; i<files.Length; i++)
   123 			{
   124 				file = Path.GetFileName(files[i]);
   125 
   126 				if (omits != null)
   127 				{
   128 					bool skip = false;
   129 					for (int j=0; j<omits.Length; j++)
   130 					{
   131 						if (file.CompareTo(omits[j]) == 0)
   132 						{
   133 							skip = true;
   134 							break;
   135 						}
   136 					}
   137 					if (skip)
   138 					{
   139 						continue;
   140 					}
   141 				}
   142 				dest = Config.PathFormat("{0}/{1}", to_base, file);
   143 				File.Copy(files[i], dest, true);
   144 			}
   145 		}
   146 
   147 		public void BuildPackage(Package pkg)
   148 		{
   149 			string path = Config.PathFormat("{0}/{1}", cfg.pkg_path, pkg.GetBaseFolder());
   150 
   151 			if (!Directory.Exists(path))
   152 			{
   153 				Directory.CreateDirectory(path);
   154 			}
   155 
   156 			/* Create all dirs */
   157 			string [] paths = pkg.GetFolders();
   158 			for (int i=0; i<paths.GetLength(0); i++)
   159 			{
   160 				path = Config.PathFormat("{0}/{1}/{2}", cfg.pkg_path, pkg.GetBaseFolder(), paths[i]);
   161 				if (!Directory.Exists(path))
   162 				{
   163 					Directory.CreateDirectory(path);
   164 				}
   165 			}
   166 
   167 			/* Do primitive copies */
   168 			pkg.OnCopyFolders(this);
   169 			pkg.OnCopyFiles(this);
   170 
   171 			/* Do libraries */
   172 			Library [] libs = pkg.GetLibraries();
   173 			for (int i=0; i<libs.Length; i++)
   174 			{
   175 				if (!BuildLibrary(pkg, libs[i]))
   176 				{
   177 					throw new System.Exception("Failed to compile library: " + libs[i].binary_name);
   178 				}
   179 			}
   180 
   181 			/* Do plugins */
   182 			Plugin [] plugins = pkg.GetPlugins();
   183 			if (plugins != null)
   184 			{
   185 				for (int i=0; i<plugins.Length; i++)
   186 				{
   187 					if (!CompilePlugin(pkg, plugins[i]))
   188 					{
   189 						throw new System.Exception("Failed to compile plugin: " + plugins[i].Source);
   190 					}
   191 				}
   192 			}
   193 		}
   194 	}
   195 }