private async generateJar()

in src/exportJarSteps/GenerateJarExecutor.ts [24:98]


    private async generateJar(stepMetadata: IStepMetadata): Promise<boolean> {
        if (_.isEmpty(stepMetadata.elements)) {
            // If the user uses wizard or custom task with a empty list of elements,
            // the classpaths should be specified manually.
            if (!(await this.generateClasspaths(stepMetadata))) {
                return false;
            }
        }
        const folder: WorkspaceFolder | undefined = stepMetadata.workspaceFolder;
        if (!folder) {
            throw new Error(ExportJarMessages.fieldUndefinedMessage(ExportJarMessages.Field.WORKSPACEFOLDER, this.currentStep));
        }
        let destPath = "";
        if (stepMetadata.outputPath === ExportJarTargets.SETTING_ASKUSER || stepMetadata.outputPath === "") {
            if (stepMetadata.outputPath === ExportJarTargets.SETTING_ASKUSER) {
                sendInfo("", { exportJarPath: stepMetadata.outputPath });
            }
            const outputUri: Uri | undefined = await window.showSaveDialog({
                defaultUri: Uri.file(join(folder.uri.fsPath, `${folder.name}.jar`)),
                filters: {
                    "Java Archive": ["jar"],
                },
            });
            if (!outputUri) {
                return Promise.reject();
            }
            destPath = outputUri.fsPath;
        } else {
            const outputPath: string | undefined = stepMetadata.outputPath;
            if (!outputPath) {
                throw new Error(ExportJarMessages.fieldUndefinedMessage(ExportJarMessages.Field.OUTPUTPATH, this.currentStep));
            }
            // Both the absolute path and the relative path (to workspace folder) are supported.
            destPath = (isAbsolute(outputPath)) ? outputPath : join(folder.uri.fsPath, outputPath);
            // Since both the specific target folder and the specific target file are supported,
            // we regard a path as a file if it ends with ".jar". Otherwise, it was regarded as a folder.
            if (extname(outputPath) !== ".jar") {
                destPath = join(destPath, folder.name + ".jar");
            }
            await ensureDir(dirname(destPath));
        }
        destPath = normalize(destPath);
        return window.withProgress({
            location: ProgressLocation.Window,
            title: "Exporting Jar : Generating jar...",
            cancellable: true,
        }, (_progress, token) => {
            return new Promise<boolean>(async (resolve, reject) => {
                token.onCancellationRequested(() => {
                    return reject();
                });
                const mainClass: string | undefined = stepMetadata.mainClass;
                // For "no main class" option, we get an empty string in stepMetadata.mainClass,
                // so this condition would be (mainClass === undefined)
                if (mainClass === undefined) {
                    return reject(new Error(ExportJarMessages.fieldUndefinedMessage(ExportJarMessages.Field.MAINCLASS, this.currentStep)));
                }
                const classpaths: IClasspath[] = stepMetadata.classpaths;
                if (_.isEmpty(classpaths)) {
                    return reject(new Error(ExportJarMessages.CLASSPATHS_EMPTY));
                }
                if (!stepMetadata.terminalId) {
                    return reject(new Error("Can't find related terminal."));
                }
                const exportResult: boolean | undefined = await Jdtls.exportJar(basename(mainClass),
                    classpaths, destPath, stepMetadata.terminalId, token);
                if (exportResult === true) {
                    stepMetadata.outputPath = destPath;
                    return resolve(true);
                } else {
                    return reject(new Error("Export jar failed."));
                }
            });
        });
    }