/*
 * Copyright 2012-2013 the original author or authors.
 *
 * 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 org.springframework.bootstrap.cli.compiler.autoconfigure;

import groovy.lang.GroovyClassLoader;

import org.codehaus.groovy.ast.AnnotationNode;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.classgen.GeneratorContext;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.springframework.bootstrap.cli.compiler.CompilerAutoConfiguration;
import org.springframework.bootstrap.cli.compiler.DependencyCustomizer;
import org.springframework.bootstrap.cli.compiler.GroovyCompilerConfiguration;

/**
 * {@link CompilerAutoConfiguration} for Spring Bootstrap.
 * 
 * @author Dave Syer
 * @author Phillip Webb
 */
public class SpringBootstrapCompilerAutoConfiguration extends CompilerAutoConfiguration {

	@Override
	public void applyDependencies(DependencyCustomizer dependencies) {
		DependencyCustomizer customizer = dependencies.ifAnyMissingClasses(
				"org.springframework.bootstrap.SpringApplication").add(
				"org.springframework.bootstrap", "spring-bootstrap", "0.0.1-SNAPSHOT");
		customizer = customizer.ifAnyResourcesPresent("logback.xml").add(
				"ch.qos.logback", "logback-classic", "1.0.7");
		customizer = customizer.ifNotAdded("cg.qos.logback", "logback-classic")
				.ifAnyResourcesPresent("log4j.properties", "log4j.xml")
				.add("org.slf4j", "slf4j-log4j12", "1.7.1")
				.add("log4j", "log4j", "1.2.16");
		customizer = customizer.ifNotAdded("ch.qos.logback", "logback-classic")
				.ifNotAdded("org.slf4j", "slf4j-log4j12")
				.add("org.slf4j", "slf4j-jdk14", "1.7.1");
		// FIXME get the version
	}

	@Override
	public void applyImports(ImportCustomizer imports) {
		imports.addImports("javax.sql.DataSource",
				"org.springframework.stereotype.Controller",
				"org.springframework.stereotype.Service",
				"org.springframework.stereotype.Component",
				"org.springframework.beans.factory.annotation.Autowired",
				"org.springframework.beans.factory.annotation.Value",
				"org.springframework.context.annotation.Import",
				"org.springframework.context.annotation.ImportResource",
				"org.springframework.context.annotation.Profile",
				"org.springframework.context.annotation.Scope",
				"org.springframework.context.annotation.Configuration",
				"org.springframework.context.annotation.Bean",
				"org.springframework.bootstrap.context.annotation.EnableAutoConfiguration");
		imports.addStarImports("org.springframework.stereotype");
	}

	@Override
	public void applyToMainClass(GroovyClassLoader loader,
			GroovyCompilerConfiguration configuration, GeneratorContext generatorContext,
			SourceUnit source, ClassNode classNode) throws CompilationFailedException {
		if (true) {
			addEnableAutoConfigurationAnnotation(source, classNode);
		}
	}

	private void addEnableAutoConfigurationAnnotation(SourceUnit source,
			ClassNode classNode) {
		if (!hasEnableAutoConfigureAnnotation(classNode)) {
			try {
				Class<?> annotationClass = source
						.getClassLoader()
						.loadClass(
								"org.springframework.bootstrap.context.annotation.EnableAutoConfiguration");
				AnnotationNode annotationNode = new AnnotationNode(new ClassNode(
						annotationClass));
				classNode.addAnnotation(annotationNode);
			} catch (ClassNotFoundException e) {
				throw new IllegalStateException(e);
			}
		}
	}

	private boolean hasEnableAutoConfigureAnnotation(ClassNode classNode) {
		for (AnnotationNode node : classNode.getAnnotations()) {
			if ("EnableAutoConfiguration".equals(node.getClassNode()
					.getNameWithoutPackage())) {
				return true;
			}
		}
		return false;
	}
}

@@ -1560,42 +1560,8 @@
 %7B%0A%09%09
-DependencyCustomizer customizer = 
 depe
@@ -1590,16 +1590,16 @@
 lasses(%0A
+
 %09%09%09%09%22org
@@ -1728,39 +1728,28 @@
 OT%22);%0A%09%09
-customizer = customizer
+dependencies
 .ifAnyRe
@@ -1782,21 +1782,16 @@
 l%22).add(
-%0A%09%09%09%09
 %22ch.qos.
@@ -1791,33 +1791,37 @@
 ch.qos.logback%22,
- 
+%0A%09%09%09%09
 %22logback-classic
@@ -1835,39 +1835,28 @@
 .7%22);%0A%09%09
-customizer = customizer
+dependencies
 .ifNotAd
@@ -2048,31 +2048,20 @@
 ;%0A%09%09
-customizer = customizer
+dependencies
 .ifN

Improve readability of SpringBootstrapCompilerAutoConfiguration