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.StringUtils;
28 import org.codehaus.plexus.util.cli.CommandLineException;
29 import org.codehaus.plexus.util.cli.CommandLineUtils;
30 import org.codehaus.plexus.util.cli.Commandline;
31 import org.codehaus.plexus.util.cli.StreamConsumer;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class GpgVersionParser {
51 private final GpgVersionConsumer consumer;
52
53 private GpgVersionParser(GpgVersionConsumer consumer) {
54 this.consumer = consumer;
55 }
56
57 public static GpgVersionParser parse(String executable) throws MojoExecutionException {
58 Commandline cmd = new Commandline();
59
60 if (StringUtils.isNotEmpty(executable)) {
61 cmd.setExecutable(executable);
62 } else {
63 cmd.setExecutable("gpg" + (Os.isFamily(Os.FAMILY_WINDOWS) ? ".exe" : ""));
64 }
65
66 cmd.createArg().setValue("--version");
67
68 GpgVersionConsumer out = new GpgVersionConsumer();
69
70 try {
71 CommandLineUtils.executeCommandLine(cmd, null, out, null);
72 } catch (CommandLineException e) {
73 throw new MojoExecutionException("failed to execute gpg", e);
74 }
75
76 return new GpgVersionParser(out);
77 }
78
79 public GpgVersion getGpgVersion() {
80 return consumer.getGpgVersion();
81 }
82
83
84
85
86
87
88
89 static class GpgVersionConsumer implements StreamConsumer {
90 private final Pattern gpgVersionPattern = Pattern.compile("gpg \\([^)]+\\) .+");
91
92 private GpgVersion gpgVersion;
93
94 @Override
95 public void consumeLine(String line) throws IOException {
96 Matcher m = gpgVersionPattern.matcher(line);
97 if (m.matches()) {
98 gpgVersion = GpgVersion.parse(m.group());
99 }
100 }
101
102 public GpgVersion getGpgVersion() {
103 return gpgVersion;
104 }
105 }
106 }