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

subscript.gradle.SubScriptPlugin.groovy Maven / Gradle / Ivy

The newest version!
package subscript.gradle

import org.gradle.api.*
import org.gradle.api.file.FileCollection
import org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency
import org.gradle.api.internal.file.collections.LazilyInitializedFileCollection
import org.gradle.api.plugins.scala.ScalaBasePlugin
import org.gradle.api.plugins.scala.ScalaPlugin
import org.gradle.api.tasks.*

import java.util.regex.Pattern

class SubScriptPlugin implements Plugin {

  private Project project

  void apply(Project project) {
    this.project = project
    alterScalaRuntime()
    project.plugins.apply ScalaPlugin
  }

  private void alterScalaRuntime() {
    def mc = org.gradle.api.tasks.ScalaRuntime.metaClass
    Project project = this.project

    // inferScalaClasspath is responsible for the inference of
    // scala-compiler from a scala-library. Unfortunately, the original
    // version assumes that scala-compiler should have "org.scala-lang"
    // group. Here we replace the original method with a new one. The method
    // below is an exact copy of the original method except the group, which
    // is now "org.subscript-lang"
    mc.inferScalaClasspath = {Iterable classpath ->
        ScalaRuntime self = delegate
        // alternatively, we could return project.files(Runnable)
        // would differ in the following ways: 1. live (not sure if we want live here) 2. no autowiring (probably want autowiring here)
        return new LazilyInitializedFileCollection() {
            @Override
            FileCollection createDelegate() {
                if (project.repositories.empty) {
                    throw new GradleException("Cannot infer Scala class path because no repository is declared in $project")
                }

                def scalaLibraryJar = self.findScalaJar(classpath, "library")
                if (scalaLibraryJar == null) {
                    throw new GradleException("Cannot infer Scala class path because no Scala library Jar was found. "
                            + "Does $project declare dependency to scala-library? Searched classpath: $classpath.")
                }

                def scalaVersion = self.getScalaVersion(scalaLibraryJar)
                if (scalaVersion == null) {
                    throw new AssertionError("Unexpectedly failed to parse version of Scala Jar file: $scalaLibraryJar in $project")
                }

                return project.configurations.detachedConfiguration(
                        new DefaultExternalModuleDependency("org.subscript-lang", "scala-compiler", scalaVersion))
            }

            // let's override this so that delegate isn't created at autowiring time (which would mean on every build)
            @Override
            TaskDependency getBuildDependencies() {
                if (classpath instanceof Buildable) {
                    return classpath.buildDependencies
                }
                return new TaskDependency() {
                    Set getDependencies(Task task) {
                        Collections.emptySet()
                    }
                }
            }
        }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy