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

org.xtext.gradle.idea.IdeaDevelopmentPlugin 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.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.function.Consumer;
import org.eclipse.xtend2.lib.StringConcatenation;
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.Procedures.Procedure1;
import org.gradle.api.Action;
import org.gradle.api.DomainObjectCollection;
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.internal.file.copy.CopySpecInternal;
import org.gradle.api.internal.file.copy.DestinationRootCopySpec;
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.JavaBasePlugin;
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.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.gradle.plugins.ide.idea.IdeaPlugin;
import org.gradle.plugins.ide.idea.model.IdeaModel;
import org.gradle.plugins.ide.idea.model.IdeaModule;
import org.xtext.gradle.idea.tasks.AssembleSandbox;
import org.xtext.gradle.idea.tasks.DownloadIdea;
import org.xtext.gradle.idea.tasks.DownloadIdeaPlugins;
import org.xtext.gradle.idea.tasks.GradleExtensions;
import org.xtext.gradle.idea.tasks.IdeaExtension;
import org.xtext.gradle.idea.tasks.IdeaPluginDependencies;
import org.xtext.gradle.idea.tasks.IdeaPluginDependency;
import org.xtext.gradle.idea.tasks.IdeaPluginRepositories;
import org.xtext.gradle.idea.tasks.RunIdea;

@SuppressWarnings("all")
public class IdeaDevelopmentPlugin implements Plugin {
  public final static String IDEA_DEVELOPMENT_EXTENSION_NAME = "ideaDevelopment";
  
  public final static String IDEA_TASK_GROUP = "Intellij Idea";
  
  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, IdeaDevelopmentPlugin.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(IdeaDevelopmentPlugin.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());
      }
    }
  };
  
  private IdeaExtension idea;
  
  private DownloadIdea downloadIdea;
  
  private DownloadIdeaPlugins downloadPlugins;
  
  private AssembleSandbox assembleSandbox;
  
  private RunIdea runIdea;
  
  private JavaPluginConvention java;
  
  private Configuration ideaProvided;
  
  @Override
  public void apply(final Project project) {
    ExtensionContainer _extensions = project.getExtensions();
    IdeaExtension _create = _extensions.create(IdeaDevelopmentPlugin.IDEA_DEVELOPMENT_EXTENSION_NAME, IdeaExtension.class, project);
    this.idea = _create;
    this.configureDownloadIdeaTask(project);
    this.configureDownloadPluginsTask(project);
    this.configureassembleSandboxTask(project);
    this.configureRunIdeaTask(project);
    this.integrateWithJavaPlugin(project);
  }
  
  private void configureDownloadIdeaTask(final Project project) {
    TaskContainer _tasks = project.getTasks();
    DownloadIdea _create = _tasks.create("downloadIdea", DownloadIdea.class);
    final Procedure1 _function = new Procedure1() {
      @Override
      public void apply(final DownloadIdea it) {
        it.setGroup(IdeaDevelopmentPlugin.IDEA_TASK_GROUP);
        it.setDescription("Downloads Intellij Idea");
        final Action _function = new Action() {
          @Override
          public void execute(final Project p) {
            File _ideaHome = IdeaDevelopmentPlugin.this.idea.getIdeaHome();
            it.setIdeaHome(_ideaHome);
            String _ideaVersion = IdeaDevelopmentPlugin.this.idea.getIdeaVersion();
            it.setIdeaVersion(_ideaVersion);
          }
        };
        project.afterEvaluate(_function);
      }
    };
    DownloadIdea _doubleArrow = ObjectExtensions.operator_doubleArrow(_create, _function);
    this.downloadIdea = _doubleArrow;
    this.idea.setDownloadIdea(this.downloadIdea);
  }
  
  private void configureDownloadPluginsTask(final Project project) {
    TaskContainer _tasks = project.getTasks();
    DownloadIdeaPlugins _create = _tasks.create("downloadIdeaPlugins", DownloadIdeaPlugins.class);
    final Procedure1 _function = new Procedure1() {
      @Override
      public void apply(final DownloadIdeaPlugins it) {
        it.setDescription("Downloads Idea plugin dependencies");
        it.setGroup(IdeaDevelopmentPlugin.IDEA_TASK_GROUP);
        final Action _function = new Action() {
          @Override
          public void execute(final Project p) {
            File _pluginsCache = IdeaDevelopmentPlugin.this.idea.getPluginsCache();
            it.setDestinationDir(_pluginsCache);
            IdeaPluginRepositories _pluginRepositories = IdeaDevelopmentPlugin.this.idea.getPluginRepositories();
            it.setPluginRepositories(_pluginRepositories);
            IdeaPluginDependencies _pluginDependencies = IdeaDevelopmentPlugin.this.idea.getPluginDependencies();
            it.setPluginDependencies(_pluginDependencies);
          }
        };
        project.afterEvaluate(_function);
      }
    };
    DownloadIdeaPlugins _doubleArrow = ObjectExtensions.operator_doubleArrow(_create, _function);
    this.downloadPlugins = _doubleArrow;
    this.idea.setDownloadPlugins(this.downloadPlugins);
  }
  
  private AssembleSandbox configureassembleSandboxTask(final Project project) {
    TaskContainer _tasks = project.getTasks();
    AssembleSandbox _create = _tasks.create(IdeaDevelopmentPlugin.ASSEMBLE_SANDBOX_TASK_NAME, AssembleSandbox.class);
    final Procedure1 _function = 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 Action _function = new Action() {
          @Override
          public void execute(final Project p) {
            File _sandboxDir = IdeaDevelopmentPlugin.this.idea.getSandboxDir();
            it.setDestinationDir(_sandboxDir);
            CopySpec _plugin = it.getPlugin();
            String _name = project.getName();
            _plugin.into(_name);
            IdeaPluginDependencies _pluginDependencies = IdeaDevelopmentPlugin.this.idea.getPluginDependencies();
            Iterable _externalDependencies = _pluginDependencies.getExternalDependencies();
            final Consumer _function = new Consumer() {
              @Override
              public void accept(final IdeaPluginDependency dependency) {
                DestinationRootCopySpec _rootSpec = it.getRootSpec();
                CopySpecInternal _addChild = _rootSpec.addChild();
                String _id = dependency.getId();
                CopySpec _into = _addChild.into(_id);
                File _pluginsCache = IdeaDevelopmentPlugin.this.idea.getPluginsCache();
                String _id_1 = dependency.getId();
                File _divide = GradleExtensions.operator_divide(_pluginsCache, _id_1);
                String _version = dependency.getVersion();
                File _divide_1 = GradleExtensions.operator_divide(_divide, _version);
                _into.from(_divide_1);
              }
            };
            _externalDependencies.forEach(_function);
            IdeaPluginDependencies _pluginDependencies_1 = IdeaDevelopmentPlugin.this.idea.getPluginDependencies();
            Iterable _endorsedDependencies = _pluginDependencies_1.getEndorsedDependencies();
            final Consumer _function_1 = new Consumer() {
              @Override
              public void accept(final IdeaPluginDependency dependency) {
                DestinationRootCopySpec _rootSpec = it.getRootSpec();
                CopySpecInternal _addChild = _rootSpec.addChild();
                String _id = dependency.getId();
                CopySpec _into = _addChild.into(_id);
                File _ideaHome = IdeaDevelopmentPlugin.this.idea.getIdeaHome();
                File _divide = GradleExtensions.operator_divide(_ideaHome, "plugins");
                String _id_1 = dependency.getId();
                File _divide_1 = GradleExtensions.operator_divide(_divide, _id_1);
                _into.from(_divide_1);
              }
            };
            _endorsedDependencies.forEach(_function_1);
            IdeaPluginDependencies _pluginDependencies_2 = IdeaDevelopmentPlugin.this.idea.getPluginDependencies();
            TreeSet _projectDependencies = _pluginDependencies_2.getProjectDependencies();
            final Function1 _function_2 = new Function1() {
              @Override
              public Project apply(final IdeaPluginDependency it) {
                String _id = it.getId();
                return project.project(_id);
              }
            };
            Iterable _map = IterableExtensions.map(_projectDependencies, _function_2);
            final Function1 _function_3 = new Function1() {
              @Override
              public Task apply(final Project it) {
                TaskContainer _tasks = it.getTasks();
                return _tasks.getAt(IdeaDevelopmentPlugin.ASSEMBLE_SANDBOX_TASK_NAME);
              }
            };
            final Iterable upstreamSandBoxTasks = IterableExtensions.map(_map, _function_3);
            it.from(upstreamSandBoxTasks);
          }
        };
        project.afterEvaluate(_function);
      }
    };
    AssembleSandbox _doubleArrow = ObjectExtensions.operator_doubleArrow(_create, _function);
    return this.assembleSandbox = _doubleArrow;
  }
  
  private void configureRunIdeaTask(final Project project) {
    TaskContainer _tasks = project.getTasks();
    RunIdea _create = _tasks.create(IdeaDevelopmentPlugin.RUN_IDEA_TASK_NAME, RunIdea.class);
    final Procedure1 _function = new Procedure1() {
      @Override
      public void apply(final RunIdea it) {
        it.dependsOn(IdeaDevelopmentPlugin.this.assembleSandbox);
        it.setDescription("Runs Intellij IDEA with this project and its dependencies installed");
        it.setGroup(IdeaDevelopmentPlugin.IDEA_TASK_GROUP);
        final Action _function = new Action() {
          @Override
          public void execute(final Project p) {
            File _sandboxDir = IdeaDevelopmentPlugin.this.idea.getSandboxDir();
            it.setSandboxDir(_sandboxDir);
            File _ideaHome = IdeaDevelopmentPlugin.this.idea.getIdeaHome();
            it.setIdeaHome(_ideaHome);
            FileCollection _ideaRunClasspath = IdeaDevelopmentPlugin.this.idea.getIdeaRunClasspath();
            it.setClasspath(_ideaRunClasspath);
          }
        };
        project.afterEvaluate(_function);
      }
    };
    RunIdea _doubleArrow = ObjectExtensions.operator_doubleArrow(_create, _function);
    this.runIdea = _doubleArrow;
    Gradle _gradle = project.getGradle();
    TaskExecutionGraph _taskGraph = _gradle.getTaskGraph();
    _taskGraph.addTaskExecutionGraphListener(IdeaDevelopmentPlugin.runIdeaValidator);
  }
  
  private DomainObjectCollection integrateWithJavaPlugin(final Project project) {
    PluginContainer _plugins = project.getPlugins();
    final Action _function = new Action() {
      @Override
      public void execute(final JavaBasePlugin it) {
        Convention _convention = project.getConvention();
        JavaPluginConvention _plugin = _convention.getPlugin(JavaPluginConvention.class);
        IdeaDevelopmentPlugin.this.java = _plugin;
        PluginContainer _plugins = project.getPlugins();
        final Action _function = new Action() {
          @Override
          public void execute(final JavaPlugin it) {
            IdeaDevelopmentPlugin.this.addIdeaProvidedDependencies(project);
            IdeaDevelopmentPlugin.this.addIdeaDependenciesToEclipseClasspath(project);
            IdeaDevelopmentPlugin.this.addIdeaDependenciesToIntelliJClasspath(project);
            IdeaDevelopmentPlugin.this.adjustTestEnvironment(project);
          }
        };
        _plugins.withType(JavaPlugin.class, _function);
      }
    };
    return _plugins.withType(JavaBasePlugin.class, _function);
  }
  
  private void addIdeaProvidedDependencies(final Project project) {
    ConfigurationContainer _configurations = project.getConfigurations();
    Configuration _create = _configurations.create(IdeaDevelopmentPlugin.IDEA_PROVIDED_CONFIGURATION_NAME);
    this.ideaProvided = _create;
    SourceSetContainer _sourceSets = this.java.getSourceSets();
    final Action _function = new Action() {
      @Override
      public void execute(final SourceSet it) {
        FileCollection _compileClasspath = it.getCompileClasspath();
        FileCollection _plus = _compileClasspath.plus(IdeaDevelopmentPlugin.this.ideaProvided);
        it.setCompileClasspath(_plus);
        FileCollection _runtimeClasspath = it.getRuntimeClasspath();
        FileCollection _plus_1 = _runtimeClasspath.plus(IdeaDevelopmentPlugin.this.ideaProvided);
        ConfigurableFileCollection _olsJar = IdeaDevelopmentPlugin.this.idea.toolsJar();
        FileCollection _plus_2 = _plus_1.plus(_olsJar);
        it.setRuntimeClasspath(_plus_2);
      }
    };
    _sourceSets.all(_function);
    final Action _function_1 = new Action() {
      @Override
      public void execute(final Project it) {
        List _ideaLibs = IdeaDevelopmentPlugin.this.idea.getIdeaLibs();
        final Consumer _function = new Consumer() {
          @Override
          public void accept(final Object it) {
            DependencyHandler _dependencies = project.getDependencies();
            String _name = IdeaDevelopmentPlugin.this.ideaProvided.getName();
            _dependencies.add(_name, it);
          }
        };
        _ideaLibs.forEach(_function);
      }
    };
    project.afterEvaluate(_function_1);
  }
  
  private void adjustTestEnvironment(final Project project) {
    final Action _function = new Action() {
      @Override
      public void execute(final Project it) {
        TaskContainer _tasks = project.getTasks();
        TaskCollection _withType = _tasks.withType(Test.class);
        final Action _function = new Action() {
          @Override
          public void execute(final Test it) {
            it.dependsOn(IdeaDevelopmentPlugin.this.assembleSandbox);
            File _ideaHome = IdeaDevelopmentPlugin.this.idea.getIdeaHome();
            it.systemProperty("idea.home.path", _ideaHome);
            File _sandboxDir = IdeaDevelopmentPlugin.this.idea.getSandboxDir();
            it.systemProperty("idea.plugins.path", _sandboxDir);
            File _buildDir = project.getBuildDir();
            String _plus = (_buildDir + "/idea-test-system");
            it.systemProperty("idea.system.path", _plus);
            File _buildDir_1 = project.getBuildDir();
            String _plus_1 = (_buildDir_1 + "/idea-test-config");
            it.systemProperty("idea.config.path", _plus_1);
          }
        };
        _withType.all(_function);
      }
    };
    project.afterEvaluate(_function);
  }
  
  private DomainObjectCollection addIdeaDependenciesToEclipseClasspath(final Project project) {
    PluginContainer _plugins = project.getPlugins();
    final Action _function = new Action() {
      @Override
      public void execute(final EclipsePlugin it) {
        TaskContainer _tasks = project.getTasks();
        final Task eclipseClasspath = _tasks.getByName(EclipsePlugin.ECLIPSE_CP_TASK_NAME);
        DownloadIdea _downloadIdea = IdeaDevelopmentPlugin.this.idea.getDownloadIdea();
        DownloadIdeaPlugins _downloadPlugins = IdeaDevelopmentPlugin.this.idea.getDownloadPlugins();
        eclipseClasspath.dependsOn(_downloadIdea, _downloadPlugins);
        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(IdeaDevelopmentPlugin.this.ideaProvided);
            final FileReferenceFactory fileReferenceFactory = new FileReferenceFactory();
            XmlFileContentMerger _file = it.getFile();
            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 = IdeaDevelopmentPlugin.this.idea.getIdeaCoreLibs();
                    FileReference _library = it.getLibrary();
                    File _file = _library.getFile();
                    return Boolean.valueOf(_ideaCoreLibs.contains(_file));
                  }
                };
                Iterable _filter_1 = IterableExtensions.filter(_filter, _function);
                final Consumer _function_1 = new Consumer() {
                  @Override
                  public void accept(final Library it) {
                    File _sourcesZip = IdeaDevelopmentPlugin.this.idea.getSourcesZip();
                    FileReference _fromFile = fileReferenceFactory.fromFile(_sourcesZip);
                    it.setSourcePath(_fromFile);
                  }
                };
                _filter_1.forEach(_function_1);
              }
            };
            _file.whenMerged(_function);
          }
        };
        ObjectExtensions.operator_doubleArrow(_classpath, _function);
      }
    };
    return _plugins.withType(EclipsePlugin.class, _function);
  }
  
  private DomainObjectCollection addIdeaDependenciesToIntelliJClasspath(final Project project) {
    PluginContainer _plugins = project.getPlugins();
    final Action _function = new Action() {
      @Override
      public void execute(final IdeaPlugin it) {
        TaskContainer _tasks = project.getTasks();
        Task _byName = _tasks.getByName("ideaModule");
        DownloadIdea _downloadIdea = IdeaDevelopmentPlugin.this.idea.getDownloadIdea();
        DownloadIdeaPlugins _downloadPlugins = IdeaDevelopmentPlugin.this.idea.getDownloadPlugins();
        _byName.dependsOn(_downloadIdea, _downloadPlugins);
        ExtensionContainer _extensions = project.getExtensions();
        final IdeaModel ideaModel = _extensions.getByType(IdeaModel.class);
        IdeaModule _module = ideaModel.getModule();
        Map>> _scopes = _module.getScopes();
        Map> _get = _scopes.get("PROVIDED");
        Collection _get_1 = _get.get("plus");
        _get_1.add(IdeaDevelopmentPlugin.this.ideaProvided);
      }
    };
    return _plugins.withType(IdeaPlugin.class, _function);
  }
}