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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
/* *******************************************************************
* Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
* All rights reserved.
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v 2.0
* which accompanies this distribution and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
*
* Contributors:
* Xerox/PARC initial implementation
* ******************************************************************/
package org.aspectj.internal.tools.ant.taskdefs;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
/**
* Check if version source file has the specified build version,
* and ensure a tag file reflects whether it does or not.
*/
public class VersionUptodate extends Task {
public VersionUptodate() {}
private String buildVersion;
private File versionSource;
private File versionTagFile;
/**
* @param buildVersion String expected as Version.text - required
*/
public void setVersion(String buildVersion) {
this.buildVersion = buildVersion;
}
/**
* @param versionSource the File Version.java containing text constant
* - required
*/
public void setVersionSourceFile(File versionSource) {
this.versionSource = versionSource;
}
/**
* @param versionTagFile the File whose existence signals that the version
* is uptodate after this task executes - required.
*/
public void setVersionTagFile(File versionTagFile) {
this.versionTagFile = versionTagFile;
}
/**
* If the Version.java source file contains the correct
* build version, then create the output tag file,
* else delete it if it exists.
* @throws BuildException if tagFile not creatable and version is incorrect
* or if version is correct and tagFile cannot be deleted.
*/
public void execute() throws BuildException {
if (null == buildVersion) {
throw new BuildException("require buildVersion");
}
if ((null == versionSource) || !versionSource.canRead()){
throw new BuildException("require versionSource");
}
if (null == versionTagFile){
throw new BuildException("require versionTagFile");
}
if (sameVersion(versionSource, buildVersion)) {
if (!versionTagFile.exists()) {
createFile(versionTagFile, buildVersion);
}
} else if (null == versionTagFile) {
throw new BuildException("no tag file, and version out of date");
} else if (versionTagFile.exists()) {
if (!versionTagFile.delete()) {
throw new BuildException("version out of date, but cannot delete " + versionTagFile);
}
}
}
/**
* Detect whether version is correct in Java sources.
* @param versionSource
* @param buildVersion
* @return boolean
*/
private boolean sameVersion(File versionSource, String buildVersion) {
// XXX build and load instead of scanning?
FileReader fileReader = null;
try {
fileReader = new FileReader(versionSource);
BufferedReader reader = new BufferedReader(fileReader);
String line;
while (null != (line = reader.readLine())) {
int loc = line.indexOf("static final String text = ");
if (-1 != loc) {
return (-1 != line.indexOf(buildVersion , loc));
}
}
return false;
} catch (IOException e) {
return false;
} finally {
if (null != fileReader) {
try {
fileReader.close();
} catch (IOException e) {
}
}
}
}
/**
* Create file with contents
*/
private void createFile(File versionTagFile, String contents) {
FileWriter writer = null;
try {
writer = new FileWriter(versionTagFile);
char[] buf = new char[contents.length()];
contents.getChars(0, buf.length, buf, 0);
writer.write(contents);
} catch (IOException e) {
throw new BuildException("writing " + versionTagFile, e);
} finally {
if (null != writer) {
try {
writer.close();
} catch (IOException e){
// ignore
}
}
}
}
}
|