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