PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/SignalR.ProxyGenerator/Program.cs

https://github.com/kpmrafeeq/SignalR
C# | 224 lines | 187 code | 36 blank | 1 comment | 18 complexity | 7b2181a8a538f93bec5156c92b02e894 MD5 | raw file
Possible License(s): MIT
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Reflection;
  7. using System.Text.RegularExpressions;
  8. using Microsoft.Ajax.Utilities;
  9. namespace SignalR.ProxyGenerator
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. bool minify = false;
  16. bool absolute = false;
  17. string path = null;
  18. string outputPath = null;
  19. string url = null;
  20. ParseArguments(args, out url, out minify, out absolute, out path, out outputPath);
  21. if (String.IsNullOrEmpty(outputPath))
  22. {
  23. outputPath = ".";
  24. }
  25. outputPath = Path.GetFullPath(outputPath);
  26. if (String.IsNullOrWhiteSpace(Path.GetExtension(outputPath)))
  27. {
  28. outputPath = Path.Combine(outputPath, "server.js");
  29. }
  30. if (!String.IsNullOrEmpty(url) && String.IsNullOrEmpty(path))
  31. {
  32. OutputHubsFromUrl(url, outputPath, minify, absolute);
  33. }
  34. else
  35. {
  36. OutputHubs(path, url, outputPath, minify);
  37. }
  38. }
  39. private static void OutputHubs(string path, string url, string outputPath, bool minify)
  40. {
  41. path = path ?? Directory.GetCurrentDirectory();
  42. url = url ?? "/signalr";
  43. var assemblies = Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories);
  44. var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
  45. Console.WriteLine("Creating temp directory {0}", tempPath);
  46. Directory.CreateDirectory(tempPath);
  47. // Copy all assemblies to temp
  48. foreach (var assemblyPath in assemblies)
  49. {
  50. Copy(assemblyPath, tempPath);
  51. }
  52. Copy(typeof(Program).Assembly.Location, tempPath);
  53. var setup = new AppDomainSetup
  54. {
  55. ApplicationBase = tempPath
  56. };
  57. var domain = AppDomain.CreateDomain("hubs", AppDomain.CurrentDomain.Evidence, setup);
  58. var minifier = new Minifier();
  59. var generator = (JavascriptGenerator)domain.CreateInstanceAndUnwrap(typeof(Program).Assembly.FullName,
  60. typeof(JavascriptGenerator).FullName);
  61. var js = generator.GenerateProxy(path, url);
  62. Generate(outputPath, minify, minifier, js);
  63. }
  64. private static void Copy(string sourcePath, string destinationPath)
  65. {
  66. string target = Path.Combine(destinationPath, Path.GetFileName(sourcePath));
  67. File.Copy(sourcePath, target, overwrite: true);
  68. }
  69. private static void OutputHubsFromUrl(string url, string outputPath, bool minify, bool absolute)
  70. {
  71. string baseUrl = null;
  72. if (!url.EndsWith("/"))
  73. {
  74. url += "/";
  75. }
  76. if (!url.EndsWith("signalr"))
  77. {
  78. url += "signalr/";
  79. }
  80. baseUrl = url;
  81. if (!url.EndsWith("hubs", StringComparison.OrdinalIgnoreCase))
  82. {
  83. url += "hubs";
  84. }
  85. var uri = new Uri(url);
  86. var minifier = new Minifier();
  87. var wc = new WebClient();
  88. string js = wc.DownloadString(uri);
  89. if (absolute)
  90. {
  91. js = Regex.Replace(js, @"=(\w+)\(""(.*?/signalr)""\)", m =>
  92. {
  93. return "=" + m.Groups[1].Value + "(\"" + baseUrl + "\")";
  94. });
  95. }
  96. Generate(outputPath, minify, minifier, js);
  97. }
  98. private static void Generate(string outputPath, bool minify, Minifier minifier, string js)
  99. {
  100. if (minify)
  101. {
  102. File.WriteAllText(outputPath, minifier.MinifyJavaScript(js));
  103. }
  104. else
  105. {
  106. File.WriteAllText(outputPath, js);
  107. }
  108. }
  109. private static void ParseArguments(string[] args, out string url, out bool minify, out bool absolute, out string path, out string outputPath)
  110. {
  111. minify = false;
  112. absolute = false;
  113. path = null;
  114. url = null;
  115. outputPath = null;
  116. foreach (var a in args)
  117. {
  118. if (!a.StartsWith("/"))
  119. {
  120. continue;
  121. }
  122. KeyValuePair<string, string> arg = ParseArg(a);
  123. switch (arg.Key)
  124. {
  125. case "minify":
  126. minify = true;
  127. break;
  128. case "absolute":
  129. absolute = true;
  130. break;
  131. case "path":
  132. path = arg.Value;
  133. break;
  134. case "url":
  135. url = arg.Value;
  136. break;
  137. case "o":
  138. outputPath = arg.Value;
  139. break;
  140. default:
  141. break;
  142. }
  143. }
  144. }
  145. private static KeyValuePair<string, string> ParseArg(string arg)
  146. {
  147. arg = arg.Substring(1);
  148. if (arg.Contains(":"))
  149. {
  150. var splitIndex = arg.IndexOf(':');
  151. var key = arg.Substring(0, splitIndex).Trim();
  152. var value = arg.Substring(splitIndex + 1).Trim();
  153. return new KeyValuePair<string, string>(key, value);
  154. }
  155. return new KeyValuePair<string, string>(arg.Trim(), null);
  156. }
  157. public class JavascriptGenerator : MarshalByRefObject
  158. {
  159. public string GenerateProxy(string path, string url)
  160. {
  161. foreach (var assemblyPath in Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories))
  162. {
  163. Assembly.Load(AssemblyName.GetAssemblyName(assemblyPath));
  164. }
  165. var signalrAssembly = (from a in AppDomain.CurrentDomain.GetAssemblies()
  166. where a.GetName().Name.Equals("SignalR", StringComparison.OrdinalIgnoreCase)
  167. select a).FirstOrDefault();
  168. if (signalrAssembly == null)
  169. {
  170. return null;
  171. }
  172. Type resolverType = signalrAssembly.GetType("SignalR.DefaultDependencyResolver");
  173. if (resolverType == null)
  174. {
  175. return null;
  176. }
  177. Type proxyGeneratorType = signalrAssembly.GetType("SignalR.Hubs.DefaultJavaScriptProxyGenerator");
  178. if (proxyGeneratorType == null)
  179. {
  180. return null;
  181. }
  182. object resolver = Activator.CreateInstance(resolverType);
  183. dynamic proxyGenerator = Activator.CreateInstance(proxyGeneratorType, resolver);
  184. return proxyGenerator.GenerateProxy(url, true);
  185. }
  186. }
  187. }
  188. }