View Javadoc
1   package com.simpligility.maven.plugins.android;
2   
3   import com.android.annotations.NonNull;
4   import com.google.api.services.androidpublisher.AndroidPublisher;
5   import com.google.api.services.androidpublisher.model.AppEdit;
6   import com.simpligility.maven.plugins.android.common.AndroidPublisherHelper;
7   
8   import org.apache.maven.plugin.MojoExecutionException;
9   import org.apache.maven.plugins.annotations.Parameter;
10  
11  import java.io.BufferedReader;
12  import java.io.File;
13  import java.io.FileInputStream;
14  import java.io.FilenameFilter;
15  import java.io.IOException;
16  import java.io.InputStreamReader;
17  import java.nio.charset.Charset;
18  import java.util.regex.Matcher;
19  import java.util.regex.Pattern;
20  
21  /**
22   *
23   * @author Joris de Groot
24   * @author Benoit Billington
25   */
26  public abstract class AbstractPublisherMojo extends AbstractAndroidMojo
27  {
28      private static final String WHATSNEW = "whatsnew.txt";
29  
30      @Parameter( property = "android.publisher.google.email", required = true )
31      private String publisherEmail;
32  
33      @Parameter( property = "android.publisher.google.p12", required = true )
34      private File p12File;
35  
36      @Parameter( property = "android.publisher.project.name" )
37      protected String projectName;
38  
39      @Parameter( property = "android.publisher.listing.directory", defaultValue = "${project.basedir}/src/main/play/" )
40      protected File listingDirectory;
41  
42      protected AndroidPublisher.Edits edits;
43  
44      protected String editId;
45  
46      // region '419' is a special case in the play store that represents latin america
47      protected static final String LOCALE_DIR_PATTERN = "^[a-z]{2}(-([A-Z]{2}|419))?";
48  
49      protected void initializePublisher( @NonNull String packageName ) throws MojoExecutionException
50      {
51          getLog().debug( "Initializing publisher" );
52          if ( projectName == null || projectName.equals( "" ) )
53          {
54              projectName = this.session.getCurrentProject().getName();
55          }
56  
57          try
58          {
59              AndroidPublisher publisher = AndroidPublisherHelper.init( projectName, publisherEmail, p12File );
60              edits = publisher.edits();
61              AndroidPublisher.Edits.Insert editRequest = edits.insert( packageName, null );
62              AppEdit edit = editRequest.execute();
63              editId = edit.getId();
64          }
65          catch ( Exception e )
66          {
67              throw new MojoExecutionException( e.getMessage(), e );
68          }
69      }
70  
71      public String readFile( File file, int maxChars ) throws IOException
72      {
73          String everything;
74          InputStreamReader isr;
75          
76          if ( sourceEncoding == null )
77          {
78              isr = new InputStreamReader( new FileInputStream( file ) ); // platform default encoding
79          }
80          else
81          {
82              isr = new InputStreamReader( new FileInputStream( file ), sourceEncoding );
83          }
84          
85          BufferedReader br = new BufferedReader( isr );
86          try
87          {
88              StringBuilder sb = new StringBuilder();
89              String line = br.readLine();
90  
91              while ( line != null )
92              {
93                  sb.append( line );
94                  sb.append( "\n" );
95                  line = br.readLine();
96              }
97              everything = sb.toString();
98  
99              if ( everything.endsWith( "\n" ) )
100             {
101                 everything = everything.substring( 0, everything.length() - 1 );
102             }
103         }
104         finally
105         {
106             br.close();
107         }
108 
109         if ( everything.length() > maxChars )
110         {
111             String message = "Too many characters in file " + file.getName() +  " max allowed is " + maxChars;
112             getLog().error( message );
113             throw new IOException( message );
114         }
115         return everything;
116     }
117 
118     public File[] getLocaleDirs()
119     {
120         if ( ! listingDirectory.exists() )
121         {
122             getLog().warn( "Play directory is missing." );
123             return null;
124         }
125         File[] localeDirs = listingDirectory.listFiles( new FilenameFilter()
126         {
127             @Override
128             public boolean accept( File dir, String name )
129             {
130                 Pattern pattern = Pattern.compile( LOCALE_DIR_PATTERN );
131                 Matcher matcher = pattern.matcher( name );
132                 return matcher.matches();
133             }
134         } );
135 
136         if ( localeDirs ==  null || localeDirs.length == 0 )
137         {
138             getLog().warn( "No locale directories found." );
139             return null;
140         }
141 
142         return localeDirs;
143     }
144 
145     public String readFileWithChecks( File dir, String fileName, int maxChars, String errorMessage )
146             throws IOException
147     {
148         File file = new File( dir, fileName );
149         if ( file.exists() )
150         {
151             return readFile( file, maxChars );
152         }
153         else
154         {
155             getLog().warn( errorMessage + " - Filename: " + fileName );
156             return null;
157         }
158     }
159     
160     protected void warnPlatformDefaultEncoding()
161     {
162         if ( sourceEncoding == null )
163         {
164             getLog().warn(
165                     "Using platform encoding ("
166                             + Charset.defaultCharset()
167                             + " actually) to read Play listing text files, i.e. build is platform dependent!" );
168         }
169     }
170 
171 }