PageRenderTime 117ms CodeModel.GetById 97ms app.highlight 11ms RepoModel.GetById 5ms app.codeStats 0ms

/src/manostool/BuildCommand.cs

http://github.com/jacksonh/manos
C# | 192 lines | 137 code | 32 blank | 23 comment | 26 complexity | fdb209e53d50fcffea21db0e930adb96 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
 26using System;
 27using System.IO;
 28using System.Collections;
 29using System.Collections.Generic;
 30using System.Reflection;
 31using System.Diagnostics;
 32using Microsoft.CSharp;
 33using System.CodeDom.Compiler;
 34
 35namespace Manos.Tool
 36{
 37	public class BuildCommand
 38	{
 39		public static readonly string COMPILED_TEMPLATES = "Templates.dll";
 40		
 41		private string [] sources;
 42		private string [] ref_asm;
 43		private string output_name;
 44		
 45		public BuildCommand (Environment env)
 46		{
 47			Environment = env;
 48		}
 49		
 50		public Environment Environment {
 51			get;
 52			private set;
 53		}
 54		
 55		public string [] Sources {
 56			get {
 57				if (sources == null)
 58					sources = CreateSourcesList ();
 59				return sources;
 60			}
 61			set {
 62				if (value == null)
 63					throw new ArgumentNullException ("value");
 64				sources = value;
 65			}
 66		}
 67		
 68		public string OutputAssembly {
 69			get {
 70				if (output_name == null)
 71					return Path.GetFileName (Directory.GetCurrentDirectory ()) + ".dll";
 72				return output_name;
 73			}
 74			set {
 75				if (value == null)
 76					throw new ArgumentNullException ("value");
 77				output_name = value;
 78			}
 79		}
 80		
 81		public string [] ReferencedAssemblies {
 82			get {
 83				if (ref_asm == null)
 84					ref_asm = CreateReferencesList ();
 85				return ref_asm;
 86			}
 87			set {
 88				ref_asm = value;
 89			}
 90		}
 91		
 92		public void Run ()
 93		{
 94			if (RunXBuild ())
 95				return;
 96			if (RunMake ())
 97				return;
 98
 99			CompileCS ();
100		}
101
102		public bool RunXBuild ()
103		{
104			string [] slns = Directory.GetFiles (Directory.GetCurrentDirectory (), "*.sln");
105			if (slns.Length < 1)
106				return false;
107
108			foreach (string sln in slns) {
109				Process p = Process.Start ("xbuild", sln);
110				p.WaitForExit ();
111			}
112
113			return true;
114		}
115
116		public bool RunMake ()
117		{
118			if (!File.Exists ("Makefile") && !File.Exists ("makefile"))
119				return false;
120
121			Process p = Process.Start ("make");
122			p.WaitForExit ();
123			
124			return true;
125		}
126
127		public void CompileCS ()
128		{
129			var provider = new CSharpCodeProvider ();
130			var options = new CompilerParameters (ReferencedAssemblies, OutputAssembly, true);
131
132			
133			var results = provider.CompileAssemblyFromFile (options, Sources);
134			
135			if (results.Errors.Count > 0) {
136				foreach (var e in results.Errors) {
137					Console.WriteLine (e);	
138				}
139			}
140		}
141		
142		private string [] CreateSourcesList ()
143		{
144			List<string> sources = new List<string> ();
145			
146			FindCSFilesRecurse (Environment.WorkingDirectory, sources);
147			
148			return sources.ToArray ();
149		}
150		
151		private void FindCSFilesRecurse (string dir, List<string> sources)
152		{
153			sources.AddRange (Directory.GetFiles (dir, "*.cs"));
154			
155			foreach (string subdir in Directory.GetDirectories (dir)) {
156				if (dir == Environment.WorkingDirectory) {
157					if (subdir == "Content" || subdir == "Templates")
158						continue;
159				}
160				if (subdir.EndsWith (".exclude"))
161					continue;
162				FindCSFilesRecurse (subdir, sources);
163			}
164		}
165		
166		private string [] CreateReferencesList ()
167		{
168			var libs = new List<string> ();
169			
170			AddDefaultReferences (libs);
171
172			foreach (string lib in Directory.GetFiles (Directory.GetCurrentDirectory ())) {
173				if (!lib.EndsWith (".dll", StringComparison.InvariantCultureIgnoreCase))
174					continue;
175				if (Path.GetFileName (lib) == OutputAssembly)
176					continue;
177				libs.Add (lib);
178			}
179			
180			return libs.ToArray ();
181		}
182		
183		private void AddDefaultReferences (List<string> libs)
184		{
185			string manosdll = Path.Combine (Environment.ManosDirectory, "Manos.dll");
186			libs.Add (manosdll);
187
188			string ninidll = Path.Combine (Environment.ManosDirectory, "Nini.dll");
189			libs.Add (ninidll);
190		}
191	}
192}