@veecode-platform/safira-cli
Version:
Generate a microservice project from your spec.
73 lines (72 loc) • 3.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GoogleJavaFormatCodeFormatter = void 0;
const tslib_1 = require("tslib");
const properties = tslib_1.__importStar(require("../properties.json"));
const file_system_utils_1 = require("../utils/file-system-utils");
const string_utils_1 = require("../utils/string-utils");
const java_utils_1 = require("../utils/java-utils");
const safira_exception_1 = require("../exception/safira-exception");
const command_execute_1 = require("../command/command-execute");
class GoogleJavaFormatDownloader {
constructor() { }
async load() {
const version = properties["google-java-format"].version;
const binaryFolder = file_system_utils_1.FileSystemUtils.buildPath(file_system_utils_1.FileSystemUtils.getOsUserHome(), ".safira", properties.home["cache-folder"], "google-java-format", version);
const binaryFile = file_system_utils_1.FileSystemUtils.buildPath(binaryFolder, "google-java-format");
if (!file_system_utils_1.FileSystemUtils.exists(binaryFile))
throw new safira_exception_1.SafiraDependencyNotFoundException("google-java-format not found. Please Reinstall Safira");
return binaryFile;
}
static get instance() {
if (!this._instance) {
this._instance = new this();
}
return this._instance;
}
}
class GoogleJavaFormatCodeFormatter {
constructor() { }
async format(directory) {
await this.apply(file_system_utils_1.FileSystemUtils.listFilesRecursive(directory, ["java"]));
}
async formatFileList(files) {
await this.apply(files);
}
async apply(files) {
if (files.length === 0)
return;
const formatterBin = await GoogleJavaFormatDownloader.instance.load();
const addExports = await java_utils_1.JavaUtils.getVersion() >= properties["google-java-format"]["add-exports-version"];
const command = `java \
${addExports ? "--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED" : ""} \
${addExports ? "--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED" : ""} \
${addExports ? "--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED" : ""} \
${addExports ? "--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED" : ""} \
${addExports ? "--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED" : ""} \
-jar ${formatterBin} --replace ${files.map(pat => pat.replace(/ /g, "\\ ")).join(" ")}`.replace(/\s\s+/g, " ");
console.log(command);
await command_execute_1.CommandExecute.instance.exec(command);
}
async getContentFormatted(file) {
if (string_utils_1.StringUtils.isNullOrEmpty(file))
return "";
const formatterBin = await GoogleJavaFormatDownloader.instance.load();
const addExports = await java_utils_1.JavaUtils.getVersion() >= properties["google-java-format"]["add-exports-version"];
const command = `java \
${addExports ? "--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED" : ""} \
${addExports ? "--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED" : ""} \
${addExports ? "--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED" : ""} \
${addExports ? "--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED" : ""} \
${addExports ? "--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED" : ""} \
-jar ${formatterBin} ${file}`.replace(/\s\s+/g, " ");
return command_execute_1.CommandExecute.instance.exec(command);
}
static get instance() {
if (!this._instance) {
this._instance = new this();
}
return this._instance;
}
}
exports.GoogleJavaFormatCodeFormatter = GoogleJavaFormatCodeFormatter;