|
| 1 | +/* |
| 2 | + * Copyright 2024 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.go; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.Serializable; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Objects; |
| 26 | + |
| 27 | +import javax.annotation.Nullable; |
| 28 | + |
| 29 | +import com.diffplug.spotless.ForeignExe; |
| 30 | +import com.diffplug.spotless.FormatterFunc; |
| 31 | +import com.diffplug.spotless.FormatterStep; |
| 32 | +import com.diffplug.spotless.ProcessRunner; |
| 33 | + |
| 34 | +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 35 | + |
| 36 | +/** |
| 37 | + * Note: gofmt doesn't have a version flag, because it's part of standard Go distribution. |
| 38 | + * So `go` executable can be used to determine base path and version, and path to gofmt can be built from it. |
| 39 | + */ |
| 40 | +public class GofmtFormatStep { |
| 41 | + public static String name() { |
| 42 | + return "gofmt"; |
| 43 | + } |
| 44 | + |
| 45 | + public static String defaultVersion() { |
| 46 | + return "go1.20.0"; |
| 47 | + } |
| 48 | + |
| 49 | + private final String version; |
| 50 | + private final @Nullable String pathToExe; |
| 51 | + |
| 52 | + private GofmtFormatStep(String version, String pathToExe) { |
| 53 | + this.version = version; |
| 54 | + this.pathToExe = pathToExe; |
| 55 | + } |
| 56 | + |
| 57 | + public static GofmtFormatStep withVersion(String version) { |
| 58 | + return new GofmtFormatStep(version, null); |
| 59 | + } |
| 60 | + |
| 61 | + public GofmtFormatStep withGoExecutable(String pathToExe) { |
| 62 | + return new GofmtFormatStep(version, pathToExe); |
| 63 | + } |
| 64 | + |
| 65 | + public FormatterStep create() { |
| 66 | + return FormatterStep.createLazy(name(), this::createState, GofmtFormatStep.State::toFunc); |
| 67 | + } |
| 68 | + |
| 69 | + private State createState() throws IOException, InterruptedException { |
| 70 | + String howToInstall = "gofmt is a part of standard go distribution. If spotless can't discover it automatically, " + |
| 71 | + "you can point Spotless to the go binary with {@code pathToExe('/path/to/go')}"; |
| 72 | + final ForeignExe exe = ForeignExe.nameAndVersion("go", version) |
| 73 | + .pathToExe(pathToExe) |
| 74 | + .versionFlag("version") |
| 75 | + .fixCantFind(howToInstall) |
| 76 | + .fixWrongVersion( |
| 77 | + "You can tell Spotless to use the version you already have with {@code gofmt('{versionFound}')}" + |
| 78 | + "or you can install the currently specified Go version, {version}.\n" + howToInstall); |
| 79 | + return new State(this, exe); |
| 80 | + } |
| 81 | + |
| 82 | + @SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED") |
| 83 | + static class State implements Serializable { |
| 84 | + private static final long serialVersionUID = -1825662355363926318L; |
| 85 | + // used for up-to-date checks and caching |
| 86 | + final String version; |
| 87 | + final transient ForeignExe exe; |
| 88 | + |
| 89 | + public State(GofmtFormatStep step, ForeignExe goExecutable) { |
| 90 | + this.version = step.version; |
| 91 | + this.exe = Objects.requireNonNull(goExecutable); |
| 92 | + } |
| 93 | + |
| 94 | + String format(ProcessRunner runner, String input, File file) throws IOException, InterruptedException { |
| 95 | + final List<String> processArgs = new ArrayList<>(); |
| 96 | + String pathToGoBinary = exe.confirmVersionAndGetAbsolutePath(); |
| 97 | + Path goBasePath = Path.of(pathToGoBinary).getParent(); |
| 98 | + if (goBasePath == null) { |
| 99 | + throw new IllegalStateException("Unable to resolve base path of Go installation directory"); |
| 100 | + } |
| 101 | + String pathToGoFmt = goBasePath.resolve("gofmt").toString(); |
| 102 | + processArgs.add(pathToGoFmt); |
| 103 | + return runner.exec(input.getBytes(StandardCharsets.UTF_8), processArgs).assertExitZero(StandardCharsets.UTF_8); |
| 104 | + } |
| 105 | + |
| 106 | + FormatterFunc.Closeable toFunc() { |
| 107 | + ProcessRunner runner = new ProcessRunner(); |
| 108 | + return FormatterFunc.Closeable.of(runner, this::format); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments