PageRenderTime 61ms CodeModel.GetById 42ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 25
 26
 27using System;
 28using System.IO;
 29using System.Collections;
 30using System.Collections.Generic;
 31
 32using NDesk.Options;
 33
 34namespace Manos.Tool
 35{
 36	class Driver
 37	{
 38		
 39		public static readonly string COMPILED_TEMPLATES_ASSEMBLY = "CompiledTemplates.dll";
 40		public static readonly string TEMPLATES_DIRECTORY = "Templates";
 41		public static readonly string DEPLOYMENT_DIRECTORY = "Deployment";
 42		
 43		private static Environment Environment = new Environment ();
 44
 45		private static StreamWriter output;
 46		
 47		public static int Main (string[] args)
 48		{
 49			args = ParseGlobalOptions (args);
 50			
 51			bool help = false;
 52			Func<IList<string>, int> command = null;
 53			
 54			var p = new OptionSet () {
 55				{ "h|?|help", v => help = v != null },
 56				{ "init|i", v => command = Init },
 57				{ "server|s", v => command = Server },
 58				{ "docs|d", v => command = Docs },
 59				{ "build|b", v => command = Build },
 60				{ "show-environment|se", v => command = ShowEnvironment },
 61				{ "run|r=", v => command = a => { return Run(v, a); } },
 62			};
 63			
 64			List<string> extra = null;
 65			try {
 66				extra = p.Parse(args);
 67			} catch (OptionException){
 68				Console.WriteLine ("Try `manos --help' for more information.");
 69				return 1;
 70			}
 71			
 72			if (help) {
 73				ShowHelp (p);
 74				return 0;
 75			}
 76			
 77			if (command == null) {
 78				ShowHelp (p);
 79				return 1;
 80			}
 81			
 82			command (extra);
 83			
 84			return 0;
 85		}
 86
 87		private static StreamWriter StreamForFile (string file)
 88		{
 89                        if (file == null)
 90                           throw new ArgumentNullException ("file");
 91
 92                        FileStream fs = new FileStream (file, FileMode.Create);
 93                        StreamWriter sw = new StreamWriter (fs);
 94			sw.AutoFlush = true;
 95
 96			return sw;
 97		}
 98
 99		private static void SetOutput (string file)
100		{
101			Console.WriteLine ("setting output:  " + file);
102			output = StreamForFile (file);
103			Console.SetOut (output);
104			Console.SetError (output);
105		}
106
107		private static string [] ParseGlobalOptions (string [] args)
108		{
109			var p = new OptionSet () {
110				{ "-data-dir=", v => Environment.DataDirectory = v },
111				{ "-out|out=", v => SetOutput (v) },
112			};
113			
114			List<string> extra = null;
115			try {
116				extra = p.Parse(args);
117			} catch (OptionException){
118				Console.WriteLine ("Try `manos --help' for more information.");
119				return null;
120			}
121			
122			if (extra == null)
123				return null;
124			
125			return extra.ToArray ();
126		}
127		
128		private static int Init (IList<string> args)
129		{
130			if (args.Count < 1) {
131				Console.WriteLine ("manos --init <AppName>");
132				Console.WriteLine ("This will initialize a new application with the supplied name.");
133			}
134				
135			Driver d = new Driver ();
136			
137			try {
138				Console.WriteLine ("initing: {0}", args [0]);
139				d.Init (args [0]);
140			} catch (Exception e) {
141				Console.WriteLine ("error while initializing application:");
142				Console.WriteLine (e);
143				return 1;
144			}
145			
146			return 0;
147		}
148		
149		public void Init (string name)
150		{
151			InitCommand initer = new InitCommand (Environment, name);
152			
153			initer.Run ();
154		}
155
156		private static int Server (IList<string> args)
157		{
158			Driver d = new Driver ();
159
160			
161			try {
162				d.RunServer (args);
163			} catch (Exception e) {
164				Console.WriteLine ("error while serving application:");
165				Console.WriteLine (e);
166				return 1;
167			}
168			
169			return 0;
170		}
171		
172		public void RunServer (IList<string> args)
173		{
174			string port = null;
175			string securePort = null;
176			string certFile = null;
177			string keyFile = null;
178			string user = null;
179			string assembly = null;
180			string ipaddress = null;
181			
182			var p = new OptionSet () {
183				{ "p|port=", v => port = v },
184				{ "P|secureport=", v => securePort = v },
185				{ "c|certfile=", v => certFile = v },
186				{ "k|keyfile=", v => keyFile = v },
187				{ "u|user=", v => user = v },
188				{ "a|assembly=", v=> assembly = v},
189				{ "l|listen=", v => ipaddress = v }
190			};
191			args = p.Parse(args);
192
193			ServerCommand cmd = new ServerCommand (Environment, args);
194			
195			if(assembly != null)
196			{
197				cmd.ApplicationAssembly = assembly;
198			}
199			
200			if (port != null) {
201				int pt;
202				if (!Int32.TryParse (port, out pt))
203					throw new ArgumentException ("Port value is not an integer.");
204				if (pt <= 0)
205					throw new ArgumentOutOfRangeException ("port", "Port must be a positive integer.");
206				cmd.Port = pt;
207			}
208			
209			if (securePort != null) {
210				if (certFile == null)
211					throw new ArgumentException ("Certificate file required for TLS.");
212				if (keyFile == null)
213					throw new ArgumentException ("Certificate private key required for TLS.");
214				int pt;
215				if (!Int32.TryParse (securePort, out pt))
216					throw new ArgumentException ("Secure port value is not an integer.");
217				if (pt <= 0)
218					throw new ArgumentOutOfRangeException ("secureport", "Secure port must be a positive integer.");
219				cmd.SecurePort = pt;
220				cmd.CertificateFile = certFile;
221				cmd.KeyFile = keyFile;
222			}
223
224			if (user != null)
225				cmd.User = user;
226
227			if (ipaddress != null)
228				cmd.IPAddress = ipaddress;
229
230			cmd.Run ();
231		}
232		
233		private static int Docs (IList<string> args)
234		{
235			Driver d = new Driver ();
236			
237			try {
238				d.RunDocs ();
239			} catch (Exception e) {
240				Console.WriteLine ("error while serving application:");
241				Console.WriteLine (e);
242				return 1;
243			}
244			
245			return 0;
246		}
247		
248		public void RunDocs ()
249		{
250			DocsCommand cmd = new DocsCommand (Environment);
251			
252			cmd.Run ();
253		}
254
255		private static int Build (IList<string> args)
256		{
257			Driver d = new Driver ();
258			
259			try {
260				d.RunBuild ();
261			} catch (Exception e) {
262				Console.WriteLine ("error while building application:");
263				Console.WriteLine (e);
264				return 1;
265			}
266			
267			return 0;
268		}
269		
270		public void RunBuild ()
271		{
272			BuildCommand cmd = new BuildCommand (Environment);
273			
274			cmd.Run ();
275		}
276
277		private static int ShowEnvironment (IList<string> args)
278		{
279			Console.WriteLine ("libdir: '{0}'", Environment.LibDirectory);
280			Console.WriteLine ("manosdir: '{0}'", Environment.ManosDirectory);
281			Console.WriteLine ("workingdir: '{0}'", Environment.WorkingDirectory);
282			Console.WriteLine ("datadir: '{0}'", Environment.DataDirectory);
283			Console.WriteLine ("datadir: '{0}'", Environment.DocsDirectory);
284
285			return 1;
286		}
287
288		private static void ShowHelp (OptionSet os)
289		{
290			Console.WriteLine ("manos usage is: manos [command] [options]");
291			Console.WriteLine ();
292			os.WriteOptionDescriptions (Console.Out);
293		}
294		
295		private static int Run (string app, IList<string> args)
296		{
297			RunCommand cmd = new RunCommand ();
298			return cmd.Run (app, args);
299		}
300	}
301}