package de.thetaphi.forbiddenapis.gradle;

/*
 * (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.
 */

import java.io.File;

import org.gradle.api.Action;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.tasks.TaskContainer;

/**
 * Forbiddenapis Gradle Plugin
 * @since 1.9
 */
public class GradlePlugin implements Plugin<Project> {
  
  static final String FORBIDDEN_APIS_TASK_NAME = "forbiddenApis";
  static final String TEST_FORBIDDEN_APIS_TASK_NAME = "testForbiddenApis";
  
  public void apply(final Project project) {
    final ConfigurationContainer configurations = project.getConfigurations();
    final TaskContainer tasks = project.getTasks();
    
    // TODO: How to import the JavaPlugin and its tasks from Maven!?
    final Task classesJavaTask = tasks.getByName("classes"),
        testClassesJavaTask = tasks.getByName("testClasses");
    
    // Create the tasks of the plugin:
    tasks.create(FORBIDDEN_APIS_TASK_NAME, GradleTask.class, new Action<GradleTask>() {
      public void execute(GradleTask task) {
        task.classesDir = (File) project.property("sourceSets.main.output.classesDir");
        task.classpath = configurations.getByName("compile");
        task.dependsOn(classesJavaTask);
      }
    });
    tasks.create(TEST_FORBIDDEN_APIS_TASK_NAME, GradleTask.class, new Action<GradleTask>() {
      public void execute(GradleTask task) {
        task.classesDir = (File) project.property("sourceSets.test.output.classesDir");
        task.classpath = configurations.getByName("testCompile");
        task.dependsOn(testClassesJavaTask);
      }
    });
  }
}

@@ -859,16 +859,76 @@
 tainer;%0A
+import org.gradle.api.plugins.PluginInstantiationException;%0A
 import o
@@ -1259,24 +1259,205 @@
  project) %7B%0A
+    if (project.getPlugins().findPlugin(%22java%22) == null) %7B%0A      throw new PluginInstantiationException(%22forbiddenapis only works in projects using the 'java' plugin.%22);%0A    %7D%0A    %0A
     final Co
@@ -1591,69 +1591,103 @@
  // 
-TODO: How to import the JavaPlugin and its tasks from Maven!?
+Get the tasks we depend on or the other one should depends on us (to insert us into the chain):
 %0A   
@@ -1804,16 +1804,160 @@
 lasses%22)
+,%0A        jarJavaTask = tasks.getByName(%22jar%22),%0A        testJavaTask = tasks.getByName(%22test%22),%0A        checkJavaTask = tasks.getByName(%22check%22)
 ;%0A    %0A 
@@ -1993,24 +1993,51 @@
  plugin:%0A   
+ final Task forbiddenTask =
  tasks.creat
@@ -2364,16 +2364,47 @@
  %7D);%0A   
+ final Task testForbiddenTask =
  tasks.c
@@ -2745,14 +2745,210 @@
     %7D);%0A
+    %0A    // Add dependencies%0A    jarJavaTask.dependsOn(forbiddenTask);%0A    testJavaTask.dependsOn(forbiddenTask, testForbiddenTask);%0A    checkJavaTask.dependsOn(forbiddenTask, testForbiddenTask);%0A
   %7D%0A%7D%0A

Add more task dependencies, based on dependency graph of Gradle Java Plugin