|
| 1 | +/* |
| 2 | + * Copyright 2016-2020 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.gradle.spotless; |
| 17 | + |
| 18 | +import org.gradle.api.Plugin; |
| 19 | +import org.gradle.api.Project; |
| 20 | +import org.gradle.api.Task; |
| 21 | +import org.gradle.api.plugins.BasePlugin; |
| 22 | + |
| 23 | +import com.diffplug.spotless.SpotlessCache; |
| 24 | + |
| 25 | +public class SpotlessPluginModern implements Plugin<Project> { |
| 26 | + @Override |
| 27 | + public void apply(Project project) { |
| 28 | + // make sure there's a `clean` task |
| 29 | + project.getPlugins().apply(BasePlugin.class); |
| 30 | + |
| 31 | + // setup the extension |
| 32 | + project.getExtensions().create(SpotlessExtension.EXTENSION, SpotlessExtensionModern.class, project); |
| 33 | + |
| 34 | + // clear spotless' cache when the user does a clean |
| 35 | + Task clean = project.getTasks().getByName(BasePlugin.CLEAN_TASK_NAME); |
| 36 | + clean.doLast(unused -> { |
| 37 | + // resolution for: https://github.com/diffplug/spotless/issues/243#issuecomment-564323856 |
| 38 | + // project.getRootProject() is consistent across every project, so only of one the clears will |
| 39 | + // actually happen (as desired) |
| 40 | + // |
| 41 | + // we use System.identityHashCode() to avoid a memory leak by hanging on to the reference directly |
| 42 | + SpotlessCache.clearOnce(System.identityHashCode(project.getRootProject())); |
| 43 | + }); |
| 44 | + } |
| 45 | +} |
0 commit comments