/src/manostool/Driver.cs

http://github.com/jacksonh/manos · C# · 301 lines · 221 code · 57 blank · 23 comment · 27 complexity · eed1c21d955db474f1afddf83832dd05 MD5 · raw file

  1. //
  2. // Copyright (C) 2010 Jackson Harper (jackson@manosdemono.com)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. //
  24. using System;
  25. using System.IO;
  26. using System.Collections;
  27. using System.Collections.Generic;
  28. using NDesk.Options;
  29. namespace Manos.Tool
  30. {
  31. class Driver
  32. {
  33. public static readonly string COMPILED_TEMPLATES_ASSEMBLY = "CompiledTemplates.dll";
  34. public static readonly string TEMPLATES_DIRECTORY = "Templates";
  35. public static readonly string DEPLOYMENT_DIRECTORY = "Deployment";
  36. private static Environment Environment = new Environment ();
  37. private static StreamWriter output;
  38. public static int Main (string[] args)
  39. {
  40. args = ParseGlobalOptions (args);
  41. bool help = false;
  42. Func<IList<string>, int> command = null;
  43. var p = new OptionSet () {
  44. { "h|?|help", v => help = v != null },
  45. { "init|i", v => command = Init },
  46. { "server|s", v => command = Server },
  47. { "docs|d", v => command = Docs },
  48. { "build|b", v => command = Build },
  49. { "show-environment|se", v => command = ShowEnvironment },
  50. { "run|r=", v => command = a => { return Run(v, a); } },
  51. };
  52. List<string> extra = null;
  53. try {
  54. extra = p.Parse(args);
  55. } catch (OptionException){
  56. Console.WriteLine ("Try `manos --help' for more information.");
  57. return 1;
  58. }
  59. if (help) {
  60. ShowHelp (p);
  61. return 0;
  62. }
  63. if (command == null) {
  64. ShowHelp (p);
  65. return 1;
  66. }
  67. command (extra);
  68. return 0;
  69. }
  70. private static StreamWriter StreamForFile (string file)
  71. {
  72. if (file == null)
  73. throw new ArgumentNullException ("file");
  74. FileStream fs = new FileStream (file, FileMode.Create);
  75. StreamWriter sw = new StreamWriter (fs);
  76. sw.AutoFlush = true;
  77. return sw;
  78. }
  79. private static void SetOutput (string file)
  80. {
  81. Console.WriteLine ("setting output: " + file);
  82. output = StreamForFile (file);
  83. Console.SetOut (output);
  84. Console.SetError (output);
  85. }
  86. private static string [] ParseGlobalOptions (string [] args)
  87. {
  88. var p = new OptionSet () {
  89. { "-data-dir=", v => Environment.DataDirectory = v },
  90. { "-out|out=", v => SetOutput (v) },
  91. };
  92. List<string> extra = null;
  93. try {
  94. extra = p.Parse(args);
  95. } catch (OptionException){
  96. Console.WriteLine ("Try `manos --help' for more information.");
  97. return null;
  98. }
  99. if (extra == null)
  100. return null;
  101. return extra.ToArray ();
  102. }
  103. private static int Init (IList<string> args)
  104. {
  105. if (args.Count < 1) {
  106. Console.WriteLine ("manos --init <AppName>");
  107. Console.WriteLine ("This will initialize a new application with the supplied name.");
  108. }
  109. Driver d = new Driver ();
  110. try {
  111. Console.WriteLine ("initing: {0}", args [0]);
  112. d.Init (args [0]);
  113. } catch (Exception e) {
  114. Console.WriteLine ("error while initializing application:");
  115. Console.WriteLine (e);
  116. return 1;
  117. }
  118. return 0;
  119. }
  120. public void Init (string name)
  121. {
  122. InitCommand initer = new InitCommand (Environment, name);
  123. initer.Run ();
  124. }
  125. private static int Server (IList<string> args)
  126. {
  127. Driver d = new Driver ();
  128. try {
  129. d.RunServer (args);
  130. } catch (Exception e) {
  131. Console.WriteLine ("error while serving application:");
  132. Console.WriteLine (e);
  133. return 1;
  134. }
  135. return 0;
  136. }
  137. public void RunServer (IList<string> args)
  138. {
  139. string port = null;
  140. string securePort = null;
  141. string certFile = null;
  142. string keyFile = null;
  143. string user = null;
  144. string assembly = null;
  145. string ipaddress = null;
  146. var p = new OptionSet () {
  147. { "p|port=", v => port = v },
  148. { "P|secureport=", v => securePort = v },
  149. { "c|certfile=", v => certFile = v },
  150. { "k|keyfile=", v => keyFile = v },
  151. { "u|user=", v => user = v },
  152. { "a|assembly=", v=> assembly = v},
  153. { "l|listen=", v => ipaddress = v }
  154. };
  155. args = p.Parse(args);
  156. ServerCommand cmd = new ServerCommand (Environment, args);
  157. if(assembly != null)
  158. {
  159. cmd.ApplicationAssembly = assembly;
  160. }
  161. if (port != null) {
  162. int pt;
  163. if (!Int32.TryParse (port, out pt))
  164. throw new ArgumentException ("Port value is not an integer.");
  165. if (pt <= 0)
  166. throw new ArgumentOutOfRangeException ("port", "Port must be a positive integer.");
  167. cmd.Port = pt;
  168. }
  169. if (securePort != null) {
  170. if (certFile == null)
  171. throw new ArgumentException ("Certificate file required for TLS.");
  172. if (keyFile == null)
  173. throw new ArgumentException ("Certificate private key required for TLS.");
  174. int pt;
  175. if (!Int32.TryParse (securePort, out pt))
  176. throw new ArgumentException ("Secure port value is not an integer.");
  177. if (pt <= 0)
  178. throw new ArgumentOutOfRangeException ("secureport", "Secure port must be a positive integer.");
  179. cmd.SecurePort = pt;
  180. cmd.CertificateFile = certFile;
  181. cmd.KeyFile = keyFile;
  182. }
  183. if (user != null)
  184. cmd.User = user;
  185. if (ipaddress != null)
  186. cmd.IPAddress = ipaddress;
  187. cmd.Run ();
  188. }
  189. private static int Docs (IList<string> args)
  190. {
  191. Driver d = new Driver ();
  192. try {
  193. d.RunDocs ();
  194. } catch (Exception e) {
  195. Console.WriteLine ("error while serving application:");
  196. Console.WriteLine (e);
  197. return 1;
  198. }
  199. return 0;
  200. }
  201. public void RunDocs ()
  202. {
  203. DocsCommand cmd = new DocsCommand (Environment);
  204. cmd.Run ();
  205. }
  206. private static int Build (IList<string> args)
  207. {
  208. Driver d = new Driver ();
  209. try {
  210. d.RunBuild ();
  211. } catch (Exception e) {
  212. Console.WriteLine ("error while building application:");
  213. Console.WriteLine (e);
  214. return 1;
  215. }
  216. return 0;
  217. }
  218. public void RunBuild ()
  219. {
  220. BuildCommand cmd = new BuildCommand (Environment);
  221. cmd.Run ();
  222. }
  223. private static int ShowEnvironment (IList<string> args)
  224. {
  225. Console.WriteLine ("libdir: '{0}'", Environment.LibDirectory);
  226. Console.WriteLine ("manosdir: '{0}'", Environment.ManosDirectory);
  227. Console.WriteLine ("workingdir: '{0}'", Environment.WorkingDirectory);
  228. Console.WriteLine ("datadir: '{0}'", Environment.DataDirectory);
  229. Console.WriteLine ("datadir: '{0}'", Environment.DocsDirectory);
  230. return 1;
  231. }
  232. private static void ShowHelp (OptionSet os)
  233. {
  234. Console.WriteLine ("manos usage is: manos [command] [options]");
  235. Console.WriteLine ();
  236. os.WriteOptionDescriptions (Console.Out);
  237. }
  238. private static int Run (string app, IList<string> args)
  239. {
  240. RunCommand cmd = new RunCommand ();
  241. return cmd.Run (app, args);
  242. }
  243. }
  244. }