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