1 package org.codehaus.mojo.siteskinner;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24
25 import org.apache.maven.plugin.logging.Log;
26 import org.apache.maven.scm.ScmException;
27 import org.apache.maven.scm.ScmFileSet;
28 import org.apache.maven.scm.ScmResult;
29 import org.apache.maven.scm.command.checkout.CheckOutScmResult;
30 import org.apache.maven.scm.command.update.UpdateScmResult;
31 import org.apache.maven.scm.manager.ScmManager;
32 import org.apache.maven.scm.provider.ScmProvider;
33 import org.apache.maven.scm.repository.ScmRepository;
34
35
36
37
38 public class ScmCommandExecutor
39 {
40 private ScmManager manager;
41
42 private String connectionUrl;
43
44 private Log log;
45
46
47
48
49
50
51
52
53 public ScmCommandExecutor( ScmManager manager, String connectionUrl, Log log )
54 {
55 this.manager = manager;
56
57 this.connectionUrl = connectionUrl;
58
59 this.log = log;
60 }
61
62
63
64
65
66
67
68 public void checkout( String targetDirectory )
69 throws ScmException
70 {
71 checkout( targetDirectory, null, null );
72 }
73
74
75
76
77
78
79
80
81
82 public void checkout( String targetDirectory, String includes, String excludes )
83 throws ScmException
84 {
85 try
86 {
87 ScmRepository repository = manager.makeScmRepository( connectionUrl );
88
89 ScmProvider provider = manager.getProviderByRepository( repository );
90
91 ScmFileSet fileSet = getFileSet( targetDirectory, includes, excludes );
92
93 CheckOutScmResult result = provider.checkOut( repository, fileSet );
94
95 if ( !checkResult( result ) )
96 {
97 throw new ScmException( "checkout failed with provider message" );
98 }
99 }
100 catch ( Exception ex )
101 {
102 throw new ScmException( "checkout failed.", ex );
103 }
104 }
105
106
107
108
109
110
111
112 public void update( String targetDirectory )
113 throws ScmException
114 {
115 update( targetDirectory, null, null );
116 }
117
118
119
120
121
122
123
124
125
126 public void update( String targetDirectory, String includes, String excludes )
127 throws ScmException
128 {
129 try
130 {
131 ScmRepository repository = manager.makeScmRepository( connectionUrl );
132
133 ScmProvider provider = manager.getProviderByRepository( repository );
134
135 ScmFileSet fileSet = getFileSet( targetDirectory, includes, excludes );
136
137 UpdateScmResult result = provider.update( repository, fileSet );
138
139 if ( !checkResult( result ) )
140 {
141 throw new ScmException( "checkout failed with provider message" );
142 }
143 }
144 catch ( Exception ex )
145 {
146 throw new ScmException( "checkout failed.", ex );
147 }
148 }
149
150 private ScmFileSet getFileSet( String path, String includes, String excludes ) throws IOException
151 {
152 File dir = new File( path );
153
154 if ( includes != null || excludes != null )
155 {
156 return new ScmFileSet( dir, includes, excludes );
157 }
158 else
159 {
160 return new ScmFileSet( dir );
161 }
162 }
163
164 private boolean checkResult( ScmResult result )
165 {
166 if ( !result.isSuccess() )
167 {
168
169 log.warn( "Provider message:" );
170
171 log.warn( result.getProviderMessage() == null ? "" : result.getProviderMessage() );
172
173 log.warn( "Command output:" );
174
175 log.warn( result.getCommandOutput() == null ? "" : result.getCommandOutput() );
176
177 return false;
178 }
179 else
180 {
181 return true;
182 }
183 }
184 }