Skip to content

Commit

Permalink
Refactored FreshMarkExtension to use lib's FreshMark, and also to han…
Browse files Browse the repository at this point in the history
…dle properties() and propertiesFile().
  • Loading branch information
nedtwigg committed Dec 7, 2016
1 parent a060ee4 commit 8bb31d1
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 12 deletions.
1 change: 0 additions & 1 deletion plugin-gradle/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, ?> properties;
public List<Action<Map<String, Object>>> 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<String, Object> map = new HashMap<>();
for (Action<Map<String, Object>> 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<Map<String, Object>> action) {
propertyActions.add(action);
}

public void properties(Map<String, ?> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 8bb31d1

Please sign in to comment.