Permalink
Browse files

Import of the core component doing artifact2gem conversion.

  • Loading branch information...
1 parent 9c74690 commit 182d5f21ee7eaf075318490781bae6b3b48fffd1 @cstamas cstamas committed Dec 18, 2009
Showing with 2,315 additions and 0 deletions.
  1. +14 −0 .gitignore
  2. +110 −0 nexus-ruby-tools/pom.xml
  3. +205 −0 nexus-ruby-tools/src/main/java/org/sonatype/nexus/ruby/DefaultMavenArtifactConverter.java
  4. +33 −0 nexus-ruby-tools/src/main/java/org/sonatype/nexus/ruby/MavenArtifact.java
  5. +54 −0 nexus-ruby-tools/src/main/java/org/sonatype/nexus/ruby/MavenArtifactConverter.java
  6. +90 −0 nexus-ruby-tools/src/main/java/org/sonatype/nexus/ruby/gem/DefaultGemPackager.java
  7. +113 −0 nexus-ruby-tools/src/main/java/org/sonatype/nexus/ruby/gem/DefaultGemSpecificationIO.java
  8. +55 −0 nexus-ruby-tools/src/main/java/org/sonatype/nexus/ruby/gem/GemDependency.java
  9. +75 −0 nexus-ruby-tools/src/main/java/org/sonatype/nexus/ruby/gem/GemFileEntry.java
  10. +25 −0 nexus-ruby-tools/src/main/java/org/sonatype/nexus/ruby/gem/GemPackager.java
  11. +52 −0 nexus-ruby-tools/src/main/java/org/sonatype/nexus/ruby/gem/GemRequirement.java
  12. +459 −0 nexus-ruby-tools/src/main/java/org/sonatype/nexus/ruby/gem/GemSpecification.java
  13. +12 −0 nexus-ruby-tools/src/main/java/org/sonatype/nexus/ruby/gem/GemSpecificationIO.java
  14. +30 −0 nexus-ruby-tools/src/main/java/org/sonatype/nexus/ruby/gem/GemVersion.java
  15. +30 −0 nexus-ruby-tools/src/main/java/org/sonatype/nexus/ruby/gem/yaml/MappingConstructor.java
  16. +38 −0 nexus-ruby-tools/src/main/java/org/sonatype/nexus/ruby/gem/yaml/MappingRepresenter.java
  17. +11 −0 nexus-ruby-tools/src/main/resources/metafile.rb.template
  18. +152 −0 nexus-ruby-tools/src/test/java/org/sonatype/nexus/ruby/MavenArtifactConverterTest.java
  19. +92 −0 nexus-ruby-tools/src/test/resources/metadata-a2ws
  20. +106 −0 nexus-ruby-tools/src/test/resources/metadata-prawn
  21. BIN ...ruby-tools/src/test/resources/repository/org/apache/ant/ant-launcher/1.7.1/ant-launcher-1.7.1.jar
  22. +1 −0 ...tools/src/test/resources/repository/org/apache/ant/ant-launcher/1.7.1/ant-launcher-1.7.1.jar.sha1
  23. +57 −0 ...ruby-tools/src/test/resources/repository/org/apache/ant/ant-launcher/1.7.1/ant-launcher-1.7.1.pom
  24. +1 −0 ...tools/src/test/resources/repository/org/apache/ant/ant-launcher/1.7.1/ant-launcher-1.7.1.pom.sha1
  25. +111 −0 nexus-ruby-tools/src/test/resources/repository/org/apache/ant/ant-parent/1.7.1/ant-parent-1.7.1.pom
  26. +1 −0 ...uby-tools/src/test/resources/repository/org/apache/ant/ant-parent/1.7.1/ant-parent-1.7.1.pom.sha1
  27. BIN nexus-ruby-tools/src/test/resources/repository/org/apache/ant/ant/1.7.1/ant-1.7.1.jar
  28. +1 −0 nexus-ruby-tools/src/test/resources/repository/org/apache/ant/ant/1.7.1/ant-1.7.1.jar.sha1
  29. +232 −0 nexus-ruby-tools/src/test/resources/repository/org/apache/ant/ant/1.7.1/ant-1.7.1.pom
  30. +1 −0 nexus-ruby-tools/src/test/resources/repository/org/apache/ant/ant/1.7.1/ant-1.7.1.pom.sha1
  31. BIN nexus-ruby-tools/src/test/resources/repository/org/slf4j/slf4j-api/1.5.8/slf4j-api-1.5.8-sources.jar
  32. +1 −0 ...by-tools/src/test/resources/repository/org/slf4j/slf4j-api/1.5.8/slf4j-api-1.5.8-sources.jar.sha1
  33. BIN nexus-ruby-tools/src/test/resources/repository/org/slf4j/slf4j-api/1.5.8/slf4j-api-1.5.8.jar
  34. +1 −0 nexus-ruby-tools/src/test/resources/repository/org/slf4j/slf4j-api/1.5.8/slf4j-api-1.5.8.jar.sha1
  35. +101 −0 nexus-ruby-tools/src/test/resources/repository/org/slf4j/slf4j-api/1.5.8/slf4j-api-1.5.8.pom
  36. +1 −0 nexus-ruby-tools/src/test/resources/repository/org/slf4j/slf4j-api/1.5.8/slf4j-api-1.5.8.pom.sha1
  37. BIN nexus-ruby-tools/src/test/resources/repository/org/slf4j/slf4j-simple/1.5.8/slf4j-simple-1.5.8.jar
  38. +1 −0 ...ruby-tools/src/test/resources/repository/org/slf4j/slf4j-simple/1.5.8/slf4j-simple-1.5.8.jar.sha1
  39. +48 −0 nexus-ruby-tools/src/test/resources/repository/org/slf4j/slf4j-simple/1.5.8/slf4j-simple-1.5.8.pom
  40. +1 −0 ...ruby-tools/src/test/resources/repository/org/slf4j/slf4j-simple/1.5.8/slf4j-simple-1.5.8.pom.sha1
View
@@ -0,0 +1,14 @@
+# Ignores for NexusPEED
+# Language is Java
+# Build tool is Apache Maven
+# IDE used is Eclipse + m2d
+# so, we have to ignore all the specifics of these above
+
+# Eclipse related (and M2e)
+.classpath
+.project
+.settings
+
+# Maven related
+target
+*.ser
View
@@ -0,0 +1,110 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.sonatype.nexus.ruby</groupId>
+ <artifactId>nexus-ruby-tools</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+
+ <packaging>jar</packaging>
+
+ <name>Nexus Ruby Tools</name>
+
+ <properties>
+ <plexus.version>1.5.1</plexus.version>
+ </properties>
+
+ <dependencies>
+
+ <!-- SnakeYaml has problems producing "correct" Ruby-readable YAML. It is here, but not used right now -->
+ <dependency>
+ <groupId>org.yaml</groupId>
+ <artifactId>snakeyaml</artifactId>
+ <version>1.4</version>
+ <optional>true</optional>
+ </dependency>
+ <!-- Used for Yaml IO -->
+ <dependency>
+ <groupId>net.sf.yamlbeans</groupId>
+ <artifactId>yamlbeans</artifactId>
+ <version>1.0</version>
+ </dependency>
+ <!-- We have components in here -->
+ <dependency>
+ <groupId>org.codehaus.plexus</groupId>
+ <artifactId>plexus-component-annotations</artifactId>
+ <version>${plexus.version}</version>
+ </dependency>
+ <!-- GEM is a tar.gz actually, so needed for creating Gems -->
+ <dependency>
+ <groupId>org.codehaus.plexus</groupId>
+ <artifactId>plexus-archiver</artifactId>
+ <version>1.0-alpha-12</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.codehaus.plexus</groupId>
+ <artifactId>plexus-container-default</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <!-- Needed to be able to read up Poms -->
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-model</artifactId>
+ <version>2.2.1</version>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.4</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.codehaus.plexus</groupId>
+ <artifactId>plexus-container-default</artifactId>
+ <version>${plexus.version}</version>
+ <scope>test</scope>
+ </dependency>
+
+ </dependencies>
+
+ <build>
+ <plugins>
+
+ <plugin>
+ <groupId>org.codehaus.plexus</groupId>
+ <artifactId>plexus-component-metadata</artifactId>
+ <version>${plexus.version}</version>
+ <executions>
+ <execution>
+ <id>process-classes</id>
+ <goals>
+ <goal>generate-metadata</goal>
+ </goals>
+ </execution>
+ <execution>
+ <id>process-test-classes</id>
+ <goals>
+ <goal>generate-test-metadata</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+
+ <!-- Have to set source level to Java5, we use annotations -->
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.0.2</version>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+
+ </plugins>
+ </build>
+
+</project>
@@ -0,0 +1,205 @@
+package org.sonatype.nexus.ruby;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Date;
+
+import org.apache.maven.model.Dependency;
+import org.apache.maven.model.Developer;
+import org.apache.maven.model.License;
+import org.apache.maven.model.Model;
+import org.codehaus.plexus.component.annotations.Component;
+import org.codehaus.plexus.component.annotations.Requirement;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.StringUtils;
+import org.sonatype.nexus.ruby.gem.GemDependency;
+import org.sonatype.nexus.ruby.gem.GemFileEntry;
+import org.sonatype.nexus.ruby.gem.GemPackager;
+import org.sonatype.nexus.ruby.gem.GemRequirement;
+import org.sonatype.nexus.ruby.gem.GemSpecification;
+import org.sonatype.nexus.ruby.gem.GemVersion;
+
+/**
+ * This is full of "workarounds" here, since for true artifact2gem conversion I would need interpolated POM!
+ *
+ * @author cstamas
+ */
+@Component( role = MavenArtifactConverter.class )
+public class DefaultMavenArtifactConverter
+ implements MavenArtifactConverter
+{
+ @Requirement
+ private GemPackager gemPackager;
+
+ public String createGemName( String groupId, String artifactId, String version )
+ {
+ // TODO: think about this
+ return groupId + "." + artifactId;
+ }
+
+ public String createGemVersion( String mavenVersion )
+ {
+ return mavenVersion;
+ }
+
+ public GemSpecification createSpecification( Model pom )
+ {
+ GemSpecification result = new GemSpecification();
+
+ // this is fix
+ result.setPlatform( "java" );
+
+ // the must ones
+ result.setName( createGemName( getGroupId( pom ), pom.getArtifactId(), getVersion( pom ) ) );
+ result.setVersion( new GemVersion( createGemVersion( getVersion( pom ) ) ) );
+
+ // dependencies
+ if ( pom.getDependencies().size() > 0 )
+ {
+ for ( Dependency dependency : pom.getDependencies() )
+ {
+ result.getDependencies().add( convertDependency( pom, dependency ) );
+ }
+ }
+
+ // and other stuff "nice to have"
+ result.setDate( new Date() ); // now
+ result.setDescription( pom.getDescription() != null ? pom.getDescription() : pom.getName() );
+ result.setSummary( pom.getName() );
+ result.setHomepage( pom.getUrl() );
+
+ if ( pom.getLicenses().size() > 0 )
+ {
+ for ( License license : pom.getLicenses() )
+ {
+ result.getLicenses().add( license.getName() + " (" + license.getUrl() + ")" );
+ }
+ }
+ if ( pom.getDevelopers().size() > 0 )
+ {
+ for ( Developer developer : pom.getDevelopers() )
+ {
+ result.getAuthors().add( developer.getName() + " (" + developer.getEmail() + ")" );
+ }
+ }
+
+ // by default, we pack into lib/ inside gem (where is the jar and the stub ruby)
+ result.getRequire_paths().add( "lib" );
+ return result;
+ }
+
+ public File createGemFromArtifact( MavenArtifact artifact )
+ throws IOException
+ {
+ GemSpecification gemspec = createSpecification( artifact.getPom() );
+
+ File gemFile =
+ new File( artifact.getArtifact().getParentFile(), gemspec.getName() + "-"
+ + gemspec.getVersion().getVersion() + "-" + gemspec.getPlatform() + ".gem" );
+
+ // create "meta" ruby file
+ File rubyStubFile = generateRubyStub( gemspec, artifact );
+ String rubyStubPath = "lib/" + gemspec.getName() + ".rb";
+
+ ArrayList<GemFileEntry> entries = new ArrayList<GemFileEntry>();
+ entries.add( new GemFileEntry( artifact.getArtifact(), true ) );
+ entries.add( new GemFileEntry( rubyStubFile, rubyStubPath, true ) );
+
+ // write file
+ gemPackager.createGem( gemspec, entries, gemFile );
+
+ return gemFile;
+ }
+
+ // ==
+
+ private File generateRubyStub( GemSpecification gemspec, MavenArtifact artifact )
+ throws IOException
+ {
+ String rubyStub = IOUtil.toString( getClass().getResourceAsStream( "/metafile.rb.template" ) );
+
+ String[] titleParts = artifact.getPom().getArtifactId().split( "-" );
+ StringBuilder titleizedArtifactId = new StringBuilder();
+ for ( String part : titleParts )
+ {
+ if ( part != null && part.length() != 0 )
+ {
+ titleizedArtifactId.append( StringUtils.capitalise( part ) );
+ }
+ }
+
+ rubyStub =
+ rubyStub.replaceFirst( "\\$\\{version\\}", gemspec.getVersion().getVersion() ).replaceFirst(
+ "\\$\\{maven_version\\}", getVersion( artifact.getPom() ) ).replaceFirst( "\\$\\{jar_file\\}",
+ artifact.getArtifact().getName() ).replaceFirst( "\\$\\{titleized_classname\\}",
+ titleizedArtifactId.toString() );
+
+ File rubyStubFile = File.createTempFile( "rubyStub", ".rb.tmp" );
+
+ FileWriter fw = new FileWriter( rubyStubFile );
+
+ fw.write( rubyStub );
+
+ fw.flush();
+
+ fw.close();
+
+ return rubyStubFile;
+ }
+
+ private GemDependency convertDependency( Model pom, Dependency dependency )
+ {
+ GemDependency result = new GemDependency();
+
+ result.setName( createGemName( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion() ) );
+
+ result.setType( getRubyDependencyType( dependency.getScope() ) );
+
+ GemRequirement requirement = new GemRequirement();
+
+ // TODO: we are adding "hard" dependencies here, but we should maybe support Maven ranges too
+ requirement.addRequirement( ">=", new GemVersion( createGemVersion( getDependencyVersion( pom, dependency ) ) ) );
+
+ result.setVersion_requirement( requirement );
+
+ return result;
+ }
+
+ private String getRubyDependencyType( String dependencyScope )
+ {
+ // ruby scopes
+ // :development
+ // :runtime
+ return ":runtime";
+ }
+
+ private String getGroupId( Model pom )
+ {
+ return pom.getGroupId() != null ? pom.getGroupId() : pom.getParent().getGroupId();
+ }
+
+ private String getVersion( Model pom )
+ {
+ return pom.getVersion() != null ? pom.getVersion() : pom.getParent().getVersion();
+ }
+
+ private String getDependencyVersion( Model pom, Dependency dependency )
+ {
+ if ( dependency.getVersion() != null )
+ {
+ return dependency.getVersion();
+ }
+ else if ( getGroupId( pom ).equals( dependency.getGroupId() ) )
+ {
+ // hm, this is same groupId, let's suppose they have same dependency!
+ return getVersion( pom );
+ }
+ else
+ {
+ // no help here, just interpolated POM
+ return "unknown";
+ }
+ }
+}
@@ -0,0 +1,33 @@
+package org.sonatype.nexus.ruby;
+
+import java.io.File;
+
+import org.apache.maven.model.Model;
+
+/**
+ * This bean holds the artifact to be converted. Model should be already loaded up, to support different loading
+ * strategies (ie. from pom.xml, from JAR itself, or using something like Maven2 support in Nexus).
+ */
+public class MavenArtifact
+{
+ private final Model pom;
+
+ private final File artifact;
+
+ public MavenArtifact( Model pom, File artifact )
+ {
+ this.pom = pom;
+
+ this.artifact = artifact;
+ }
+
+ public Model getPom()
+ {
+ return pom;
+ }
+
+ public File getArtifact()
+ {
+ return artifact;
+ }
+}
Oops, something went wrong.

0 comments on commit 182d5f2

Please sign in to comment.