Project.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. //
  2. // Project.cs: Project class
  3. //
  4. // Author:
  5. // Marek Sieradzki ([email protected])
  6. //
  7. // (C) 2005 Marek Sieradzki
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. #if NET_2_0
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Specialized;
  31. using System.IO;
  32. using System.Text;
  33. using System.Xml;
  34. using System.Xml.Schema;
  35. using Microsoft.Build.Framework;
  36. using Mono.XBuild.Framework;
  37. namespace Microsoft.Build.BuildEngine {
  38. public class Project : IProject {
  39. static string separator = ";";
  40. bool buildEnabled;
  41. IDictionary conditionedProperties;
  42. Encoding currentEncoding;
  43. string[] defaultTargets;
  44. IList directlyImportedProjects;
  45. BuildPropertyGroup environmentProperties;
  46. BuildItemGroup evaluatedItems;
  47. BuildItemGroup evaluatedItemsIgnoringCondition;
  48. IDictionary evaluatedItemsByName;
  49. IDictionary evaluatedItemsByNameIgnoringCondition;
  50. BuildPropertyGroup evaluatedProperties;
  51. string firstTargetName;
  52. string fullFileName;
  53. BuildPropertyGroup globalProperties;
  54. GroupingCollection groups;
  55. bool isDirty;
  56. bool isValidated;
  57. bool isReset;
  58. BuildItemGroupCollection itemGroups;
  59. IDictionary importedProjects;
  60. Engine parentEngine;
  61. BuildPropertyGroupCollection propertyGroups;
  62. BuildPropertyGroup reservedProperties;
  63. string schemaFile;
  64. TaskDatabase taskDatabase;
  65. TargetCollection targets;
  66. DateTime timeOfLastDirty;
  67. IList usingTaskElements;
  68. XmlDocument xmlDocument;
  69. XmlElement xmlElement;
  70. public Project ()
  71. : this (null)
  72. {
  73. }
  74. public Project (Engine engine)
  75. {
  76. parentEngine = engine;
  77. xmlDocument = new XmlDocument ();
  78. evaluatedItems = new BuildItemGroup (this);
  79. evaluatedItemsByName = CollectionsUtil.CreateCaseInsensitiveHashtable ();
  80. evaluatedItemsByNameIgnoringCondition = CollectionsUtil.CreateCaseInsensitiveHashtable ();
  81. evaluatedItemsIgnoringCondition = new BuildItemGroup (this);
  82. evaluatedProperties = new BuildPropertyGroup (false, null);
  83. groups = new GroupingCollection ();
  84. itemGroups = new BuildItemGroupCollection (groups);
  85. propertyGroups = new BuildPropertyGroupCollection (groups);
  86. targets = new TargetCollection (this);
  87. usingTaskElements = new ArrayList ();
  88. taskDatabase = new TaskDatabase ();
  89. }
  90. [MonoTODO]
  91. public void AddNewImport (string importLocation,
  92. string importCondition)
  93. {
  94. throw new NotImplementedException ();
  95. }
  96. [MonoTODO]
  97. public BuildItem AddNewItem (string itemName,
  98. string itemInclude)
  99. {
  100. throw new NotImplementedException ();
  101. }
  102. [MonoTODO]
  103. public BuildItemGroup AddNewItemGroup ()
  104. {
  105. throw new NotImplementedException ();
  106. }
  107. [MonoTODO]
  108. public BuildPropertyGroup AddNewPropertyGroup (bool insertAtEndOfProject)
  109. {
  110. throw new NotImplementedException ();
  111. }
  112. public bool Build (string[] targetNamesToBuild,
  113. IDictionary targetOutputs)
  114. {
  115. if (targetNamesToBuild.Length == 0) {
  116. if (defaultTargets.Length != 0) {
  117. targetNamesToBuild = defaultTargets;
  118. }
  119. else if (firstTargetName != null) {
  120. targetNamesToBuild = new string [1] { firstTargetName};
  121. }
  122. else
  123. return false;
  124. }
  125. foreach (string target in targetNamesToBuild) {
  126. if (BuildTarget (target, targetOutputs) == false) {
  127. return false;
  128. }
  129. }
  130. return true;
  131. }
  132. public bool BuildTarget (string targetName,
  133. IDictionary targetOutputs)
  134. {
  135. return BuildTargetWithFlags (targetName, targetOutputs, BuildSettings.None);
  136. }
  137. public bool BuildTargetWithFlags (string targetName,
  138. IDictionary targetOutputs,
  139. BuildSettings buildFlags)
  140. {
  141. if (targets.Exists (targetName) == false)
  142. throw new Exception ("Target specified to build does not exist.");
  143. this.targets [targetName].Build ();
  144. return true;
  145. }
  146. public string[] GetConditionedPropertyValues (string propertyName)
  147. {
  148. StringCollection sc = (StringCollection) conditionedProperties [propertyName];
  149. string[] propertyValues = new string [sc.Count];
  150. int i = 0;
  151. foreach (string propertyValue in sc)
  152. propertyValues [i++] = propertyValue;
  153. return propertyValues;
  154. }
  155. public string[] GetDirectlyImportedProjects ()
  156. {
  157. string[] dip = new string [directlyImportedProjects.Count];
  158. int i = 0;
  159. foreach (string importedProject in directlyImportedProjects)
  160. dip [i++] = importedProject;
  161. return dip;
  162. }
  163. public BuildItemGroup GetEvaluatedItemsByName (string itemName)
  164. {
  165. return (BuildItemGroup) evaluatedItemsByName [itemName];
  166. }
  167. public BuildItemGroup GetEvaluatedItemsByNameIgnoringCondition (string itemName)
  168. {
  169. return (BuildItemGroup) evaluatedItemsByNameIgnoringCondition [itemName];
  170. }
  171. public string GetEvaluatedProperty (string propertyName)
  172. {
  173. return evaluatedProperties [propertyName];
  174. }
  175. [MonoTODO]
  176. public string[] GetNonImportedItemNames ()
  177. {
  178. throw new NotImplementedException ();
  179. }
  180. [MonoTODO]
  181. public string[] GetNonImportedPropertyNames ()
  182. {
  183. throw new NotImplementedException ();
  184. }
  185. public string[] GetNonImportedTargetNames ()
  186. {
  187. ArrayList temporaryNonImportedTargets = new ArrayList ();
  188. foreach (Target target in targets)
  189. if (target.IsImported == false)
  190. temporaryNonImportedTargets.Add (target);
  191. string[] nonImportedTargetNames = new string [temporaryNonImportedTargets.Count];
  192. int i = 0;
  193. foreach (Target target in temporaryNonImportedTargets)
  194. nonImportedTargetNames [i++] = target.Name;
  195. return nonImportedTargetNames;
  196. }
  197. [MonoTODO]
  198. public string[] GetNonImportedUsingTasks ()
  199. {
  200. throw new NotImplementedException ();
  201. }
  202. [MonoTODO]
  203. public string GetProjectExtensions (string id)
  204. {
  205. throw new NotImplementedException ();
  206. }
  207. public void LoadFromFile (string projectFileName)
  208. {
  209. this.fullFileName = Path.GetFullPath (projectFileName);
  210. XmlSchemaCollection xmlSchemaCollection = null;
  211. XmlTextReader xmlTextReader = null;
  212. XmlValidatingReader xmlValidatingReader = null;
  213. if (this.schemaFile != null) {
  214. xmlSchemaCollection = new XmlSchemaCollection ();
  215. xmlSchemaCollection.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
  216. xmlSchemaCollection.Add (null, this.schemaFile);
  217. if (xmlSchemaCollection.Count > 0) {
  218. xmlTextReader = new XmlTextReader (projectFileName);
  219. xmlValidatingReader = new XmlValidatingReader (xmlTextReader);
  220. xmlValidatingReader.ValidationType = ValidationType.Schema;
  221. xmlValidatingReader.Schemas.Add (xmlSchemaCollection);
  222. xmlValidatingReader.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
  223. }
  224. } else {
  225. xmlTextReader = new XmlTextReader (projectFileName);
  226. }
  227. if (xmlValidatingReader != null)
  228. xmlDocument.Load (xmlValidatingReader);
  229. else if (xmlTextReader != null)
  230. xmlDocument.Load (xmlTextReader);
  231. else
  232. throw new Exception ();
  233. xmlElement = xmlDocument.DocumentElement;
  234. if (xmlElement.Name != "Project")
  235. throw new InvalidProjectFileException ("Invalid root element.");
  236. if (xmlElement.GetAttributeNode ("DefaultTargets") != null)
  237. defaultTargets = xmlElement.GetAttribute ("DefaultTargets").Split (';');
  238. else
  239. defaultTargets = new string [0];
  240. ProcessElements (xmlElement, null);
  241. isDirty = false;
  242. }
  243. public void LoadFromXml (XmlDocument projectXml)
  244. {
  245. fullFileName = "";
  246. xmlDocument = projectXml;
  247. xmlElement = xmlDocument.DocumentElement;
  248. if (xmlElement.Name != "Project")
  249. throw new InvalidProjectFileException ("Invalid root element.");
  250. if (xmlElement.GetAttributeNode ("DefaultTargets") != null)
  251. defaultTargets = xmlElement.GetAttribute ("DefaultTargets").Split (';');
  252. else
  253. defaultTargets = new string [0];
  254. ProcessElements (xmlElement, null);
  255. isDirty = false;
  256. }
  257. public void MarkProjectAsDirty ()
  258. {
  259. isDirty = true;
  260. }
  261. [MonoTODO]
  262. public void RemoveAllItemGroups ()
  263. {
  264. throw new NotImplementedException ();
  265. }
  266. [MonoTODO]
  267. public void RemoveAllItemsGroupsByCondition (string condition)
  268. {
  269. throw new NotImplementedException ();
  270. }
  271. [MonoTODO]
  272. public void RemoveAllPropertyGroups ()
  273. {
  274. throw new NotImplementedException ();
  275. }
  276. [MonoTODO]
  277. public void RemoveAllPropertyGroupsByCondition (string condition)
  278. {
  279. throw new NotImplementedException ();
  280. }
  281. [MonoTODO]
  282. public void RemoveItem (BuildItem itemToRemove)
  283. {
  284. throw new NotImplementedException ();
  285. }
  286. [MonoTODO]
  287. public void RemoveItemGroup (BuildItemGroup itemGroupToRemove)
  288. {
  289. throw new NotImplementedException ();
  290. }
  291. [MonoTODO]
  292. public void RemoveItemsByName (string itemName)
  293. {
  294. throw new NotImplementedException ();
  295. }
  296. [MonoTODO]
  297. public void RemovePropertyGroup (BuildPropertyGroup propertyGroupToRemove)
  298. {
  299. throw new NotImplementedException ();
  300. }
  301. [MonoTODO]
  302. public void ResetBuildStatus ()
  303. {
  304. throw new NotImplementedException ();
  305. }
  306. public void SaveToFile (string projectFileName)
  307. {
  308. xmlDocument.Save (projectFileName);
  309. }
  310. public void SaveToFile (string projectFileName,
  311. ProjectFileEncoding encoding)
  312. {
  313. SaveToFile (projectFileName);
  314. }
  315. public void SaveToTextWriter (TextWriter outTextWriter)
  316. {
  317. xmlDocument.Save (outTextWriter);
  318. }
  319. [MonoTODO]
  320. public void SetImportedProperty (string propertyName,
  321. string propertyValue,
  322. string condition,
  323. Project importProject)
  324. {
  325. throw new NotImplementedException ();
  326. }
  327. [MonoTODO]
  328. public void SetImportedPropertyAt (string propertyName,
  329. string propertyValue,
  330. string condition,
  331. Project importedProject,
  332. PropertyPosition position)
  333. {
  334. throw new NotImplementedException ();
  335. }
  336. [MonoTODO]
  337. public void SetProjectExtensions (string id, string xmlText)
  338. {
  339. throw new NotImplementedException ();
  340. }
  341. [MonoTODO]
  342. public void SetProperty (string propertyName,
  343. string propertyValue,
  344. string condition)
  345. {
  346. throw new NotImplementedException ();
  347. }
  348. [MonoTODO]
  349. public void SetPropertyAt (string propertyName,
  350. string propertyValue,
  351. string condition,
  352. PropertyPosition position)
  353. {
  354. throw new NotImplementedException ();
  355. }
  356. [MonoTODO]
  357. public void Unload ()
  358. {
  359. throw new NotImplementedException ();
  360. }
  361. private void ProcessElements (XmlElement rootElement, ImportedProject ip)
  362. {
  363. foreach (XmlNode xn in rootElement.ChildNodes) {
  364. if (xn is XmlElement) {
  365. XmlElement xe = (XmlElement) xn;
  366. switch (xe.Name) {
  367. case "ProjectExtensions":
  368. AddProjectExtensions (xe);
  369. break;
  370. case "Warning":
  371. case "Message":
  372. case "Error":
  373. AddMessage (xe);
  374. break;
  375. case "Target":
  376. AddTarget (xe, ip);
  377. break;
  378. case "UsingTask":
  379. AddUsingTask (xe, ip);
  380. break;
  381. case "Import":
  382. AddImport (xe, ip);
  383. break;
  384. case "ItemGroup":
  385. AddItemGroup (xe);
  386. break;
  387. case "PropertyGroup":
  388. AddPropertyGroup (xe);
  389. break;
  390. case "Choose":
  391. AddChoose (xe);
  392. break;
  393. default:
  394. throw new InvalidProjectFileException ("Invalid element in project file.");
  395. }
  396. }
  397. }
  398. }
  399. private void AddProjectExtensions (XmlElement xmlElement)
  400. {
  401. if (xmlElement == null)
  402. throw new ArgumentNullException ("xmlElement");
  403. }
  404. private void AddMessage (XmlElement xmlElement)
  405. {
  406. if (xmlElement == null)
  407. throw new ArgumentNullException ("xmlElement");
  408. }
  409. private void AddTarget (XmlElement xmlElement, ImportedProject importedProject)
  410. {
  411. if (xmlElement == null)
  412. throw new ArgumentNullException ("xmlElement");
  413. Target target = targets.AddNewTarget (xmlElement.GetAttribute ("Name"));
  414. target.BindToXml (xmlElement);
  415. if (importedProject == null) {
  416. target.IsImported = false;
  417. if (firstTargetName == null)
  418. firstTargetName = target.Name;
  419. } else
  420. target.IsImported = true;
  421. }
  422. private void AddUsingTask (XmlElement xmlElement, ImportedProject importedProject)
  423. {
  424. if (xmlElement == null)
  425. throw new ArgumentNullException ("xmlElement");
  426. if (xmlElement.GetAttribute ("TaskName") == String.Empty)
  427. throw new InvalidProjectFileException ("TaskName attribute must be specified.");
  428. usingTaskElements.Add (xmlElement);
  429. AssemblyLoadInfo loadInfo = null;
  430. string filename = null;
  431. string name = null;
  432. string taskName = xmlElement.GetAttribute ("TaskName");
  433. if (xmlElement.GetAttribute ("AssemblyName") != String.Empty) {
  434. name = xmlElement.GetAttribute ("AssemblyName");
  435. loadInfo = new AssemblyLoadInfo (name, taskName);
  436. taskDatabase.RegisterTask (taskName, loadInfo);
  437. } else if (xmlElement.GetAttribute ("AssemblyFile") != String.Empty) {
  438. filename = xmlElement.GetAttribute ("AssemblyFile");
  439. if (Path.IsPathRooted (filename) == false) {
  440. if (importedProject == null)
  441. filename = Path.Combine (Path.GetDirectoryName (fullFileName), filename);
  442. else
  443. filename = Path.Combine (Path.GetDirectoryName (importedProject.FullFileName), filename);
  444. }
  445. loadInfo = new AssemblyLoadInfo (LoadInfoType.AssemblyFilename, filename, null, null, null, null, taskName);
  446. taskDatabase.RegisterTask (taskName, loadInfo);
  447. } else
  448. throw new InvalidProjectFileException ("AssemblyName or AssemblyFile attribute must be specified.");
  449. }
  450. private void AddImport (XmlElement xmlElement, ImportedProject importingProject)
  451. {
  452. if (xmlElement == null)
  453. throw new ArgumentNullException ("xmlElement");
  454. string importedFile;
  455. Expression importedFileExpr;
  456. ImportedProject ImportedProject;
  457. importedFileExpr = new Expression (this, xmlElement.GetAttribute ("Project"));
  458. importedFile = (string) importedFileExpr.ToNonArray (typeof (string));
  459. if (importedFile == String.Empty)
  460. throw new InvalidProjectFileException ("Project attribute must be specified.");
  461. if (Path.IsPathRooted (importedFile) == false) {
  462. if (importingProject == null)
  463. importedFile = Path.Combine (Path.GetDirectoryName (fullFileName), importedFile);
  464. else
  465. importedFile = Path.Combine (Path.GetDirectoryName (importingProject.FullFileName), importedFile);
  466. }
  467. ImportedProject importedProject = new ImportedProject ();
  468. try {
  469. importedProject.Load (importedFile);
  470. ProcessElements (importedProject.XmlDocument.DocumentElement, importedProject);
  471. }
  472. catch (Exception ex) {
  473. Console.WriteLine (ex);
  474. }
  475. }
  476. private void AddItemGroup (XmlElement xmlElement)
  477. {
  478. if (xmlElement == null)
  479. throw new ArgumentNullException ("xmlElement");
  480. BuildItemGroup big = new BuildItemGroup (this);
  481. big.BindToXml (xmlElement);
  482. itemGroups.Add (big);
  483. }
  484. private void AddPropertyGroup (XmlElement xmlElement)
  485. {
  486. if (xmlElement == null)
  487. throw new ArgumentNullException ("xmlElement");
  488. BuildPropertyGroup bpg = new BuildPropertyGroup (true, this);
  489. bpg.BindToXml (xmlElement);
  490. propertyGroups.Add (bpg);
  491. }
  492. private void AddChoose (XmlElement xmlElement)
  493. {
  494. if (xmlElement == null)
  495. throw new ArgumentNullException ("xmlElement");
  496. }
  497. private static void ValidationCallBack (object sender, ValidationEventArgs e)
  498. {
  499. Console.WriteLine ("Validation Error: {0}", e.Message);
  500. }
  501. public bool BuildEnabled {
  502. get {
  503. return buildEnabled;
  504. }
  505. set {
  506. buildEnabled = value;
  507. }
  508. }
  509. public ProjectFileEncoding CurrentProjectFileEncoding {
  510. get {
  511. if (currentEncoding == Encoding.UTF8)
  512. return ProjectFileEncoding.Utf8;
  513. if (currentEncoding == Encoding.Unicode)
  514. return ProjectFileEncoding.Unicode;
  515. if (currentEncoding == Encoding.GetEncoding ("ANSI"))
  516. return ProjectFileEncoding.Ansi;
  517. throw new Exception ();
  518. }
  519. }
  520. public string DefaultTargets {
  521. get { return xmlElement.GetAttribute ("DefaultTargets"); }
  522. set {
  523. xmlElement.SetAttribute ("DefaultTargets",value);
  524. defaultTargets = value.Split (';');
  525. }
  526. }
  527. public BuildItemGroup EvaluatedItems {
  528. get { return evaluatedItems; }
  529. }
  530. public BuildItemGroup EvaluatedItemsIgnoringCondition {
  531. get { return evaluatedItemsIgnoringCondition; }
  532. }
  533. internal IDictionary EvaluatedItemsByName {
  534. get { return evaluatedItemsByName; }
  535. }
  536. internal IDictionary EvaluatedItemsByNameIgnoringCondition {
  537. get { return evaluatedItemsByNameIgnoringCondition; }
  538. }
  539. public BuildPropertyGroup EvaluatedProperties {
  540. get { return evaluatedProperties; }
  541. }
  542. public string FullFileName {
  543. get { return fullFileName; }
  544. set { fullFileName = value; }
  545. }
  546. public BuildPropertyGroup GlobalProperties {
  547. get { return globalProperties; }
  548. set {
  549. globalProperties = value;
  550. foreach (BuildProperty bp in globalProperties)
  551. evaluatedProperties.AddFromExistingProperty (bp);
  552. }
  553. }
  554. public bool IsDirty {
  555. get { return isDirty; }
  556. }
  557. public bool IsValidated {
  558. get { return isValidated; }
  559. set { isValidated = value; }
  560. }
  561. public BuildItemGroupCollection ItemGroups {
  562. get { return itemGroups; }
  563. }
  564. public Engine ParentEngine {
  565. get { return parentEngine; }
  566. }
  567. public BuildPropertyGroupCollection PropertyGroups {
  568. get { return propertyGroups; }
  569. }
  570. public string SchemaFile {
  571. get { return schemaFile; }
  572. set { schemaFile = value; }
  573. }
  574. public TargetCollection Targets {
  575. get { return targets; }
  576. }
  577. public DateTime TimeOfLastDirty {
  578. get { return timeOfLastDirty; }
  579. }
  580. public XmlDocument Xml {
  581. get { return xmlDocument; }
  582. }
  583. internal TaskDatabase TaskDatabase {
  584. get { return taskDatabase; }
  585. }
  586. internal BuildPropertyGroup EnvironmentProperties {
  587. set {
  588. environmentProperties = value;
  589. foreach (BuildProperty bp in environmentProperties)
  590. evaluatedProperties.AddFromExistingProperty (bp);
  591. }
  592. }
  593. internal BuildPropertyGroup ReservedProperties {
  594. set {
  595. reservedProperties = value;
  596. foreach (BuildProperty bp in reservedProperties)
  597. evaluatedProperties.AddFromExistingProperty (bp);
  598. }
  599. }
  600. }
  601. }
  602. #endif