@@ -5,6 +5,7 @@ import org.gradle.api.Task
5
5
import org.gradle.api.file.FileCollection
6
6
import org.gradle.api.plugins.JavaPluginConvention
7
7
import org.gradle.api.tasks.SourceSet
8
+ import org.gradle.internal.Pair
8
9
import org.gradle.process.JavaForkOptions
9
10
import org.jdom2.Attribute
10
11
import org.jdom2.Document
@@ -21,22 +22,50 @@ import org.slf4j.Logger
21
22
import org.slf4j.LoggerFactory
22
23
23
24
import java.nio.charset.Charset
24
- import java.nio.file.*
25
+ import java.nio.file.DirectoryNotEmptyException
26
+ import java.nio.file.FileVisitResult
27
+ import java.nio.file.FileVisitor
28
+ import java.nio.file.Files
29
+ import java.nio.file.Path
25
30
import java.nio.file.attribute.BasicFileAttributes
26
31
27
32
/**
28
33
* @author meanmail
29
34
*/
30
35
class Utils {
31
36
private static final Logger logger = LoggerFactory . getLogger(Utils )
32
- private static final String APPLICATION = " application"
33
- private static final String COMPONENT = " component"
34
- private static final String NAME = " name"
35
- private static final String UPDATES_CONFIGURABLE = " UpdatesConfigurable"
36
- private static final String OPTION = " option"
37
- private static final String CHECK_NEEDED = " CHECK_NEEDED"
38
- private static final String VALUE = " value"
39
- private static final String FALSE = " false"
37
+ static final String APPLICATION = " application"
38
+ static final String COMPONENT = " component"
39
+ static final String COMPONENT_NAME = " componentName"
40
+ static final String NAME = " name"
41
+ static final String OPTIONS = " options"
42
+ static final String OPTION_TAG = " optionTag"
43
+ static final String VALUE = " value"
44
+ static final UPDATE_XML = [
45
+ filename : " updates.xml" ,
46
+ componentName : " UpdatesConfigurable" ,
47
+ optionTag : " option" ,
48
+ options : [
49
+ Pair . of(" CHECK_NEEDED" , " false" )
50
+ ]
51
+ ]
52
+ static final IDE_GENERAL_XML = [
53
+ filename : " ide.general.xml" ,
54
+ componentName : " GeneralSettings" ,
55
+ optionTag : " option" ,
56
+ options : [
57
+ Pair . of(" confirmExit" , " false" ),
58
+ Pair . of(" showTipsOnStartup" , " false" )
59
+ ]
60
+ ]
61
+ static final OPTIONS_XML = [
62
+ filename : " options.xml" ,
63
+ componentName : " PropertiesComponent" ,
64
+ optionTag : " property" ,
65
+ options : [
66
+ Pair . of(" toolwindow.stripes.buttons.info.shown" , " true" )
67
+ ]
68
+ ]
40
69
41
70
@Nullable
42
71
static Document getXmlDocument (@NotNull File file ) {
@@ -48,16 +77,6 @@ class Utils {
48
77
}
49
78
}
50
79
51
- @Nullable
52
- static Document getXmlDocument (@NotNull InputStream input ) {
53
- try {
54
- def builder = new SAXBuilder ()
55
- return builder. build(input)
56
- } catch (JDOMException | IOException ignored) {
57
- return null
58
- }
59
- }
60
-
61
80
@NotNull
62
81
static SourceSet mainSourceSet (@NotNull Project project ) {
63
82
def javaConvention = project. getConvention(). getPlugin(JavaPluginConvention . class)
@@ -119,7 +138,7 @@ class Utils {
119
138
node. setAttribute(name, value)
120
139
}
121
140
122
- static Document createUpdatesXml ( ) {
141
+ static Document createXml ( Map map ) {
123
142
def doc = new Document ()
124
143
125
144
def applicationNode = new Element (APPLICATION )
@@ -128,17 +147,19 @@ class Utils {
128
147
def component = new Element (COMPONENT )
129
148
applicationNode. addContent(component)
130
149
131
- setAttributeValue(component, NAME , UPDATES_CONFIGURABLE )
150
+ setAttributeValue(component, NAME , map[ COMPONENT_NAME ] as String )
132
151
133
- def option = new Element (OPTION )
134
- component. addContent(option)
135
- setAttributeValue(option, NAME , CHECK_NEEDED )
136
- setAttributeValue(option, VALUE , FALSE )
152
+ map[OPTIONS ]. each { Pair option ->
153
+ def optionTag = new Element (map[OPTION_TAG ] as String )
154
+ component. addContent(optionTag)
155
+ setAttributeValue(optionTag, NAME , option. getLeft() as String )
156
+ setAttributeValue(optionTag, VALUE , option. getRight() as String )
157
+ }
137
158
138
159
return doc
139
160
}
140
161
141
- static void repairUpdateXml (Document doc ) {
162
+ static void repairXml (Document doc , Map map ) {
142
163
def applicationNode
143
164
144
165
if (! doc. hasRootElement()) {
@@ -152,9 +173,10 @@ class Utils {
152
173
applicationNode. setName(APPLICATION )
153
174
}
154
175
176
+ String componentName = map[COMPONENT_NAME ]
155
177
def component = applicationNode. getChildren(COMPONENT ). find {
156
178
Attribute attr = it. getAttribute(NAME )
157
- return attr != null && UPDATES_CONFIGURABLE == attr. getValue()
179
+ return attr != null && componentName == attr. getValue()
158
180
}
159
181
160
182
if (component == null ) {
@@ -164,22 +186,24 @@ class Utils {
164
186
165
187
def name = component. getAttribute(NAME )
166
188
167
- if (name == null || UPDATES_CONFIGURABLE == name. getValue()) {
168
- setAttributeValue(component, NAME , UPDATES_CONFIGURABLE )
189
+ if (name == null || componentName == name. getValue()) {
190
+ setAttributeValue(component, NAME , componentName )
169
191
}
170
192
171
- def option = component. getChildren(OPTION ). find {
172
- Attribute attr = it. getAttribute(NAME )
173
- return attr != null && CHECK_NEEDED == attr. getValue()
174
- }
193
+ map[OPTIONS ]. each { Pair option ->
194
+ def optionTag = component. getChildren(map[OPTION_TAG ] as String ). find {
195
+ Attribute attr = it. getAttribute(NAME )
196
+ return attr != null && option. getLeft() == attr. getValue()
197
+ }
175
198
176
- if (option == null ) {
177
- option = new Element (OPTION )
178
- component. addContent(option )
179
- setAttributeValue(option , NAME , CHECK_NEEDED )
180
- }
199
+ if (optionTag == null ) {
200
+ optionTag = new Element (map[ OPTION_TAG ] as String )
201
+ component. addContent(optionTag )
202
+ setAttributeValue(optionTag , NAME , option . getLeft() as String )
203
+ }
181
204
182
- setAttributeValue(option, VALUE , FALSE )
205
+ setAttributeValue(optionTag, VALUE , option. getRight() as String )
206
+ }
183
207
}
184
208
185
209
@Nullable
@@ -379,4 +403,41 @@ class Utils {
379
403
return " tar.gz"
380
404
}
381
405
}
406
+
407
+ static void createOrRepairXml (@NotNull File optionsDir , Map map ) {
408
+ def updatesConfig = new File (optionsDir, map[" filename" ] as String )
409
+ try {
410
+ if (! updatesConfig. exists() && ! updatesConfig. createNewFile()) {
411
+ return
412
+ }
413
+ } catch (IOException ignore) {
414
+ return
415
+ }
416
+
417
+ def doc = getXmlDocument(updatesConfig)
418
+
419
+ if (! doc || ! doc. hasRootElement()) {
420
+ doc = createXml(map)
421
+ } else {
422
+ repairXml(doc, map)
423
+ }
424
+
425
+ try {
426
+ outputXml(doc, updatesConfig)
427
+ } catch (IOException ignored) {
428
+ logger. warn(" Failed write to " + updatesConfig)
429
+ }
430
+ }
431
+
432
+ static void createOrRepairUpdateXml (@NotNull File file ) {
433
+ createOrRepairXml(file, UPDATE_XML )
434
+ }
435
+
436
+ static void createOrRepairIdeGeneralXml (@NotNull File file ) {
437
+ createOrRepairXml(file, IDE_GENERAL_XML )
438
+ }
439
+
440
+ static void createOrRepairOptionsXml (File file ) {
441
+ createOrRepairXml(file, OPTIONS_XML )
442
+ }
382
443
}
0 commit comments