/*
 * (C) Copyright Uwe Schindler (Generics Policeman) and others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.thetaphi.forbiddenapis.gradle;

import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyCodeSource;
import groovy.util.DelegatingScript;

import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;

import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.plugins.PluginInstantiationException;

/**
 * Forbiddenapis Gradle Plugin (requires at least Gradle 2.3)
 * @since 2.0
 */
public class ForbiddenApisPlugin implements Plugin<Project> {
  
  /** Resource with Groovy script that initializes the plugin. */
  private static final String PLUGIN_INIT_SCRIPT = "plugin-init.groovy";
  
  /** Name of the base task that depends on one for every SourceSet */
  public static final String FORBIDDEN_APIS_TASK_NAME = "forbiddenApis";
  
  /** Name of the extension to define defaults for all tasks of this module. */
  public static final String FORBIDDEN_APIS_EXTENSION_NAME = "forbiddenApis";
  
  private static final Class<? extends DelegatingScript> compiledScript;
  static {
    final ImportCustomizer importCustomizer = new ImportCustomizer().addStarImports(ForbiddenApisPlugin.class.getPackage().getName());
    final CompilerConfiguration configuration = new CompilerConfiguration().addCompilationCustomizers(importCustomizer);
    configuration.setScriptBaseClass(DelegatingScript.class.getName());
    configuration.setSourceEncoding("UTF-8");
    final URL scriptUrl = ForbiddenApisPlugin.class.getResource(PLUGIN_INIT_SCRIPT);
    if (scriptUrl == null) {
      throw new RuntimeException("Cannot find resource with script: " + PLUGIN_INIT_SCRIPT);
    }
    compiledScript = AccessController.doPrivileged(new PrivilegedAction<Class<? extends DelegatingScript>>() {
      @Override
      public Class<? extends DelegatingScript> run() {
        try {
          final GroovyClassLoader loader = new GroovyClassLoader(ForbiddenApisPlugin.class.getClassLoader(), configuration);
          final GroovyCodeSource csrc = new GroovyCodeSource(scriptUrl);
          @SuppressWarnings("unchecked") final Class<? extends DelegatingScript> clazz =
              loader.parseClass(csrc, false).asSubclass(DelegatingScript.class);
          return clazz;
        } catch (Exception e) {
          throw new RuntimeException("Cannot compile Groovy script: " + PLUGIN_INIT_SCRIPT);
        }
      }
    });
  }
  
  @Override
  public void apply(Project project) {
    try {
      final DelegatingScript script = compiledScript.newInstance();
      script.setDelegate(this);
      script.setProperty("project", project);
      script.run();
    } catch (Exception e) {
      throw new PluginInstantiationException("Cannot execute Groovy script for apply(Project).", e);
    }
  }
  
}

@@ -1118,16 +1118,54 @@
 ception;
+%0Aimport org.gradle.util.GradleVersion;
 %0A%0A/**%0A *
@@ -1516,16 +1516,17 @@
 ourceSet
+.
  */%0A  pu
@@ -1673,24 +1673,24 @@
  module. */%0A
-
   public sta
@@ -1758,16 +1758,167 @@
 is%22;%0A  %0A
+  /** Minimum Gradle version this plugin requires to run. */%0A  public static final GradleVersion MIN_GRADLE_VERSION = GradleVersion.version(%222.3%22);%0A  %0A
   privat
@@ -3373,24 +3373,251 @@
  project) %7B%0A
+    if (GradleVersion.current().compareTo(MIN_GRADLE_VERSION) %3C 0) %7B%0A      throw new PluginInstantiationException(%22Forbiddenapis needs at least %22 + MIN_GRADLE_VERSION + %22, running version is %22 + GradleVersion.current());%0A    %7D%0A
     try %7B%0A  

Enforce minimum Gradle version