@@ -5,6 +5,7 @@ import org.gradle.api.Task
55import org.gradle.api.file.FileCollection
66import org.gradle.api.plugins.JavaPluginConvention
77import org.gradle.api.tasks.SourceSet
8+ import org.gradle.internal.Pair
89import org.gradle.process.JavaForkOptions
910import org.jdom2.Attribute
1011import org.jdom2.Document
@@ -21,22 +22,50 @@ import org.slf4j.Logger
2122import org.slf4j.LoggerFactory
2223
2324import 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
2530import java.nio.file.attribute.BasicFileAttributes
2631
2732/**
2833 * @author meanmail
2934 */
3035class Utils {
3136 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+ ]
4069
4170 @Nullable
4271 static Document getXmlDocument (@NotNull File file ) {
@@ -48,16 +77,6 @@ class Utils {
4877 }
4978 }
5079
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-
6180 @NotNull
6281 static SourceSet mainSourceSet (@NotNull Project project ) {
6382 def javaConvention = project. getConvention(). getPlugin(JavaPluginConvention . class)
@@ -119,7 +138,7 @@ class Utils {
119138 node. setAttribute(name, value)
120139 }
121140
122- static Document createUpdatesXml ( ) {
141+ static Document createXml ( Map map ) {
123142 def doc = new Document ()
124143
125144 def applicationNode = new Element (APPLICATION )
@@ -128,17 +147,19 @@ class Utils {
128147 def component = new Element (COMPONENT )
129148 applicationNode. addContent(component)
130149
131- setAttributeValue(component, NAME , UPDATES_CONFIGURABLE )
150+ setAttributeValue(component, NAME , map[ COMPONENT_NAME ] as String )
132151
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+ }
137158
138159 return doc
139160 }
140161
141- static void repairUpdateXml (Document doc ) {
162+ static void repairXml (Document doc , Map map ) {
142163 def applicationNode
143164
144165 if (! doc. hasRootElement()) {
@@ -152,9 +173,10 @@ class Utils {
152173 applicationNode. setName(APPLICATION )
153174 }
154175
176+ String componentName = map[COMPONENT_NAME ]
155177 def component = applicationNode. getChildren(COMPONENT ). find {
156178 Attribute attr = it. getAttribute(NAME )
157- return attr != null && UPDATES_CONFIGURABLE == attr. getValue()
179+ return attr != null && componentName == attr. getValue()
158180 }
159181
160182 if (component == null ) {
@@ -164,22 +186,24 @@ class Utils {
164186
165187 def name = component. getAttribute(NAME )
166188
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 )
169191 }
170192
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+ }
175198
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+ }
181204
182- setAttributeValue(option, VALUE , FALSE )
205+ setAttributeValue(optionTag, VALUE , option. getRight() as String )
206+ }
183207 }
184208
185209 @Nullable
@@ -379,4 +403,41 @@ class Utils {
379403 return " tar.gz"
380404 }
381405 }
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+ }
382443}
0 commit comments