|
1 |
/*******************************************************************************
|
|
2 |
* Copyright (c) 2004, 2010 IBM Corporation and others.
|
|
3 |
* All rights reserved. This program and the accompanying materials
|
|
4 |
* are made available under the terms of the Eclipse Public License v1.0
|
|
5 |
* which accompanies this distribution, and is available at
|
|
6 |
* http://www.eclipse.org/legal/epl-v10.html
|
|
7 |
*
|
|
8 |
* Contributors:
|
|
9 |
* IBM Corporation - initial API and implementation
|
|
10 |
*******************************************************************************/
|
|
11 |
package org.eclipse.pde.internal.build.site;
|
|
12 |
|
|
13 |
import com.ibm.icu.util.Calendar;
|
|
14 |
|
|
15 |
import java.io.BufferedReader;
|
|
16 |
import java.io.File;
|
|
17 |
import java.io.FileOutputStream;
|
|
18 |
import java.io.FileWriter;
|
|
19 |
import java.io.IOException;
|
|
20 |
import java.io.InputStreamReader;
|
|
21 |
import java.nio.charset.StandardCharsets;
|
|
22 |
import java.text.SimpleDateFormat;
|
|
23 |
import java.util.Properties;
|
|
24 |
import java.util.regex.Matcher;
|
|
25 |
import java.util.regex.Pattern;
|
|
26 |
|
|
27 |
import org.eclipse.pde.internal.build.AbstractScriptGenerator;
|
|
28 |
import org.eclipse.pde.internal.build.IBuildPropertiesConstants;
|
|
29 |
import org.osgi.framework.Version;
|
|
30 |
//import org.tmatesoft.svn.core.SVNDirEntry;
|
|
31 |
//import org.tmatesoft.svn.core.SVNURL;
|
|
32 |
//import org.tmatesoft.svn.core.io.SVNRepository;
|
|
33 |
//import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
|
|
34 |
//import org.tmatesoft.svn.core.wc.SVNClientManager;
|
|
35 |
//import org.tmatesoft.svn.core.wc.SVNInfo;
|
|
36 |
//import org.tmatesoft.svn.core.wc.SVNRevision;
|
|
37 |
//import org.tmatesoft.svn.core.wc.SVNWCClient;
|
|
38 |
|
|
39 |
public class QualifierReplacer implements IBuildPropertiesConstants {
|
|
40 |
private static final String PROPERTY_SVN = "svn";
|
|
41 |
// private static final String DOT_QUALIFIER = '.' + PROPERTY_QUALIFIER;
|
|
42 |
private static String globalQualifier = null;
|
|
43 |
|
|
44 |
|
|
45 |
private static void printDebug(String mess) {
|
|
46 |
FileWriter writer = null;
|
|
47 |
try {
|
|
48 |
writer = new FileWriter(new File(System.getProperty("java.io.tmpdir"), "pde_build.txt"), true);
|
|
49 |
writer.write(mess+"\n");
|
|
50 |
writer.close();
|
|
51 |
} catch(Exception e) {}
|
|
52 |
finally {
|
|
53 |
if (writer != null)
|
|
54 |
try {
|
|
55 |
writer.close();
|
|
56 |
} catch (IOException e) {
|
|
57 |
// TODO Auto-generated catch block
|
|
58 |
e.printStackTrace();
|
|
59 |
}
|
|
60 |
}
|
|
61 |
}
|
|
62 |
/**
|
|
63 |
* Replaces the 'qualifier' in the given version with the qualifier replacement.
|
|
64 |
*
|
|
65 |
* @param version version to replace qualifier in
|
|
66 |
* @param id id used when building a replacement using newVersions properties
|
|
67 |
* @param replaceTag replace qualifier with this tag instead of global qualifier, may be <code>null</code>
|
|
68 |
* @param newVersions properties that can be used to generate an alternative qualifier
|
|
69 |
* @return string version with qualifier replaced
|
|
70 |
*/
|
|
71 |
public static String replaceQualifierInVersion(String version, String id, String replaceTag, Properties newVersions) {
|
|
72 |
|
|
73 |
//printDebug("REPLACE version="+version+" id="+id+" replaceTag="+replaceTag+" newVersions="+newVersions);
|
|
74 |
//newVersions.put(PROPERTY_QUALIFIER, "fdp");
|
|
75 |
|
|
76 |
if (!AbstractScriptGenerator.getPropertyAsBoolean(IBuildPropertiesConstants.PROPERTY_PACKAGER_AS_NORMALIZER))
|
|
77 |
return version;
|
|
78 |
if (!version.endsWith(PROPERTY_QUALIFIER))
|
|
79 |
return version;
|
|
80 |
|
|
81 |
String newQualifier = null;
|
|
82 |
if (replaceTag == null || replaceTag.equalsIgnoreCase(PROPERTY_CONTEXT)) {
|
|
83 |
if (globalQualifier != null)
|
|
84 |
newQualifier = globalQualifier;
|
|
85 |
|
|
86 |
if (newQualifier == null && newVersions != null && newVersions.size() != 0) { //Skip the lookup in the file if there is no entries
|
|
87 |
newQualifier = (String) newVersions.get(getQualifierKey(id, version)); //First we check to see if there is a precise version
|
|
88 |
if (newQualifier == null) //If not found, then lookup for the id,0.0.0
|
|
89 |
newQualifier = (String) newVersions.get(id + ',' + Version.emptyVersion.toString());
|
|
90 |
if (newQualifier == null)
|
|
91 |
newQualifier = newVersions.getProperty(DEFAULT_MATCH_ALL);
|
|
92 |
}
|
|
93 |
|
|
94 |
|
|
95 |
if (newQualifier == null)
|
|
96 |
newQualifier = getDateQualifier();
|
|
97 |
} else if (replaceTag.equalsIgnoreCase(PROPERTY_NONE)) {
|
|
98 |
newQualifier = ""; //$NON-NLS-1$
|
|
99 |
} else if (replaceTag.equalsIgnoreCase(PROPERTY_SVN)) {
|
|
100 |
newQualifier = getSVNQualifier(id); //$NON-NLS-1$
|
|
101 |
} else {
|
|
102 |
newQualifier = replaceTag;
|
|
103 |
}
|
|
104 |
|
|
105 |
version = version.replaceFirst(PROPERTY_QUALIFIER, newQualifier);
|
|
106 |
if (version.endsWith(".")) //$NON-NLS-1$
|
|
107 |
version = version.substring(0, version.length() - 1);
|
|
108 |
return version;
|
|
109 |
}
|
|
110 |
|
|
111 |
/**
|
|
112 |
* find out SVN revision number of a project
|
|
113 |
* @param id
|
|
114 |
* @return
|
|
115 |
*/
|
|
116 |
public static String getSVNQualifier(String id) {
|
|
117 |
String projectDirectory = new File(System.getProperty("user.home"), "workspace047/"+id+"/").getAbsolutePath();
|
|
118 |
if (!projectDirectory.endsWith("/")) projectDirectory += "/";
|
|
119 |
|
|
120 |
try {
|
|
121 |
// SVNWCClient client = SVNClientManager.newInstance().getWCClient();
|
|
122 |
// SVNInfo info = client.doInfo(projectDirectory, SVNRevision.WORKING);
|
|
123 |
// SVNURL location = info.getURL();
|
|
124 |
// SVNRepository repository = SVNRepositoryFactory.create(location);
|
|
125 |
//
|
|
126 |
//// long latestRevision = repository.getLatestRevision();
|
|
127 |
//// System.out.println("[" + location.toString() + "] latest revision: " + latestRevision);
|
|
128 |
//
|
|
129 |
//
|
|
130 |
// SVNDirEntry entry = repository.info(".", -1);
|
|
131 |
//// System.out.println("[" + location.toString() + "] latest revision: " + entry.getRevision());
|
|
132 |
//// System.out.println("[" + location.toString() + "] latest date: " + entry.getDate());
|
|
133 |
// String svndate = entry.getDate()
|
|
134 |
//printDebug("svn info in "+projectDirectory);
|
|
135 |
String xml = getXML("svn","info","--xml", projectDirectory);
|
|
136 |
//printDebug("xml "+xml);
|
|
137 |
String rev = getRev(xml);
|
|
138 |
printDebug(id+"="+rev);
|
|
139 |
return rev;
|
|
140 |
/*
|
|
141 |
String svndate = null;
|
|
142 |
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd-HHmm");
|
|
143 |
//SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
|
|
144 |
return formatter.format(svndate);
|
|
145 |
*/
|
|
146 |
} catch (Exception e) {
|
|
147 |
e.printStackTrace();
|
|
148 |
return "svn_error_"+id+"_"+projectDirectory;
|
|
149 |
}
|
|
150 |
}
|
|
151 |
|
|
152 |
private static String getRev(String xml) {
|
|
153 |
Pattern p = Pattern.compile("<commit\n" +
|
|
154 |
" revision=\"([^\\\"]+)\"");
|
|
155 |
Matcher m = p.matcher(xml);
|
|
156 |
if (m.find()) {
|
|
157 |
return m.group(1).trim();
|
|
158 |
}
|
|
159 |
printDebug("Wrong XML ? "+xml);
|
|
160 |
return "error";
|
|
161 |
}
|
|
162 |
|
|
163 |
private static String getXML(String... args) throws IOException {
|
|
164 |
ProcessBuilder builder = new ProcessBuilder(args);
|
|
165 |
builder.redirectErrorStream(true);
|
|
166 |
Process process = builder.start();
|
|
167 |
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
|
|
168 |
StringBuilder sb = new StringBuilder();
|
|
169 |
String line = null;
|
|
170 |
while ((line = reader.readLine ()) != null) {
|
|
171 |
sb.append(line+"\n");
|
|
172 |
}
|
|
173 |
reader.close();
|
|
174 |
return sb.toString();
|
|
175 |
}
|
|
176 |
|
|
177 |
//given a version ending in "qualifier" return the key to look up the replacement
|
|
178 |
public static String getQualifierKey(String id, String version) {
|
|
179 |
if (version == null || !version.endsWith(PROPERTY_QUALIFIER))
|
|
180 |
return null;
|
|
181 |
|
|
182 |
Version osgiVersion = new Version(version);
|
|
183 |
String qualifier = osgiVersion.getQualifier();
|
|
184 |
qualifier = qualifier.substring(0, qualifier.length() - PROPERTY_QUALIFIER.length());
|
|
185 |
|
|
186 |
StringBuffer keyBuffer = new StringBuffer(id);
|
|
187 |
keyBuffer.append(',');
|
|
188 |
keyBuffer.append(osgiVersion.getMajor());
|
|
189 |
keyBuffer.append('.');
|
|
190 |
keyBuffer.append(osgiVersion.getMinor());
|
|
191 |
keyBuffer.append('.');
|
|
192 |
keyBuffer.append(osgiVersion.getMicro());
|
|
193 |
|
|
194 |
if (qualifier.length() > 0) {
|
|
195 |
keyBuffer.append('.');
|
|
196 |
keyBuffer.append(qualifier);
|
|
197 |
}
|
|
198 |
return keyBuffer.toString();
|
|
199 |
}
|
|
200 |
|
|
201 |
/**
|
|
202 |
* Returns the current date/time as a string to be used as a qualifier
|
|
203 |
* replacement. This is the default qualifier replacement. Will
|
|
204 |
* be of the form YYYYMMDDHHMM.
|
|
205 |
* @return current date/time as a qualifier replacement
|
|
206 |
*/
|
|
207 |
public static String getDateQualifier() {
|
|
208 |
final String empty = ""; //$NON-NLS-1$
|
|
209 |
int monthNbr = Calendar.getInstance().get(Calendar.MONTH) + 1;
|
|
210 |
String month = (monthNbr < 10 ? "0" : empty) + monthNbr; //$NON-NLS-1$
|
|
211 |
|
|
212 |
int dayNbr = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
|
|
213 |
String day = (dayNbr < 10 ? "0" : empty) + dayNbr; //$NON-NLS-1$
|
|
214 |
|
|
215 |
int hourNbr = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
|
|
216 |
String hour = (hourNbr < 10 ? "0" : empty) + hourNbr; //$NON-NLS-1$
|
|
217 |
|
|
218 |
int minuteNbr = Calendar.getInstance().get(Calendar.MINUTE);
|
|
219 |
String minute = (minuteNbr < 10 ? "0" : empty) + minuteNbr; //$NON-NLS-1$
|
|
220 |
|
|
221 |
return empty + Calendar.getInstance().get(Calendar.YEAR) + month + day + hour + minute;
|
|
222 |
}
|
|
223 |
|
|
224 |
/**
|
|
225 |
* Sets the global variable used as the qualifier replacement during calls to
|
|
226 |
* {@link #replaceQualifierInVersion(String, String, String, Properties)}
|
|
227 |
* Setting the qualifier to <code>null</code> will result in the default
|
|
228 |
* date qualifier being used.
|
|
229 |
* @param globalQualifier string replacement or <code>null</code>
|
|
230 |
*/
|
|
231 |
public static void setGlobalQualifier(String globalQualifier) {
|
|
232 |
if (globalQualifier == null || globalQualifier.length() == 0)
|
|
233 |
QualifierReplacer.globalQualifier = null;
|
|
234 |
else if (globalQualifier.length() > 0 && globalQualifier.charAt(0) != '$')
|
|
235 |
QualifierReplacer.globalQualifier = globalQualifier;
|
|
236 |
}
|
|
237 |
}
|
0 |
238 |
|