package com.github.kamatama41.gradle.embulk;

import com.github.jrubygradle.JRubyExec;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.Task;

import java.io.File;

/**
 * Why is this class needed?
 * => For some reason, com.github.jrubygradle.JRubyExec cannot be loaded on Kotlin
 */
public class JRubyExecTasks {
    static void gemPushTask(Project project, final EmbulkExtension extension) {
        final JRubyExec task = project.getTasks().create("gemPush", JRubyExec.class);
        task.dependsOn("gem");
        task.setGroup(EmbulkPlugin.getGroupName());

        project.afterEvaluate(new Action<Project>() {
            @Override
            public void execute(Project project) {
                task.setJrubyVersion(extension.getJrubyVersion());
                task.jrubyArgs("-S");
                task.script("gem");
                task.scriptArgs("push", project.file(String.format("pkg/%s-%s.gem", project.getName(), project.getVersion())));

                JRubyExec.updateJRubyDependencies(project);
            }
        });
    }

    static void gemTask(final Project project, final EmbulkExtension extension) {
        final JRubyExec task = project.getTasks().create("gem", JRubyExec.class);
        task.dependsOn("gemspec", "classpath");
        task.setGroup(EmbulkPlugin.getGroupName());

        project.afterEvaluate(new Action<Project>() {
            @Override
            public void execute(final Project project) {
                task.setJrubyVersion(extension.getJrubyVersion());
                task.jrubyArgs("-S");
                task.script("gem");
                task.scriptArgs("build", project.file(project.getName() + ".gemspec"));

                task.doLast(new Action<Task>() {
                    @Override
                    public void execute(Task task) {
                        String gemFile = String.format("%s-%s.gem", project.getName(), project.getVersion());
                        File dst = project.file("pkg/" + gemFile);
                        if(!dst.getParentFile().exists()) {
                            if(!dst.getParentFile().mkdirs()) {
                                throw new IllegalStateException("Prent dir was not made.");
                            }
                        }
                        if(!project.file(gemFile).renameTo(project.file("pkg/" + gemFile))) {
                            throw new IllegalStateException("File was not renamed.");
                        }
                    }
                });

                JRubyExec.updateJRubyDependencies(project);
            }
        });
    }
}

@@ -106,73 +106,15 @@
 api.
-Action;%0Aimport org.gradle.api.Project;%0Aimport org.gradle.api.Task
+Project
 ;%0A%0Ai
@@ -568,94 +568,25 @@
 ate(
-new Action%3CProject%3E() %7B%0A            @Override%0A            public void execute(
+evaluated
 Project 
 proj
@@ -577,39 +577,29 @@
 atedProject 
-project) %7B%0A    
+-%3E %7B%0A
             
@@ -641,36 +641,32 @@
 rubyVersion());%0A
-    
             task
@@ -687,36 +687,32 @@
 %22);%0A            
-    
 task.script(%22gem
@@ -707,36 +707,32 @@
 .script(%22gem%22);%0A
-    
             task
@@ -751,17 +751,26 @@
 %22push%22, 
-p
+evaluatedP
 roject.f
@@ -796,33 +796,42 @@
 pkg/%25s-%25s.gem%22, 
-p
+evaluatedP
 roject.getName()
@@ -824,33 +824,42 @@
 ject.getName(), 
-p
+evaluatedP
 roject.getVersio
@@ -863,28 +863,24 @@
 sion())));%0A%0A
-    
             
@@ -913,39 +913,34 @@
 dencies(
-project);%0A            %7D
+evaluatedProject);
 %0A       
@@ -1250,114 +1250,30 @@
 ate(
-new Action%3CProject%3E() %7B%0A            @Override%0A            public void execute(final Project project) %7B%0A   
+evaluatedProject -%3E %7B%0A
     
@@ -1272,33 +1272,32 @@
 %3E %7B%0A            
- 
 task.setJrubyVer
@@ -1335,36 +1335,32 @@
 ));%0A            
-    
 task.jrubyArgs(%22
@@ -1357,36 +1357,32 @@
 rubyArgs(%22-S%22);%0A
-    
             task
@@ -1401,36 +1401,32 @@
 %22);%0A            
-    
 task.scriptArgs(
@@ -1434,17 +1434,26 @@
 build%22, 
-p
+evaluatedP
 roject.f
@@ -1456,17 +1456,26 @@
 ct.file(
-p
+evaluatedP
 roject.g
@@ -1508,28 +1508,24 @@
             
-    
 task.doLast(
@@ -1528,120 +1528,21 @@
 ast(
-new Action%3CTask%3E() %7B%0A                    @Override%0A                    public void execute(Task task) %7B%0A        
+ignored -%3E %7B%0A
     
@@ -1597,17 +1597,26 @@
 s.gem%22, 
-p
+evaluatedP
 roject.g
@@ -1625,17 +1625,26 @@
 Name(), 
-p
+evaluatedP
 roject.g
@@ -1673,24 +1673,16 @@
         
-        
 File dst
@@ -1684,17 +1684,26 @@
 e dst = 
-p
+evaluatedP
 roject.f
@@ -1725,24 +1725,16 @@
 mFile);%0A
-        
         
@@ -1793,32 +1793,24 @@
             
-        
 if(!dst.getP
@@ -1829,32 +1829,24 @@
 mkdirs()) %7B%0A
-        
             
@@ -1913,32 +1913,24 @@
 ot made.%22);%0A
-        
             
@@ -1955,34 +1955,18 @@
         
-        %7D%0A        
+%7D%0A
         
@@ -1977,17 +1977,26 @@
     if(!
-p
+evaluatedP
 roject.f
@@ -2017,17 +2017,26 @@
 enameTo(
-p
+evaluatedP
 roject.f
@@ -2057,32 +2057,24 @@
 emFile))) %7B%0A
-        
             
@@ -2159,44 +2159,10 @@
     
-        %7D%0A                    %7D%0A    
+%7D%0A
     
@@ -2170,28 +2170,24 @@
        %7D);%0A%0A
-    
             
@@ -2224,31 +2224,26 @@
 ies(
-project);%0A            %7D
+evaluatedProject);
 %0A   

Use Lambda instead of anonymous class