package de.saumya.mojo.gem;

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

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.sonatype.aether.RepositorySystemSession;

import de.saumya.mojo.ruby.gems.GemException;
import de.saumya.mojo.ruby.script.Script;
import de.saumya.mojo.ruby.script.ScriptException;

/**
 * goal to push a given gem or a gem artifact to rubygems.org via the 
 * command "gem push {gemfile}"
 * 
 * @goal push
 * @phase deploy
 */
public class PushMojo extends AbstractGemMojo {

    /**
     * arguments for the ruby script given through file parameter.
     * <br/>
     * Command line -Dpush.args=...
     * 
     * @parameter expression="${push.args}"
     */
    protected String pushArgs = null;

    /**
     * arguments for the ruby script given through file parameter.
     * <br/>
     * Command line -Dgem=...
     * 
     * @parameter expression="${gem}"
     */
    protected File gem;
    
    /**
     * @parameter default-value="${repositorySystemSession}"
     * @readonly
     */
    protected RepositorySystemSession repoSession;

    @Override
    public void executeWithGems() throws MojoExecutionException,
            ScriptException, IOException, MojoFailureException, GemException {
        gemsInstaller.installOpenSSLGem(this.repoSession, localRepository);
        final Script script = this.factory.newScriptFromJRubyJar("gem")
                .addArg("push");
        if(this.project.getArtifact().getFile() == null){
            File f = new File(this.project.getBuild().getDirectory(), this.project.getBuild().getFinalName() +".gem");
            if (f.exists()) {
                this.project.getArtifact().setFile(f);
            }
        }
        // no given gem and pom artifact in place
        if (this.gem == null && this.project.getArtifact() != null
                && this.project.getArtifact().getFile() != null
                && this.project.getArtifact().getFile().exists()) {
            final GemArtifact gemArtifact = new GemArtifact(this.project);
            // skip artifact unless it is a gem.
            // this allows to use this mojo for installing arbitrary gems
            if (!gemArtifact.isGem()) {
                throw new MojoExecutionException("not a gem artifact");
            }
            script.addArg(gemArtifact.getFile());
        }
        else {
            // no pom artifact and no given gem so search for a gem
            if (this.gem == null) {
                for (final File f : this.launchDirectory().listFiles()) {
                    if (f.getName().endsWith(".gem")) {
                        if (this.gem == null) {
                            this.gem = f;
                        }
                        else {
                            throw new MojoFailureException("more than one gem file found, use -Dgem=... to specifiy one");
                        }
                    }
                }
            }
            if (this.gem != null) {
                getLog().info("use gem: " + this.gem);
                script.addArg(this.gem);
            }
        }
        script.addArgs(this.pushArgs);
        script.addArgs(this.args);
        script.execute();
    }
}

@@ -568,16 +568,202 @@
 Mojo %7B%0A%0A
+    /**%0A     * skip the pushng the gem%0A     * %3Cbr/%3E%0A     * Command line -Dpush.skipp=...%0A     * %0A     * @parameter expression=%22$%7Bpush.skip%7D%22%0A     */%0A    protected boolean skip = false;%0A%0A
     /**%0A
@@ -1323,16 +1323,260 @@
 ession;%0A
+    %0A    @Override%0A    public void execute() throws MojoExecutionException, MojoFailureException%7B%0A        if (skip)%7B%0A            getLog().info( %22skipping to push gem%22 );%0A        %7D%0A        else %7B%0A            super.execute();%0A        %7D%0A    %7D%0A    
 %0A    @Ov

allow push to be skipped