|
| 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.spotless.maven.generic; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.util.LinkedHashMap; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import org.apache.maven.plugins.annotations.Parameter; |
| 23 | + |
| 24 | +import com.diffplug.spotless.FormatterStep; |
| 25 | +import com.diffplug.spotless.maven.FormatterStepConfig; |
| 26 | +import com.diffplug.spotless.maven.FormatterStepFactory; |
| 27 | +import com.diffplug.spotless.npm.PrettierConfig; |
| 28 | +import com.diffplug.spotless.npm.PrettierFormatterStep; |
| 29 | + |
| 30 | +public class Prettier implements FormatterStepFactory { |
| 31 | + |
| 32 | + @Parameter |
| 33 | + private String prettierVersion; |
| 34 | + |
| 35 | + @Parameter |
| 36 | + private Map<String, String> devDependencies; |
| 37 | + |
| 38 | + @Parameter |
| 39 | + private Map<String, String> config; |
| 40 | + |
| 41 | + @Parameter |
| 42 | + private String configFile; |
| 43 | + |
| 44 | + @Parameter |
| 45 | + private String npmExecutable; |
| 46 | + |
| 47 | + @Override |
| 48 | + public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { |
| 49 | + |
| 50 | + // check if config is only setup in one way |
| 51 | + if (this.prettierVersion != null && this.devDependencies != null) { |
| 52 | + throw onlyOneConfig(); |
| 53 | + } |
| 54 | + |
| 55 | + // set dev dependencies |
| 56 | + if (devDependencies == null) { |
| 57 | + if (prettierVersion == null || prettierVersion.isEmpty()) { |
| 58 | + devDependencies = PrettierFormatterStep.defaultDevDependencies(); |
| 59 | + } else { |
| 60 | + devDependencies = PrettierFormatterStep.defaultDevDependenciesWithPrettier(prettierVersion); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + File npm = npmExecutable != null ? stepConfig.getFileLocator().locateLocal(npmExecutable) : null; |
| 65 | + |
| 66 | + // process config file or inline config |
| 67 | + File configFileHandler; |
| 68 | + if (this.configFile != null) { |
| 69 | + configFileHandler = stepConfig.getFileLocator().locateLocal(this.configFile); |
| 70 | + } else { |
| 71 | + configFileHandler = null; |
| 72 | + } |
| 73 | + |
| 74 | + Map<String, Object> configInline; |
| 75 | + if (config != null) { |
| 76 | + configInline = new LinkedHashMap<>(); |
| 77 | + // try to parse string values as integers or booleans |
| 78 | + for (Map.Entry<String, String> e : config.entrySet()) { |
| 79 | + try { |
| 80 | + configInline.put(e.getKey(), Integer.parseInt(e.getValue())); |
| 81 | + } catch (NumberFormatException ignore) { |
| 82 | + try { |
| 83 | + configInline.put(e.getKey(), Boolean.parseBoolean(e.getValue())); |
| 84 | + } catch (IllegalArgumentException ignore2) { |
| 85 | + configInline.put(e.getKey(), e.getValue()); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } else { |
| 90 | + configInline = null; |
| 91 | + } |
| 92 | + |
| 93 | + // create the format step |
| 94 | + PrettierConfig prettierConfig = new PrettierConfig(configFileHandler, configInline); |
| 95 | + File buildDir = stepConfig.getFileLocator().getBuildDir(); |
| 96 | + return PrettierFormatterStep.create(devDependencies, stepConfig.getProvisioner(), buildDir, npm, prettierConfig); |
| 97 | + } |
| 98 | + |
| 99 | + private static IllegalArgumentException onlyOneConfig() { |
| 100 | + return new IllegalArgumentException("must specify exactly one configFile or config"); |
| 101 | + } |
| 102 | +} |
0 commit comments