1 | |
package org.apache.maven.plugins.repository; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.plugin.MojoExecutionException; |
23 | |
import org.apache.maven.plugin.logging.Log; |
24 | |
import org.codehaus.plexus.components.interactivity.InputHandler; |
25 | |
|
26 | |
import java.io.File; |
27 | |
import java.io.FilenameFilter; |
28 | |
import java.io.IOException; |
29 | |
import java.util.ArrayList; |
30 | |
import java.util.Arrays; |
31 | |
import java.util.Collections; |
32 | |
import java.util.Comparator; |
33 | |
import java.util.List; |
34 | |
import java.util.StringTokenizer; |
35 | |
|
36 | |
final class BundleUtils |
37 | |
{ |
38 | |
private BundleUtils() |
39 | 0 | { |
40 | 0 | } |
41 | |
|
42 | |
public static List<File> selectProjectFiles( final File dir, final InputHandler inputHandler, final String finalName, |
43 | |
final File pom, final Log log, final boolean batchMode ) |
44 | |
throws MojoExecutionException |
45 | |
{ |
46 | 30 | File[] projectFiles = dir.listFiles( new FilenameFilter() |
47 | |
{ |
48 | 30 | public boolean accept( File dir, String name ) |
49 | |
{ |
50 | 112 | return new File( dir, name ).isFile() && name.startsWith( finalName ); |
51 | |
} |
52 | |
} ); |
53 | |
|
54 | 30 | List<File> result = new ArrayList<File>(); |
55 | |
|
56 | 30 | if ( projectFiles == null ) |
57 | |
{ |
58 | 0 | return result; |
59 | |
} |
60 | |
|
61 | 130 | for ( int i = 0; i < projectFiles.length; i++ ) |
62 | |
{ |
63 | 100 | if ( projectFiles[i].getName().endsWith( ".pom" ) ) |
64 | |
{ |
65 | 12 | if ( !projectFiles[i].equals( pom ) ) |
66 | |
{ |
67 | 0 | log.info( "Detected POM file will be excluded:\n" + projectFiles[i] |
68 | |
+ "\n\nInstead, the bundle will include the POM from:\n" + pom ); |
69 | |
} |
70 | |
} |
71 | 88 | else if ( projectFiles[i].getName().endsWith( "-bundle.jar" ) ) |
72 | |
{ |
73 | 18 | log.warn( "Skipping project file which collides with repository bundle filename:\n" + projectFiles[i] ); |
74 | |
} |
75 | |
else |
76 | |
{ |
77 | 70 | result.add( projectFiles[i] ); |
78 | |
} |
79 | |
} |
80 | |
|
81 | 30 | if ( result.isEmpty() ) |
82 | |
{ |
83 | 4 | return result; |
84 | |
} |
85 | |
|
86 | 26 | Collections.sort( result, new Comparator<File>() |
87 | |
{ |
88 | 90 | public int compare( File first, File second ) |
89 | |
{ |
90 | 64 | String f = first.getName(); |
91 | 64 | String s = second.getName(); |
92 | |
|
93 | 64 | if ( f.length() == s.length() ) |
94 | |
{ |
95 | 20 | return f.compareTo( s ); |
96 | |
} |
97 | |
|
98 | 44 | return f.length() < s.length() ? -1 : 1; |
99 | |
} |
100 | |
} ); |
101 | |
|
102 | 26 | result = reviseFileList( result, inputHandler, log, batchMode ); |
103 | |
|
104 | 26 | return result; |
105 | |
} |
106 | |
|
107 | |
public static List<File> reviseFileList( List<File> input, InputHandler inputHandler, Log log, boolean batchMode ) |
108 | |
throws MojoExecutionException |
109 | |
{ |
110 | 26 | List<File> result = new ArrayList<File>( input ); |
111 | |
|
112 | 26 | if ( batchMode ) |
113 | |
{ |
114 | 0 | return result; |
115 | |
} |
116 | |
|
117 | |
while( true ) |
118 | |
{ |
119 | 42 | StringBuffer message = new StringBuffer(); |
120 | 42 | message.append( "The following files are marked for inclusion in the repository bundle:\n" ); |
121 | 42 | message.append( "\n0.) Done" ); |
122 | |
|
123 | 42 | int i = 1; |
124 | 42 | for ( File f : result ) |
125 | |
{ |
126 | 90 | message.append( "\n" ).append( (i++) ).append( ".) " ).append( f.getName() ); |
127 | |
} |
128 | |
|
129 | 42 | message.append( "\n\nPlease select the number(s) for any files you wish to exclude, " + |
130 | |
"or '0' when you're done.\nSeparate the numbers for multiple files with a " + |
131 | |
"comma (',').\n\nSelection: " ); |
132 | |
|
133 | 42 | log.info( message ); |
134 | 42 | String response = null; |
135 | |
try |
136 | |
{ |
137 | 42 | response = inputHandler.readLine(); |
138 | |
} |
139 | 0 | catch ( IOException e ) |
140 | |
{ |
141 | 0 | throw new MojoExecutionException( "Project file selection failed with an I/O exception: " + e.getMessage(), e ); |
142 | 42 | } |
143 | |
|
144 | 42 | if ( response == null || "0".equals( response ) ) |
145 | |
{ |
146 | 0 | break; |
147 | |
} |
148 | |
|
149 | 16 | StringTokenizer st = new StringTokenizer( response, "," ); |
150 | |
|
151 | 16 | if ( st.countTokens() > 0 ) |
152 | |
{ |
153 | 16 | int[] idxs = new int[st.countTokens()]; |
154 | 44 | for ( int j = 0; j < idxs.length; j++ ) |
155 | |
{ |
156 | 28 | idxs[j] = Integer.parseInt( st.nextToken().trim() ); |
157 | |
} |
158 | |
|
159 | 16 | Arrays.sort( idxs ); |
160 | |
|
161 | 44 | for( int k = idxs.length - 1; k > -1; k-- ) |
162 | |
{ |
163 | 28 | if ( idxs[k] < 1 || idxs[k] > result.size() ) |
164 | |
{ |
165 | 0 | log.warn( "NOT removing: " + idxs[k] + "; no such file." ); |
166 | 0 | continue; |
167 | |
} |
168 | |
|
169 | 28 | File removed = (File) result.remove( idxs[k] -1 ); |
170 | 28 | log.info( "Removed: " + removed.getName() ); |
171 | |
} |
172 | |
} |
173 | |
else |
174 | |
{ |
175 | |
break; |
176 | |
} |
177 | 16 | } |
178 | |
|
179 | 26 | return result; |
180 | |
} |
181 | |
|
182 | |
} |