From 8bb31d17bf1c13970d0421e67731d31148a0acab Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 7 Dec 2016 01:24:50 -0800 Subject: [PATCH] Refactored FreshMarkExtension to use lib's FreshMark, and also to handle properties() and propertiesFile(). --- plugin-gradle/build.gradle | 1 - .../freshmark/FreshMarkExtension.java | 48 +++++++++++++----- .../freshmark/FreshMarkExtensionTest.java | 49 +++++++++++++++++++ 3 files changed, 86 insertions(+), 12 deletions(-) create mode 100644 plugin-gradle/src/test/java/com/diffplug/gradle/spotless/freshmark/FreshMarkExtensionTest.java diff --git a/plugin-gradle/build.gradle b/plugin-gradle/build.gradle index 613f6673f9..2be121e070 100644 --- a/plugin-gradle/build.gradle +++ b/plugin-gradle/build.gradle @@ -10,7 +10,6 @@ dependencies { compile project(':lib-extra') compile "com.diffplug.durian:durian-core:${VER_DURIAN}" compile "com.diffplug.durian:durian-io:${VER_DURIAN}" - compile "com.diffplug.freshmark:freshmark:1.3.1" testCompile project(':testlib') testCompile "junit:junit:${VER_JUNIT}" diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/freshmark/FreshMarkExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/freshmark/FreshMarkExtension.java index 09c78282de..527473525d 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/freshmark/FreshMarkExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/freshmark/FreshMarkExtension.java @@ -15,32 +15,58 @@ */ package com.diffplug.gradle.spotless.freshmark; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.Properties; -import com.diffplug.freshmark.FreshMark; +import org.gradle.api.Action; + +import com.diffplug.common.base.Errors; +import com.diffplug.common.io.Files; import com.diffplug.gradle.spotless.BaseFormatTask; import com.diffplug.gradle.spotless.FormatExtension; +import com.diffplug.gradle.spotless.GradleProvisioner; import com.diffplug.gradle.spotless.SpotlessExtension; +import com.diffplug.spotless.markdown.FreshMarkStep; public class FreshMarkExtension extends FormatExtension { public static final String NAME = "freshmark"; - public Map properties; + public List>> propertyActions = new ArrayList<>(); public FreshMarkExtension(SpotlessExtension root) { super(NAME, root); - customLazy(NAME, () -> { - // defaults to all project properties - if (properties == null) { - properties = getProject().getProperties(); + addStep(FreshMarkStep.create(() -> { + Map map = new HashMap<>(); + for (Action> action : propertyActions) { + action.execute(map); } - FreshMark freshMark = new FreshMark(properties, getProject().getLogger()::warn); - return freshMark::compile; - }); + return map; + }, GradleProvisioner.fromProject(getProject()))); + } + + public void properties(Action> action) { + propertyActions.add(action); } - public void properties(Map properties) { - this.properties = properties; + public void propertiesFile(Object file) { + propertyActions.add(map -> { + File propFile = getProject().file(file); + try (InputStream input = Files.asByteSource(propFile).openBufferedStream()) { + Properties props = new Properties(); + props.load(input); + for (String key : props.stringPropertyNames()) { + map.put(key, props.getProperty(key)); + } + } catch (IOException e) { + throw Errors.asRuntime(e); + } + }); } @Override diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/freshmark/FreshMarkExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/freshmark/FreshMarkExtensionTest.java new file mode 100644 index 0000000000..9fb3733235 --- /dev/null +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/freshmark/FreshMarkExtensionTest.java @@ -0,0 +1,49 @@ +/* + * Copyright 2016 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.freshmark; + +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Test; + +import com.diffplug.gradle.spotless.GradleIntegrationTest; + +public class FreshMarkExtensionTest extends GradleIntegrationTest { + @Test + public void integration() throws IOException { + write("build.gradle", + "plugins {", + " id 'java'", + " id 'com.diffplug.gradle.spotless'", + "}", + "repositories { mavenCentral() }", + "spotless {", + " freshmark {", + " properties {", + " it.put('lib', 'MyLib')", + " it.put('author', 'Me')", + " }", + " }", + "}"); + String unformatted = getTestResource("freshmark/FreshMarkUnformatted.test"); + write("README.md", unformatted); + gradleRunner().withArguments("spotlessApply", "--quiet").forwardOutput().build(); + String result = read("README.md"); + String formatted = getTestResource("freshmark/FreshMarkFormatted.test"); + Assert.assertEquals(formatted, result); + } +}