29
29
import java .util .zip .ZipFile ;
30
30
31
31
import org .apache .commons .io .FilenameUtils ;
32
+ import org .apache .commons .lang3 .StringUtils ;
32
33
import org .eclipse .core .resources .IFile ;
33
34
import org .eclipse .core .resources .IProject ;
34
35
import org .eclipse .core .resources .IWorkspaceRoot ;
35
36
import org .eclipse .core .resources .ResourcesPlugin ;
36
37
import org .eclipse .core .runtime .CoreException ;
37
38
import org .eclipse .core .runtime .IPath ;
38
39
import org .eclipse .core .runtime .IProgressMonitor ;
40
+ import org .eclipse .core .runtime .IStatus ;
41
+ import org .eclipse .core .runtime .MultiStatus ;
39
42
import org .eclipse .core .runtime .NullProgressMonitor ;
40
43
import org .eclipse .core .runtime .Path ;
41
44
import org .eclipse .jdt .core .IJavaElement ;
67
70
68
71
public final class ProjectCommand {
69
72
73
+ private static String COMMAND_EXPORT_JAR_REPORT = "java.view.package.exportJarReport" ;
74
+
70
75
private static class MainClassInfo {
71
76
public String name ;
72
77
public String path ;
@@ -83,23 +88,6 @@ private static class Classpath {
83
88
public boolean isArtifact ;
84
89
}
85
90
86
- private static class ExportResult {
87
- public boolean result ;
88
- public String message ;
89
- public String log ;
90
-
91
- ExportResult (boolean result ) {
92
- this .result = result ;
93
- this .log = "" ;
94
- }
95
-
96
- ExportResult (boolean result , String message ) {
97
- this .result = result ;
98
- this .message = message ;
99
- this .log = "" ;
100
- }
101
- }
102
-
103
91
private static final Gson gson = new GsonBuilder ().registerTypeAdapterFactory (new CollectionTypeAdapter .Factory ())
104
92
.registerTypeAdapterFactory (new EnumTypeAdapter .Factory ()).create ();
105
93
@@ -151,16 +139,14 @@ private static IWorkspaceRoot getWorkspaceRoot() {
151
139
return ResourcesPlugin .getWorkspace ().getRoot ();
152
140
}
153
141
154
- public static ExportResult exportJar (List <Object > arguments , IProgressMonitor monitor ) {
155
- if (arguments .size () < 3 ) {
156
- return new ExportResult (false , "Invalid export Arguments" );
157
- }
158
- if (monitor .isCanceled ()) {
159
- return new ExportResult (false , "User cancelled" );
142
+ public static boolean exportJar (List <Object > arguments , IProgressMonitor monitor ) {
143
+ if (arguments .size () < 4 ) {
144
+ return false ;
160
145
}
161
146
String mainClass = gson .fromJson (gson .toJson (arguments .get (0 )), String .class );
162
147
Classpath [] classpaths = gson .fromJson (gson .toJson (arguments .get (1 )), Classpath [].class );
163
148
String destination = gson .fromJson (gson .toJson (arguments .get (2 )), String .class );
149
+ String taskLabel = gson .fromJson (gson .toJson (arguments .get (3 )), String .class );
164
150
Manifest manifest = new Manifest ();
165
151
manifest .getMainAttributes ().put (Attributes .Name .MANIFEST_VERSION , "1.0" );
166
152
if (mainClass .length () > 0 ) {
@@ -170,24 +156,39 @@ public static ExportResult exportJar(List<Object> arguments, IProgressMonitor mo
170
156
Set <String > directories = new HashSet <>();
171
157
for (Classpath classpath : classpaths ) {
172
158
if (monitor .isCanceled ()) {
173
- return new ExportResult ( false , "User cancelled" ) ;
159
+ return false ;
174
160
}
175
161
if (classpath .isArtifact ) {
176
- writeArchive (new ZipFile (classpath .source ), /* areDirectoryEntriesIncluded = */ true ,
177
- /* isCompressed = */ true , target , directories , monitor );
162
+ MultiStatus resultStatus = writeArchive (new ZipFile (classpath .source ),
163
+ /* areDirectoryEntriesIncluded = */ true , /* isCompressed = */ true , target , directories , monitor );
164
+ int severity = resultStatus .getSeverity ();
165
+ if (severity == IStatus .OK ) {
166
+ java .nio .file .Path path = java .nio .file .Paths .get (classpath .source );
167
+ reportExportJarMessage (taskLabel , IStatus .OK , "Successfully added the file to the exported jar: " + path .getFileName ().toString ());
168
+ continue ;
169
+ }
170
+ if (resultStatus .isMultiStatus ()) {
171
+ for (IStatus childStatus : resultStatus .getChildren ()) {
172
+ reportExportJarMessage (taskLabel , severity , childStatus .getMessage ());
173
+ }
174
+ } else {
175
+ reportExportJarMessage (taskLabel , severity , resultStatus .getMessage ());
176
+ }
178
177
} else {
179
178
try {
180
179
writeFile (new File (classpath .source ), new Path (classpath .destination ), /* areDirectoryEntriesIncluded = */ true ,
181
180
/* isCompressed = */ true , target , directories );
181
+ reportExportJarMessage (taskLabel , IStatus .OK , "Successfully added the file to the exported jar: " + classpath .destination );
182
182
} catch (CoreException e ) {
183
- // TODO: Collect reports
183
+ reportExportJarMessage ( taskLabel , IStatus . ERROR , e . getMessage ());
184
184
}
185
185
}
186
186
}
187
187
} catch (IOException e ) {
188
- return new ExportResult (false , e .getMessage ());
188
+ reportExportJarMessage (taskLabel , IStatus .ERROR , e .getMessage ());
189
+ return false ;
189
190
}
190
- return new ExportResult ( true ) ;
191
+ return true ;
191
192
}
192
193
193
194
public static List <MainClassInfo > getMainClasses (List <Object > arguments , IProgressMonitor monitor ) throws Exception {
@@ -253,4 +254,28 @@ public static String getModuleName(IJavaProject project) {
253
254
}
254
255
}
255
256
257
+ private static void reportExportJarMessage (String taskLabel , int severity , String message ) {
258
+ if (StringUtils .isNotBlank (message ) && StringUtils .isNotBlank (taskLabel )) {
259
+ String readableSeverity = getSeverityString (severity );
260
+ JavaLanguageServerPlugin .getInstance ().getClientConnection ().executeClientCommand (COMMAND_EXPORT_JAR_REPORT ,
261
+ taskLabel , "[" + readableSeverity + "] " + message );
262
+ }
263
+ }
264
+
265
+ private static String getSeverityString (int severity ) {
266
+ switch (severity ) {
267
+ case IStatus .INFO :
268
+ return "INFO" ;
269
+ case IStatus .WARNING :
270
+ return "WARNING" ;
271
+ case IStatus .ERROR :
272
+ return "ERROR" ;
273
+ case IStatus .CANCEL :
274
+ return "CANCEL" ;
275
+ case IStatus .OK :
276
+ return "OK" ;
277
+ default :
278
+ return "UNKNOWN STATUS" ;
279
+ }
280
+ }
256
281
}
0 commit comments