|
| 1 | +// Copyright 2024 The MathWorks, Inc. |
| 2 | + |
| 3 | +import { promises as fs } from 'fs'; |
| 4 | +import { findExecutableOnPath, resolveSymlink } from '../utils/FsUtils'; |
| 5 | +import * as path from 'path'; |
| 6 | +import * as xml2js from 'xml2js'; |
| 7 | +import { VersionInfoXML } from './types'; |
| 8 | +import Logger from '../logging/Logger'; |
| 9 | + |
| 10 | +const VERSION_INFO_FILENAME = 'VersionInfo.xml'; |
| 11 | + |
| 12 | +export const MWI_AUTH_TOKEN_NAME_FOR_HTTP = 'mwi-auth-token' |
| 13 | + |
| 14 | +export const MWI_AUTH_TOKEN_LENGTH = 32 |
| 15 | + |
| 16 | +export const MWI_ENABLE_TOKEN_AUTH = true |
| 17 | + |
| 18 | +export const MWI_LICENSING_SESSION_COOKIE_NAME = 'matlab-licensing-session' |
| 19 | + |
| 20 | +let installPath: string | null = null; |
| 21 | +let matlabVersion: string | null = null; |
| 22 | +export const staticFolderPath: string = path.join(__dirname, 'licensing', 'static') |
| 23 | + |
| 24 | +/** |
| 25 | + * Sets the MATLAB install path. Is called when: |
| 26 | + * 1) The LSP is initialzed |
| 27 | + * & |
| 28 | + * 2) The installPath setting changes by its config change handler |
| 29 | + * @param path - The MATLAB install path |
| 30 | + */ |
| 31 | +export function setInstallPath (path: string): void { |
| 32 | + installPath = path; |
| 33 | + |
| 34 | + // When installPath changes, update MATLAB version |
| 35 | + getMatlabVersionFromInstallPath(installPath).then((version) => { |
| 36 | + matlabVersion = version; |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Sets the MATLAB version. This function is called to update the current |
| 42 | + * MATLAB version in the application state. |
| 43 | + * |
| 44 | + * @param version - The MATLAB version to be set |
| 45 | + */ |
| 46 | +export function setMatlabVersion (version: string): void { |
| 47 | + matlabVersion = version |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Gets the MATLAB version |
| 52 | + * @returns {Promise<string | null>} The MATLAB version or null if it cannot be determined |
| 53 | + */ |
| 54 | +export async function getMatlabVersion (): Promise<string | null> { |
| 55 | + // If MATLAB version was already determined (either by this function or the setInstallPath function), return it directly. |
| 56 | + if (matlabVersion) { |
| 57 | + return matlabVersion |
| 58 | + } else { |
| 59 | + const matlabExecutableOnPath = await findExecutableOnPath('matlab') |
| 60 | + // If there's no matlab executable on system PATH return null |
| 61 | + if (!matlabExecutableOnPath) { |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + const absoluteMatlabPath = await resolveSymlink(matlabExecutableOnPath); |
| 66 | + const matlabRoot = path.resolve(absoluteMatlabPath, '..', '..') |
| 67 | + |
| 68 | + // Update matlabVersion variable before returning to avoid recomputation. |
| 69 | + matlabVersion = await getMatlabVersionFromInstallPath(matlabRoot) |
| 70 | + return matlabVersion |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Retrieves the MATLAB version from the installation path. |
| 76 | + * |
| 77 | + * @param pathToMatlabRoot - The path to the MATLAB ROOT. |
| 78 | + * @returns {Promise<string|null>} A promise that resolves to the MATLAB version as a string, or null if an error occurs. |
| 79 | + */ |
| 80 | +async function getMatlabVersionFromInstallPath (pathToMatlabRoot: string) { |
| 81 | + const versionInfoPath = path.join(pathToMatlabRoot, VERSION_INFO_FILENAME); |
| 82 | + |
| 83 | + try { |
| 84 | + const fileContent = await fs.readFile(versionInfoPath, { encoding: 'utf-8' }) |
| 85 | + const xmlData = (await xml2js.parseStringPromise(fileContent)) as VersionInfoXML |
| 86 | + const versionInfo = xmlData.MathWorks_version_info.release[0] |
| 87 | + return versionInfo |
| 88 | + } catch (error) { |
| 89 | + Logger.error(`Failed to read version info file at path:${versionInfoPath} with error:${error}`) |
| 90 | + return null; |
| 91 | + } |
| 92 | +} |
0 commit comments