-
Notifications
You must be signed in to change notification settings - Fork 91
feat: Report messages when exporting jar #499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,13 +29,16 @@ | |
| import java.util.zip.ZipFile; | ||
|
|
||
| import org.apache.commons.io.FilenameUtils; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.eclipse.core.resources.IFile; | ||
| import org.eclipse.core.resources.IProject; | ||
| import org.eclipse.core.resources.IWorkspaceRoot; | ||
| import org.eclipse.core.resources.ResourcesPlugin; | ||
| import org.eclipse.core.runtime.CoreException; | ||
| import org.eclipse.core.runtime.IPath; | ||
| import org.eclipse.core.runtime.IProgressMonitor; | ||
| import org.eclipse.core.runtime.IStatus; | ||
| import org.eclipse.core.runtime.MultiStatus; | ||
| import org.eclipse.core.runtime.NullProgressMonitor; | ||
| import org.eclipse.core.runtime.Path; | ||
| import org.eclipse.jdt.core.IJavaElement; | ||
|
|
@@ -67,6 +70,8 @@ | |
|
|
||
| public final class ProjectCommand { | ||
|
|
||
| private static String COMMAND_EXPORT_JAR_REPORT = "java.view.package.exportJarReport"; | ||
|
|
||
| private static class MainClassInfo { | ||
| public String name; | ||
| public String path; | ||
|
|
@@ -83,23 +88,6 @@ private static class Classpath { | |
| public boolean isArtifact; | ||
| } | ||
|
|
||
| private static class ExportResult { | ||
| public boolean result; | ||
| public String message; | ||
| public String log; | ||
|
|
||
| ExportResult(boolean result) { | ||
| this.result = result; | ||
| this.log = ""; | ||
| } | ||
|
|
||
| ExportResult(boolean result, String message) { | ||
| this.result = result; | ||
| this.message = message; | ||
| this.log = ""; | ||
| } | ||
| } | ||
|
|
||
| private static final Gson gson = new GsonBuilder().registerTypeAdapterFactory(new CollectionTypeAdapter.Factory()) | ||
| .registerTypeAdapterFactory(new EnumTypeAdapter.Factory()).create(); | ||
|
|
||
|
|
@@ -151,16 +139,14 @@ private static IWorkspaceRoot getWorkspaceRoot() { | |
| return ResourcesPlugin.getWorkspace().getRoot(); | ||
| } | ||
|
|
||
| public static ExportResult exportJar(List<Object> arguments, IProgressMonitor monitor) { | ||
| if (arguments.size() < 3) { | ||
| return new ExportResult(false, "Invalid export Arguments"); | ||
| } | ||
| if (monitor.isCanceled()) { | ||
| return new ExportResult(false, "User cancelled"); | ||
| public static boolean exportJar(List<Object> arguments, IProgressMonitor monitor) { | ||
| if (arguments.size() < 4) { | ||
| return false; | ||
| } | ||
| String mainClass = gson.fromJson(gson.toJson(arguments.get(0)), String.class); | ||
| Classpath[] classpaths = gson.fromJson(gson.toJson(arguments.get(1)), Classpath[].class); | ||
| String destination = gson.fromJson(gson.toJson(arguments.get(2)), String.class); | ||
| String taskLabel = gson.fromJson(gson.toJson(arguments.get(3)), String.class); | ||
| Manifest manifest = new Manifest(); | ||
| manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); | ||
| if (mainClass.length() > 0) { | ||
|
|
@@ -170,24 +156,39 @@ public static ExportResult exportJar(List<Object> arguments, IProgressMonitor mo | |
| Set<String> directories = new HashSet<>(); | ||
| for (Classpath classpath : classpaths) { | ||
| if (monitor.isCanceled()) { | ||
| return new ExportResult(false, "User cancelled"); | ||
| return false; | ||
| } | ||
| if (classpath.isArtifact) { | ||
| writeArchive(new ZipFile(classpath.source), /* areDirectoryEntriesIncluded = */true, | ||
| /* isCompressed = */true, target, directories, monitor); | ||
| MultiStatus resultStatus = writeArchive(new ZipFile(classpath.source), | ||
| /* areDirectoryEntriesIncluded = */true, /* isCompressed = */true, target, directories, monitor); | ||
| int severity = resultStatus.getSeverity(); | ||
| if (severity == IStatus.OK) { | ||
| java.nio.file.Path path = java.nio.file.Paths.get(classpath.source); | ||
| reportExportJarMessage(taskLabel, IStatus.OK, "successfully export " + path.getFileName().toString()); | ||
| continue; | ||
| } | ||
| if (resultStatus.isMultiStatus()) { | ||
| for (IStatus childStatus : resultStatus.getChildren()) { | ||
| reportExportJarMessage(taskLabel, severity, childStatus.getMessage()); | ||
| } | ||
| } else { | ||
| reportExportJarMessage(taskLabel, severity, resultStatus.getMessage()); | ||
| } | ||
| } else { | ||
| try { | ||
| writeFile(new File(classpath.source), new Path(classpath.destination), /* areDirectoryEntriesIncluded = */true, | ||
| /* isCompressed = */true, target, directories); | ||
| reportExportJarMessage(taskLabel, IStatus.OK, "successfully export " + classpath.destination); | ||
|
||
| } catch (CoreException e) { | ||
| // TODO: Collect reports | ||
| reportExportJarMessage(taskLabel, IStatus.ERROR, e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
| } catch (IOException e) { | ||
| return new ExportResult(false, e.getMessage()); | ||
| reportExportJarMessage(taskLabel, IStatus.ERROR, e.getMessage()); | ||
| return false; | ||
| } | ||
| return new ExportResult(true); | ||
| return true; | ||
| } | ||
|
|
||
| public static List<MainClassInfo> getMainClasses(List<Object> arguments, IProgressMonitor monitor) throws Exception { | ||
|
|
@@ -253,4 +254,28 @@ public static String getModuleName(IJavaProject project) { | |
| } | ||
| } | ||
|
|
||
| private static void reportExportJarMessage(String taskLabel, int severity, String message) { | ||
| if (StringUtils.isNotBlank(message) && StringUtils.isNotBlank(taskLabel)) { | ||
| String readableSeverity = getSeverityString(severity); | ||
| JavaLanguageServerPlugin.getInstance().getClientConnection().executeClientCommand(COMMAND_EXPORT_JAR_REPORT, | ||
| taskLabel, readableSeverity + ": " + message); | ||
| } | ||
| } | ||
|
|
||
| private static String getSeverityString(int severity) { | ||
| switch (severity) { | ||
| case IStatus.INFO: | ||
| return "INFO"; | ||
| case IStatus.WARNING: | ||
| return "WARNING"; | ||
| case IStatus.ERROR: | ||
| return "ERROR"; | ||
| case IStatus.CANCEL: | ||
| return "CANCEL"; | ||
| case IStatus.OK: | ||
| return "OK"; | ||
| default: | ||
| return "UNKNOWN STATUS"; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| import { lstat } from "fs-extra"; | ||
| import * as globby from "globby"; | ||
| import * as _ from "lodash"; | ||
| import { platform } from "os"; | ||
| import { EOL, platform } from "os"; | ||
| import { dirname, extname, isAbsolute, join, relative } from "path"; | ||
| import { | ||
| CustomExecution, Event, EventEmitter, Pseudoterminal, Task, TaskDefinition, | ||
|
|
@@ -44,6 +44,7 @@ export async function executeExportJarTask(node?: INodeData): Promise<void> { | |
| isExportingJar = true; | ||
| const stepMetadata: IStepMetadata = { | ||
| entry: node, | ||
| taskLabel: "exportjar:default", | ||
| steps: [], | ||
| projectList: [], | ||
| elements: [], | ||
|
|
@@ -55,7 +56,7 @@ export async function executeExportJarTask(node?: INodeData): Promise<void> { | |
| throw new Error(ExportJarMessages.stepErrorMessage(ExportJarMessages.StepAction.FINDEXECUTOR, ExportJarStep.ResolveJavaProject)); | ||
| } | ||
| await resolveJavaProjectExecutor.execute(stepMetadata); | ||
| tasks.executeTask(ExportJarTaskProvider.getTask(stepMetadata)); | ||
| tasks.executeTask(ExportJarTaskProvider.getDefaultTask(stepMetadata)); | ||
| } catch (err) { | ||
| if (err) { | ||
| failMessage(`${err}`); | ||
|
|
@@ -68,13 +69,13 @@ export class ExportJarTaskProvider implements TaskProvider { | |
|
|
||
| public static exportJarType: string = "java"; | ||
|
|
||
| public static getTask(stepMetadata: IStepMetadata): Task { | ||
| public static getDefaultTask(stepMetadata: IStepMetadata): Task { | ||
| if (!stepMetadata.workspaceFolder) { | ||
| throw new Error(ExportJarMessages.fieldUndefinedMessage(ExportJarMessages.Field.WORKSPACEFOLDER, ExportJarStep.ResolveTask)); | ||
| } | ||
| const defaultDefinition: IExportJarTaskDefinition = { | ||
| type: ExportJarTaskProvider.exportJarType, | ||
| label: `${ExportJarTaskProvider.exportJarType}: exportjar:default`, | ||
| label: "exportjar:default", | ||
| targetPath: Settings.getExportJarTargetPath(), | ||
| elements: [], | ||
| mainClass: undefined, | ||
|
|
@@ -93,9 +94,10 @@ export class ExportJarTaskProvider implements TaskProvider { | |
| const definition: IExportJarTaskDefinition = <IExportJarTaskDefinition>task.definition; | ||
| const folder: WorkspaceFolder = <WorkspaceFolder>task.scope; | ||
| const resolvedTask: Task = new Task(definition, folder, task.name, ExportJarTaskProvider.exportJarType, | ||
| new CustomExecution(async (resolvedDefinition: TaskDefinition): Promise<Pseudoterminal> => { | ||
| new CustomExecution(async (resolvedDefinition: IExportJarTaskDefinition): Promise<Pseudoterminal> => { | ||
| const stepMetadata: IStepMetadata = { | ||
| entry: undefined, | ||
| taskLabel: resolvedDefinition.label || `${ExportJarTaskProvider.exportJarType}: exportjar:${folder.name}`, | ||
| workspaceFolder: folder, | ||
| projectList: await Jdtls.getProjects(folder.uri.toString()), | ||
| steps: [], | ||
|
|
@@ -139,10 +141,11 @@ export class ExportJarTaskProvider implements TaskProvider { | |
| targetPath: Settings.getExportJarTargetPath(), | ||
| elements: elementList, | ||
| }; | ||
| const defaultTask: Task = new Task(defaultDefinition, folder, `exportjar:${folder.name}`, | ||
| ExportJarTaskProvider.exportJarType, new CustomExecution(async (resolvedDefinition: TaskDefinition): Promise<Pseudoterminal> => { | ||
| const defaultTask: Task = new Task(defaultDefinition, folder, `exportjar:${folder.name}`, ExportJarTaskProvider.exportJarType, | ||
| new CustomExecution(async (resolvedDefinition: IExportJarTaskDefinition): Promise<Pseudoterminal> => { | ||
| const stepMetadata: IStepMetadata = { | ||
| entry: undefined, | ||
| taskLabel: resolvedDefinition.label || `${ExportJarTaskProvider.exportJarType}: exportjar:${folder.name}`, | ||
|
||
| workspaceFolder: folder, | ||
| projectList: await Jdtls.getProjects(folder.uri.toString()), | ||
| steps: [], | ||
|
|
@@ -170,11 +173,16 @@ class ExportJarTaskTerminal implements Pseudoterminal { | |
|
|
||
| constructor(exportJarTaskDefinition: IExportJarTaskDefinition, stepMetadata: IStepMetadata) { | ||
| this.stepMetadata = stepMetadata; | ||
| this.stepMetadata.taskLabel = exportJarTaskDefinition.label || ""; | ||
| this.stepMetadata.mainClass = exportJarTaskDefinition.mainClass; | ||
| this.stepMetadata.outputPath = exportJarTaskDefinition.targetPath; | ||
| this.stepMetadata.elements = exportJarTaskDefinition.elements || []; | ||
| } | ||
|
|
||
| public handleInput(data: string): void { | ||
| this.writeEmitter.fire(data + EOL); | ||
| } | ||
|
|
||
| public async open(_initialDimensions: TerminalDimensions | undefined): Promise<void> { | ||
| let exportResult: boolean | undefined; | ||
| try { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The message is not accurate.
Successfully added the file to the exported jar: path.getFileName().toString()