PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/BuildTools/LocalizationTool/LocalizationTool/ResXCompiler.cs

http://duplicati.googlecode.com/
C# | 230 lines | 191 code | 21 blank | 18 comment | 20 complexity | 9c876379a7b053057d5dea11a6e53436 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-2.0, Apache-2.0, GPL-3.0, CC-BY-SA-3.0
  1. #region Disclaimer / License
  2. // Copyright (C) 2011, Kenneth Skovhede
  3. // http://www.hexad.dk, opensource@hexad.dk
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public
  16. // License along with this library; if not, write to the Free Software
  17. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. //
  19. #endregion
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Text;
  23. using System.Linq;
  24. using System.Configuration;
  25. namespace LocalizationTool
  26. {
  27. public static class ResXCompiler
  28. {
  29. private static string ExecuteAndRead(string command, string arguments)
  30. {
  31. var psi = new System.Diagnostics.ProcessStartInfo(command, arguments);
  32. psi.RedirectStandardOutput = true;
  33. psi.UseShellExecute = false;
  34. var pi = System.Diagnostics.Process.Start(psi);
  35. pi.WaitForExit(5000);
  36. if (pi.HasExited)
  37. return pi.StandardOutput.ReadToEnd().Trim();
  38. return null;
  39. }
  40. public static void CompileResxFiles(string folder, List<string> excludeFolders, string @namespace, string assemblyname, string versionAssembly, string keyfile, string culture, string productname, string fxVer)
  41. {
  42. string resgenexe;
  43. string alexe;
  44. if (Duplicati.Library.Utility.Utility.IsClientLinux)
  45. {
  46. resgenexe = ExecuteAndRead("which", "resgen");
  47. alexe = ExecuteAndRead("which", "al");
  48. }
  49. else //Windows
  50. {
  51. //Order of these paths is also the search order
  52. string[] known_sdk_paths = null;
  53. if (fxVer == "2.0")
  54. {
  55. known_sdk_paths = new string[]
  56. {
  57. Environment.ExpandEnvironmentVariables("%WINDIR%\\Microsoft.Net\\Framework\\v2.0.50727\\"),
  58. Environment.ExpandEnvironmentVariables("%PROGRAMFILES%\\Microsoft.NET\\SDK\\v2.0\\bin\\"),
  59. Environment.ExpandEnvironmentVariables("%PROGRAMFILES(X86)%\\Microsoft.NET\\SDK\\v2.0\\bin\\"),
  60. };
  61. }
  62. else if (fxVer == "3.0")
  63. {
  64. known_sdk_paths = new string[]
  65. {
  66. Environment.ExpandEnvironmentVariables("%WINDIR%\\Microsoft.Net\\Framework\\v3.0\\"),
  67. Environment.ExpandEnvironmentVariables("%PROGRAMFILES%\\Microsoft SDKs\\Windows\\v6.0A\\bin\\"),
  68. Environment.ExpandEnvironmentVariables("%PROGRAMFILES(X86)%\\Microsoft SDKs\\Windows\\v6.0A\\bin\\"),
  69. };
  70. }
  71. else if (fxVer == "3.5")
  72. {
  73. known_sdk_paths = new string[]
  74. {
  75. Environment.ExpandEnvironmentVariables("%WINDIR%\\Microsoft.Net\\Framework\\v3.5\\"),
  76. Environment.ExpandEnvironmentVariables("%PROGRAMFILES%\\Microsoft SDKs\\Windows\\v7.0A\\bin\\"),
  77. Environment.ExpandEnvironmentVariables("%PROGRAMFILES(X86)%\\Microsoft SDKs\\Windows\\v7.0A\\bin\\"),
  78. };
  79. }
  80. else
  81. {
  82. known_sdk_paths = new string[]
  83. {
  84. Environment.ExpandEnvironmentVariables("%WINDIR%\\Microsoft.Net\\Framework\\v4.0\\"),
  85. Environment.ExpandEnvironmentVariables("%WINDIR%\\Microsoft.Net\\Framework\\v4.0.30319\\"),
  86. Environment.ExpandEnvironmentVariables("%PROGRAMFILES%\\Microsoft SDKs\\Windows\\v8.0A\\bin\\"),
  87. Environment.ExpandEnvironmentVariables("%PROGRAMFILES%\\Microsoft SDKs\\Windows\\v8.0A\\bin\\NETFX 4.0 Tools\\"),
  88. Environment.ExpandEnvironmentVariables("%PROGRAMFILES%\\Microsoft SDKs\\Windows\\v7.1\\Bin\\NETFX 4.0 Tools\\"),
  89. Environment.ExpandEnvironmentVariables("%PROGRAMFILES(X86)%\\Microsoft SDKs\\Windows\\v8.0A\\bin\\"),
  90. Environment.ExpandEnvironmentVariables("%PROGRAMFILES(X86)%\\Microsoft SDKs\\Windows\\v8.0A\\bin\\NETFX 4.0 Tools\\"),
  91. Environment.ExpandEnvironmentVariables("%PROGRAMFILES(X86)%\\Microsoft SDKs\\Windows\\v7.1\\Bin\\NETFX 4.0 Tools\\"),
  92. };
  93. }
  94. resgenexe = "resgen.exe";
  95. alexe = "al.exe";
  96. foreach(var p in known_sdk_paths)
  97. if (System.IO.File.Exists(System.IO.Path.Combine(p, resgenexe)))
  98. {
  99. resgenexe = System.IO.Path.Combine(p, resgenexe);
  100. break;
  101. }
  102. foreach (var p in known_sdk_paths)
  103. if (System.IO.File.Exists(System.IO.Path.Combine(p, alexe)))
  104. {
  105. alexe = System.IO.Path.Combine(p, alexe);
  106. break;
  107. }
  108. }
  109. if (!System.IO.File.Exists(resgenexe))
  110. {
  111. Console.WriteLine("Unable to locate file: {0}", resgenexe);
  112. Console.WriteLine("This can be fixed by installing a microsoft platform SDK, or visual studio (express is fine)");
  113. return;
  114. }
  115. if (!System.IO.File.Exists(alexe))
  116. {
  117. Console.WriteLine("Unable to locate file: {0}", alexe);
  118. Console.WriteLine("This can be fixed by installing the .Net framework version 2.0");
  119. return;
  120. }
  121. if (!string.IsNullOrEmpty(keyfile) && Duplicati.Library.Utility.Utility.IsClientLinux)
  122. keyfile = keyfile.Replace("\\", System.IO.Path.DirectorySeparatorChar.ToString());
  123. List<string> resources = new List<string>();
  124. folder = Duplicati.Library.Utility.Utility.AppendDirSeparator(folder);
  125. foreach (string s in Duplicati.Library.Utility.Utility.EnumerateFiles(folder))
  126. {
  127. if (s.ToLower().EndsWith("." + culture.ToLower() + ".resx"))
  128. {
  129. if (excludeFolders.Any(xf => s.ToLower().StartsWith(Duplicati.Library.Utility.Utility.AppendDirSeparator(xf).ToLower())))
  130. continue;
  131. string resname = System.IO.Path.ChangeExtension(s, ".resources");
  132. if (!System.IO.File.Exists(resname) || System.IO.File.GetLastWriteTime(resname) < System.IO.File.GetLastWriteTime(s))
  133. {
  134. Console.WriteLine("Compiling: " + s);
  135. System.Diagnostics.ProcessStartInfo pi = new System.Diagnostics.ProcessStartInfo(resgenexe, "\"" + s + "\"");
  136. pi.CreateNoWindow = true;
  137. pi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  138. pi.RedirectStandardOutput = true;
  139. pi.RedirectStandardError = true;
  140. pi.UseShellExecute = false;
  141. pi.WorkingDirectory = System.IO.Path.GetDirectoryName(s);
  142. System.Diagnostics.Process pr = System.Diagnostics.Process.Start(pi);
  143. pr.WaitForExit();
  144. if (pr.ExitCode != 0)
  145. {
  146. Console.WriteLine("Error");
  147. Console.WriteLine(pr.StandardOutput.ReadToEnd());
  148. Console.WriteLine(pr.StandardError.ReadToEnd());
  149. throw new Exception("Resgen failure: " + s);
  150. }
  151. }
  152. else
  153. Console.WriteLine("Not modified: " + s);
  154. resources.Add(resname);
  155. }
  156. }
  157. if (resources.Count == 0)
  158. return;
  159. if (!System.IO.File.Exists(versionAssembly) && Duplicati.Library.Utility.Utility.IsClientLinux)
  160. versionAssembly = versionAssembly.Replace("\\", System.IO.Path.DirectorySeparatorChar.ToString());
  161. if (!System.IO.File.Exists(versionAssembly))
  162. {
  163. Console.WriteLine("Unable to locate file: {0}", versionAssembly);
  164. Console.WriteLine("This can be fixed by compiling the application or modifying the file configuration.xml");
  165. return;
  166. }
  167. using (Duplicati.Library.Utility.TempFile tf = new Duplicati.Library.Utility.TempFile())
  168. {
  169. using (System.IO.StreamWriter sw = new System.IO.StreamWriter(tf))
  170. {
  171. System.Reflection.Assembly asm = System.Reflection.Assembly.ReflectionOnlyLoadFrom(versionAssembly);
  172. sw.WriteLine("/t:lib");
  173. sw.WriteLine("/out:\"" + assemblyname + "\"");
  174. sw.WriteLine("/product:\"" + productname + "\"");
  175. sw.WriteLine("/title:\"" + productname + "\"");
  176. sw.WriteLine("/version:" + asm.GetName().Version.ToString());
  177. if (!string.IsNullOrEmpty(keyfile))
  178. sw.WriteLine("/keyfile:\"" + keyfile + "\"");
  179. sw.WriteLine("/culture:" + culture);
  180. foreach (string s in resources)
  181. {
  182. string resname = s.Substring(folder.Length);
  183. resname = resname.Replace("\\", ".");
  184. resname = resname.Replace(" ", "_");
  185. resname = @namespace + "." + resname;
  186. sw.WriteLine("/embed:\"" + s + "\"," + resname);
  187. }
  188. }
  189. Console.WriteLine("Linking ...");
  190. System.Diagnostics.ProcessStartInfo pi = new System.Diagnostics.ProcessStartInfo(alexe, "@\"" + tf + "\"");
  191. pi.CreateNoWindow = true;
  192. pi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  193. System.Diagnostics.Process pr = System.Diagnostics.Process.Start(pi);
  194. pr.WaitForExit();
  195. if (pr.ExitCode != 0)
  196. throw new Exception("Linker failure");
  197. }
  198. }
  199. }
  200. }