diff --git a/Utils/Functions/Filesystem/Files/script.js b/Utils/Functions/Filesystem/Files/script.js index 85bb4615ab..cd836055c8 100644 --- a/Utils/Functions/Filesystem/Files/script.js +++ b/Utils/Functions/Filesystem/Files/script.js @@ -2,202 +2,224 @@ var fileAnalyser = Bean("fileAnalyser"); var fileUtilities = Bean("fileUtilities"); /** -* lists files and directories -* @param {string} directoryPath directory path -* @returns {string[]} list of files and directories -*/ -function ls(directoryPath) { // eslint-disable-line no-unused-vars - var FileClass = Java.type('java.io.File'); - return fileUtilities.ls(new FileClass(directoryPath)); + * Lists all files and directories contained in the given path + * + * @param {string} directoryPath directory path + * @returns {string[]} list of files and directories + */ +// eslint-disable-next-line no-unused-vars +function ls(directoryPath) { + return fileUtilities.ls(directoryPath); } /** -* creates directory -* @param {string} directoryPath directory path -* @returns {void} -*/ -function mkdir(directoryPath) { // eslint-disable-line no-unused-vars - var FileClass = Java.type('java.io.File'); - fileUtilities.mkdir(new FileClass(directoryPath)); + * Creates the given directory + * + * @param {string} directoryPath directory path + * @returns {void} + */ +// eslint-disable-next-line no-unused-vars +function mkdir(directoryPath) { + fileUtilities.mkdir(directoryPath); } /** -* check if file exists -* @param {string} filePath file path -* @returns {boolean} true if file exists -*/ -function fileExists(filePath) { // eslint-disable-line no-unused-vars - var FileClass = Java.type('java.io.File'); - return new FileClass(filePath).exists(); + * Checks if the given file exists + * + * @param {string} filePath file path + * @returns {boolean} true if file exists + */ +// eslint-disable-next-line no-unused-vars +function fileExists(filePath) { + return fileUtilities.exists(filePath); } /** -* returns file content -* @param {string} filePath file path -* @returns {string} content -*/ -function cat(filePath) { // eslint-disable-line no-unused-vars - var FileClass = Java.type('java.io.File'); - return Bean("fileUtilities").getFileContent(new FileClass(filePath)); + * Returns the file content of the given file + * + * @param {string} filePath file path + * @returns {string} content + */ +// eslint-disable-next-line no-unused-vars +function cat(filePath) { + return fileUtilities.getFileContent(filePath); } /** -* copies file -* @param {string} source source -* @param {string} target target -* @returns {void} -*/ -function cp(source, target) { // eslint-disable-line no-unused-vars - var FileClass = Java.type('java.io.File'); - return Bean("fileUtilities").copy(new FileClass(source), new FileClass(target)); + * Copies the given source file to the target location + * + * @param {string} source Source file + * @param {string} target Target location + * @returns {void} + */ +// eslint-disable-next-line no-unused-vars +function cp(source, target) { + return fileUtilities.copy(source, target); } /** -* returns file size -* @param {string} filePath file path -* @returns {number} file size -*/ -function getFileSize(filePath) { // eslint-disable-line no-unused-vars - var FileClass = Java.type('java.io.File'); - return Bean("fileUtilities").getSize(new FileClass(filePath)); + * Returns the file size of the given file + * + * @param {string} filePath file path + * @returns {number} file size + */ +// eslint-disable-next-line no-unused-vars +function getFileSize(filePath) { + return fileUtilities.getSize(filePath); } /** -* returns file name -* @param {string} filePath file path -* @returns {string} file name -*/ -function fileName(filePath) { // eslint-disable-line no-unused-vars - var FileClass = Java.type('java.io.File'); - return new FileClass(filePath).getName(); + * Returns the file name of the given file + * + * @param {string} filePath file path + * @returns {string} file name + */ +// eslint-disable-next-line no-unused-vars +function fileName(filePath) { + return fileUtilities.getFileName(filePath); } /** -* creates link -* @param {string} target target -* @param {string} destination destination -* @returns {void} -*/ -function lns(target, destination) { // eslint-disable-line no-unused-vars - var FileClass = Java.type('java.io.File'); - return Bean("fileUtilities").createSymbolicLink(new FileClass(destination), new FileClass(target)); + * Creates a symbolic link + * + * @param {string} target target + * @param {string} link destination + * @returns {void} + */ +// eslint-disable-next-line no-unused-vars +function lns(target, link) { + return fileUtilities.createSymbolicLink(link, target); } /** -* removes file -* @param {string} filePath file path -* @returns {void} -*/ -function remove(filePath) { // eslint-disable-line no-unused-vars - var FileClass = Java.type('java.io.File'); - return Bean("fileUtilities").remove(new FileClass(filePath)); + * Removes the given file + * + * @param {string} filePath file path + * @returns {void} + */ +// eslint-disable-next-line no-unused-vars +function remove(filePath) { + return fileUtilities.remove(filePath); } /** -* creates empty file -* @param {string} filePath file path -* @returns {void} -*/ -function touch(filePath) { // eslint-disable-line no-unused-vars + * Creates the given file if it does not exist + * + * @param {string} filePath file path + * @returns {void} + */ +// eslint-disable-next-line no-unused-vars +function touch(filePath) { if (!fileExists(filePath)) { - var FileClass = Java.type('java.io.File'); - Bean("fileUtilities").writeToFile(new FileClass(filePath), ""); + fileUtilities.writeToFile(filePath, ""); } } /** -* writes content into file -* @param {string} filePath file path -* @param {string} content content which shall be written -* @returns {void} -*/ -function writeToFile(filePath, content) { // eslint-disable-line no-unused-vars - var FileClass = Java.type('java.io.File'); - Bean("fileUtilities").writeToFile(new FileClass(filePath), content); + * Writes the given content to the given file + * + * @param {string} filePath file path + * @param {string} content content which shall be written + * @returns {void} + */ +// eslint-disable-next-line no-unused-vars +function writeToFile(filePath, content) { + fileUtilities.writeToFile(filePath, content); } /** -* creates temporary file -* @param {string} extension file extension -* @returns {string} file path of created temporary file -*/ -function createTempFile(extension) { // eslint-disable-line no-unused-vars - var tmpFile = Bean("fileUtilities").createTmpFile(extension); - return tmpFile.getAbsolutePath(); + * Creates a new temporary file with the given file extension + * + * @param {string} extension file extension + * @returns {string} file path of created temporary file + */ +// eslint-disable-next-line no-unused-vars +function createTempFile(extension) { + return fileUtilities.createTmpFile(extension); } /** - * creates temporary directory + * Creates a new temporary temporary directory + * * @returns {string} file path of created temporary directory */ -function createTempDir() { // eslint-disable-line no-unused-vars - var tmpFile = Bean("fileUtilities").createTmpDir(); - return tmpFile.getAbsolutePath(); +// eslint-disable-next-line no-unused-vars +function createTempDir() { + return fileUtilities.createTmpDir(); } /** -* Checksum prototype -* @constructor -*/ -function Checksum() { - this._method = "SHA"; - this._checksumCalculator = Bean("checksumCalculator"); + * Sets the given file permissions + * + * @param {string} filePath file path + * @param {string} permissions file permissions (e.g. "r--r--r--") + * @returns {void} + */ +// eslint-disable-next-line no-unused-vars +function chmod(filePath, permissions) { + fileUtilities.chmod(filePath, permissions); } /** -* sets wizard -* @param {SetupWizard} wizard setup wizard -* @returns {Checksum} Checksum object -*/ -Checksum.prototype.wizard = function (wizard) { - this._wizard = wizard; - return this; -} + * Checksum + */ +// eslint-disable-next-line no-unused-vars +class Checksum { + constructor() { + this.checksumCalculator = Bean("checksumCalculator"); + this._method = "SHA"; + } -/** -* sets checksum algorithm -* @param {string} algorithm algorithm (e.g. "SHA") -* @returns {Checksum} Checksum object -*/ -Checksum.prototype.method = function (algorithm) { - this._method = algorithm; - return this; -} + /** + * Sets the setup wizard + * + * @param {SetupWizard} wizard The setup wizard + * @returns {Checksum} The Checksum object + */ + wizard(wizard) { + this._wizard = wizard; -/** -* sets file for which the checksum shall be computed -* @param {string} file file for which the checksum shall be computed -* @returns {Checksum} Checksum object -*/ -Checksum.prototype.of = function (file) { - this._file = file; - return this; -} + return this; + } -/** -* returns calculated checksum -* @returns {string} calculated checksum -*/ -Checksum.prototype.get = function () { - if (this._wizard) { - var progressBar = this._wizard.progressBar(tr("Checking file consistency...")); + /** + * Sets the used checksum algorithm + * + * @param {string} algorithm The used algorithm (e.g. "SHA") + * @returns {Checksum} The Checksum object + */ + method(algorithm) { + this._method = algorithm; + + return this; + } + + /** + * Sets the file for which the checksum shall be computed + * + * @param {string} file The file for which the checksum shall be computed + * @returns {Checksum} The Checksum object + */ + of(file) { + this._file = file; + + return this; } - return this._checksumCalculator.calculate(this._file, this._method, function (progressEntity) { - if (progressBar) { - progressBar.accept(progressEntity); + /** + * Calculates and returns the checksum for the previously set file + * + * @returns {string} The calculated checksum + */ + get() { + if (this._wizard) { + var progressBar = this._wizard.progressBar(tr("Checking file consistency...")); } - }); -} -/** -* sets file permissions -* @param {string} filePath file path -* @param {string} permissions file permissions (e.g. "r--r--r--") -* @returns {void} -*/ -function chmod(filePath, permissions) { // eslint-disable-line no-unused-vars - var permissionsObj = java.nio.file.attribute.PosixFilePermissions.fromString(permissions); - var filePathObj = java.nio.file.Paths.get(filePath); - java.nio.file.Files.setPosixFilePermissions(filePathObj, permissionsObj); + return this.checksumCalculator.calculate(this._file, this._method, progressEntity => { + if (progressBar) { + progressBar.accept(progressEntity); + } + }); + } }