# HG changeset patch
# User smith@nwoca.org
# Date 1329343870 18000
# Node ID 9f2ab59a53337333425e27dc72a2a06a3ae5b47a
# Parent be111b66cdc6e7d7cbd1a491e61d8f0ff38be333
improve ivy clean up and control order of attribs
diff -r be111b66cdc6 -r 9f2ab59a5333 fix-ivy.groovy
--- a/fix-ivy.groovy Tue Feb 14 15:52:29 2012 -0500
+++ b/fix-ivy.groovy Wed Feb 15 17:11:10 2012 -0500
@@ -1,35 +1,489 @@
+
+import groovy.xml.QName;
+import org.codehaus.groovy.runtime.InvokerHelper;
+
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
import groovy.xml.*
-def xml = new XmlSlurper()
-def ivy = xml.parse( new File('ivy.xml'))
+println args
+
+if (args.any { it.toUpperCase() == '-R'} ) {
+
+
+} else {
+ processIvy(new File('ivy.xml')
+}
+
+def processIvy(file) {
+
+ def xml = new XmlSlurper()
+ def ivy = xml.parse( file)
-def cfgs = ivy.configurations
+ def cfgs = ivy.configurations
+
+ // default mapping for most dependencies:
+ cfgs.@defaultconfmapping = 'compile->default'
+
+ // Add default config, if missing:
+ if (!cfgs.children().find { it.@name == 'default' } ) {
+ println "adding default config"
+ cfgs.appendNode {
+ conf(name:"default")
+ }
+ }
+
+
+ ivy.dependencies.children().each { dep ->
+ if ( dep.@conf.text()?.contains('test') ) {
+ dep.@conf = 'compile-test->default'
+ } else {
+ dep.attributes().remove('conf')
+ }
+ }
+
+ // find groovy dependencies (or null)
+ groovy = ivy.dependencies.children().find { it.@org == 'org.codehaus.groovy' && it.@name.text().startsWith('groovy') }
+
+ if (groovy) {
+ // Add a groovy conig for groovy projects:
+ if (!cfgs.children().any { it.@name == 'groovy' } ) {
+ println "added private groovy conf"
+ cfgs.appendNode {
+ conf(name:'groovy',visibility:'private',transitive: true)
+ }
+ }
+ // Adjust 'compile' to exend from 'groovy' conf
+ cfgs.children().find { it.@name == 'compile' }.@extends = 'groovy'
+ cfgs.children().find { it.@name == 'groovy' }.@transitive = 'true'
+ groovy.@conf = "groovy->default"
+ println "Groovy ${groovy.@rev} conf changed to ${groovy.@conf}"
+ // Switch project to use groovy-all instead of groovy + deps
+ if (groovy.@name == 'groovy') {
+ println "changed groovy to groovy-all"
+ groovy.@name = 'groovy-all'
+ }
+ // remove exclude for groovy-all
+ ivy.dependencies.children().find { it.name() == 'exclude' && it.@module.text() == 'groovy-all' }.replaceNode {}
+ }
+
+ //def sameConfMappings = ivy.dependencies.children().findAll { it.@conf in ['*->@','*->*'] }
+ //
+ //sameConfMappings.each {
+ // it.@conf = null // "default->compile;%->@"
+ // println "changing conf for ${it.@name} to ${it.@conf}"
+ //}
+
+
+ cfgs.children().each { it.@visibility = 'private' }
+ cfgs.children().find {it.@name == 'runtime' }.@extends = 'compile'
+ cfgs.children().find {it.@name == 'compile-test' }.@extends = 'compile'
+ cfgs.children().find {it.@name == 'runtime-test' }.@extends = 'runtime,compile-test'
+ cfgs.children().find {it.@name == 'default' }.@visibility = 'public'
+ cfgs.children().find {it.@name == 'default' }.@extends = 'runtime'
+
+ if (!cfgs.children().any { it.@name == 'archives' } ) {
+ println "added public 'archives' conf"
+ cfgs.appendNode {
+ conf(name:'archives',visibility:'public')
+ }
+ }
+ cfgs.children().find {it.@name == 'archives' }.@visibility = 'public'
+
+
+ def outputBuilder = new StreamingMarkupBuilder()
+ //new File('ivy.xml').write( XmlUtil.serialize( outputBuilder.bind { mkp.yield ivy }).replaceAll('>','>'))
+
+ def stringWriter = new StringWriter()
+
+
+ def node = new XmlParser().parseText( XmlUtil.serialize( outputBuilder.bind { mkp.yield ivy }))
+ new MyNodePrinter(new IndentPrinter( new PrintWriter(stringWriter))).print(node)
+ file.write(stringWriter.toString())
+
+}
+/*
+The class below is a copy/paste of groovy's XmlNodePrinter customized to order Ivy.xml attributes in
+the desired order and to add some whitespace.
+*/
+class MyNodePrinter {
-if (cfgs.@defaultconfmapping.size() == 0) {
- println "adding defaultconfmapping"
- cfgs.@defaultconfmapping = '*->default'
-}
+ protected void printNameAttributes(Map attributes, ctx) {
+
+ if (attributes == null || attributes.isEmpty()) {
+ return;
+ }
+ def writer = new StringBuffer()
+ attributes.entrySet().sort{
+ switch (it.key ) {
+ case 'org':
+ 1
+ break
+ case 'name': 2
+ break
+ case 'rev': 3
+ break
+ case 'extends': 5
+ break
+ default: 99
+ }
+ }.each { p ->
+ def tmp = new StringBuffer()
+ Map.Entry entry = (Map.Entry) p;
+
+ tmp << " "
+ tmp << getName(entry.getKey())
+ tmp << "="
+ Object value = entry.getValue();
+ tmp << quote
+ tmp << value
+// if (value instanceof String) {
+// printEscaped((String) value);
+// } else {
+// printEscaped(InvokerHelper.toString(value));
+// }
+ tmp << quote
+ writer << String.format(" %-35s",tmp.toString())
+// printNamespace(entry.getKey(), ctx);
+ }
+ out.print(" " + writer.toString().trim())
+ }
+
+ protected final IndentPrinter out;
+ private String quote;
+ private boolean namespaceAware = true;
+ private boolean preserveWhitespace = false;
+
+ public MyNodePrinter(PrintWriter out) {
+ this(out, " ");
+ }
+
+ public MyNodePrinter(PrintWriter out, String indent) {
+ this(out, indent, "\"");
+ }
+
+ public MyNodePrinter(PrintWriter out, String indent, String quote) {
+ this(new IndentPrinter(out, indent), quote);
+ }
+
+ public MyNodePrinter(IndentPrinter out) {
+ this(out, "\"");
+ }
-if (!cfgs.children().find { it.@name == 'default' } ) {
- println "adding default config"
- cfgs.appendNode {
- conf(name:"default")
+ public MyNodePrinter(IndentPrinter out, String quote) {
+ if (out == null) {
+ throw new IllegalArgumentException("Argument 'IndentPrinter out' must not be null!");
+ }
+ this.out = out;
+ this.quote = quote;
+ }
+
+ public MyNodePrinter() {
+ this(new PrintWriter(new OutputStreamWriter(System.out)));
+ }
+
+ public void print(Node node) {
+ print(node, new NamespaceContext());
+ }
+
+ /**
+ * Check if namespace handling is enabled.
+ * Defaults to true
.
+ *
+ * @return true if namespace handling is enabled
+ */
+ public boolean isNamespaceAware() {
+ return namespaceAware;
+ }
+
+ /**
+ * Enable and/or disable namespace handling.
+ *
+ * @param namespaceAware the new desired value
+ */
+ public void setNamespaceAware(boolean namespaceAware) {
+ this.namespaceAware = namespaceAware;
}
-}
+
+ /**
+ * Check if whitespace preservation is enabled.
+ * Defaults to false
.
+ *
+ * @return true if whitespaces are honoured when printing simple text nodes
+ */
+ public boolean isPreserveWhitespace() {
+ return preserveWhitespace;
+ }
+
+ /**
+ * Enable and/or disable preservation of whitespace.
+ *
+ * @param preserveWhitespace the new desired value
+ */
+ public void setPreserveWhitespace(boolean preserveWhitespace) {
+ this.preserveWhitespace = preserveWhitespace;
+ }
+
+ /**
+ * Get Quote to use when printing attributes.
+ *
+ * @return the quote character
+ */
+ public String getQuote() {
+ return quote;
+ }
+
+ /**
+ * Set Quote to use when printing attributes.
+ *
+ * @param quote the quote character
+ */
+ public void setQuote(String quote) {
+ this.quote = quote;
+ }
-groovy = ivy.dependencies.children().find { it.@org == 'org.codehaus.groovy' && it.@name == 'groovy' }
-if (groovy) {
- groovy.@conf = "*->default,optional"
- println "Groovy ${groovy.@rev} conf changed to ${groovy.@conf}"
-}
+ protected void print(Node node, NamespaceContext ctx) {
+ /*
+ * Handle empty elements like '
', ' or '