Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1974 jgill 1
package com.erggroup.mass.ant;
2
 
3
import java.io.*;
4
import java.util.ArrayList;
5
import java.util.Collection;
6
import org.apache.tools.ant.BuildException;
7
import org.apache.tools.ant.Project;
8
import org.apache.tools.ant.taskdefs.Copy;
9
import org.apache.tools.ant.taskdefs.Echo;
10
import org.apache.tools.ant.types.FileSet;
11
 
12
public class SandboxElement implements JatsElement
13
{
14
	private Jats parent;
15
	private Project project;
16
	private String name;
17
	private String version;
18
	private String includes; //= "**/*";
19
	private String excludes;
20
	private Boolean verbose;
21
	private boolean runtime = false;
22
	private static Collection sandboxed = new ArrayList();
23
 
24
	public String getElementType()
25
	{
26
		return "sandbox";
27
	}
28
 
29
	public void execute(Jats parent)
30
	{
31
		this.parent = parent;
32
		this.project = parent.getProject();
33
 
34
		if (name == null || version == null)
35
		{
36
			throw new BuildException("Both \'name\' and \'version\' must be specified for <sandbox/>");
37
		}
38
 
39
		if ( isVerbose() )
40
		{
41
			this.doEcho("Updating package \'" + name + "\' version \'" + version + "\'." );
42
		}
43
 
44
		Copy copyTask = new Copy();
45
		copyTask.setProject(project);
46
		copyTask.setTaskName("sandbox");
47
		copyTask.setTodir(parent.getTodir());
48
		copyTask.setIncludeEmptyDirs(false);
49
 
50
		FileSet set = getFileSet();
51
		set.setProject(project);
52
 
53
		if (set != null)
54
		{
55
			copyTask.addFileset(set);
56
		}
57
 
58
		copyTask.perform();
59
 
60
		sandboxed.add(this);
61
 
62
		PackageRef packageRef = new PackageRef();
63
		packageRef.setProject(project);
64
		copyTask.setTaskName("sandbox");
65
		packageRef.setPackage(name);
66
		packageRef.setVersion(version);
67
		packageRef.setVerbose( "false" );
68
		packageRef.execute();
69
	}
70
 
71
	private FileSet getFileSet()
72
	{
73
		if (name == null || version == null)
74
		{
75
			throw new BuildException("Either 'name' or 'version' was not specifies for <jats> <sandbox>");
76
		}
77
 
78
		FileSet fileSet = null;
79
 
80
		File projectNameDir = new File(parent.getFromdir(), name);
81
		File projectVersionDir = new File(projectNameDir, version);
82
		if (projectVersionDir.exists() == false || projectVersionDir.isDirectory() == false)
83
		{
84
			throw new BuildException("Directory " + projectVersionDir.toString() +
85
							 " for package \'" + name
86
							 + "\' version \'" + version
87
							 + "\' does not exist");
88
		}
89
		else
90
		{
91
			validatePackageDescriptionFile(projectVersionDir);
92
 
93
			// Setup the file set
94
			fileSet = new FileSet();
95
			fileSet.setProject(project);
96
			fileSet.setDir(projectVersionDir);
97
			if ( includes == null )
98
			{
99
				includes = parent.getIncludes();
100
			}
101
			fileSet.setIncludes(includes);
102
			fileSet.setExcludes("descpkg");
103
			if (excludes != null)
104
			{
105
				fileSet.setExcludes(excludes);
106
			}
107
		}
108
 
109
		return fileSet;
110
	}
111
 
112
	private void doEcho(String message)
113
	{
114
		Echo echo = new Echo();
115
		echo.setTaskName(this.getElementType());
116
		echo.setProject(this.project);
117
		echo.setMessage(message);
118
		echo.execute();
119
	}
120
 
121
	private void validatePackageDescriptionFile(File location)
122
	{
123
		// Look for the descpkg file and determine whether it contains the correct info.
124
		//
125
		File descpkg = new File(location, "descpkg");
126
		if (descpkg.exists() == false || descpkg.isFile() == false)
127
		{
128
			throw new BuildException("The \'descpkg\' file for package \'" + name
129
								   + "\' version \'" + version
130
								   + "\' does not exist");
131
		}
132
		else
133
		{
134
			// Read in the file, and determine whether in contain
135
			// the correct 'version' string.
136
			//
137
			try
138
			{
139
				FileReader reader = new FileReader(descpkg);
140
 
141
				BufferedReader buffer = new BufferedReader(reader);
142
				try
143
				{
144
					String line = buffer.readLine();
145
					String strippedLine = line.trim();
146
					/** @todo currently there is an issue with some descpkg file
147
					  * not having the correct format (i.e. 1.2.2 cr instead of 1.2.2.cr)
148
					  */
149
					if (line == null /*|| strippedLine.endsWith(version) == false*/)
150
					{
151
						throw new BuildException("The \'descpkg\' file for package \'" + name
152
										   + "\' version \'" + version
153
										   + "\' does contain the correct version info.");
154
					}
155
				}
156
				catch (IOException ex)
157
				{
158
					throw new BuildException("The \'descpkg\' file for package \'" + name
159
										   + "\' version \'" + version
160
										   + "\' could not be read.");
161
				}
162
			}
163
			catch (FileNotFoundException ex)
164
			{
165
				throw new BuildException("The \'descpkg\' file for package \'" + name
166
								   + "\' version \'" + version
167
								   + "\' does not exist");
168
			}
169
		}
170
	}
171
 
172
	public void setName(String name)
173
	{
174
		this.name = name;
175
	}
176
 
177
	public void setVersion(String version)
178
	{
179
		this.version = version;
180
	}
181
 
182
	public void setIncludes(String includes)
183
	{
184
		this.includes = includes;
185
	}
186
 
187
	public void setExcludes(String excludes)
188
	{
189
		this.excludes = excludes;
190
	}
191
 
192
	public void setRuntime( String newValue )
193
	{
194
		this.runtime = ( "true".equalsIgnoreCase(newValue) );
195
	}
196
 
197
	static Collection getSandboxElements()
198
	{
199
		return sandboxed;
200
	}
201
 
202
	public String getName()
203
	{
204
		return name;
205
	}
206
 
207
	public String getVersion()
208
	{
209
		return version;
210
	}
211
 
212
	public boolean isRuntime()
213
	{
214
		return runtime;
215
	}
216
 
217
	public void setVerbose( String newValue )
218
	{
219
		this.verbose = new Boolean(!( "false".equalsIgnoreCase(newValue) ));
220
	}
221
 
222
	public boolean isVerbose()
223
	{
224
		if ( verbose == null )
225
		{
226
			return parent.isVerbose();
227
		}
228
		return verbose.booleanValue();
229
	}
230
}