Révision 3066

tmp/org.eclipse.pde.hack/src/org/eclipse/pde/internal/build/site/QualifierReplacer.java (revision 3066)
10 10
 *******************************************************************************/
11 11
package org.eclipse.pde.internal.build.site;
12 12

  
13
import com.ibm.icu.util.Calendar;
14

  
15 13
import java.io.BufferedReader;
16 14
import java.io.File;
17
import java.io.FileOutputStream;
18 15
import java.io.FileWriter;
19 16
import java.io.IOException;
20 17
import java.io.InputStreamReader;
21 18
import java.nio.charset.StandardCharsets;
22
import java.text.SimpleDateFormat;
23 19
import java.util.Properties;
24
import java.util.regex.Matcher;
25
import java.util.regex.Pattern;
26 20

  
27 21
import org.eclipse.pde.internal.build.AbstractScriptGenerator;
28 22
import org.eclipse.pde.internal.build.IBuildPropertiesConstants;
......
36 30
//import org.tmatesoft.svn.core.wc.SVNRevision;
37 31
//import org.tmatesoft.svn.core.wc.SVNWCClient;
38 32

  
33
import com.ibm.icu.util.Calendar;
34

  
39 35
public class QualifierReplacer implements IBuildPropertiesConstants {
36
	
40 37
	private static final String PROPERTY_SVN = "svn";
41
	//	private static final String DOT_QUALIFIER = '.' + PROPERTY_QUALIFIER;
38
	
39
	// private static final String DOT_QUALIFIER = '.' + PROPERTY_QUALIFIER;
42 40
	private static String globalQualifier = null;
43

  
44

  
41
	
42
	
45 43
	private static void printDebug(String mess) {
46 44
		FileWriter writer = null;
47 45
		try {
48 46
			writer = new FileWriter(new File(System.getProperty("java.io.tmpdir"), "pde_build.txt"), true);
49
			writer.write(mess+"\n");
47
			writer.write(mess + "\n");
50 48
			writer.close();
51
		} catch(Exception e) {}
49
		}
50
		catch (Exception e) {
51
		}
52 52
		finally {
53 53
			if (writer != null)
54 54
				try {
55
					writer.close();
56
				} catch (IOException e) {
57
					// TODO Auto-generated catch block
58
					e.printStackTrace();
55
				writer.close();
59 56
				}
57
				catch (IOException e) {
58
				// TODO Auto-generated catch block
59
				e.printStackTrace();
60
				}
60 61
		}
61 62
	}
63
	
62 64
	/**
63 65
	 * Replaces the 'qualifier' in the given version with the qualifier replacement.
64 66
	 * 
......
69 71
	 * @return string version with qualifier replaced
70 72
	 */
71 73
	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

  
74
		
75
		// printDebug("REPLACE version="+version+" id="+id+" replaceTag="+replaceTag+" newVersions="+newVersions);
76
		// newVersions.put(PROPERTY_QUALIFIER, "fdp");
77
		
76 78
		if (!AbstractScriptGenerator.getPropertyAsBoolean(IBuildPropertiesConstants.PROPERTY_PACKAGER_AS_NORMALIZER))
77 79
			return version;
78 80
		if (!version.endsWith(PROPERTY_QUALIFIER))
79 81
			return version;
80

  
82
		
81 83
		String newQualifier = null;
82 84
		if (replaceTag == null || replaceTag.equalsIgnoreCase(PROPERTY_CONTEXT)) {
83 85
			if (globalQualifier != null)
84 86
				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
87
			
88
			if (newQualifier == null && newVersions != null && newVersions.size() != 0) { // Skip the lookup in the file if there is no entries
89
				newQualifier = (String) newVersions.get(getQualifierKey(id, version)); // First we check to see if there is a precise version
90
				if (newQualifier == null) // If not found, then lookup for the id,0.0.0
89 91
					newQualifier = (String) newVersions.get(id + ',' + Version.emptyVersion.toString());
90 92
				if (newQualifier == null)
91 93
					newQualifier = newVersions.getProperty(DEFAULT_MATCH_ALL);
92 94
			}
93

  
94

  
95
			
96
			
95 97
			if (newQualifier == null)
96 98
				newQualifier = getDateQualifier();
97
		} else if (replaceTag.equalsIgnoreCase(PROPERTY_NONE)) {
99
		}
100
		else if (replaceTag.equalsIgnoreCase(PROPERTY_NONE)) {
98 101
			newQualifier = ""; //$NON-NLS-1$
99
		} else if (replaceTag.equalsIgnoreCase(PROPERTY_SVN)) {
100
			newQualifier = getSVNQualifier(id); //$NON-NLS-1$
101
		} else {
102
		}
103
		else if (replaceTag.equalsIgnoreCase(PROPERTY_SVN)) {
104
			newQualifier = getSVNQualifier(id);
105
		}
106
		else {
102 107
			newQualifier = replaceTag;
103 108
		}
104

  
109
		
105 110
		version = version.replaceFirst(PROPERTY_QUALIFIER, newQualifier);
106 111
		if (version.endsWith(".")) //$NON-NLS-1$
107 112
			version = version.substring(0, version.length() - 1);
108 113
		return version;
109 114
	}
110

  
115
	
111 116
	/**
112 117
	 * find out the last SVN revision number of a project
113 118
	 * 
......
115 120
	 * @return
116 121
	 */
117 122
	public static String getSVNQualifier(String id) {
118
		File projectDirectory = new File(System.getProperty("user.home"), "workspace047/"+id+"/");
119
		File svnDirectory = new File(projectDirectory, ".svn"); 
123
		File projectDirectory = new File(System.getProperty("user.home"), "workspace047/" + id + "/");
124
		File svnDirectory = new File(projectDirectory, ".svn");
120 125
		String projectDirectoryPath = projectDirectory.getAbsolutePath();
121 126
		if (!projectDirectoryPath.endsWith("/")) projectDirectoryPath += "/";
122 127
		
123 128
		try {
124
			//		SVNWCClient client = SVNClientManager.newInstance().getWCClient();
125
			//		SVNInfo info = client.doInfo(projectDirectory, SVNRevision.WORKING);
126
			//		SVNURL location = info.getURL();
127
			//		SVNRepository repository = SVNRepositoryFactory.create(location);
128
			//		
129
			////		long latestRevision = repository.getLatestRevision();
130
			////		System.out.println("[" + location.toString() + "] latest revision: " + latestRevision); 
129
			// SVNWCClient client = SVNClientManager.newInstance().getWCClient();
130
			// SVNInfo info = client.doInfo(projectDirectory, SVNRevision.WORKING);
131
			// SVNURL location = info.getURL();
132
			// SVNRepository repository = SVNRepositoryFactory.create(location);
131 133
			//
134
			//// long latestRevision = repository.getLatestRevision();
135
			//// System.out.println("[" + location.toString() + "] latest revision: " + latestRevision);
132 136
			//
133
			//		SVNDirEntry entry = repository.info(".", -1);
134
			////		System.out.println("[" + location.toString() + "] latest revision: " + entry.getRevision());
135
			////		System.out.println("[" + location.toString() + "] latest date: " + entry.getDate());
136
			//		String svndate = entry.getDate()
137
			//printDebug("svn info in "+projectDirectory);
137
			//
138
			// SVNDirEntry entry = repository.info(".", -1);
139
			//// System.out.println("[" + location.toString() + "] latest revision: " + entry.getRevision());
140
			//// System.out.println("[" + location.toString() + "] latest date: " + entry.getDate());
141
			// String svndate = entry.getDate()
142
			// printDebug("svn info in "+projectDirectory);
138 143
			if (projectDirectory.exists() && svnDirectory.exists()) {
139 144
				String rev = getVersions("svnversion", projectDirectoryPath).trim();
140 145
				if (rev.contains(":")) {
141
					rev = rev.substring(rev.indexOf(":")+1);
146
					rev = rev.substring(rev.indexOf(":") + 1);
142 147
				}
143 148
				if (rev.endsWith("M")) {
144
					rev = rev.substring(0, rev.length()-1);
149
					rev = rev.substring(0, rev.length() - 1);
145 150
				}
146
				//printDebug("xml "+xml);
147
				printDebug(id+"="+rev);
151
				// printDebug("xml "+xml);
152
				printDebug(id + "=" + rev);
148 153
				return rev;
149
			} else {
154
			}
155
			else {
150 156
				return getDateQualifier();
151 157
			}
152 158
			
153 159
			/*
154
		String svndate = null;
155
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd-HHmm");
156
		//SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
157
		return formatter.format(svndate);
160
			 * String svndate = null;
161
			 * SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd-HHmm");
162
			 * //SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
163
			 * return formatter.format(svndate);
158 164
			 */
159
		} catch (Exception e) {
165
		}
166
		catch (Exception e) {
160 167
			e.printStackTrace();
161
			return "svn_error_"+id+"_"+projectDirectory;
168
			return "svn_error_" + id + "_" + projectDirectory;
162 169
		}
163 170
	}
164

  
171
	
165 172
	private static String getVersions(String... args) throws IOException {
166 173
		ProcessBuilder builder = new ProcessBuilder(args);
167 174
		builder.redirectErrorStream(true);
......
169 176
		BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
170 177
		StringBuilder sb = new StringBuilder();
171 178
		String line = null;
172
		while ((line = reader.readLine ()) != null) {
173
			sb.append(line+"\n");
179
		while ((line = reader.readLine()) != null) {
180
			sb.append(line + "\n");
174 181
		}
175 182
		reader.close();
176 183
		return sb.toString();
177 184
	}
178

  
179
	//given a version ending in "qualifier" return the key to look up the replacement
185
	
186
	// given a version ending in "qualifier" return the key to look up the replacement
180 187
	public static String getQualifierKey(String id, String version) {
181 188
		if (version == null || !version.endsWith(PROPERTY_QUALIFIER))
182 189
			return null;
183

  
190
		
184 191
		Version osgiVersion = new Version(version);
185 192
		String qualifier = osgiVersion.getQualifier();
186 193
		qualifier = qualifier.substring(0, qualifier.length() - PROPERTY_QUALIFIER.length());
187

  
194
		
188 195
		StringBuffer keyBuffer = new StringBuffer(id);
189 196
		keyBuffer.append(',');
190 197
		keyBuffer.append(osgiVersion.getMajor());
......
192 199
		keyBuffer.append(osgiVersion.getMinor());
193 200
		keyBuffer.append('.');
194 201
		keyBuffer.append(osgiVersion.getMicro());
195

  
202
		
196 203
		if (qualifier.length() > 0) {
197 204
			keyBuffer.append('.');
198 205
			keyBuffer.append(qualifier);
199 206
		}
200 207
		return keyBuffer.toString();
201 208
	}
202

  
209
	
203 210
	/**
204 211
	 * Returns the current date/time as a string to be used as a qualifier
205
	 * replacement.  This is the default qualifier replacement.  Will
212
	 * replacement. This is the default qualifier replacement. Will
206 213
	 * be of the form YYYYMMDDHHMM.
207
	 * @return current date/time as a qualifier replacement 
214
	 * 
215
	 * @return current date/time as a qualifier replacement
208 216
	 */
209 217
	public static String getDateQualifier() {
210 218
		final String empty = ""; //$NON-NLS-1$
211 219
		int monthNbr = Calendar.getInstance().get(Calendar.MONTH) + 1;
212 220
		String month = (monthNbr < 10 ? "0" : empty) + monthNbr; //$NON-NLS-1$
213

  
221
		
214 222
		int dayNbr = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
215 223
		String day = (dayNbr < 10 ? "0" : empty) + dayNbr; //$NON-NLS-1$
216

  
224
		
217 225
		int hourNbr = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
218 226
		String hour = (hourNbr < 10 ? "0" : empty) + hourNbr; //$NON-NLS-1$
219

  
227
		
220 228
		int minuteNbr = Calendar.getInstance().get(Calendar.MINUTE);
221 229
		String minute = (minuteNbr < 10 ? "0" : empty) + minuteNbr; //$NON-NLS-1$
222

  
230
		
223 231
		return empty + Calendar.getInstance().get(Calendar.YEAR) + month + day + hour + minute;
224 232
	}
225

  
233
	
226 234
	/**
227 235
	 * Sets the global variable used as the qualifier replacement during calls to
228 236
	 * {@link #replaceQualifierInVersion(String, String, String, Properties)}
229 237
	 * Setting the qualifier to <code>null</code> will result in the default
230 238
	 * date qualifier being used.
239
	 * 
231 240
	 * @param globalQualifier string replacement or <code>null</code>
232 241
	 */
233 242
	public static void setGlobalQualifier(String globalQualifier) {

Formats disponibles : Unified diff