forked from halestudio/hale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply-preferences.groovy
executable file
·299 lines (242 loc) · 8.77 KB
/
apply-preferences.groovy
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env groovy
import java.text.DateFormat
import java.util.Map.Entry
import java.util.jar.Manifest
/*
* Class and interface definitions
*/
class SortedProperties extends Properties {
synchronized Enumeration<Object> keys() {
Enumeration keysEnum = super.keys()
Vector keyList = new Vector()
while (keysEnum.hasMoreElements()) {
keyList.add(keysEnum.nextElement())
}
Collections.sort(keyList)
return keyList.elements()
}
}
interface ProjectFilter {
/**
* Determines if a project shall be processed
*
* @param projectDir the project directory
*
* @return if the project is accepted
*/
boolean acceptProject(File projectDir);
}
class ProjectPreferencesApplier {
private final Map<String, SortedProperties> defaults = [:]
private static final DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM)
/**
* Load default preferences from all .prefs files in the given directory
*
* @param prefsPath the path to the default preferences directory
*
* @return if any preference files were found
*/
boolean loadDefaults(File path) {
File[] prefFiles = path.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".prefs")
}
});
boolean foundPreferences = false
if (prefFiles != null) {
for (File prefFile : prefFiles) {
SortedProperties properties = new SortedProperties()
try {
properties.load(new BufferedReader(new FileReader(prefFile)))
if (properties.isEmpty()) {
println("Warning: empty preference file: " + prefFile.getAbsolutePath())
}
String key = prefFile.getName()
defaults.put(key, properties)
foundPreferences = true
// println("Loaded default preferences: " + prefFile.getName())
} catch (FileNotFoundException e) {
// ignore
} catch (IOException e) {
println("Error reading preference file: " + prefFile.getAbsolutePath())
}
}
println('Loaded preferences from: ' + path as String)
}
return foundPreferences
}
/**
* Apply the preference defaults to projects found in the given search path
*
* @param searchPath the search path
* @param projectFilter the project filter
*/
public void applyDefaults(File searchPath, ProjectFilter projectFilter) {
Collection<? extends File> projectDirs = findProjectDirectories(searchPath, true)
int ignored = 0
for (File projectDir : projectDirs) {
if (projectFilter.acceptProject(projectDir)) {
// apply project preferences
applyProjectDefaults(projectDir)
}
else {
ignored++
}
}
println("Ignored " + ignored + " project directories.")
}
/**
* Apply the project preference defaults to the project in the given
* directory
*
* @param projectDir the project direktory
*/
private void applyProjectDefaults(File projectDir) {
// settings directory
File settingsDir = new File(projectDir, ".settings")
if (!settingsDir.exists()) {
settingsDir.mkdir()
}
boolean success = true;
for (Entry<String, SortedProperties> entry : defaults.entrySet()) {
String filename = entry.getKey()
SortedProperties defaults = entry.getValue()
File propertiesFile = new File(settingsDir, filename)
if (propertiesFile.exists()) {
// apply defaults to existing file
SortedProperties current = new SortedProperties()
try {
current.load(new BufferedReader(new FileReader(propertiesFile)))
boolean changed = false
// copy defaults
Enumeration<Object> en = defaults.keys()
while (en.hasMoreElements()) {
String key = (String) en.nextElement()
String defaultValue = defaults.getProperty(key)
String oldValue = current.getProperty(key)
if (oldValue == null || !oldValue.equals(defaultValue)) {
current.setProperty(key, defaultValue)
changed = true
}
}
// save file
if (changed) {
current.store(new FileWriter(propertiesFile), "Updated from default preferences " + dateFormat.format(new Date()))
}
} catch (FileNotFoundException e) {
e.printStackTrace()
success = false
} catch (IOException e) {
println("Error updating preferences '" + filename + "' for project in " + projectDir.getAbsolutePath())
success = false
}
}
else {
// create initial file
try {
defaults.store(new FileWriter(propertiesFile), "Created from default preferences " + dateFormat.format(new Date()))
} catch (IOException e) {
println("Error saving default preferences '" + filename + "' for project in " + projectDir.getAbsolutePath())
success = false
}
}
}
if (success) {
println("Successfully updated project preferences for " + projectDir.getName())
}
}
/**
* Find the project directories in the given search path
*
* @param searchPath the search path
* @param searchInProjects if inside project directories shall also be searched for projects
*
* @return the project directories
*/
private Collection<? extends File> findProjectDirectories(File searchPath,
boolean searchInProjects) {
List<File> projectDirs = []
boolean project = false;
File projectFile = new File(searchPath, ".project")
File classpathFile = new File(searchPath, ".classpath") // check also for classpath file to filter non-Java projects
if (projectFile.exists() && classpathFile.exists()) {
// search path is project dir
projectDirs.add(searchPath);
project = true;
}
if (!project || searchInProjects) {
File[] candidates = searchPath.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
// ignore files starting with .
if (name.startsWith(".")) {
return false
}
// ignore build directories
if (name.equals("build")) {
return false
}
return true
}
});
if (candidates != null) {
for (File candidate : candidates) {
projectDirs.addAll(findProjectDirectories(candidate, false))
}
}
}
return projectDirs
}
}
void apply(File prefs, File searchPath, ProjectFilter projectFilter = { File projectDir -> true }) {
ProjectPreferencesApplier applier = new ProjectPreferencesApplier()
if (!applier.loadDefaults(prefs)) {
println("No preference files found in " + prefs + ".")
}
else {
applier.applyDefaults(searchPath, projectFilter)
}
}
/*
* Script
*/
// filter that detects Java 8 projects and updates the classpath setting
def isJava8 = { File projectDir ->
File manifestFile = new File(new File(projectDir, 'META-INF'), 'MANIFEST.MF')
boolean match = false
if (manifestFile.exists()) {
manifestFile.withInputStream {
def manifest = new Manifest(it)
def attributes = manifest.getMainAttributes()
def env = attributes.getValue('Bundle-RequiredExecutionEnvironment')
match = (env == 'JavaSE-1.8')
}
}
if (match) {
// update to Java 8 -> also replace classpath setting
// XXX kind of a hack to do it in the filter
File classpathFile = new File(projectDir, '.classpath')
if (classpathFile.exists()) {
def fileContent = classpathFile.text
def java7cp = 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7'
def java8cp = 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
if (fileContent.contains(java7cp)) {
classpathFile.text = fileContent.replace(java7cp, java8cp)
}
}
}
match
}
// apply preferences to projects
def java7 = 'platform/preferences/java7' as File
def java8 = 'platform/preferences/java8' as File
def searchPaths = ['common', 'cst', 'io', 'server', 'doc', 'ui', 'util', 'app', 'ext/styledmap', 'ext/geom', 'ext/adv']
searchPaths.each {
// default: Java 8
apply(java8, it as File, { !isJava8(it) } as ProjectFilter)
// Java 8
apply(java8, it as File, isJava8 as ProjectFilter)
}
// external contributions w/ different project settings
apply('platform/preferences/xslt' as File, 'ext/xslt' as File)
apply('platform/preferences/xslt' as File, 'ext/ageobw' as File)
apply('platform/preferences/mdl' as File, 'ext/mdl' as File)