Skip to content

Commit 8bb31d1

Browse files
committed
Refactored FreshMarkExtension to use lib's FreshMark, and also to handle properties() and propertiesFile().
1 parent a060ee4 commit 8bb31d1

File tree

3 files changed

+86
-12
lines changed

3 files changed

+86
-12
lines changed

plugin-gradle/build.gradle

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ dependencies {
1010
compile project(':lib-extra')
1111
compile "com.diffplug.durian:durian-core:${VER_DURIAN}"
1212
compile "com.diffplug.durian:durian-io:${VER_DURIAN}"
13-
compile "com.diffplug.freshmark:freshmark:1.3.1"
1413

1514
testCompile project(':testlib')
1615
testCompile "junit:junit:${VER_JUNIT}"

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/freshmark/FreshMarkExtension.java

+37-11
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,58 @@
1515
*/
1616
package com.diffplug.gradle.spotless.freshmark;
1717

18+
import java.io.File;
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.util.ArrayList;
22+
import java.util.HashMap;
23+
import java.util.List;
1824
import java.util.Map;
25+
import java.util.Properties;
1926

20-
import com.diffplug.freshmark.FreshMark;
27+
import org.gradle.api.Action;
28+
29+
import com.diffplug.common.base.Errors;
30+
import com.diffplug.common.io.Files;
2131
import com.diffplug.gradle.spotless.BaseFormatTask;
2232
import com.diffplug.gradle.spotless.FormatExtension;
33+
import com.diffplug.gradle.spotless.GradleProvisioner;
2334
import com.diffplug.gradle.spotless.SpotlessExtension;
35+
import com.diffplug.spotless.markdown.FreshMarkStep;
2436

2537
public class FreshMarkExtension extends FormatExtension {
2638
public static final String NAME = "freshmark";
2739

28-
public Map<String, ?> properties;
40+
public List<Action<Map<String, Object>>> propertyActions = new ArrayList<>();
2941

3042
public FreshMarkExtension(SpotlessExtension root) {
3143
super(NAME, root);
32-
customLazy(NAME, () -> {
33-
// defaults to all project properties
34-
if (properties == null) {
35-
properties = getProject().getProperties();
44+
addStep(FreshMarkStep.create(() -> {
45+
Map<String, Object> map = new HashMap<>();
46+
for (Action<Map<String, Object>> action : propertyActions) {
47+
action.execute(map);
3648
}
37-
FreshMark freshMark = new FreshMark(properties, getProject().getLogger()::warn);
38-
return freshMark::compile;
39-
});
49+
return map;
50+
}, GradleProvisioner.fromProject(getProject())));
51+
}
52+
53+
public void properties(Action<Map<String, Object>> action) {
54+
propertyActions.add(action);
4055
}
4156

42-
public void properties(Map<String, ?> properties) {
43-
this.properties = properties;
57+
public void propertiesFile(Object file) {
58+
propertyActions.add(map -> {
59+
File propFile = getProject().file(file);
60+
try (InputStream input = Files.asByteSource(propFile).openBufferedStream()) {
61+
Properties props = new Properties();
62+
props.load(input);
63+
for (String key : props.stringPropertyNames()) {
64+
map.put(key, props.getProperty(key));
65+
}
66+
} catch (IOException e) {
67+
throw Errors.asRuntime(e);
68+
}
69+
});
4470
}
4571

4672
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2016 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.freshmark;
17+
18+
import java.io.IOException;
19+
20+
import org.junit.Assert;
21+
import org.junit.Test;
22+
23+
import com.diffplug.gradle.spotless.GradleIntegrationTest;
24+
25+
public class FreshMarkExtensionTest extends GradleIntegrationTest {
26+
@Test
27+
public void integration() throws IOException {
28+
write("build.gradle",
29+
"plugins {",
30+
" id 'java'",
31+
" id 'com.diffplug.gradle.spotless'",
32+
"}",
33+
"repositories { mavenCentral() }",
34+
"spotless {",
35+
" freshmark {",
36+
" properties {",
37+
" it.put('lib', 'MyLib')",
38+
" it.put('author', 'Me')",
39+
" }",
40+
" }",
41+
"}");
42+
String unformatted = getTestResource("freshmark/FreshMarkUnformatted.test");
43+
write("README.md", unformatted);
44+
gradleRunner().withArguments("spotlessApply", "--quiet").forwardOutput().build();
45+
String result = read("README.md");
46+
String formatted = getTestResource("freshmark/FreshMarkFormatted.test");
47+
Assert.assertEquals(formatted, result);
48+
}
49+
}

0 commit comments

Comments
 (0)