1 package org.apache.maven.plugins.gpg;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25
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 {
52 private final GpgVersionConsumer consumer;
53
54 private GpgVersionParser( GpgVersionConsumer consumer )
55 {
56 this.consumer = consumer;
57
58 }
59
60 public static GpgVersionParser parse( String executable )
61 {
62 Commandline cmd = new Commandline();
63
64 if ( StringUtils.isNotEmpty( executable ) )
65 {
66 cmd.setExecutable( executable );
67 }
68 else
69 {
70 cmd.setExecutable( "gpg" + ( Os.isFamily( Os.FAMILY_WINDOWS ) ? ".exe" : "" ) );
71 }
72
73
74 cmd.createArg().setValue( "--version" );
75
76 GpgVersionConsumer out = new GpgVersionConsumer();
77
78 try
79 {
80 CommandLineUtils.executeCommandLine( cmd, null, out, null );
81 }
82 catch ( CommandLineException e )
83 {
84
85 }
86
87 return new GpgVersionParser( out );
88 }
89
90 public GpgVersion getGpgVersion()
91 {
92 return consumer.getGpgVersion();
93
94 }
95
96
97
98
99
100
101
102 static class GpgVersionConsumer
103 implements StreamConsumer
104 {
105 private final Pattern gpgVersionPattern = Pattern.compile( "gpg \\([^)]+\\) .+" );
106
107 private GpgVersion gpgVersion;
108
109 @Override
110 public void consumeLine( String line )
111 throws IOException
112 {
113 Matcher m = gpgVersionPattern.matcher( line );
114 if ( m.matches() )
115 {
116 gpgVersion = GpgVersion.parse( m.group() );
117 }
118 }
119
120 public GpgVersion getGpgVersion()
121 {
122 return gpgVersion;
123 }
124 }
125
126 }