Skip to content

Instantly share code, notes, and snippets.

@KamilPesek
Created June 3, 2022 06:40
Show Gist options
  • Save KamilPesek/e0447004a0cd75534836eca0ef242cbd to your computer and use it in GitHub Desktop.
Save KamilPesek/e0447004a0cd75534836eca0ef242cbd to your computer and use it in GitHub Desktop.
var settings = {
// Set to 'true' (without quotes) if run on Windows 64bit. Set to 'false' (without quotes) otherwise.
x64: true,
// Set to disk letter, where PhpStorm was installed to (e.g. C:)
disk_letter: 'C:',
// Set to folder name, where PhpStorm was installed to (e.g. 'PhpStorm')
folder_name: 'PhpStorm 2022.1.2',
// Set to window title (only text after dash sign), that you see, when switching to running PhpStorm instance
window_title: 'PhpStorm 2022.1.2',
// In case your file is mapped via a network share and paths do not match.
// eg. /var/www will can replaced with Y:/
projects_basepath: '',
projects_path_alias: '',
// PhpStorm directory name in Toolbox directory
// eg. for C:\Users\%username%\AppData\Local\JetBrains\Toolbox\apps\PhpStorm\ch-1 use 'ch-1'
toolbox_phpstorm_dir: 'ch-0'
};
// flag to active Jetbrain Toolbox configuration
settings.toolBoxActive = isToolboxInstalled(settings);
// don't change anything below this line, unless you know what you're doing
var url = WScript.Arguments(0),
match = /^phpstorm:\/\/open\/?\?(url=file:\/\/|file=)(.+)&line=(\d+)$/.exec(url),
project = '',
editor = '"' + settings.disk_letter + '\\' + ( settings.x64 ? 'Program Files' : 'Program Files (x86)' ) + '\\JetBrains\\' + settings.folder_name + ( settings.x64 ? '\\bin\\phpstorm64.exe' : '\\bin\\phpstorm.exe' ) + '"';
if (settings.toolBoxActive) {
configureToolboxSettings(settings);
}
if (match) {
var shell = new ActiveXObject('WScript.Shell'),
file_system = new ActiveXObject('Scripting.FileSystemObject'),
file = decodeURIComponent(match[ 2 ]).replace(/\+/g, ' '),
search_path = file.replace(/\//g, '\\');
if (settings.projects_basepath !== '' && settings.projects_path_alias !== '') {
file = file.replace(new RegExp('^' + settings.projects_basepath), settings.projects_path_alias);
}
while (search_path.lastIndexOf('\\') !== -1) {
search_path = search_path.substring(0, search_path.lastIndexOf('\\'));
if (file_system.FileExists(search_path + '\\.idea\\.name')) {
project = search_path;
break;
}
}
if (project !== '') {
editor += ' "%project%"';
}
editor += ' --line %line% "%file%"';
var command = editor.replace(/%line%/g, match[ 3 ])
.replace(/%file%/g, file)
.replace(/%project%/g, project)
.replace(/\//g, '\\');
shell.Exec(command);
shell.AppActivate(settings.window_title);
}
function isToolboxInstalled(settings) {
var shell = new ActiveXObject('WScript.Shell'),
appDataLocal = shell.ExpandEnvironmentStrings("%localappdata%"),
toolboxDirectory = appDataLocal + '\\JetBrains\\Toolbox\\apps\\PhpStorm\\' + settings.toolbox_phpstorm_dir;
return (new ActiveXObject('Scripting.FileSystemObject')).FolderExists(toolboxDirectory);
}
function configureToolboxSettings(settings) {
var shell = new ActiveXObject('WScript.Shell'),
appDataLocal = shell.ExpandEnvironmentStrings("%localappdata%"),
toolboxDirectory = appDataLocal + '\\JetBrains\\Toolbox\\apps\\PhpStorm\\' + settings.toolbox_phpstorm_dir + '\\';
// Reference the FileSystemObject
var fso = new ActiveXObject('Scripting.FileSystemObject');
// Reference the Text directory
var folder = fso.GetFolder(toolboxDirectory);
// Reference the File collection of the Text directory
var fileCollection = folder.SubFolders;
var maxMajor = 0,
maxMinor = 0,
maxPatch = 0,
maxVersionFolder = "";
// Traverse through the fileCollection using the FOR loop
// read the maximum version from toolbox filesystem
for (var objEnum = new Enumerator(fileCollection); !objEnum.atEnd(); objEnum.moveNext()) {
var folderObject = ( objEnum.item() );
if (folderObject.Name.lastIndexOf('plugins') !== -1) {
continue;
}
var versionMatch = /(\d+)\.(\d+)\.(\d+)/.exec(folderObject.Name),
major = parseInt(versionMatch[ 1 ]),
minor = parseInt(versionMatch[ 2 ]),
patch = parseInt(versionMatch[ 3 ]);
if (maxMajor === 0 || maxMajor <= major) {
if (maxMajor < major) {
maxMinor = 0;
maxPatch = 0;
}
maxMajor = major;
if (maxMinor === 0 || maxMinor <= minor) {
if (maxMinor < minor) {
maxPatch = 0;
}
maxMinor = minor;
if (maxPatch === 0 || maxPatch <= patch) {
maxPatch = patch;
maxVersionFolder = folderObject.Name;
}
}
}
}
settings.folder_name = maxVersionFolder;
// read version name and product name from product-info.json
var versionFile = fso.OpenTextFile(toolboxDirectory + settings.folder_name + "\\product-info.json", 1, true);
var content = versionFile.ReadAll();
eval('var productVersion = ' + content + ';');
settings.window_title = 'PhpStorm ' + productVersion.version;
editor = '"' + toolboxDirectory + settings.folder_name + '\\' + productVersion.launch[ 0 ].launcherPath.replace(/\//g, '\\') + '"';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment