From 5d094f8ab53e6a1c915be432304b953e9acaa34f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Bispo?= Date: Thu, 8 Aug 2024 16:39:19 +0100 Subject: [PATCH] [Lara-JS] Adds System.getCurrentFile() and .getCurrentFolder() --- Lara-JS/src-api/lara/System.ts | 80 +++++++++++++++++++++++++++++++++ LaraApi/src-lara/lara/System.js | 52 +++++++++++++++++++++ 2 files changed, 132 insertions(+) diff --git a/Lara-JS/src-api/lara/System.ts b/Lara-JS/src-api/lara/System.ts index 11a56130f..f63d693b0 100644 --- a/Lara-JS/src-api/lara/System.ts +++ b/Lara-JS/src-api/lara/System.ts @@ -117,4 +117,84 @@ export default class System { static getNumLogicalCores(): number { return JavaTypes.Runtime.getRuntime().availableProcessors(); } + + static getCurrentFile(): string | undefined { + return this.getCurrentFilePrivate(3); + } + + static getCurrentFolder(): string | undefined { + const filepath = this.getCurrentFilePrivate(3); + + if (filepath === undefined) { + return undefined; + } + + return Io.getPath(filepath).getParentFile().getAbsolutePath(); + } + + private static getCurrentFilePrivate(depth: number): string | undefined { + // Store originakl stack trace limit + const originalStackTraceLimit = Error.stackTraceLimit; + + // Set to the depth we want to go + Error.stackTraceLimit = depth; + + // Create an Error to capture the stack trace + const err = new Error(); + + // Restore original stack trace limit + Error.stackTraceLimit = originalStackTraceLimit; + + // Process the stack trace + const stackTrace = err.stack; + + // Return if no stack trace was obtained + if (stackTrace === undefined) { + return undefined; + } + + // Split the stack trace into lines + const stackLines = stackTrace.split("\n"); + + // The stack trace format is usually: + // at FunctionName (filePath:lineNumber) + // Go to the depth we are interested in + + let stackline; + let currentDepth = 0; + for (let i = 0; i < stackLines.length; i++) { + console.log("Line " + i + ": " + stackLines[i]); + const match = stackLines[i].match(/\(([^)]+)\)/); + if (match && match[1]) { + currentDepth++; + stackline = match[1]; + + if (currentDepth === depth) { + break; + } + } + } + + // Could not find a stack line at the required depth + if (stackline === undefined) { + return undefined; + } + + // Extract file path + const lastColonIndex = stackline.lastIndexOf(":"); + const filePathTemp = + lastColonIndex == -1 ? stackline : stackline.substring(0, lastColonIndex); + + console.log("Potential Path: " + filePathTemp); + + let file = Io.getPath(filePathTemp); + + if (!file.isAbsolute()) { + console.log("Base dir: " + __dirname); + file = Io.getPath(Io.getPath(__dirname), file.getPath()); + } + + const absolutePath = file.getAbsolutePath(); + return absolutePath; + } } diff --git a/LaraApi/src-lara/lara/System.js b/LaraApi/src-lara/lara/System.js index fd37c7ad1..27339b230 100644 --- a/LaraApi/src-lara/lara/System.js +++ b/LaraApi/src-lara/lara/System.js @@ -78,5 +78,57 @@ export default class System { static getNumLogicalCores() { return JavaTypes.Runtime.getRuntime().availableProcessors(); } + static getCurrentFile() { + return this.getCurrentFilePrivate(3); + } + static getCurrentFilePrivate(depth) { + // Store originakl stack trace limit + const originalStackTraceLimit = Error.stackTraceLimit; + // Set to the depth we want to go + Error.stackTraceLimit = depth; + // Create an Error to capture the stack trace + const err = new Error(); + // Restore original stack trace limit + Error.stackTraceLimit = originalStackTraceLimit; + // Process the stack trace + const stackTrace = err.stack; + // Return if no stack trace was obtained + if (stackTrace === undefined) { + return undefined; + } + // Split the stack trace into lines + const stackLines = stackTrace.split("\n"); + // The stack trace format is usually: + // at FunctionName (filePath:lineNumber) + // Go to the depth we are interested in + let stackline; + let currentDepth = 0; + for (let i = 0; i < stackLines.length; i++) { + console.log("Line " + i + ": " + stackLines[i]); + const match = stackLines[i].match(/\(([^)]+)\)/); + if (match && match[1]) { + currentDepth++; + stackline = match[1]; + if (currentDepth === depth) { + break; + } + } + } + // Could not find a stack line at the required depth + if (stackline === undefined) { + return undefined; + } + // Extract file path + const lastColonIndex = stackline.lastIndexOf(":"); + const filePathTemp = lastColonIndex == -1 ? stackline : stackline.substring(0, lastColonIndex); + console.log("Potential Path: " + filePathTemp); + let file = Io.getPath(filePathTemp); + if (!file.isAbsolute()) { + console.log("Base dir: " + __dirname); + file = Io.getPath(Io.getPath(__dirname), file.getPath()); + } + const absolutePath = file.getAbsolutePath(); + return absolutePath; + } } //# sourceMappingURL=System.js.map \ No newline at end of file