1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.gpg;
20
21 import java.io.IOException;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.codehaus.plexus.util.Os;
27 import org.codehaus.plexus.util.cli.CommandLineException;
28 import org.codehaus.plexus.util.cli.CommandLineUtils;
29 import org.codehaus.plexus.util.cli.Commandline;
30 import org.codehaus.plexus.util.cli.StreamConsumer;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class GpgVersionParser {
50 private final GpgVersionConsumer consumer;
51
52 private GpgVersionParser(GpgVersionConsumer consumer) {
53 this.consumer = consumer;
54 }
55
56 public static GpgVersionParser parse(String executable) throws MojoExecutionException {
57 Commandline cmd = new Commandline();
58
59 if (executable != null && !executable.isEmpty()) {
60 cmd.setExecutable(executable);
61 } else {
62 cmd.setExecutable("gpg" + (Os.isFamily(Os.FAMILY_WINDOWS) ? ".exe" : ""));
63 }
64
65 cmd.createArg().setValue("--version");
66
67 GpgVersionConsumer out = new GpgVersionConsumer();
68
69 try {
70 CommandLineUtils.executeCommandLine(cmd, null, out, null);
71 } catch (CommandLineException e) {
72 throw new MojoExecutionException("failed to execute gpg", e);
73 }
74
75 return new GpgVersionParser(out);
76 }
77
78 public GpgVersion getGpgVersion() {
79 return consumer.getGpgVersion();
80 }
81
82
83
84
85
86
87
88 static class GpgVersionConsumer implements StreamConsumer {
89 private final Pattern gpgVersionPattern = Pattern.compile("gpg \\([^)]+\\) .+");
90
91 private GpgVersion gpgVersion;
92
93 @Override
94 public void consumeLine(String line) throws IOException {
95 Matcher m = gpgVersionPattern.matcher(line);
96 if (m.matches()) {
97 gpgVersion = GpgVersion.parse(m.group());
98 }
99 }
100
101 public GpgVersion getGpgVersion() {
102 return gpgVersion;
103 }
104 }
105 }