All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.eriwen.gradle.js.source.internal.DefaultJavaScriptProcessingChain Maven / Gradle / Ivy

There is a newer version: 1.12.0
Show newest version
package com.eriwen.gradle.js.source.internal;

import com.eriwen.gradle.js.source.JavaScriptProcessingChain;
import com.eriwen.gradle.js.source.JavaScriptSourceSet;
import groovy.lang.Closure;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.DefaultNamedDomainObjectList;
import org.gradle.api.tasks.SourceTask;

import java.util.Collections;
import java.util.concurrent.Callable;

public class DefaultJavaScriptProcessingChain extends DefaultNamedDomainObjectList implements JavaScriptProcessingChain {

    private final DefaultJavaScriptSourceSet source;
    private final Project project;

    public DefaultJavaScriptProcessingChain(Project project, DefaultJavaScriptSourceSet source) {
        super(SourceTask.class, InternalGradle.toInstantiator(project), new Task.Namer());
        this.source = source;
        this.project = project;
        wireChain();
    }

    public JavaScriptSourceSet getSource() {
        return source;
    }

    protected void wireChain() {
        all(new Action() {
            public void execute(final SourceTask sourceTask) {
                sourceTask.source(new Callable() {
                    public FileCollection call() throws Exception {
                        int index = indexOf(sourceTask);
                        if (index == -1) {
                            return null; // task has been removed, noop
                        } else if (index == 0) {
                            return getSource().getJs();
                        } else {
                            SourceTask previous = get(index - 1);
                            return previous.getOutputs().getFiles();
                        }
                    }
                });
            }
        });
    }

    public  T task(Class type) {
        return task(calculateName(type), type);
    }
    
    public  T task(String name, Class type) {
        return task(name, type, null);
    }
    
    public  T task(Class type, Closure closure) {
        return task(calculateName(type), type, closure);
    }
    
    public  T task(String name, Class type, Closure closure) {
        T task = (T)project.task(Collections.singletonMap("type", type), name, closure);
        add(task);
        return task;
    }
    
    protected String calculateName(Class type) {
        String name = type.getName();
        if (name.endsWith("Task")) {
            name = name.substring(0, name.length() - 4);
        }

        return source.getName() + name;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy