/*
 * Copyright 2016 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.use.resolve.internal;

import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.ResolveException;
import org.gradle.api.artifacts.dsl.RepositoryHandler;
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
import org.gradle.api.internal.artifacts.DependencyResolutionServices;
import org.gradle.api.internal.artifacts.configurations.ConfigurationContainerInternal;
import org.gradle.api.internal.artifacts.configurations.ConfigurationInternal;
import org.gradle.api.internal.initialization.ClassLoaderScope;
import org.gradle.api.internal.plugins.PluginInspector;
import org.gradle.api.specs.Specs;
import org.gradle.internal.Factories;
import org.gradle.internal.Factory;
import org.gradle.internal.classpath.ClassPath;
import org.gradle.internal.classpath.DefaultClassPath;
import org.gradle.internal.exceptions.Contextual;
import org.gradle.plugin.use.internal.InvalidPluginRequestException;
import org.gradle.plugin.use.internal.PluginRequest;

import java.io.File;
import java.util.Set;

public class CustomRepositoryPluginResolver implements PluginResolver {
    private final ClassLoaderScope parentScope;
    private final PluginInspector pluginInspector;
    private final Factory<DependencyResolutionServices> dependencyResolutionServicesFactory;

    public CustomRepositoryPluginResolver(ClassLoaderScope parentScope, PluginInspector pluginInspector, Factory<DependencyResolutionServices> dependencyResolutionServicesFactory) {
        this.parentScope = parentScope;
        this.pluginInspector = pluginInspector;
        this.dependencyResolutionServicesFactory = dependencyResolutionServicesFactory;
    }

    @Override
    public void resolve(PluginRequest pluginRequest, PluginResolutionResult result) throws InvalidPluginRequestException {
        final String artifactAddress = pluginRequest.getId() + ":" + pluginRequest.getId() + ":" + pluginRequest.getVersion();
        ClassPath classPath = resolvePluginDependencies(System.getProperty("org.gradle.plugin.repoUrl"), artifactAddress);
        result.found("Custom Repository", new ClassPathPluginResolution(pluginRequest.getId(), parentScope, Factories.constant(classPath), pluginInspector));
    }

    private ClassPath resolvePluginDependencies(final String repoUrl, final String groupArtifactVersion) {
        DependencyResolutionServices resolution = dependencyResolutionServicesFactory.create();

        RepositoryHandler repositories = resolution.getResolveRepositoryHandler();
        repositories.maven(new Action<MavenArtifactRepository>() {
            public void execute(MavenArtifactRepository mavenArtifactRepository) {
                mavenArtifactRepository.setUrl(repoUrl);
            }
        });

        Dependency dependency = resolution.getDependencyHandler().create(groupArtifactVersion);

        ConfigurationContainerInternal configurations = (ConfigurationContainerInternal) resolution.getConfigurationContainer();
        ConfigurationInternal configuration = configurations.detachedConfiguration(dependency);

        try {
            Set<File> files = configuration.getResolvedConfiguration().getFiles(Specs.satisfyAll());
            return new DefaultClassPath(files);
        } catch (ResolveException e) {
            throw new DependencyResolutionException("Failed to resolve all plugin dependencies from " + repoUrl, e.getCause());
        }
    }

    @Contextual
    public static class DependencyResolutionException extends GradleException {
        public DependencyResolutionException(String message, Throwable cause) {
            super(message, cause);
        }
    }
}

@@ -1711,16 +1711,73 @@
 o.File;%0A
+import java.net.URI;%0Aimport java.net.URISyntaxException;%0A
 import j
@@ -1790,16 +1790,16 @@
 il.Set;%0A
-
 %0Apublic 
@@ -2745,54 +2745,19 @@
 ies(
-System.getProperty(%22org.gradle.plugin.r
+getR
 epoUr
-l%22
+i(
 ), a
@@ -2934,24 +2934,785 @@
 r));%0A    %7D%0A%0A
+    private URI getRepoUri() %7B%0A        String repoUrl = System.getProperty(%22org.gradle.plugin.repoUrl%22);%0A        URI uri = URI.create(repoUrl);%0A        if (!uri.isAbsolute()) %7B%0A            /*%0A             * TODO this is a workaround for the fact that this code currently runs in a%0A             * context that does not have a base dir, so the identity file resolver is used.%0A             * That resolver cannot deal with relative paths. We use the current working dir%0A             * as a workaround. In the final implementation, the repository handler will live%0A             * in a context that hase a base dir (settings scope or project scope).%0A             */%0A            uri = new File(repoUrl).getAbsoluteFile().toURI();%0A        %7D%0A        return uri;%0A    %7D%0A%0A
     private 
@@ -3753,30 +3753,27 @@
 s(final 
-String
+URI
  repoUr
-l
+i
 , final 
@@ -4182,25 +4182,36 @@
 etUrl(repoUr
-l
+i.toString()
 );%0A         
@@ -4863,17 +4863,17 @@
 + repoUr
-l
+i
 , e.getC

Allow relative paths in custom repository handler