|
| 1 | +/* |
| 2 | + * Copyright 2021-2023 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.gherkin; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.Serializable; |
| 20 | +import java.lang.reflect.Constructor; |
| 21 | +import java.lang.reflect.InvocationTargetException; |
| 22 | +import java.util.Objects; |
| 23 | + |
| 24 | +import com.diffplug.spotless.FormatterFunc; |
| 25 | +import com.diffplug.spotless.FormatterStep; |
| 26 | +import com.diffplug.spotless.JarState; |
| 27 | +import com.diffplug.spotless.Provisioner; |
| 28 | + |
| 29 | +public class GherkinUtilsStep { |
| 30 | + private static final String MAVEN_COORDINATE = "io.cucumber:gherkin-utils:"; |
| 31 | + private static final String DEFAULT_VERSION = "8.0.2"; |
| 32 | + |
| 33 | + public static String defaultVersion() { |
| 34 | + return DEFAULT_VERSION; |
| 35 | + } |
| 36 | + |
| 37 | + public static FormatterStep create(GherkinUtilsConfig gherkinSimpleConfig, |
| 38 | + String formatterVersion, Provisioner provisioner) { |
| 39 | + Objects.requireNonNull(provisioner, "provisioner cannot be null"); |
| 40 | + return FormatterStep.createLazy("gherkin", () -> new GherkinUtilsStep.State(gherkinSimpleConfig, formatterVersion, provisioner), GherkinUtilsStep.State::toFormatter); |
| 41 | + } |
| 42 | + |
| 43 | + private static final class State implements Serializable { |
| 44 | + private static final long serialVersionUID = 1L; |
| 45 | + |
| 46 | + private final GherkinUtilsConfig gherkinSimpleConfig; |
| 47 | + private final JarState jarState; |
| 48 | + |
| 49 | + private State(GherkinUtilsConfig gherkinSimpleConfig, String formatterVersion, Provisioner provisioner) throws IOException { |
| 50 | + this.gherkinSimpleConfig = gherkinSimpleConfig; |
| 51 | + this.jarState = JarState.from(MAVEN_COORDINATE + formatterVersion, provisioner); |
| 52 | + } |
| 53 | + |
| 54 | + FormatterFunc toFormatter() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, |
| 55 | + InstantiationException, IllegalAccessException { |
| 56 | + Class<?> formatterFunc = jarState.getClassLoader().loadClass("com.diffplug.spotless.glue.gherkin.GherkinUtilsFormatterFunc"); |
| 57 | + Constructor<?> constructor = formatterFunc.getConstructor(GherkinUtilsConfig.class); |
| 58 | + return (FormatterFunc) constructor.newInstance(gherkinSimpleConfig); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private GherkinUtilsStep() { |
| 63 | + // cannot be directly instantiated |
| 64 | + } |
| 65 | +} |
0 commit comments