|
15 | 15 | */
|
16 | 16 | package com.diffplug.gradle.spotless.freshmark;
|
17 | 17 |
|
| 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; |
18 | 24 | import java.util.Map;
|
| 25 | +import java.util.Properties; |
19 | 26 |
|
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; |
21 | 31 | import com.diffplug.gradle.spotless.BaseFormatTask;
|
22 | 32 | import com.diffplug.gradle.spotless.FormatExtension;
|
| 33 | +import com.diffplug.gradle.spotless.GradleProvisioner; |
23 | 34 | import com.diffplug.gradle.spotless.SpotlessExtension;
|
| 35 | +import com.diffplug.spotless.markdown.FreshMarkStep; |
24 | 36 |
|
25 | 37 | public class FreshMarkExtension extends FormatExtension {
|
26 | 38 | public static final String NAME = "freshmark";
|
27 | 39 |
|
28 |
| - public Map<String, ?> properties; |
| 40 | + public List<Action<Map<String, Object>>> propertyActions = new ArrayList<>(); |
29 | 41 |
|
30 | 42 | public FreshMarkExtension(SpotlessExtension root) {
|
31 | 43 | 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); |
36 | 48 | }
|
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); |
40 | 55 | }
|
41 | 56 |
|
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 | + }); |
44 | 70 | }
|
45 | 71 |
|
46 | 72 | @Override
|
|
0 commit comments