|
1 | 1 | package gitflow; |
2 | 2 |
|
3 | | -import com.intellij.execution.configurations.GeneralCommandLine; |
4 | | -import com.intellij.execution.ExecutionException; |
5 | | -import com.intellij.execution.util.ExecUtil; |
6 | | -import com.intellij.execution.process.ProcessOutput; |
| 3 | +import com.intellij.openapi.components.ServiceManager; |
| 4 | +import com.intellij.openapi.diagnostic.Logger; |
| 5 | +import com.intellij.openapi.project.Project; |
| 6 | +import com.intellij.openapi.util.Disposer; |
| 7 | +import org.jetbrains.annotations.NotNull; |
| 8 | + |
| 9 | +import javax.annotation.Nullable; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.concurrent.ConcurrentHashMap; |
7 | 12 |
|
8 | 13 | public class GitflowVersionTester { |
9 | | - static Boolean isSupportedVersion = null; |
10 | | - |
11 | | - static boolean isSupportedVersion(){ |
12 | | - if (isSupportedVersion == null) { |
13 | | - |
14 | | - ProcessOutput output = null; |
15 | | - GeneralCommandLine commandLine = new GeneralCommandLine(); |
16 | | - commandLine.setExePath("git"); |
17 | | - commandLine.addParameters("flow"); |
18 | | - commandLine.addParameters("version"); |
19 | | - try { |
20 | | - output = ExecUtil.execAndGetOutput(commandLine); |
21 | | - } catch (ExecutionException e) { |
22 | | - e.printStackTrace(); |
| 14 | + |
| 15 | + private static final Logger logger = Logger.getInstance(GitflowVersionTester.class); |
| 16 | + |
| 17 | + private static final Map<Project, GitflowVersionTester> testers = new ConcurrentHashMap<>(); |
| 18 | + |
| 19 | + public static GitflowVersionTester forProject(@NotNull Project project) { |
| 20 | + return testers.computeIfAbsent( |
| 21 | + project, |
| 22 | + p -> { |
| 23 | + Disposer.register(p, () -> testers.remove(p)); |
| 24 | + return new GitflowVersionTester(ServiceManager.getService(Gitflow.class), p); |
23 | 25 | } |
24 | | - String stdout = output.getStdout(); |
25 | | -// System.out.println("output: " + stdout); |
26 | | - // test that the installed git flow CLI version is AVH and not the unmaintained NVIE version |
27 | | - isSupportedVersion = stdout.contains("AVH"); |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + @NotNull private final Gitflow gitflow; |
| 30 | + @NotNull private final Project project; |
| 31 | + |
| 32 | + private String version = null; |
| 33 | + |
| 34 | + private GitflowVersionTester(@NotNull Gitflow gitflow, @NotNull Project project) { |
| 35 | + this.gitflow = gitflow; |
| 36 | + this.project = project; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * <p>Returns the installed {@code git-flow} version. The version |
| 41 | + * is loaded on the first call to this method and is determined |
| 42 | + * by looking at the output of a {@code git flow version} command.</p> |
| 43 | + * <p>If the command fails, {@code null} is returned.</p> |
| 44 | + * |
| 45 | + * @return the {@code git flow} version, or {@code null} |
| 46 | + */ |
| 47 | + @Nullable |
| 48 | + public String getVersion() { |
| 49 | + return version; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Returns true if the {@code git flow} version can be determined |
| 54 | + * and is any AVH version ({@code #contains("AVH")}) and |
| 55 | + * not the unmaintained NVIE version. |
| 56 | + * |
| 57 | + * @return true if we think the git flow version is an AVH version. |
| 58 | + */ |
| 59 | + public boolean isSupportedVersion() { |
| 60 | + return version != null && version.contains("AVH"); |
| 61 | + } |
| 62 | + |
| 63 | + public void init(){ |
| 64 | + String returnedVersion = null; |
| 65 | + try { |
| 66 | + returnedVersion = gitflow.version(project).getOutputOrThrow(); |
| 67 | + logger.info("git flow version: " + version); |
| 68 | + } catch (Exception e) { |
| 69 | + logger.error("Could not determine git flow version", e); |
| 70 | + } |
| 71 | + if (returnedVersion != null){ |
| 72 | + version = returnedVersion; |
28 | 73 | } |
29 | | - return isSupportedVersion; |
30 | 74 | } |
31 | 75 | } |
0 commit comments