/*
 * 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.integtests.fixtures.mirror;

import org.gradle.integtests.fixtures.RepoScriptBlockUtil;
import org.gradle.samples.model.Command;
import org.gradle.samples.model.Sample;
import org.gradle.samples.test.runner.SampleModifier;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class SetMirrorsSampleModifier implements SampleModifier {
    @Override
    public Sample modify(Sample sample) {
        if (sample.getId().contains("usePluginsInInitScripts")) {
            // usePluginsInInitScripts asserts using https://repo.gradle.org/gradle/repo
            return sample;
        }
        List<Command> commands = sample.getCommands();
        List<Command> modifiedCommands = new ArrayList<Command>();
        File initScript = RepoScriptBlockUtil.createMirrorInitScript();
        for (Command command : commands) {
            if ("gradle".equals(command.getExecutable())) {
                List<String> args = new ArrayList<String>(command.getArgs());
                args.add("--init-script");
                args.add(initScript.getAbsolutePath());
                modifiedCommands.add(command.toBuilder().setArgs(args).build());
            } else {
                modifiedCommands.add(command);
            }
        }
        return new Sample(sample.getId(), sample.getProjectDir(), modifiedCommands);
    }
}

@@ -926,16 +926,222 @@
 .List;%0A%0A
+import static org.gradle.api.internal.artifacts.BaseRepositoryFactory.PLUGIN_PORTAL_OVERRIDE_URL_PROPERTY;%0Aimport static org.gradle.integtests.fixtures.RepoScriptBlockUtil.gradlePluginRepositoryMirrorUrl;%0A%0A
 public c
@@ -1916,24 +1916,136 @@
 utePath());%0A
+                args.add(%22-D%22 + PLUGIN_PORTAL_OVERRIDE_URL_PROPERTY + %22=%22 + gradlePluginRepositoryMirrorUrl());%0A
             

Set mirror for sample tests