-
Notifications
You must be signed in to change notification settings - Fork 0
/
projectbackup.js
366 lines (318 loc) · 10.7 KB
/
projectbackup.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* Project Backup
*
* Backup Project files when changing between projects or exiting editor.
* Uses /-Zip for creating archives (Must be installed on system)
* <https://7-zip.org/>
*
* Also uses SnoreToast for generating Windoes Toast Notifications.
* <https://github.com/KDE/snoretoast>
*
* @category WeBuilder Plugin
* @package Project Backup
* @author Peter Klein <[email protected]>
* @copyright 2018
* @license http://www.freebsd.org/copyright/license.html BSD License
* @version 1.0
*/
/**
* [CLASS/FUNCTION INDEX of SCRIPT]
*
* 55 function ReadProjectBackupFile()
* 96 function CopyProjectBackupFile(Sender)
* 115 function EditProjectBackupFile(Sender)
* 134 function CreateProjectBackup(Sender)
* 145 function CreateArchive(mode)
* 206 function Quote(str)
* 217 function SnoreToast(message)
* 231 function ReplaceMarkers(str)
* 278 function CopyFile(source, destination, overwrite)
* 293 function OnBeforeSelectProject(Sender)
* 302 function OnReady()
* 311 function OnExit()
* 323 function OnDisabled()
* 332 function OnEnabled()
* 341 function OnInstalled()
*
* TOTAL FUNCTIONS: 15
* (This index is automatically created/updated by the WeBuilder plugin "DocBlock Comments")
*
*/
/**
* Flag for detecting if exit signal is caused by disabling plugin or exiting editor
*
* @var pluginAction
*/
var pluginAction = false;
/**
* Read and process ProjectBackup.ini from current project root
*
* @return void
*/
function ReadProjectBackupFile() {
var projectPath = Script.ProjectSettings.SelectedProjectRoot;
var projectBackupFile = projectPath + "\\ProjectBackup.ini";
// Silent exit if no ProjectBackup.ini is present
if (FileExists(projectBackupFile) == false) return;
// Read the ProjectBackup.ini file
var projectsIni = new TIniFile(projectBackupFile);
var section = "ProjectBackup";
if (!projectsIni.SectionExists(section)) {
SnoreToast("Error: Invalid ProjectBackup.ini format");
delete projectsIni;
return;
}
var backupPath = projectsIni.ReadString(section, "BackupPath", "");
var backupName = ReplaceMarkers(projectsIni.ReadString(section, "BackupName", ""));
var backupFormat = projectsIni.ReadString(section, "BackupFormat", "zip");
delete projectsIni;
if (DirectoryExists(backupPath) == false) {
SnoreToast("Error: Invalid backup path defined in ProjectBackup.ini");
return;
}
if (backupName == "") {
SnoreToast("Error: No backup name defined in ProjectBackup.ini");
return;
}
return backupPath + backupName + "." + backupFormat;
}
/**
* Copy default ProjectBackup.ini to current project root
*
* @param object Sender
*
* @return void
*/
function CopyProjectBackupFile(Sender) {
var projectPath = Script.ProjectSettings.SelectedProjectRoot;
if (FileExists(projectPath + "\\ProjectBackup.ini") == true) {
if (Confirm("A ProjectBackup.ini file is already present. Overwrite it?") == false) return;
}
CopyFile(Script.Path + "ProjectBackup.ini", projectPath + "\\", true);
}
/**
* Open/Edit ProjectBackup.ini from current project root
*
* @param object Sender
*
* @return void
*/
function EditProjectBackupFile(Sender) {
var projectBackupFile = Script.ProjectSettings.SelectedProjectRoot + "\\ProjectBackup.ini";
if (FileExists(projectBackupFile) == false) {
if (Confirm("No ProjectBackup.ini file found. Create default?") == false) return;
CopyProjectBackupFile(Sender);
}
Documents.OpenDocument(projectBackupFile);
}
/**
* Manually trigger creation of Project Backup
*
* @param object Sender
*
* @return void
*/
function CreateProjectBackup(Sender) {
CreateArchive(true);
}
/**
* Create backup archive using 7-Zip commandline version
*
* @param boolean mode true if manually triggered
*
* @return void
*/
function CreateArchive(mode) {
var bMode = StrToBool(Script.ReadSetting("AutoBackup", "1"));
// Silent exit if not manually triggered when AutoBackup is false
if ((bMode == false) && (mode != true)) return;
// 7zip command line
var zPath = Script.ReadSetting("Location of 7z", "");
if (!FileExists(zPath)) {
SnoreToast("Error: 7z.exe not found!");
return;
}
zPath = Quote(zPath);
// Get the backup archive path/name based on info from ProjectBackup.ini file
var archiveName = Quote(ReadProjectBackupFile());
var spRoot = Quote(Script.ProjectSettings.SelectedProjectRoot + "\\");
var spExcludeFilters = Script.ProjectSettings.SelectedProjectExcludeFilters;
var spFileTypes = Script.ProjectSettings.SelectedProjectFileTypes;
// Build exclude part of 7-Zip switches
var exclude = "";
var SL = new TStringList;
SL.Text = spExcludeFilters;
for (var i=0; i< SL.Count; i++) {
if (RegexMatch(SL[i], "\\\\(\\*\\.)?\\*$", false) != "") {
// Folder - recursive
exclude += " -xr!" + Quote(RegexReplace(SL[i], "\\\\(\\*\\.)?\\*$", "", false));
}
else {
// File
exclude += " -x!" + Quote(SL[i]);
}
}
delete SL;
if (exclude != "") {
Quote(RegexReplace(exclude, "\"","\"\"",false));
}
// Build Include/FileType list
//var include = RegexReplace(spFileTypes, ";", " ", false);
//include = RegexReplace(include, "\*\.\*", "*", false);
var include = "*";
var cmd = Quote(Script.Path + "projectbackup.cmd") + " " + spRoot + " " + zPath + " " + archiveName + " " + Quote(include) + " " + exclude;
var WSO = CreateOleObject("WScript.Shell");
//WSO.run("cmd.exe /K \""+ cmd + "\"", 1);
WSO.run("cmd.exe /C \""+ cmd + "\"", 0);
}
/**
* Wrap doublequotes around string
*
* @param string str
*
* @return string
*/
function Quote(str) {
return "\"" + str + "\"";
}
/**
* Sends Windows Toast Notification using SnoreToast executable
* https://github.com/KDE/snoretoast
*
* @param string message
*
* @return void
*/
function SnoreToast(message) {
var snoreToast = Quote(Script.Path + "SnoreToastGui.exe");
var title = Quote("Webuilder Project Backup");
var image = Quote(Script.Path + "webuilder.png");
ExecuteCommand(snoreToast + " -t " + title + " -m " + Quote( message) + " -p " + image, res);
}
/**
* Replace markers with values
*
* @param string str String containing markers
*
* @return string
*/
function ReplaceMarkers(str) {
var dt = Now;
// Project name
str = Replace(str, "%p", Script.ProjectSettings.SelectedProjectName);
// Day
str = Replace(str, "%d", FormatDateTime("dd", dt));
// Month
str = Replace(str, "%m", FormatDateTime("mm", dt));
// Month - Text format
str = Replace(str, "%M", FormatDateTime("mmm", dt));
// Year
str = Replace(str, "%y", FormatDateTime("yy", dt));
// Year - Long format
str = Replace(str, "%Y", FormatDateTime("yyyy", dt));
// Hours
str = Replace(str, "%h", FormatDateTime("hh", dt));
// Minutes
str = Replace(str, "%n", FormatDateTime("nn", dt));
// Seconds
str = Replace(str, "%s", FormatDateTime("ss", dt));
return str;
}
/**
* Copy one or more files from one location (the source) to another (destination).
*
* If source contains wildcard characters or destination ends with a path separator (\),
* it is assumed that destination is an existing folder in which to copy matching files.
* Otherwise, destination is assumed to be the name of a file to create. In either case,
* three things can happen when an individual file is copied.
* If destination does not exist, source gets copied. This is the usual case.
* If destination is an existing file, an error occurs if overwrite is false. Otherwise,
* an attempt is made to copy source over the existing file.
* If destination is a directory, an error occurs.
*
* @param string source location of one or more files to be copied
* @param string destination location to where one or more files in source will be copied
* @param bool overwrite True allows the overwriting of existing files in the destination
*
* @return void
*/
function CopyFile(source, destination, overwrite) {
var FSO = CreateOleObject("Scripting.FileSystemObject");
if (FSO.FileExists(source)) {
FSO.CopyFile(source, destination, overwrite);
}
}
/**
* project_before_select callback function.
* Fired after a new project have been selected, but before old project is unloaded.
*
* @param object Sender
*
* @return void
*/
function OnBeforeSelectProject(Sender) {
CreateArchive(false);
}
/**
* Signal triggered when plugin/editor is loaded
*
* @return void
*/
function OnReady() {
if (pluginAction) pluginAction = false;
}
/**
* Signal triggered when exiting plugin/editor
*
* @return void
*/
function OnExit() {
if (pluginAction) pluginAction = false;
else {
CreateArchive(false);
}
}
/**
* Signal triggered when plugin is disabled through Plugin Manager.
*
* @return void
*/
function OnDisabled() {
pluginAction = true;
}
/**
* Signal triggered when plugin is enabled through Plugin Manager.
*
* @return void
*/
function OnEnabled() {
pluginAction = true;
}
/**
* Signal triggered when plugin is installed through Plugin Manager.
*
* @return void
*/
function OnInstalled() {
Alert("Project Backup 1.0 by Peter Klein installed sucessfully!");
}
// Signals for plugin setup
Script.ConnectSignal("installed", "OnInstalled");
// Signals to detect change of Project etc.
Script.ConnectSignal("project_before_select", "OnBeforeSelectProject");
Script.ConnectSignal("ready", "OnReady");
Script.ConnectSignal("exit", "OnExit");
Script.ConnectSignal("disabled", "OnDisabled");
Script.ConnectSignal("enabled", "OnEnabled");
// Action for displaying GUI
var bmp = new TBitmap, act;
LoadFileToBitmap(Script.Path + "project-backup-icon1.png", bmp);
act = Script.RegisterAction("Project Backup", "Create Project Backup", "", "CreateProjectBackup");
Actions.SetIcon(act, bmp);
LoadFileToBitmap(Script.Path + "project-backup-icon2.png", bmp);
act = Script.RegisterAction("Project Backup", "Copy default \"ProjectBackup.ini\" to project root", "", "CopyProjectBackupFile");
Actions.SetIcon(act, bmp);
LoadFileToBitmap(Script.Path + "project-backup-icon3.png", bmp);
act = Script.RegisterAction("Project Backup", "Edit \"ProjectBackup.ini\" from project root", "", "EditProjectBackupFile");
Actions.SetIcon(act, bmp);
delete bmp;