1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.simpligility.maven.plugins.android.standalonemojos;
18
19 import com.android.ddmlib.AdbCommandRejectedException;
20 import com.android.ddmlib.IDevice;
21 import com.android.ddmlib.SyncException;
22 import com.android.ddmlib.SyncService;
23 import com.android.ddmlib.TimeoutException;
24 import com.simpligility.maven.plugins.android.AbstractAndroidMojo;
25 import com.simpligility.maven.plugins.android.DeviceCallback;
26 import com.simpligility.maven.plugins.android.common.DeviceHelper;
27 import com.simpligility.maven.plugins.android.common.LogSyncProgressMonitor;
28 import com.simpligility.maven.plugins.android.config.ConfigHandler;
29 import com.simpligility.maven.plugins.android.config.ConfigPojo;
30 import com.simpligility.maven.plugins.android.config.PullParameter;
31 import com.simpligility.maven.plugins.android.configuration.Push;
32
33 import org.apache.commons.io.FileUtils;
34 import org.apache.maven.plugin.MojoExecutionException;
35 import org.apache.maven.plugin.MojoFailureException;
36 import org.apache.maven.plugins.annotations.Mojo;
37 import org.apache.maven.plugins.annotations.Parameter;
38
39 import java.io.File;
40 import java.io.IOException;
41 import java.util.Collection;
42 import java.util.HashMap;
43 import java.util.Map;
44
45
46
47
48
49
50 @SuppressWarnings( "unused" )
51 @Mojo( name = "push", requiresProject = false )
52 public class PushMojo extends AbstractAndroidMojo
53 {
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 @Parameter
76 @ConfigPojo
77 private Push push;
78
79
80
81
82
83
84
85 @Parameter( property = "android.push.source" )
86 private String pushSource;
87
88 @PullParameter( required = true )
89 private String parsedSource;
90
91
92
93
94
95
96
97 @Parameter( property = "android.push.destination" )
98 private String pushDestination;
99
100 @PullParameter( required = true )
101 private String parsedDestination;
102
103 public void execute() throws MojoExecutionException, MojoFailureException
104 {
105
106 ConfigHandler configHandler = new ConfigHandler( this, this.session, this.execution );
107 configHandler.parseConfiguration();
108
109 final Map<String, String> sourceDestinationMap = calculateSourceDestinationMapping();
110
111 doWithDevices( new DeviceCallback()
112 {
113 public void doWithDevice( final IDevice device ) throws MojoExecutionException
114 {
115 String deviceLogLinePrefix = DeviceHelper.getDeviceLogLinePrefix( device );
116
117
118 String message = "";
119
120 try
121 {
122 SyncService syncService = device.getSyncService();
123
124 for ( Map.Entry<String, String> pushFileEntry : sourceDestinationMap.entrySet() )
125 {
126 String sourcePath = pushFileEntry.getKey();
127 String destinationPath = pushFileEntry.getValue();
128
129 message = deviceLogLinePrefix + "Push of " + sourcePath + " to " + destinationPath + " on "
130 + DeviceHelper.getDescriptiveName( device );
131
132 syncService.pushFile( sourcePath, destinationPath, new LogSyncProgressMonitor( getLog() ) );
133
134 getLog().info( message + " successful." );
135 }
136 }
137 catch ( SyncException e )
138 {
139 throw new MojoExecutionException( message + " failed.", e );
140 }
141 catch ( IOException e )
142 {
143 throw new MojoExecutionException( message + " failed.", e );
144 }
145 catch ( TimeoutException e )
146 {
147 throw new MojoExecutionException( message + " failed.", e );
148 }
149 catch ( AdbCommandRejectedException e )
150 {
151 throw new MojoExecutionException( message + " failed.", e );
152 }
153 }
154 } );
155 }
156
157
158
159
160
161
162
163
164
165 private Map<String, String> calculateSourceDestinationMapping() throws MojoExecutionException
166 {
167 Map<String, String> result = new HashMap<String, String>();
168
169 File sourceFile = new File( parsedSource );
170 final String destinationPath;
171 if ( parsedDestination.endsWith( "/" ) )
172 {
173 destinationPath = parsedDestination + sourceFile.getName();
174 }
175 else
176 {
177 destinationPath = parsedDestination;
178 }
179
180 if ( sourceFile.isFile() )
181 {
182
183 final String sourcePath = sourceFile.getAbsolutePath();
184 result.put( sourcePath, destinationPath );
185 }
186 else
187 {
188 if ( sourceFile.isDirectory() )
189 {
190
191 @SuppressWarnings( "unchecked" ) Collection<File> filesList = FileUtils
192 .listFiles( sourceFile, null, true );
193 for ( File file : filesList )
194 {
195
196
197 String filePath = file.getAbsolutePath().substring( sourceFile.getAbsolutePath().length() );
198 filePath = filePath.replace( System.getProperty( "file.separator" ), "/" );
199 result.put( file.getAbsolutePath(), destinationPath + filePath );
200 }
201 }
202 else
203 {
204 throw new MojoExecutionException( "Cannot execute push goal: File or directory "
205 + sourceFile.getAbsolutePath() + " does not exist." );
206 }
207 }
208 return result;
209 }
210 }