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

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

package org.xtext.gradle.idea.tasks;

import com.google.common.base.Objects;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Date;
import org.eclipse.xtend.lib.annotations.Accessors;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.eclipse.xtext.xbase.lib.Pure;
import org.gradle.StartParameter;
import org.gradle.api.Action;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.file.CopySpec;
import org.gradle.api.file.FileTree;
import org.gradle.api.invocation.Gradle;
import org.gradle.api.specs.Spec;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.TaskAction;
import org.xtext.gradle.idea.tasks.GradleExtensions;
import org.xtext.gradle.idea.tasks.IdeaDistribution;

@Accessors
@SuppressWarnings("all")
public class DownloadIdea extends DefaultTask {
  @OutputDirectory
  private File ideaHome;
  
  @Input
  @Optional
  private String ideaVersion;
  
  public DownloadIdea() {
    final Spec _function = new Spec() {
      @Override
      public boolean isSatisfiedBy(final Task it) {
        boolean _and = false;
        boolean _notEquals = (!Objects.equal(DownloadIdea.this.ideaVersion, null));
        if (!_notEquals) {
          _and = false;
        } else {
          IdeaDistribution _ideaDistribution = new IdeaDistribution(DownloadIdea.this.ideaVersion);
          boolean _needsRedownload = DownloadIdea.this.needsRedownload(_ideaDistribution);
          _and = _needsRedownload;
        }
        return _and;
      }
    };
    this.onlyIf(_function);
  }
  
  @TaskAction
  public boolean download() {
    try {
      boolean _xblockexpression = false;
      {
        final IdeaDistribution buildInfo = new IdeaDistribution(this.ideaVersion);
        Project _project = this.getProject();
        _project.delete(this.ideaHome);
        final Procedure1 _function = new Procedure1() {
          @Override
          public void apply(final File tmp) {
            try {
              String _archiveName = buildInfo.getArchiveName();
              final File archiveFile = new File(tmp, _archiveName);
              String _archiveUrl = buildInfo.getArchiveUrl();
              URL _uRL = new URL(_archiveUrl);
              InputStream _openStream = _uRL.openStream();
              Path _path = archiveFile.toPath();
              Files.copy(_openStream, _path);
              Project _project = DownloadIdea.this.getProject();
              final Action _function = new Action() {
                @Override
                public void execute(final CopySpec it) {
                  it.into(DownloadIdea.this.ideaHome);
                  Project _project = DownloadIdea.this.getProject();
                  FileTree _zipTree = _project.zipTree(archiveFile);
                  it.from(_zipTree);
                  it.setIncludeEmptyDirs(false);
                }
              };
              _project.copy(_function);
            } catch (Throwable _e) {
              throw Exceptions.sneakyThrow(_e);
            }
          }
        };
        GradleExtensions.usingTmpDir(_function);
        String _sourceArchiveName = buildInfo.getSourceArchiveName();
        final File sourceArchiveFile = new File(this.ideaHome, _sourceArchiveName);
        String _sourceArchiveUrl = buildInfo.getSourceArchiveUrl();
        URL _uRL = new URL(_sourceArchiveUrl);
        InputStream _openStream = _uRL.openStream();
        Path _path = sourceArchiveFile.toPath();
        Files.copy(_openStream, _path);
        Date _date = new Date();
        _xblockexpression = this.setLastDownloaded(_date);
      }
      return _xblockexpression;
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  private File lastDownloadedFile() {
    return GradleExtensions.operator_divide(
      this.ideaHome, ".lastDownloaded");
  }
  
  private long getLastDownloaded() {
    File _lastDownloadedFile = this.lastDownloadedFile();
    return _lastDownloadedFile.lastModified();
  }
  
  private boolean setLastDownloaded(final Date lastModified) {
    try {
      boolean _xblockexpression = false;
      {
        final File file = this.lastDownloadedFile();
        file.createNewFile();
        long _time = lastModified.getTime();
        _xblockexpression = file.setLastModified(_time);
      }
      return _xblockexpression;
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  private boolean needsRedownload(final IdeaDistribution distribution) {
    boolean _or = false;
    boolean _or_1 = false;
    Project _project = this.getProject();
    Gradle _gradle = _project.getGradle();
    StartParameter _startParameter = _gradle.getStartParameter();
    boolean _isRefreshDependencies = _startParameter.isRefreshDependencies();
    if (_isRefreshDependencies) {
      _or_1 = true;
    } else {
      long _lastDownloaded = this.getLastDownloaded();
      boolean _equals = (_lastDownloaded == 0);
      _or_1 = _equals;
    }
    if (_or_1) {
      _or = true;
    } else {
      boolean _and = false;
      boolean _isSnapshot = distribution.isSnapshot();
      if (!_isSnapshot) {
        _and = false;
      } else {
        long _lastDownloaded_1 = this.getLastDownloaded();
        Date _date = new Date();
        long _time = _date.getTime();
        long _minus = (_time - (((1000 * 60) * 60) * 24));
        boolean _lessThan = (_lastDownloaded_1 < _minus);
        _and = _lessThan;
      }
      _or = _and;
    }
    return _or;
  }
  
  @Pure
  public File getIdeaHome() {
    return this.ideaHome;
  }
  
  public void setIdeaHome(final File ideaHome) {
    this.ideaHome = ideaHome;
  }
  
  @Pure
  public String getIdeaVersion() {
    return this.ideaVersion;
  }
  
  public void setIdeaVersion(final String ideaVersion) {
    this.ideaVersion = ideaVersion;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy