/*
 * Copyright 2018 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.gradle.plugin.devel.plugins

import org.gradle.integtests.fixtures.AbstractIntegrationSpec

class JavaGradlePluginLegacyPluginPublishingIntegrationTest extends AbstractIntegrationSpec {

    def setup() {
        buildFile << """
            apply plugin: 'java-gradle-plugin'
            group = 'com.example'
            version = '1.0'
        """
        settingsFile << """
            rootProject.name = 'plugins'
        """
    }

    def "can resolve plugin publications in an afterEvaluate hook"() {
        given:
        plugin('foo', 'com.example.foo')
        publishToIvy()
        publishToMaven()
        buildFile << """
            afterEvaluate {
                assert publishing.publications.size() == 4
            }
        """

        expect:
        succeeds 'help'
    }
    def publishToMaven() {
        buildFile << """
            apply plugin: 'maven-publish'
            publishing {
                repositories {
                    maven {
                        url '${mavenRepo.uri}'
                    }
                }
            }
        """
    }

    def publishToIvy() {
        buildFile << """
            apply plugin: 'ivy-publish'
            publishing {
                repositories {
                    ivy {
                        url '${ivyRepo.uri}'
                    }
                }
            }
        """
    }

    def plugin(String name, String pluginId) {
        String implementationClass = name.capitalize();

        file("src/main/java/com/xxx/${implementationClass}.java") << """
package com.xxx;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
public class ${implementationClass} implements Plugin<Project> {
    public void apply(Project project) { }
}
"""
        buildFile << """
            gradlePlugin {
                plugins {
                    ${name} {
                        id = '${pluginId}'
                        implementationClass = '${implementationClass}'
                    }
                }
            }
        """
    }
}
