Skip to content

Commit

Permalink
Import of the core component doing artifact2gem conversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Dec 18, 2009
1 parent 9c74690 commit 182d5f2
Show file tree
Hide file tree
Showing 40 changed files with 2,315 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
@@ -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
110 changes: 110 additions & 0 deletions nexus-ruby-tools/pom.xml
@@ -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;
}
}

0 comments on commit 182d5f2

Please sign in to comment.