-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathrun_editor.js
246 lines (192 loc) · 8.62 KB
/
run_editor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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:',
// (only, when not using JetBrains Toolbox) Set to folder name, where PhpStorm was installed to (e.g. 'PhpStorm')
folder_name: '<phpstorm_folder_name>',
// (only, when not using JetBrains Toolbox) Set to window title (only text after dash sign), that you see, when switching to running PhpStorm instance
window_title: '<phpstorm_window_title>',
// 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'
// Leave null to use the first PHPStorm version in Toolbox
toolbox_update_channel_dir: null,
// Set to PhpStorm shell script (filename ends with "*.cmd") from the "C:\Users\%username%\AppData\Local\JetBrains\Toolbox\scripts" directory.
toolbox_shell_script: 'PhpStorm.cmd'
};
// flag to active Jetbrain Toolbox configuration
settings.toolBoxActive = isToolboxInstalled();
// 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 = '';
// add JSON support
includeFile('json2.js');
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, '\\'),
editor = '"' + getPhpStormCommandPath() + '"';
if (settings.projects_basepath !== '' && settings.projects_path_alias !== '') {
file = file.replace(new RegExp('^' + settings.projects_basepath), settings.projects_path_alias);
}
// If only a folder is specified, don't look for a project file or line number
var isFolder = file_system.FolderExists(search_path);
var isFile = file_system.FileExists(search_path);
if (isFolder) {
project = search_path;
} else if (isFile) {
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%"';
}
if (match[3]) {
editor += ' --line %line% "%file%"';
} else if (isFile) {
editor += ' "%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() {
var shell = new ActiveXObject('WScript.Shell'),
appDataLocal = shell.ExpandEnvironmentStrings("%localappdata%"),
toolboxDirectory = appDataLocal + '\\JetBrains\\Toolbox\\apps\\PhpStorm';
return (new ActiveXObject('Scripting.FileSystemObject')).FolderExists(toolboxDirectory);
}
function getPhpStormCommandPath() {
var shell = new ActiveXObject('WScript.Shell'),
appDataLocal = shell.ExpandEnvironmentStrings("%localappdata%"),
toolboxShellScript = getToolboxShellScript(appDataLocal);
if (toolboxShellScript !== undefined) {
return toolboxShellScript;
}
var settingsStateFile = appDataLocal + '\\JetBrains\\Toolbox\\state.json',
defaultCommandPath = settings.disk_letter + '\\' + ( settings.x64 ? 'Program Files' : 'Program Files (x86)' ) + '\\JetBrains\\' + settings.folder_name + ( settings.x64 ? '\\bin\\phpstorm64.exe' : '\\bin\\phpstorm.exe' );
try {
var fileStream = (new ActiveXObject('Scripting.FileSystemObject')).OpenTextFile(settingsStateFile, 1, false);
} catch (error) {
return defaultCommandPath;
}
var state = JSON.parse(fileStream.ReadAll());
fileStream.Close();
var tools = state.tools || [];
for (var i = 0; i < tools.length; i++) {
if (tools[i].toolId == 'PhpStorm') {
return tools[i].installLocation + '\\' + tools[i].launchCommand.replace(/\//g, '\\');
}
}
return defaultCommandPath;
}
function getFavoritePhpStormChannel() {
var shell = new ActiveXObject('WScript.Shell'),
appDataLocal = shell.ExpandEnvironmentStrings("%localappdata%"),
settingsFile = appDataLocal + '\\JetBrains\\Toolbox\\.settings.json';
try {
var fileStream = (new ActiveXObject('Scripting.FileSystemObject')).OpenTextFile(settingsFile, 1, false);
} catch (error) {
return 'ch-0';
}
var settings = JSON.parse(fileStream.ReadAll());
fileStream.Close()
var apps = (settings.ordering || {}).local || [];
for (var i = 0; i < apps.length; i++) {
if (apps[i].application_id == 'PhpStorm') {
return apps[i].channel_id;
}
}
return 'ch-0'
}
function configureToolboxSettings(settings) {
var shell = new ActiveXObject('WScript.Shell'),
appDataLocal = shell.ExpandEnvironmentStrings("%localappdata%"),
toolboxShellScript = getToolboxShellScript(appDataLocal);
// The JetBrains Toolbox Shell Script is clever enough to autofocus PhpStorm window after opening a file in it
if (toolboxShellScript !== undefined) {
return;
}
// Detect Toolbox PHPStorm top channel
if (settings.toolbox_update_channel_dir == null) {
settings.toolbox_update_channel_dir = getFavoritePhpStormChannel();
}
var toolboxDirectory = appDataLocal + '\\JetBrains\\Toolbox\\apps\\PhpStorm\\' + settings.toolbox_update_channel_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 productVersion = JSON.parse(versionFile.ReadAll());
settings.window_title = 'PhpStorm ' + productVersion.version;
editor = '"' + toolboxDirectory + settings.folder_name + '\\' + productVersion.launch[ 0 ].launcherPath.replace(/\//g, '\\') + '"';
}
function getToolboxShellScript(appDataLocal) {
var shellScript = appDataLocal + '\\JetBrains\\Toolbox\\scripts\\' + settings.toolbox_shell_script;
if ((new ActiveXObject('Scripting.FileSystemObject')).FileExists(shellScript)) {
return shellScript;
}
return undefined;
}
function includeFile (filename) {
var fso = new ActiveXObject ("Scripting.FileSystemObject");
var currentDirectory = WScript.ScriptFullName.substring(0, WScript.ScriptFullName.lastIndexOf('\\'));
var fileStream = fso.openTextFile (currentDirectory + '\\' + filename);
var fileData = fileStream.readAll();
fileStream.Close();
eval(fileData);
}