2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / nant / src / Target.cs
1 // NAnt - A .NET build tool\r
2 // Copyright (C) 2001 Gerry Shaw\r
3 //\r
4 // This program is free software; you can redistribute it and/or modify\r
5 // it under the terms of the GNU General Public License as published by\r
6 // the Free Software Foundation; either version 2 of the License, or\r
7 // (at your option) any later version.\r
8 //\r
9 // This program is distributed in the hope that it will be useful,\r
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
12 // GNU General Public License for more details.\r
13 //\r
14 // You should have received a copy of the GNU General Public License\r
15 // along with this program; if not, write to the Free Software\r
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
17 //\r
18 // Gerry Shaw (gerry_shaw@yahoo.com)\r
19 // Ian MacLean (ian_maclean@another.com)\r
20 \r
21 namespace SourceForge.NAnt {\r
22 \r
23     using System;\r
24     using System.Collections.Specialized;\r
25     using System.Xml;\r
26 \r
27     public class Target {\r
28 \r
29         string _name;\r
30         Project _project;\r
31         bool _hasExecuted = false;\r
32         TaskCollection _tasks = new TaskCollection();\r
33         StringCollection _dependencies = new StringCollection();\r
34 \r
35         public Target(Project project) {\r
36             Project = project;\r
37         }\r
38 \r
39         public string Name {\r
40             get { return _name; }\r
41             set { _name = value; }\r
42         }\r
43 \r
44         public Project Project {\r
45             get { return _project; }\r
46             set { _project = value; }\r
47         }\r
48 \r
49         public bool HasExecuted {\r
50             get { return _hasExecuted; }\r
51         }\r
52 \r
53         public TaskCollection Tasks {\r
54             get { return _tasks; }\r
55         }\r
56 \r
57         public StringCollection Dependencies {\r
58             get { return _dependencies; }\r
59         }\r
60 \r
61         public void Initialize(XmlNode targetNode) {\r
62             // get target name\r
63             XmlNode nameNode = targetNode.SelectSingleNode("@name");\r
64             if (nameNode == null) {\r
65                 // TODO: add Location to exception\r
66                 throw new BuildException("target must have a name attribute");\r
67             }\r
68             Name = nameNode.Value;\r
69 \r
70             // add dependicies\r
71             XmlNode dependsNode = targetNode.SelectSingleNode("@depends");\r
72             if (dependsNode != null) {\r
73                 string depends = dependsNode.Value;\r
74                 foreach (string str in depends.Split(new char[]{','})) {\r
75                     string dependency = str.Trim();\r
76                     if (dependency.Length > 0) {\r
77                         Dependencies.Add(dependency);\r
78                     }\r
79                 }\r
80             }\r
81 \r
82             // select all the non-target nodes (these are global tasks for the project)\r
83             XmlNodeList taskList = targetNode.SelectNodes("*");\r
84             foreach (XmlNode taskNode in taskList) {\r
85                 Task task = Project.CreateTask(taskNode, this);\r
86                 if (task != null) {\r
87                     Tasks.Add(task);\r
88                 }\r
89             }\r
90         }\r
91 \r
92         public void Execute() {\r
93             if (!HasExecuted) {\r
94                 try {\r
95                     foreach (string targetName in Dependencies) {\r
96                         Target target = Project.Targets.Find(targetName);\r
97                         if (target == null) {\r
98                             // TODO: add Location to exception\r
99                             throw new BuildException(String.Format("unknown dependent target '{0}' of target '{1}'", targetName, Name));\r
100                         }\r
101                         target.Execute();\r
102                     }\r
103 \r
104                     Log.WriteLine();\r
105                     Log.WriteLine("{0}:", Name);\r
106                     foreach (Task task in Tasks) {\r
107                         task.Execute();\r
108                     }\r
109                 } finally {\r
110                     _hasExecuted = true;\r
111                 }\r
112             }\r
113         }\r
114     }\r
115 }\r