/*
 * Copyright 2021 DiffPlug
 *
 * 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 com.diffplug.gradle.spotless;

import java.util.SortedMap;
import java.util.TreeMap;

import org.gradle.api.Action;
import org.gradle.api.Project;

public class SpotlessExtensionPredeclare extends SpotlessExtension {
	private final SortedMap<String, FormatExtension> toSetup = new TreeMap<>();

	public SpotlessExtensionPredeclare(Project project, GradleProvisioner.Policy policy) {
		super(project);
		getRegisterDependenciesTask().getTaskService().get().predeclaredProvisioner = policy.dedupingProvisioner(project);
		project.afterEvaluate(unused -> {
			toSetup.forEach((name, formatExtension) -> {
				for (Action<FormatExtension> lazyAction : formatExtension.lazyActions) {
					lazyAction.execute(formatExtension);
				}
				getRegisterDependenciesTask().steps.addAll(formatExtension.steps);
			});
		});
	}

	@Override
	protected void createFormatTasks(String name, FormatExtension formatExtension) {
		toSetup.put(name, formatExtension);
	}

	@Override
	protected void predeclare(GradleProvisioner.Policy policy) {
		throw new UnsupportedOperationException("predeclare can't be called from within `" + EXTENSION_PREDECLARE + "`");
	}
}

@@ -13,16 +13,21 @@
 ght 2021
+-2022
  DiffPlu
@@ -28,16 +28,16 @@
 iffPlug%0A
-
  *%0A * Li
@@ -746,16 +746,70 @@
 oject;%0A%0A
+import com.diffplug.spotless.LazyForwardingEquality;%0A%0A
 public c
@@ -1379,16 +1379,16 @@
 ;%0A%09%09%09%09%7D%0A
-
 %09%09%09%09getR
@@ -1450,16 +1450,209 @@
 steps);%0A
+%09%09%09%09// needed to fix Deemon memory leaks (#1194), but this line came from https://github.com/diffplug/spotless/pull/1206%0A%09%09%09%09LazyForwardingEquality.unlazy(getRegisterDependenciesTask().steps);%0A
 %09%09%09%7D);%0A%09

Fix the predeclare case.