diff --git a/tesla-polyglot/tesla-polyglot-atom/src/main/java/org/sonatype/maven/polyglot/atom/AtomModelWriter.java b/tesla-polyglot/tesla-polyglot-atom/src/main/java/org/sonatype/maven/polyglot/atom/AtomModelWriter.java index 2d30b7ff6..080023fef 100644 --- a/tesla-polyglot/tesla-polyglot-atom/src/main/java/org/sonatype/maven/polyglot/atom/AtomModelWriter.java +++ b/tesla-polyglot/tesla-polyglot-atom/src/main/java/org/sonatype/maven/polyglot/atom/AtomModelWriter.java @@ -16,13 +16,6 @@ package org.sonatype.maven.polyglot.atom; -import java.io.IOException; -import java.io.PrintWriter; -import java.io.Writer; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - import org.apache.maven.model.Dependency; import org.apache.maven.model.Model; import org.apache.maven.model.Plugin; @@ -34,6 +27,13 @@ import org.codehaus.plexus.util.xml.Xpp3Dom; import org.sonatype.maven.polyglot.io.ModelWriterSupport; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.Writer; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + @Component(role = ModelWriter.class, hint = "atom") public class AtomModelWriter extends ModelWriterSupport { @@ -51,11 +51,11 @@ public void write(final Writer output, final Map options, final project(pw, model); id(pw, model); parent(pw, model); - packaging(pw, model); properties(pw, model); dependencyManagement(pw, model); dependencies(pw, model); modules(pw, model); + pw.println(); pluginManagement(pw, model); plugins(pw, model); @@ -83,7 +83,9 @@ private void project(PrintWriter pw, Model model) { if (name == null) { name = model.getArtifactId(); } - pw.println("project \"" + name + "\" @ \"" + model.getUrl() + "\""); + String url = model.getUrl() == null ? "" : model.getUrl(); + pw.print("project \"" + name + "\" @ \"" + url + "\""); + packaging(pw, model); } private void id(PrintWriter pw, Model model) { @@ -101,19 +103,17 @@ private void id(PrintWriter pw, Model model) { private void parent(PrintWriter pw, Model model) { if (model.getParent() != null) { - pw.print(indent + "inherit: " + model.getParent().getGroupId() + ":" + model.getParent().getArtifactId() + ":" + model.getParent().getVersion()); + pw.print(indent + "inherits: " + model.getParent().getGroupId() + ":" + model.getParent().getArtifactId() + ":" + model.getParent().getVersion()); if (model.getParent().getRelativePath() != null) { //pw.println(":" + model.getParent().getRelativePath()); - pw.println(":" + "../pom.atom"); - } else { - pw.println(); +// pw.println(":" + "../pom.atom"); } + pw.println(); } } private void packaging(PrintWriter pw, Model model) { - pw.println(indent + "packaging: " + model.getPackaging()); - pw.println(); + pw.println(" as " + model.getPackaging()); } private void properties(PrintWriter pw, Model model) { @@ -134,7 +134,6 @@ private void properties(PrintWriter pw, Model model) { } } pw.println(" ]"); - pw.println(); } } @@ -145,7 +144,7 @@ private void modules(PrintWriter pw, Model model) { for (int i = 0; i < modules.size(); i++) { String module = modules.get(i); if (i != 0) { - pw.print(" "); + pw.print(indent + " "); } pw.print(module); if (i + 1 != modules.size()) { @@ -153,7 +152,6 @@ private void modules(PrintWriter pw, Model model) { } } pw.println(" ]"); - pw.println(); } } @@ -182,17 +180,16 @@ private void deps(PrintWriter pw, String elementName, List deps) { // We are assuming the model is well-formed and that the parent is providing a version // for this particular dependency. // - pw.print(d.getGroupId() + ":" + d.getArtifactId()); + pw.print(d.getGroupId() + ":" + d.getArtifactId()); } if (d.getClassifier() != null) { pw.print("(" + d.getClassifier() + ")"); - } + } if (i + 1 != deps.size()) { pw.println(); } } pw.println(" ]"); - pw.println(); } } @@ -211,40 +208,57 @@ private void plugins(PrintWriter pw, Model model) { // need to write nested objects private void plugins(PrintWriter pw, String elementName, List plugins) { if (!plugins.isEmpty()) { - pw.print(indent + elementName + ": [ "); for (int i = 0; i < plugins.size(); i++) { Plugin plugin = plugins.get(i); - if (i != 0) { - pw.print(" "); - } - pw.print(plugin.getGroupId() + ":" + plugin.getArtifactId() + ":" + plugin.getVersion()); + pw.println("plugin"); + + pw.print(indent + "id: " + plugin.getGroupId() + ":" + plugin.getArtifactId()); + if (plugin.getVersion() != null) + pw.print(":" + plugin.getVersion()); if (plugin.getConfiguration() != null) { pw.println(); Xpp3Dom configuration = (Xpp3Dom) plugin.getConfiguration(); - if (configuration.getChildCount() != 0) { - pw.print(" properties:[ "); - int count = configuration.getChildCount(); - for (int j = 0; j < count; j++) { - Xpp3Dom c = configuration.getChild(j); - if (c.getValue() != null) { - if (j != 0) { - pw.print(" "); - } - pw.print(c.getName() + ": " + c.getValue()); - if (j + 1 != count) { - pw.println(); - } - } - } - pw.print(" ]"); - } + printChildren(pw, configuration); } if (i + 1 != plugins.size()) { pw.println(); } } - pw.println(" ]"); pw.println(); } } -} \ No newline at end of file + + private boolean flipBrackets = false; + + private void printChildren(PrintWriter pw, Xpp3Dom configuration) { + if (configuration.getChildCount() > 0) { + int count = configuration.getChildCount(); + for (int j = 0; j < count; j++) { + Xpp3Dom c = configuration.getChild(j); + if (c.getValue() != null) { + pw.print(indent + c.getName() + ": " + c.getValue()); + if (j + 1 != count) { + pw.println(); + } + } else { + pw.println(indent + c.getName() + ": " + lbraceket()); + String oldIndent = indent; + indent += " "; + flipBrackets = !flipBrackets; + printChildren(pw, c); + flipBrackets = !flipBrackets; + indent = oldIndent; + pw.print("\n" + indent + rbraceket()); + } + } + } + } + + private char lbraceket() { + return (flipBrackets ? '{' : '['); + } + + private char rbraceket() { + return (flipBrackets ? '}' : ']'); + } +} diff --git a/tesla-polyglot/tesla-polyglot-atom/src/main/java/org/sonatype/maven/polyglot/atom/parsing/AtomParser.java b/tesla-polyglot/tesla-polyglot-atom/src/main/java/org/sonatype/maven/polyglot/atom/parsing/AtomParser.java index 2c1a8908f..277ab0b5c 100644 --- a/tesla-polyglot/tesla-polyglot-atom/src/main/java/org/sonatype/maven/polyglot/atom/parsing/AtomParser.java +++ b/tesla-polyglot/tesla-polyglot-atom/src/main/java/org/sonatype/maven/polyglot/atom/parsing/AtomParser.java @@ -39,11 +39,13 @@ public AtomParser(ModelSource modelSource, List tokens) { } private void parseException(String message, Throwable t) { - throw new RuntimeException("Error parsing " + modelSource.getLocation() + ": " + message, t); + String location = modelSource != null ? modelSource.getLocation() : ""; + throw new RuntimeException("Error parsing " + location + ": " + message, t); } private void parseException(String message) { - throw new RuntimeException("Error parsing " + modelSource.getLocation() + ": " + message); + String location = modelSource != null ? modelSource.getLocation() : ""; + throw new RuntimeException("Error parsing " + location + ": " + message); } public Project parse() { @@ -336,7 +338,7 @@ private Plugin plugin() { return null; } - Id pluginId = id(); + Id pluginId = id(true); if (pluginId == null) { log.severe("Plugin id declaration malformed"); return null; @@ -409,9 +411,13 @@ private Map configurationMap() { match(Kind.EOL); } else { // This is a multilevel thing, recurse! - if (match(Kind.LBRACKET) != null) { + if (anyOf(Kind.LBRACKET, Kind.LBRACE) != null) { + chewEols(); + chewIndents(); Map childProps = configurationMap(); - if (match(Kind.RBRACKET) == null) + chewEols(); + chewIndents(); + if (match(Kind.RBRACKET) == null && match(Kind.RBRACE) == null) parseException("Expected ']' after configuration properties"); // stash into parent map. @@ -468,7 +474,7 @@ private List modules() { chewIndents(); String module; - while ((module = idFragment()).length() != 0) { + while ((module = idFragment()) != null) { chewEols(); chewIndents(); modules.add(module); @@ -556,7 +562,7 @@ private Id id(boolean allowNullVersion) { return null; } else { version = idFragment(); - if (version == null) { + if (version == null && !allowNullVersion) { return null; } } @@ -647,6 +653,23 @@ private String idFragment() { } } + // Try parsing as property expression. + if (fragment.length() == 1 && fragment.charAt(0) == '$') { + List startOfExpr = match(Kind.LBRACE); + if (startOfExpr == null) + return null; + + fragment.append("{"); + String prop = idFragment(); + if (prop == null) + parseException("Expected a property expression after ${"); + + if (match(Kind.RBRACE) == null) + parseException("Expected '}' after property expression"); + + fragment.append(prop).append("}"); + } + // Nothing matched. if (fragment.length() == 0) return null; diff --git a/tesla-polyglot/tesla-polyglot-atom/src/main/java/org/sonatype/maven/polyglot/atom/parsing/Token.java b/tesla-polyglot/tesla-polyglot-atom/src/main/java/org/sonatype/maven/polyglot/atom/parsing/Token.java index 80008753b..f0fde06f9 100644 --- a/tesla-polyglot/tesla-polyglot-atom/src/main/java/org/sonatype/maven/polyglot/atom/parsing/Token.java +++ b/tesla-polyglot/tesla-polyglot-atom/src/main/java/org/sonatype/maven/polyglot/atom/parsing/Token.java @@ -44,6 +44,8 @@ public static enum Kind { RPAREN, LBRACKET, RBRACKET, + LBRACE, + RBRACE, // keywords REQUIRE, @@ -93,8 +95,8 @@ public static enum Kind { TOKEN_MAP.put(")", RPAREN); TOKEN_MAP.put("[", LBRACKET); TOKEN_MAP.put("]", RBRACKET); - TOKEN_MAP.put("{", LBRACKET); // Alias braces as brackets. - TOKEN_MAP.put("}", RBRACKET); + TOKEN_MAP.put("{", LBRACE); // Alias braces as brackets. + TOKEN_MAP.put("}", RBRACE); TOKEN_MAP.put("\n", EOL); TOKEN_MAP.put("id", ID); @@ -159,4 +161,4 @@ public String toString() { ", kind=" + kind + '}'; } -} \ No newline at end of file +} diff --git a/tesla-polyglot/tesla-polyglot-atom/src/test/java/org/sonatype/maven/polyglot/atom/AtomModelWithSBTest.java b/tesla-polyglot/tesla-polyglot-atom/src/test/java/org/sonatype/maven/polyglot/atom/AtomModelWithSBTest.java index 090b555ca..6d9c4bc17 100644 --- a/tesla-polyglot/tesla-polyglot-atom/src/test/java/org/sonatype/maven/polyglot/atom/AtomModelWithSBTest.java +++ b/tesla-polyglot/tesla-polyglot-atom/src/test/java/org/sonatype/maven/polyglot/atom/AtomModelWithSBTest.java @@ -24,7 +24,7 @@ public class AtomModelWithSBTest extends InjectedTestCase { @Named("${basedir}/src/test/poms") private File poms; - public void _testAtomModelWriter() throws Exception { + public void testAtomModelWriter() throws Exception { File pom = new File(poms, "sitebricks-parent-pom.xml"); MavenXpp3Reader xmlModelReader = new MavenXpp3Reader(); Model xmlModel = xmlModelReader.read(new FileInputStream(pom)); @@ -56,6 +56,9 @@ public void _testAtomModelWriter() throws Exception { MavenXpp3Writer xmlWriter = new MavenXpp3Writer(); xmlWriter.write(w, xmlModel); System.out.println(w.toString()); + + assertEquals(xmlModel.getModules(), atomModel.getModules()); + assertEquals(xmlModel.getRepositories().size(), atomModel.getRepositories().size()); } public void testAtomModelWriterWhereModelHasDependenciesWithNoVersions() throws Exception { @@ -82,9 +85,7 @@ public void testAtomModelWriterWhereModelHasDependenciesWithNoVersions() throws // // Test for fidelity // - //assertNotNull(atomModel); - - + assertNotNull(atomModel); } void testMavenModelForCompleteness(Model model) { @@ -138,5 +139,4 @@ String gav(Dependency d) { String gav(Plugin p) { return p.getGroupId() + ":" + p.getArtifactId() + ":" + p.getVersion(); } - } diff --git a/tesla-polyglot/tesla-polyglot-atom/src/test/resources/org/sonatype/maven/polyglot/atom/parsing/AtomParserTest.java b/tesla-polyglot/tesla-polyglot-atom/src/test/java/org/sonatype/maven/polyglot/atom/parsing/AtomParserTest.java similarity index 100% rename from tesla-polyglot/tesla-polyglot-atom/src/test/resources/org/sonatype/maven/polyglot/atom/parsing/AtomParserTest.java rename to tesla-polyglot/tesla-polyglot-atom/src/test/java/org/sonatype/maven/polyglot/atom/parsing/AtomParserTest.java diff --git a/tesla-polyglot/tesla-polyglot-atom/src/test/poms/sitebricks-pom.xml b/tesla-polyglot/tesla-polyglot-atom/src/test/poms/sitebricks-pom.xml index d5630ab05..084c63d77 100644 --- a/tesla-polyglot/tesla-polyglot-atom/src/test/poms/sitebricks-pom.xml +++ b/tesla-polyglot/tesla-polyglot-atom/src/test/poms/sitebricks-pom.xml @@ -1,4 +1,6 @@ - + 4.0.0 com.google.sitebricks @@ -7,6 +9,7 @@ sitebricks Sitebricks :: Core + http://sitebricks.org @@ -21,8 +24,8 @@ sitebricks-converter - com.google.sitebricks - sitebricks-client + com.google.sitebricks + sitebricks-client org.mvel @@ -137,4 +140,30 @@ + + + + maven-assembly-plugin + + + source-release-assembly + + + true + + + + + + someValue + someValue + + someValue + + + + + + +