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

org.xtext.gradle.idea.tasks.IdeaExtension Maven / Gradle / Ivy

package org.xtext.gradle.idea.tasks;

import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import groovy.lang.Closure;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.TreeSet;
import java.util.concurrent.Callable;
import org.eclipse.xtend.lib.annotations.Accessors;
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.Functions.Function2;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.Pure;
import org.gradle.api.Project;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.ConfigurableFileTree;
import org.gradle.api.file.FileCollection;
import org.gradle.api.invocation.Gradle;
import org.gradle.api.tasks.util.PatternFilterable;
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.IdeaDistribution;
import org.xtext.gradle.idea.tasks.IdeaPluginDependencies;
import org.xtext.gradle.idea.tasks.IdeaPluginDependency;
import org.xtext.gradle.idea.tasks.IdeaPluginRepositories;

@Accessors
@SuppressWarnings("all")
public class IdeaExtension {
  private final Project project;
  
  private final IdeaPluginRepositories pluginRepositories;
  
  private final IdeaPluginDependencies pluginDependencies;
  
  private Object ideaHome;
  
  private String ideaVersion;
  
  private DownloadIdea downloadIdea;
  
  private DownloadIdeaPlugins downloadPlugins;
  
  public IdeaExtension(final Project project) {
    this.project = project;
    IdeaPluginRepositories _ideaPluginRepositories = new IdeaPluginRepositories();
    this.pluginRepositories = _ideaPluginRepositories;
    IdeaPluginDependencies _ideaPluginDependencies = new IdeaPluginDependencies(project);
    this.pluginDependencies = _ideaPluginDependencies;
  }
  
  public List getIdeaLibs() {
    ArrayList _xblockexpression = null;
    {
      final ArrayList result = CollectionLiterals.newArrayList();
      TreeSet _projectDependencies = this.pluginDependencies.getProjectDependencies();
      final Function1 _function = new Function1() {
        @Override
        public Project apply(final IdeaPluginDependency it) {
          String _id = it.getId();
          return IdeaExtension.this.project.project(_id);
        }
      };
      Iterable _map = IterableExtensions.map(_projectDependencies, _function);
      Iterables.addAll(result, _map);
      FileCollection _externalLibs = this.getExternalLibs();
      result.add(_externalLibs);
      _xblockexpression = result;
    }
    return _xblockexpression;
  }
  
  public FileCollection getExternalLibs() {
    ConfigurableFileCollection _files = this.project.files(new Callable() {
      @Override
      public FileCollection call() throws Exception {
        FileCollection _xblockexpression = null;
        {
          Iterable _externalDependencies = IdeaExtension.this.pluginDependencies.getExternalDependencies();
          final Function1 _function = new Function1() {
            @Override
            public File apply(final IdeaPluginDependency it) {
              File _pluginsCache = IdeaExtension.this.getPluginsCache();
              String _id = it.getId();
              File _divide = GradleExtensions.operator_divide(_pluginsCache, _id);
              String _version = it.getVersion();
              return GradleExtensions.operator_divide(_divide, _version);
            }
          };
          Iterable _map = IterableExtensions.map(_externalDependencies, _function);
          Iterable _endorsedDependencies = IdeaExtension.this.pluginDependencies.getEndorsedDependencies();
          final Function1 _function_1 = new Function1() {
            @Override
            public File apply(final IdeaPluginDependency it) {
              File _ideaHome = IdeaExtension.this.getIdeaHome();
              File _divide = GradleExtensions.operator_divide(_ideaHome, "plugins");
              String _id = it.getId();
              return GradleExtensions.operator_divide(_divide, _id);
            }
          };
          Iterable _map_1 = IterableExtensions.map(_endorsedDependencies, _function_1);
          final Iterable unpackedDependencies = Iterables.concat(_map, _map_1);
          final Function1 _function_2 = new Function1() {
            @Override
            public ConfigurableFileCollection apply(final File it) {
              ConfigurableFileCollection _xblockexpression = null;
              {
                final File classesDir = GradleExtensions.operator_divide(it, "classes");
                ConfigurableFileCollection _xifexpression = null;
                boolean _exists = classesDir.exists();
                if (_exists) {
                  _xifexpression = IdeaExtension.this.project.files(classesDir);
                } else {
                  _xifexpression = IdeaExtension.this.project.files();
                }
                _xblockexpression = _xifexpression;
              }
              return _xblockexpression;
            }
          };
          Iterable _map_2 = IterableExtensions.map(unpackedDependencies, _function_2);
          final Function2 _function_3 = new Function2() {
            @Override
            public FileCollection apply(final FileCollection a, final FileCollection b) {
              return a.plus(b);
            }
          };
          final FileCollection dependencyClasses = IterableExtensions.reduce(_map_2, _function_3);
          final Function1 _function_4 = new Function1() {
            @Override
            public FileCollection apply(final File it) {
              File _divide = GradleExtensions.operator_divide(it, "lib");
              ConfigurableFileTree _fileTree = IdeaExtension.this.project.fileTree(_divide);
              PatternFilterable _include = _fileTree.include("*.jar");
              return ((FileCollection) _include);
            }
          };
          Iterable _map_3 = IterableExtensions.map(unpackedDependencies, _function_4);
          final Function2 _function_5 = new Function2() {
            @Override
            public FileCollection apply(final FileCollection a, final FileCollection b) {
              return a.plus(b);
            }
          };
          final FileCollection dependencyLibs = IterableExtensions.reduce(_map_3, _function_5);
          FileCollection _ideaCoreLibs = IdeaExtension.this.getIdeaCoreLibs();
          Iterable _filterNull = IterableExtensions.filterNull(Collections.unmodifiableList(CollectionLiterals.newArrayList(_ideaCoreLibs, dependencyClasses, dependencyLibs)));
          final Function2 _function_6 = new Function2() {
            @Override
            public FileCollection apply(final FileCollection a, final FileCollection b) {
              return a.plus(b);
            }
          };
          _xblockexpression = IterableExtensions.reduce(_filterNull, _function_6);
        }
        return _xblockexpression;
      }
    });
    return _files.builtBy(this.downloadIdea, this.downloadPlugins);
  }
  
  public FileCollection getIdeaCoreLibs() {
    File _ideaHome = this.getIdeaHome();
    String _plus = (_ideaHome + "/lib");
    ConfigurableFileTree _fileTree = this.project.fileTree(_plus);
    ConfigurableFileTree _builtBy = _fileTree.builtBy(this.downloadIdea);
    PatternFilterable _include = _builtBy.include("*.jar");
    return ((FileCollection) _include);
  }
  
  public FileCollection getIdeaRunClasspath() {
    FileCollection _ideaCoreLibs = this.getIdeaCoreLibs();
    ConfigurableFileCollection _olsJar = this.toolsJar();
    return _ideaCoreLibs.plus(_olsJar);
  }
  
  public ConfigurableFileCollection toolsJar() {
    StringConcatenation _builder = new StringConcatenation();
    String _property = System.getProperty("java.home");
    _builder.append(_property, "");
    _builder.append("/../lib/tools.jar");
    return this.project.files(_builder);
  }
  
  public File getIdeaHome() {
    File _xifexpression = null;
    boolean _equals = Objects.equal(this.ideaHome, null);
    if (_equals) {
      File _xifexpression_1 = null;
      boolean _equals_1 = Objects.equal(this.ideaVersion, null);
      if (_equals_1) {
        throw new IllegalStateException("Cannot determine IDEA home directory. Neither \'ideaHome\' nor \'ideaVersion\' were set.");
      } else {
        Gradle _gradle = this.project.getGradle();
        File _gradleUserHomeDir = _gradle.getGradleUserHomeDir();
        File _divide = GradleExtensions.operator_divide(_gradleUserHomeDir, "ideaSDK");
        _xifexpression_1 = GradleExtensions.operator_divide(_divide, this.ideaVersion);
      }
      _xifexpression = _xifexpression_1;
    } else {
      _xifexpression = this.project.file(this.ideaHome);
    }
    return _xifexpression;
  }
  
  public File getPluginsCache() {
    Gradle _gradle = this.project.getGradle();
    File _gradleUserHomeDir = _gradle.getGradleUserHomeDir();
    return GradleExtensions.operator_divide(_gradleUserHomeDir, "ideaPluginDependencies");
  }
  
  public File getSourcesZip() {
    File _ideaHome = this.getIdeaHome();
    IdeaDistribution _ideaDistribution = new IdeaDistribution(this.ideaVersion);
    String _sourceArchiveName = _ideaDistribution.getSourceArchiveName();
    return GradleExtensions.operator_divide(_ideaHome, _sourceArchiveName);
  }
  
  public File getSandboxDir() {
    File _buildDir = this.project.getBuildDir();
    return GradleExtensions.operator_divide(_buildDir, "ideaSandbox");
  }
  
  public Object pluginDependencies(final Closure config) {
    return this.project.configure(((Object) this.pluginDependencies), config);
  }
  
  public Object pluginRepositories(final Closure config) {
    return this.project.configure(((Object) this.pluginRepositories), config);
  }
  
  @Pure
  public Project getProject() {
    return this.project;
  }
  
  @Pure
  public IdeaPluginRepositories getPluginRepositories() {
    return this.pluginRepositories;
  }
  
  @Pure
  public IdeaPluginDependencies getPluginDependencies() {
    return this.pluginDependencies;
  }
  
  public void setIdeaHome(final Object ideaHome) {
    this.ideaHome = ideaHome;
  }
  
  @Pure
  public String getIdeaVersion() {
    return this.ideaVersion;
  }
  
  public void setIdeaVersion(final String ideaVersion) {
    this.ideaVersion = ideaVersion;
  }
  
  @Pure
  public DownloadIdea getDownloadIdea() {
    return this.downloadIdea;
  }
  
  public void setDownloadIdea(final DownloadIdea downloadIdea) {
    this.downloadIdea = downloadIdea;
  }
  
  @Pure
  public DownloadIdeaPlugins getDownloadPlugins() {
    return this.downloadPlugins;
  }
  
  public void setDownloadPlugins(final DownloadIdeaPlugins downloadPlugins) {
    this.downloadPlugins = downloadPlugins;
  }
}