Minor changes to improve testability of helpers
[castle.git] / Experiments / Castle.MonoRail.Generator / App.cs
blobd575bc2c03a2eb0150aa3c69783f07de35794e29
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Generator
17 using System;
18 using System.Reflection;
19 using System.Collections;
20 using System.Collections.Specialized;
22 using Castle.MonoRail.Generator.Generators;
25 /// <summary>
26 ///
27 /// </summary>
28 public abstract class App
30 public static void Main()
32 IGenerator[] generators = CreateGenerators();
34 ShowHeader();
36 String[] args = Environment.GetCommandLineArgs();
38 if (args.Length == 1)
40 ShowUsage();
41 return;
43 else
45 IDictionary dic = BuildDictionary(args);
46 // bool found = false;
48 foreach(IGenerator generator in generators)
50 if (generator.Accept(args[1], dic, Console.Out))
52 // found = true;
53 generator.Execute(dic, Console.Out);
54 break;
58 // if (!found)
59 // {
60 // Console.Out.WriteLine("No generator found for '{0}'", args[1]);
61 // }
65 private static IDictionary BuildDictionary(String[] args)
67 HybridDictionary dic = new HybridDictionary(true);
69 for(int i=1; i < args.Length; i++)
71 String key = args[i];
72 String value = String.Empty;
74 int index = key.IndexOf(':');
76 if (index != -1)
78 key = args[i].Substring(0, index);
79 value = args[i].Substring(index+1);
80 value = NormalizeValue(value);
83 dic[key] = value;
86 return dic;
89 private static void ShowUsage()
91 Console.WriteLine("Usage: generator action options");
92 Console.WriteLine("");
93 Console.WriteLine("Actions:");
94 Console.WriteLine(" project");
95 Console.WriteLine(" controller");
96 Console.WriteLine("");
97 Console.WriteLine("Try 'generator action' for more information about the action");
100 private static void ShowHeader()
102 Console.WriteLine("MonoRail Generator - " + Assembly.GetExecutingAssembly().GetName().Version.ToString() );
103 Console.WriteLine("Released under Apache Software License 2.0");
104 Console.WriteLine("Copyright (c) 2004-2007 the original author/authors");
105 Console.WriteLine("http://www.castleproject.org/");
106 Console.WriteLine("");
107 Console.WriteLine("");
110 private static IGenerator[] CreateGenerators()
112 return new IGenerator[] { new ProjectGenerator(), new ControllerGenerator() };
115 private static string NormalizeValue(string value)
117 // TODO: remove quotes if present
118 return value;