package de.saumya.mojo.gem;

import java.io.File;
import java.io.IOException;

import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.MojoExecutionException;

import de.saumya.mojo.gems.GemspecConverter;
import de.saumya.mojo.jruby.AbstractJRubyMojo;
import de.saumya.mojo.ruby.script.ScriptException;

/**
 * goal to converts a gemspec file into pom.xml.
 * 
 * @goal pom
 */
public class PomMojo extends AbstractJRubyMojo {

    /**
     * @parameter expression="${pom}" default-value="pom.xml"
     */
    protected File    pom;

    /**
     * @parameter default-value="${pom.force}"
     */
    protected boolean force = false;

    /**
     * @parameter default-value="${pom.gemspec}"
     */
    protected File    gemspecFile;

    /**
     * @plugin
     */
    Plugin            plugin;

    @Override
    public void executeJRuby() throws MojoExecutionException {
        if (this.pom.exists() && !this.force) {
            getLog().info(this.pom.getName()
                    + " already exists. use '-Dpom.force=true' to overwrite");
            return;
        }
        if (this.gemspecFile == null) {
            getLog().debug("no gemspec file given, see if there is single one");
            for (final File file : new File(".").listFiles()) {
                if (file.getName().endsWith(".gemspec")) {
                    if (this.gemspecFile != null) {
                        getLog().info("there is no gemspec file given but there are more then one in the current directory.");
                        getLog().info("use '-Dpom.gemspec=...' to select the gemspec file to process");
                        return;
                    }
                    this.gemspecFile = file;
                }
            }
        }
        if (this.gemspecFile == null) {
            getLog().info("there is no gemspec file given and no gemspec file found (*.gemspec). nothing to do.");
            return;
        }
        else {
            try {
                final GemspecConverter gemspec = new GemspecConverter(this.logger,
                        this.factory);
                String version = null;
                for (final Plugin plugin : this.project.getBuild().getPlugins()) {
                    if (plugin.getArtifactId().equals("gem-maven-plugin")) {
                        version = plugin.getVersion();
                        break;
                    }
                }
                if (version == null) {
                    throw new IllegalArgumentException("did not find gem-maven-plugin in POM");
                }
                gemspec.createPom(this.gemspecFile, version, this.pom);

            }
            catch (final ScriptException e) {
                throw new MojoExecutionException("error in script", e);
            }
            catch (final IOException e) {
                throw new MojoExecutionException("IO error", e);
            }
        }
    }
}

@@ -100,19 +100,36 @@
 ven.
-model.Plugi
+plugin.MojoExecutionExceptio
 n;%0Ai
@@ -158,38 +158,43 @@
 .plugin.
-MojoExecutionException
+descriptor.PluginDescriptor
 ;%0A%0Aimpor
@@ -456,16 +456,102 @@
 Mojo %7B%0A%0A
+    /** @parameter expression=%22$%7Bplugin%7D%22 @readonly */%0A    PluginDescriptor  plugin;%0A%0A
     /**%0A
@@ -806,32 +806,32 @@
 mspec%7D%22%0A     */%0A
+
     protected Fi
@@ -854,70 +854,8 @@
 e;%0A%0A
-    /**%0A     * @plugin%0A     */%0A    Plugin            plugin;%0A%0A
     
@@ -2157,529 +2157,68 @@
     
-String version = null;%0A                for (final Plugin plugin : this.project.getBuild().getPlugins()) %7B%0A                    if (plugin.getArtifactId().equals(%22gem-maven-plugin%22)) %7B%0A                        version = plugin.getVersion();%0A                        break;%0A                    %7D%0A                %7D%0A                if (version == null) %7B%0A                    throw new IllegalArgumentException(%22did not find gem-maven-plugin in POM%22);%0A                %7D%0A                gemspec.createPom(this.gemspecFile, v
+gemspec.createPom(this.gemspecFile, this.plugin.getV
 ersion
+()
 , th

use plugin version from pluginDescription to generate pom