/
backupVersion.js
255 lines (235 loc) · 7.76 KB
/
backupVersion.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
// https://akelpad.sourceforge.net/forum/viewtopic.php?p=10791#p10791
// https://infocatcher.ucoz.net/js/akelpad_scripts/backupVersion.js
// https://github.com/Infocatcher/AkelPad_scripts/blob/master/backupVersion.js
// (c) Infocatcher 2011-2012, 2014-2015
// Version: 0.1.5 - 2015-01-12
// Author: Infocatcher
//===================
//// Tries find file version and copy current file to the same directory
// file.js -> file-%version%.js
// file.user.js -> file-%version%.user.js
// Arguments:
// -forceDate=true - force use last modification date
// -dateType=0 - yyyy-mm-dd (see dateToString())
// =1 - yyyy-mm-dd_hh-mm
// =2 - yyyy-mm-dd_hh-mm-ss
// -openBackup=true - open created backup file
// -warnings=false - skip "Can't detect file version!" warning, just ask for file name
// Usage:
// Call("Scripts::Main", 1, "backupVersion.js")
// Call("Scripts::Main", 1, "backupVersion.js", "-forceDate=true -dateType=1")
//===================
//== Settings begin
var getVersionPattern = /(^|[^a-z])(version|ver\.?|v\.?)[ \t]*[-:=|]?[ \t]*(\S+)/i;
var versionBracketsNumber = 3; // We don't use (?:something) for backward compatibility with old JScript
var testVersionPattern = /\d/; // Version string must contain number
var linesLimit = 20; // Check only first 20 lines of file
var addVersionPattern = /(\.user)?(\.[^.\s_-]+)?$/; // {fileName}{versionSeparator}{version}{addVersionPattern}
function getVersionSeparator(version) {
return /-/.test(version) ? "_" : "-";
}
var dateTypesCount = 3;
function dateToString(date, type) {
function pad(n) {
return n < 10 ? "0" + n : n;
}
var ret = date.getFullYear() + "-" + pad(date.getMonth() + 1) + "-" + pad(date.getDate());
if(type == 0)
return ret;
ret += "_" + pad(date.getHours()) + "-" + pad(date.getMinutes());
if(type == 1)
return ret;
ret += "-" + pad(date.getSeconds());
return ret;
}
function trimVersion(version) {
return version
.replace(/^v\.?\s*/i, "")
.replace(/[.,]+$/, "");
}
//== Settings end
function _localize(s) {
var strings = {
"Save file first!": {
ru: "Сначала сохраните файл!"
},
"Can't detect file version!": {
ru: "Не удалось определить версию файла!"
},
"Can't use original file name!": {
ru: "Нельзя использовать имя исходного файла!"
},
"File “%S” already exists!\nOverwrite?": {
ru: "Файл «%S» уже существует!\nПерезаписать?"
},
"Can't copy current file to “%F”\nError:\n%E": {
ru: "Не удалось копировать текущий файл в «%F»\nОшибка:\n%E"
},
"Backup file was succefully created:\n%S": {
ru: "Файл резервной копии успешно создан:\n%S"
},
"Name of backup file:": {
ru: "Имя файла резервной копии:"
}
};
var lng = "en";
switch(AkelPad.GetLangId(1 /*LANGID_PRIMARY*/)) {
case 0x19: lng = "ru";
}
_localize = function(s) {
return strings[s] && strings[s][lng] || s;
};
return _localize(s);
}
var hMainWnd = AkelPad.GetMainWnd();
if(!hMainWnd)
WScript.Quit();
var hWndEdit = AkelPad.GetEditWnd();
var oSys = AkelPad.SystemFunction();
var fso = new ActiveXObject("Scripting.FileSystemObject");
var curPath = AkelPad.GetEditFile(0);
var dialogTitle = WScript.ScriptName.replace(/^[!-\-_]+/, "");
dialogTitle = dialogTitle.charAt(0).toUpperCase() + dialogTitle.substr(1);
if(!curPath) {
AkelPad.MessageBox(hMainWnd, _localize("Save file first!"), dialogTitle, 48 /*MB_ICONEXCLAMATION*/);
WScript.Quit();
}
copyFile();
function copyFile() {
var forceDate = AkelPad.GetArgValue("forceDate", false);
var curName = fso.GetFileName(curPath);
var curDir = fso.GetParentFolderName(curPath);
var newName;
var overwrite = false;
var exists = function(name) {
var path = curDir + "\\" + name;
//return fso.FileExists(path) || fso.FolderExists(path);
return oSys.Call("kernel32::GetFileAttributes" + _TCHAR, path) != -1;
};
if(!forceDate)
var version = getVersion();
if(version)
newName = addVersion(curName, version);
else {
var dateType = AkelPad.GetArgValue("dateType", 0);
var askName = curName;
var lastMod = new Date(fso.GetFile(curPath).DateLastModified);
for(var i = dateType; i < dateTypesCount; ++i) {
var lastModStr = dateToString(lastMod, i);
var testName = addVersion(curName, lastModStr);
if(!exists(testName)) {
askName = testName;
break;
}
}
var forceAsk = i > dateType;
if(!forceDate && AkelPad.GetArgValue("warnings", true))
AkelPad.MessageBox(hMainWnd, _localize("Can't detect file version!"), dialogTitle, 48 /*MB_ICONEXCLAMATION*/);
newName = !forceDate || forceAsk
? askFileName(testName || askName)
: askName;
if(!newName)
return;
}
for(;;) {
if(newName == curName) {
AkelPad.MessageBox(
hMainWnd,
_localize("Can't use original file name!"),
dialogTitle,
48 /*MB_ICONEXCLAMATION*/
);
}
else {
if(!exists(newName) && _copy(curPath, curDir, newName, overwrite))
break;
var btn = AkelPad.MessageBox(
hMainWnd,
_localize("File “%S” already exists!\nOverwrite?").replace("%S", newName),
dialogTitle,
35 /*MB_YESNOCANCEL|MB_ICONQUESTION*/
);
if(btn == 2 /*IDCANCEL*/)
return;
overwrite = btn == 6 /*IDYES*/;
if(overwrite && _copy(curPath, curDir, newName, overwrite))
break;
}
newName = askFileName(newName);
if(!newName)
return;
}
}
function askFileName(fileName) {
return AkelPad.InputBox(hMainWnd, dialogTitle, _localize("Name of backup file:"), fileName);
}
function _copy(curPath, curDir, newName, overwrite) {
try {
fso.CopyFile(curPath, curDir + "\\" + newName, overwrite);
if(AkelPad.GetArgValue("openBackup", false)) {
var lpFrame = AkelPad.SendMessage(hMainWnd, 1288 /*AKD_FRAMEFIND*/, 1 /*FWF_CURRENT*/, 0);
AkelPad.OpenFile(curDir + "\\" + newName, 0, AkelPad.GetEditCodePage(hWndEdit), AkelPad.GetEditBOM(hWndEdit));
lpFrame && AkelPad.SendMessage(hMainWnd, 1285 /*AKD_FRAMEACTIVATE*/, 0, lpFrame);
}
}
catch(e) {
AkelPad.MessageBox(
hMainWnd,
_localize("Can't copy current file to “%F”\nError:\n%E")
.replace("%F", newName)
.replace("%E", e.message || e),
dialogTitle,
16 /*MB_ICONQUESTION*/
);
return false;
}
//if(overwrite)
// return true;
new ActiveXObject("WScript.Shell").Popup(
_localize("Backup file was succefully created:\n%S").replace("%S", newName),
2, // Autoclose after 2 seconds
dialogTitle,
64 /*MB_ICONINFORMATION*/
);
return true;
}
function getVersion() {
var version = "";
var ww = AkelPad.SendMessage(hWndEdit, 3241 /*AEM_GETWORDWRAP*/, 0, 0);
if(ww) {
setRedraw(hWndEdit, false);
AkelPad.Command(4209 /*IDM_VIEW_WORDWRAP*/);
}
var lineStart = 0;
var line = 0;
var linesCount = AkelPad.SendMessage(hWndEdit, 3129 /*AEM_GETLINENUMBER*/, 0 /*AEGL_LINECOUNT*/, 0);
var maxLine = Math.min(linesLimit, linesCount) - 1;
for(;;) {
var lineLength = AkelPad.SendMessage(hWndEdit, 193 /*EM_LINELENGTH*/, lineStart, 0);
var lineEnd = lineStart + lineLength;
var lineText = AkelPad.GetTextRange(lineStart, lineEnd);
lineText = lineText.replace(/(http|ftp)s?:\/\S+/, "");
if(getVersionPattern.test(lineText)) {
var tmp = RegExp["$" + versionBracketsNumber];
if(testVersionPattern.test(tmp)) {
version = trimVersion(tmp);
break;
}
}
lineStart = lineEnd + 1;
if(++line > maxLine)
break;
}
if(ww) {
AkelPad.Command(4209 /*IDM_VIEW_WORDWRAP*/);
setRedraw(hWndEdit, true);
}
return version;
}
function addVersion(name, version) {
return name.replace(addVersionPattern, getVersionSeparator(version) + version + "$&");
}
function setRedraw(hWnd, bRedraw) {
AkelPad.SendMessage(hWnd, 11 /*WM_SETREDRAW*/, bRedraw, 0);
bRedraw && oSys.Call("user32::InvalidateRect", hWnd, 0, true);
}