|
1 | 1 | /*
|
2 |
| - * Copyright 2016-2021 DiffPlug |
| 2 | + * Copyright 2016-2023 DiffPlug |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
15 | 15 | */
|
16 | 16 | package com.diffplug.spotless.maven;
|
17 | 17 |
|
| 18 | +import java.io.File; |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.file.Files; |
| 21 | + |
18 | 22 | import com.diffplug.common.base.Errors;
|
19 | 23 | import com.diffplug.common.io.ByteStreams;
|
20 | 24 | import com.diffplug.spotless.Formatter;
|
21 | 25 | import com.diffplug.spotless.PaddedCell;
|
22 | 26 |
|
23 |
| -import java.io.File; |
24 |
| -import java.io.IOException; |
25 |
| -import java.nio.file.Files; |
26 |
| -import java.util.List; |
27 |
| - |
28 | 27 | class IdeHook {
|
29 | 28 |
|
30 | 29 | private static void dumpIsClean() {
|
31 | 30 | System.err.println("IS CLEAN");
|
32 | 31 | }
|
33 | 32 |
|
34 |
| - static void performHook(Iterable<File> projectFiles, String path, Formatter formatter, File projectDir, String ratchetFrom, boolean spotlessIdeHookUseStdIn, boolean spotlessIdeHookUseStdOut) { |
| 33 | + //No need to check ratchet (using isClean()) as it is performed in Gradle's IDE hook, since we have already gathered the available git files from ratchet. |
| 34 | + static void performHook(Iterable<File> projectFiles, Formatter formatter, String path, boolean spotlessIdeHookUseStdIn, boolean spotlessIdeHookUseStdOut) { |
35 | 35 | File file = new File(path);
|
36 | 36 | if (!file.isAbsolute()) {
|
37 | 37 | System.err.println("Argument passed to spotlessIdeHook must be an absolute path");
|
38 | 38 | return;
|
39 | 39 | }
|
40 |
| - if (projectContainsFile(projectFiles, file)) { |
41 |
| - try { |
42 |
| - if (ratchetFrom != null) { |
43 |
| - GitRatchetMaven ratchet = GitRatchetMaven.instance(); |
44 |
| - if (ratchet.isClean(projectDir, ratchet.rootTreeShaOf(projectDir, ratchetFrom), file)) { |
45 |
| - dumpIsClean(); |
46 |
| - return; |
47 |
| - } |
48 |
| - } |
49 |
| - byte[] bytes; |
50 |
| - if (spotlessIdeHookUseStdIn) { |
51 |
| - bytes = ByteStreams.toByteArray(System.in); |
52 |
| - } else { |
53 |
| - bytes = Files.readAllBytes(file.toPath()); |
54 |
| - } |
55 |
| - PaddedCell.DirtyState dirty = PaddedCell.calculateDirtyState(formatter, file, bytes); |
56 |
| - if (dirty.isClean()) { |
57 |
| - dumpIsClean(); |
58 |
| - } else if (dirty.didNotConverge()) { |
59 |
| - System.err.println("DID NOT CONVERGE"); |
60 |
| - System.err.println("Run 'spotlessDiagnose' for details https://github.com/diffplug/spotless/blob/main/PADDEDCELL.md"); |
| 40 | + |
| 41 | + if (!projectContainsFile(projectFiles, file)) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + try { |
| 46 | + byte[] bytes; |
| 47 | + if (spotlessIdeHookUseStdIn) { |
| 48 | + bytes = ByteStreams.toByteArray(System.in); |
| 49 | + } else { |
| 50 | + bytes = Files.readAllBytes(file.toPath()); |
| 51 | + } |
| 52 | + PaddedCell.DirtyState dirty = PaddedCell.calculateDirtyState(formatter, file, bytes); |
| 53 | + if (dirty.isClean()) { |
| 54 | + dumpIsClean(); |
| 55 | + } else if (dirty.didNotConverge()) { |
| 56 | + System.err.println("DID NOT CONVERGE"); |
| 57 | + System.err.println("See details https://github.com/diffplug/spotless/blob/main/PADDEDCELL.md"); |
| 58 | + } else { |
| 59 | + System.err.println("IS DIRTY"); |
| 60 | + if (spotlessIdeHookUseStdOut) { |
| 61 | + dirty.writeCanonicalTo(System.out); |
61 | 62 | } else {
|
62 |
| - System.err.println("IS DIRTY"); |
63 |
| - if (spotlessIdeHookUseStdOut) { |
64 |
| - dirty.writeCanonicalTo(System.out); |
65 |
| - } else { |
66 |
| - dirty.writeCanonicalTo(file); |
67 |
| - } |
| 63 | + dirty.writeCanonicalTo(file); |
68 | 64 | }
|
69 |
| - } catch (IOException e) { |
70 |
| - e.printStackTrace(System.err); |
71 |
| - throw Errors.asRuntime(e); |
72 |
| - } finally { |
73 |
| - System.err.close(); |
74 |
| - System.out.close(); |
75 | 65 | }
|
| 66 | + } catch (IOException e) { |
| 67 | + e.printStackTrace(System.err); |
| 68 | + throw Errors.asRuntime(e); |
| 69 | + } finally { |
| 70 | + System.err.close(); |
| 71 | + System.out.close(); |
76 | 72 | }
|
77 | 73 | }
|
78 | 74 |
|
|
0 commit comments