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

org.xtext.gradle.idea.IdeaComponentPlugin Maven / Gradle / Ivy

There is a newer version: 2.0.9-alpha.3
Show newest version
package org.xtext.gradle.idea;

import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Pair;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.artifacts.dsl.DependencyHandler;
import org.gradle.api.execution.TaskExecutionGraph;
import org.gradle.api.execution.TaskExecutionGraphListener;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.CopySpec;
import org.gradle.api.file.FileCollection;
import org.gradle.api.invocation.Gradle;
import org.gradle.api.plugins.BasePlugin;
import org.gradle.api.plugins.Convention;
import org.gradle.api.plugins.ExtensionContainer;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.plugins.PluginContainer;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.TaskCollection;
import org.gradle.api.tasks.TaskContainer;
import org.gradle.api.tasks.testing.Test;
import org.gradle.listener.ActionBroadcast;
import org.gradle.plugins.ide.api.XmlFileContentMerger;
import org.gradle.plugins.ide.eclipse.EclipsePlugin;
import org.gradle.plugins.ide.eclipse.model.Classpath;
import org.gradle.plugins.ide.eclipse.model.ClasspathEntry;
import org.gradle.plugins.ide.eclipse.model.EclipseClasspath;
import org.gradle.plugins.ide.eclipse.model.EclipseModel;
import org.gradle.plugins.ide.eclipse.model.FileReference;
import org.gradle.plugins.ide.eclipse.model.Library;
import org.gradle.plugins.ide.eclipse.model.internal.FileReferenceFactory;
import org.xtext.gradle.idea.IdeaDevelopmentPlugin;
import org.xtext.gradle.idea.tasks.AssembleSandbox;
import org.xtext.gradle.idea.tasks.DownloadIdea;
import org.xtext.gradle.idea.tasks.DownloadPlugins;
import org.xtext.gradle.idea.tasks.IdeaExtension;
import org.xtext.gradle.idea.tasks.IdeaPluginDependencies;
import org.xtext.gradle.idea.tasks.RunIdea;

@SuppressWarnings("all")
public class IdeaComponentPlugin implements Plugin {
  public final static String IDEA_PROVIDED_CONFIGURATION_NAME = "ideaProvided";
  
  public final static String ASSEMBLE_SANDBOX_TASK_NAME = "assembleSandbox";
  
  public final static String RUN_IDEA_TASK_NAME = "runIdea";
  
  private final static TaskExecutionGraphListener runIdeaValidator = new TaskExecutionGraphListener() {
    @Override
    public void graphPopulated(final TaskExecutionGraph graph) {
      List _allTasks = graph.getAllTasks();
      final Function1 _function = new Function1() {
        @Override
        public Boolean apply(final Task it) {
          String _name = it.getName();
          return Boolean.valueOf(Objects.equal(_name, IdeaComponentPlugin.RUN_IDEA_TASK_NAME));
        }
      };
      final Iterable runTasks = IterableExtensions.filter(_allTasks, _function);
      int _size = IterableExtensions.size(runTasks);
      boolean _greaterThan = (_size > 1);
      if (_greaterThan) {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("There are multiple ");
        _builder.append(IdeaComponentPlugin.RUN_IDEA_TASK_NAME, "");
        _builder.append(" tasks in the task graph.");
        _builder.newLineIfNotEmpty();
        _builder.append("When calling runIdea on an aggregate project, make sure you fully qualify the task name,");
        _builder.newLine();
        _builder.append("e.g. \':myProject:runIdea\'");
        _builder.newLine();
        throw new GradleException(_builder.toString());
      }
    }
  };
  
  @Override
  public void apply(final Project project) {
    PluginContainer _plugins = project.getPlugins();
    _plugins.apply(IdeaDevelopmentPlugin.class);
    PluginContainer _plugins_1 = project.getPlugins();
    _plugins_1.apply(JavaPlugin.class);
    ExtensionContainer _extensions = project.getExtensions();
    final IdeaExtension idea = _extensions.getByType(IdeaExtension.class);
    Convention _convention = project.getConvention();
    final JavaPluginConvention java = _convention.getPlugin(JavaPluginConvention.class);
    ConfigurationContainer _configurations = project.getConfigurations();
    final Configuration compile = _configurations.getAt(JavaPlugin.COMPILE_CONFIGURATION_NAME);
    Pair _mappedTo = Pair.of("module", "guava");
    compile.exclude(Collections.unmodifiableMap(CollectionLiterals.newHashMap(_mappedTo)));
    Pair _mappedTo_1 = Pair.of("module", "log4j");
    compile.exclude(Collections.unmodifiableMap(CollectionLiterals.newHashMap(_mappedTo_1)));
    ConfigurationContainer _configurations_1 = project.getConfigurations();
    final Configuration ideaProvided = _configurations_1.create(IdeaComponentPlugin.IDEA_PROVIDED_CONFIGURATION_NAME);
    SourceSetContainer _sourceSets = java.getSourceSets();
    final Action _function = new Action() {
      @Override
      public void execute(final SourceSet it) {
        FileCollection _compileClasspath = it.getCompileClasspath();
        FileCollection _plus = _compileClasspath.plus(ideaProvided);
        it.setCompileClasspath(_plus);
        FileCollection _runtimeClasspath = it.getRuntimeClasspath();
        FileCollection _plus_1 = _runtimeClasspath.plus(ideaProvided);
        ConfigurableFileCollection _olsJar = idea.toolsJar();
        FileCollection _plus_2 = _plus_1.plus(_olsJar);
        it.setRuntimeClasspath(_plus_2);
      }
    };
    _sourceSets.all(_function);
    TaskContainer _tasks = project.getTasks();
    AssembleSandbox _create = _tasks.create(IdeaComponentPlugin.ASSEMBLE_SANDBOX_TASK_NAME, AssembleSandbox.class);
    final Procedure1 _function_1 = new Procedure1() {
      @Override
      public void apply(final AssembleSandbox it) {
        it.setDescription("Creates a folder containing the plugins to run Idea with");
        it.setGroup(BasePlugin.BUILD_GROUP);
      }
    };
    final AssembleSandbox assembleSandboxTask = ObjectExtensions.operator_doubleArrow(_create, _function_1);
    final Action _function_2 = new Action() {
      @Override
      public void execute(final Project it) {
        List _ideaLibs = idea.getIdeaLibs();
        final Procedure1 _function = new Procedure1() {
          @Override
          public void apply(final Object it) {
            DependencyHandler _dependencies = project.getDependencies();
            String _name = ideaProvided.getName();
            _dependencies.add(_name, it);
          }
        };
        IterableExtensions.forEach(_ideaLibs, _function);
        File _sandboxDir = idea.getSandboxDir();
        assembleSandboxTask.setDestinationDir(_sandboxDir);
        CopySpec _plugin = assembleSandboxTask.getPlugin();
        String _name = project.getName();
        _plugin.into(_name);
        DownloadPlugins _downloadPlugins = idea.getDownloadPlugins();
        File _destinationDir = _downloadPlugins.getDestinationDir();
        assembleSandboxTask.from(_destinationDir);
        IdeaPluginDependencies _pluginDependencies = idea.getPluginDependencies();
        HashSet _projectDependencies = _pluginDependencies.getProjectDependencies();
        final Function1 _function_1 = new Function1() {
          @Override
          public Project apply(final String it) {
            return project.project(it);
          }
        };
        Iterable _map = IterableExtensions.map(_projectDependencies, _function_1);
        final Function1 _function_2 = new Function1() {
          @Override
          public AssembleSandbox apply(final Project it) {
            TaskContainer _tasks = it.getTasks();
            Task _at = _tasks.getAt(IdeaComponentPlugin.ASSEMBLE_SANDBOX_TASK_NAME);
            return ((AssembleSandbox) _at);
          }
        };
        final Iterable upstreamSandBoxTasks = IterableExtensions.map(_map, _function_2);
        assembleSandboxTask.from(upstreamSandBoxTasks);
        assembleSandboxTask.exclude("*.zip");
      }
    };
    project.afterEvaluate(_function_2);
    TaskContainer _tasks_1 = project.getTasks();
    RunIdea _create_1 = _tasks_1.create(IdeaComponentPlugin.RUN_IDEA_TASK_NAME, RunIdea.class);
    final Procedure1 _function_3 = new Procedure1() {
      @Override
      public void apply(final RunIdea it) {
        it.dependsOn(assembleSandboxTask);
        it.setDescription("Runs Intellij IDEA with this project and its dependencies installed");
        it.setGroup(IdeaDevelopmentPlugin.IDEA_TASK_GROUP);
      }
    };
    final RunIdea runIdea = ObjectExtensions.operator_doubleArrow(_create_1, _function_3);
    final Action _function_4 = new Action() {
      @Override
      public void execute(final Project it) {
        File _sandboxDir = idea.getSandboxDir();
        runIdea.setSandboxDir(_sandboxDir);
        File _ideaHome = idea.getIdeaHome();
        runIdea.setIdeaHome(_ideaHome);
        FileCollection _ideaRunClasspath = idea.getIdeaRunClasspath();
        runIdea.setClasspath(_ideaRunClasspath);
      }
    };
    project.afterEvaluate(_function_4);
    Gradle _gradle = project.getGradle();
    TaskExecutionGraph _taskGraph = _gradle.getTaskGraph();
    _taskGraph.addTaskExecutionGraphListener(IdeaComponentPlugin.runIdeaValidator);
    TaskContainer _tasks_2 = project.getTasks();
    TaskCollection _withType = _tasks_2.withType(Test.class);
    final Action _function_5 = new Action() {
      @Override
      public void execute(final Test it) {
        it.dependsOn(assembleSandboxTask);
        File _ideaHome = idea.getIdeaHome();
        it.systemProperty("idea.home.path", _ideaHome);
        File _sandboxDir = idea.getSandboxDir();
        it.systemProperty("idea.plugins.path", _sandboxDir);
      }
    };
    _withType.all(_function_5);
    PluginContainer _plugins_2 = project.getPlugins();
    final Action _function_6 = new Action() {
      @Override
      public void execute(final EclipsePlugin it) {
        TaskContainer _tasks = project.getTasks();
        String _eCLIPSE_CP_TASK_NAME = EclipsePlugin.getECLIPSE_CP_TASK_NAME();
        Task _byName = _tasks.getByName(_eCLIPSE_CP_TASK_NAME);
        DownloadIdea _downloadIdea = idea.getDownloadIdea();
        _byName.dependsOn(_downloadIdea);
        ExtensionContainer _extensions = project.getExtensions();
        EclipseModel _byType = _extensions.getByType(EclipseModel.class);
        EclipseClasspath _classpath = _byType.getClasspath();
        final Procedure1 _function = new Procedure1() {
          @Override
          public void apply(final EclipseClasspath it) {
            Collection _plusConfigurations = it.getPlusConfigurations();
            _plusConfigurations.add(ideaProvided);
            final FileReferenceFactory fileReferenceFactory = new FileReferenceFactory();
            final File sourceZip = idea.getSourcesZip();
            XmlFileContentMerger _file = it.getFile();
            ActionBroadcast _whenMerged = _file.getWhenMerged();
            final Action _function = new Action() {
              @Override
              public void execute(final Classpath it) {
                List _entries = it.getEntries();
                Iterable _filter = Iterables.filter(_entries, Library.class);
                final Function1 _function = new Function1() {
                  @Override
                  public Boolean apply(final Library it) {
                    FileCollection _ideaCoreLibs = idea.getIdeaCoreLibs();
                    FileReference _library = it.getLibrary();
                    File _file = _library.getFile();
                    return Boolean.valueOf(_ideaCoreLibs.contains(_file));
                  }
                };
                Iterable _filter_1 = IterableExtensions.filter(_filter, _function);
                final Procedure1 _function_1 = new Procedure1() {
                  @Override
                  public void apply(final Library it) {
                    FileReference _fromFile = fileReferenceFactory.fromFile(sourceZip);
                    it.setSourcePath(_fromFile);
                  }
                };
                IterableExtensions.forEach(_filter_1, _function_1);
              }
            };
            _whenMerged.add(_function);
          }
        };
        ObjectExtensions.operator_doubleArrow(_classpath, _function);
      }
    };
    _plugins_2.withType(EclipsePlugin.class, _function_6);
  }
}