File: stardict-prepare-installer.js

package info (click to toggle)
stardict 3.0.6-0.2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 28,072 kB
  • sloc: cpp: 74,453; xml: 20,424; sh: 16,098; ansic: 3,883; python: 1,086; makefile: 1,069; php: 308; perl: 223
file content (532 lines) | stat: -rw-r--r-- 16,613 bytes parent folder | download
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/* prepares StarDict installer
To run the script, execute the command in Windows Console:
cscript //nologo stardict-prepare-installer.js
this script must run in the root StarDict directory
You may need to tweak the options below.
*/

// options
var InstallDirMustNotExist = false;
var VSConfig = "release"; // release or debug
var UnixDistDir = "stardict-unix-dist";
var Portable = false;
var MakeNSISExe = "c:\\Program Files\\NSIS\\makensis.exe";
var PortableInstallerExe = "c:\\portableapps\\PortableApps\\PortableApps.comInstaller\\PortableApps.comInstaller.exe";
var MsgfmtExe = "c:\\Program Files\\GnuWin32\\bin\\msgfmt.exe"

var BaseDir = GetBaseDir();
var InstallRootDir;
var InstallDir; // application directory, it contains stardict.exe, stardict.dll, etc.
var MSVSOutputDir;
var MSVSDir = BaseDir + "msvc_2008\\";
var NSISLog = "nsis.log";
var fso = WScript.CreateObject("Scripting.FileSystemObject");
var shell = WScript.CreateObject("WScript.Shell");

function usage()
{
	var txt;
	txt =  "Usage:\n";
	txt += "  cscript " + WScript.ScriptName + " <options>\n";
	txt += "  cscript " + WScript.ScriptName + " help\n\n";
	txt += "Options can be specified in the form <option>=<value>.\n\n";
	txt += "\nDefault value given in parentheses:\n\n";
	txt += "  vsconfig:     Visual Studio configuration [release|debug] (" + VSConfig + ") \n";
	txt += "  portable:     Build portable version of StarDict [yes|no] (" + (Portable ? "yes" : "no") + ") \n";
	txt += "  distdir:      StarDict source directory after executing `make dist` or\n";
	txt += "                a directory with unpacked distribution tarball for Unix.\n";
	txt += "                Used to get html help files.\n";
	txt += "                (" + UnixDistDir + ") \n";
	txt += "  makensis:     Path to makensis.exe.\n";
	txt += "                (" + MakeNSISExe + ") \n";
	txt += "  portinst:     Path to portable installer (PortableApps.comInstaller.exe).\n";
	txt += "                (" + PortableInstallerExe + ") \n";
	txt += "  msgfmt:       Path msgfmt.exe. To get this utility, download and install \n"
	txt += "                the gettext package from\n";
	txt += "                http://gnuwin32.sourceforge.net/packages/gettext.htm.\n";
	txt += "                (" + MsgfmtExe + ") \n";
	WScript.Echo(txt);
}

/* This function was inspired by configure.js script of libxml2 package. */
function ParseCommandLine() {
	var error = 0;
	for (i = 0; (i < WScript.Arguments.length) && (error == 0); i++) {
		var arg, opt;
		arg = WScript.Arguments(i);
		opt = arg.substring(0, arg.indexOf("="));
		if (opt.length == 0)
			opt = arg.substring(0, arg.indexOf(":"));
		if (opt.length > 0) {
			if (opt == "vsconfig")
				VSConfig = arg.substring(opt.length + 1, arg.length);
			else if (opt == "portable")
				Portable = strToBool(arg.substring(opt.length + 1, arg.length));
			else if (opt == "distdir")
				UnixDistDir = arg.substring(opt.length + 1, arg.length);
			else if (opt == "makensis")
				MakeNSISExe = arg.substring(opt.length + 1, arg.length);
			else if (opt == "portinst")
				PortableInstallerExe = arg.substring(opt.length + 1, arg.length);
			else if (opt == "msgfmt")
				MsgfmtExe = arg.substring(opt.length + 1, arg.length);
		} else if (i == 0) {
			if (arg == "help") {
				usage();
				WScript.Quit(0);
			}
		} else {
			error = 1;
		}
	}
	// If we fail here, it is because the user supplied an unrecognised argument.
	if (error != 0) {
		usage();
		WScript.Quit(error);
	}
}

/* Helper function, transforms the argument string into a boolean
   value. */
function strToBool(opt)
{
	if (opt == 0 || opt == "no")
		return false;
	else if (opt == 1 || opt == "yes")
		return true;
	error = 1;
	return false;
}

function GetBaseDir() {
	var t = WScript.ScriptFullName.lastIndexOf("\\");
	return WScript.ScriptFullName.substr(0, t+1);
}

function CopyFile(src, target) {
	if(!fso.FileExists(src)) {
		WScript.Echo("File not found: " + src);
		WScript.Quit(1);
	}
	//WScript.Echo("src: " + src + ", target: " + target);
	fso.CopyFile(src, target);
}

/* copy content of the folder SrcDir into TargetDir. 
TargetDir must exist. */
function CopyFolderContent(SrcDir, TargetDir) {
	/* fix TargetDir to end with "\\" */
	if(!TargetDir.match(/.*\\$/)) {
		TargetDir += "\\";
	}
	var oFolder = fso.GetFolder(SrcDir);
	var oFolders = new Enumerator(oFolder.SubFolders);
	for(; !oFolders.atEnd(); oFolders.moveNext())
	{
		if(oFolders.item().name.toLowerCase() == ".svn") {
			continue;
		}
		//WScript.Echo("folder: " + oFolders.item().path);
		var NewDir = TargetDir + oFolders.item().name + "\\";
		CreateFolder(NewDir);
		CopyFolderContent(oFolders.item().path, NewDir);
	}
	var oFiles = new Enumerator(oFolder.Files);
	for(; !oFiles.atEnd(); oFiles.moveNext()) 
	{
		if(oFiles.item().name.toLowerCase() == "makefile.am"
		|| oFiles.item().name.toLowerCase() == "makefile.in") {
			continue;
		}
		//WScript.Echo("file: " + oFiles.item().path);
		fso.CopyFile(oFiles.item().path, TargetDir);
	}
}

function CreateFolder(path) {
	var start = 0;
	var dir;
	var i;
	do {
		i = path.indexOf("\\", start);
		if(i < 0)
			dir = path;
		else {
			dir = path.substr(0, i);
			start = i + 1;
		}
		if(!fso.FolderExists(dir))
			fso.CreateFolder(dir);
	} while(i >= 0);
}

/* Remove traling backslash if present */
function CanonizePath(path) {
	if(path.charAt(path.length-1) == "\\")
		return path.substr(0, path.length-1);
	else
		return path;
}

function DeleteFolder(path) {
	path = CanonizePath(path);
	if(fso.FolderExists(path)) {
		fso.DeleteFolder(path);
	}
}

/* path - directory name where to search for files,
name_regex - a regular expression that file name must match */
function FindFiles(path, name_regex) {
	var oFolder = fso.GetFolder(path);
	var oFiles = new Enumerator(oFolder.Files);
	var arr = new Array();
	for (; !oFiles.atEnd(); oFiles.moveNext())
	{
		var name = oFiles.item().name;
		if(name.match(name_regex)) {
			arr.push(name);
		}
	}
	return arr;
}

if (!fso.FileExists(BaseDir + "stardict-installer.nsi")) {
	WScript.Echo("This script must be started in the Stardict source directory.");
	usage();
	WScript.Quit(1);
}

ParseCommandLine();

{	// assign global variables
	MSVSOutputDir = MSVSDir + VSConfig + "\\";
	if(Portable) {
		InstallRootDir = BaseDir + "win32-portable-install-dir\\";
		InstallDir = InstallRootDir + "StarDictPortable\\App\\StarDict\\";
	} else {
		InstallRootDir = BaseDir + "win32-install-dir\\";
		InstallDir = InstallRootDir;
	}
}

if(!fso.FolderExists(UnixDistDir)) {
	WScript.Echo("StarDict distribution directory does not exist.\n"
		+ "This directory must contain unpacked distribution tarball for Unix or\n"
		+ "it must be StarDict source directory after executing `make dist`.\n"
		+ "Folder: " + UnixDistDir);
	usage();
	WScript.Quit(1);
}

if(!fso.FileExists(MakeNSISExe)) {
	WScript.Echo("Unable to find makensis.exe.\n"
		+	"Path: " + MakeNSISExe + "\n"
		+	"Use makensis parameter to specify the path.");
	WScript.Quit(1);
}

if(!fso.FileExists(MsgfmtExe)) {
	WScript.Echo("Unable to find msgfmt.exe.\n"
		+	"Path: " + MsgfmtExe + "\n"
		+	"Use msgfmt parameter to specify the path.");
	WScript.Quit(1);
}

if(Portable) {
	if(!fso.FileExists(PortableInstallerExe)) {
		WScript.Echo("Unable to find portable installer.\n"
			+	"Path: " + PortableInstallerExe + "\n"
			+	"Use portinst parameter to specify the path.");
		WScript.Quit(1);
	}
}

if(InstallDirMustNotExist) {
	if (fso.FolderExists(InstallRootDir)) {
		WScript.Echo("Directory " + InstallRootDir + " already exists, "
			+ "remove it and start this script again.");
		WScript.Quit(1);
	} 
} else {
	DeleteFolder(InstallRootDir);
}
CreateFolder(InstallRootDir);
CreateFolder(InstallDir);
CopyFile(MSVSOutputDir + "stardict-loader.exe", InstallDir + "stardict.exe");
CopyFile(MSVSOutputDir + "stardict.dll", InstallDir);
CopyFile(MSVSOutputDir + "TextOutSpy.dll", InstallDir);
CopyFile(MSVSOutputDir + "TextOutHook.dll", InstallDir);
CopyFile(MSVSOutputDir + "stardict-editor.dll", InstallDir);
CopyFile(MSVSOutputDir + "stardict-editor-loader.exe", InstallDir + "stardict-editor.exe");
{
	var oFolder = fso.GetFolder(fso.BuildPath(UnixDistDir, "dict\\po"));
	var oFiles = new Enumerator(oFolder.Files);
	var cnt = 0;
	for (; !oFiles.atEnd(); oFiles.moveNext())
	{
		var oFile = oFiles.item();
		if(oFile.name.match(/\.po$/i)) {
			++cnt;
			var BaseName = oFile.name.replace(/(.*)\..*/, "$1");
			var LocaleDir = InstallDir + "locale\\" + BaseName + "\\LC_MESSAGES";
			CreateFolder(LocaleDir);
			var OutFile = LocaleDir + "\\stardict.mo";
			var cmd = "\"" + MsgfmtExe + "\" -o \"" + OutFile + "\" \"" + oFile.Path + "\"";
			var status = shell.Run(cmd, 1, true);
			if(status != 0) {
				WScript.Echo("msgfmt command failed. Unable to create Machine Object file.");
				WScript.Quit(status);
			}
		}
	}
	if(cnt == 0) {
		WScript.Echo("No translation files found: " + oFolder.Path + "\\*.po");
		usage();
		WScript.Quit(1);
	}
}
{
	var PixmapsDir = InstallDir + "pixmaps\\";
	CreateFolder(PixmapsDir);
	CopyFile(BaseDir + "pixmaps\\stardict.png", PixmapsDir);
	fso.CopyFile(BaseDir + "src\\pixmaps\\*.png", PixmapsDir);
	fso.DeleteFile(PixmapsDir + "docklet_gpe_*.png");
}

{
	var SoundsDir = InstallDir + "sounds\\";
	CreateFolder(SoundsDir);
	fso.CopyFile(BaseDir + "src\\sounds\\*.wav",  SoundsDir);
}

{
	var DicDir = InstallDir + "dic\\";
	CreateFolder(DicDir);
	CopyFolderContent("src\\dic\\", DicDir);
}

CreateFolder(InstallDir + "treedict\\");
CreateFolder(InstallDir + "skins\\");

{
	var InstallHelpDir = InstallDir + "help\\";
	CreateFolder(InstallHelpDir);
	var oFolder = fso.GetFolder(fso.BuildPath(UnixDistDir, "dict\\help\\"));
	var oFolders = new Enumerator(oFolder.SubFolders);
	var cnt = 0;
	for(; !oFolders.atEnd(); oFolders.moveNext())
	{
		var oLocaleFolder = oFolders.item();
		if(oLocaleFolder.name == ".svn" || oLocaleFolder.name.match(/^PaxHeaders/i)) {
			continue;
		}
		var HtmlDir = oLocaleFolder.path + "\\html\\";
		if(!fso.FolderExists(HtmlDir)) {
			WScript.Echo("Html help folder is not found: " + HtmlDir);
			WScript.Quit(1);
		}
		var InstallLocaleDir = InstallHelpDir + oLocaleFolder.name + "\\";
		CreateFolder(InstallLocaleDir);
		CopyFolderContent(HtmlDir, InstallLocaleDir);
		++cnt;
	}
	if(cnt == 0) {
		WScript.Echo("Html help is not found: " + oFolder.Path + "\\C\\html");
		usage();
		WScript.Quit(1);
	}
}

{
	var PluginsDir = InstallDir + "plugins\\";
	CreateFolder(PluginsDir);
	{
		var files = FindFiles(MSVSOutputDir, /.*\.dll$/i);
		for(var i=0; i<files.length; i++) {
			if(!files[i].match(/stardict-.*-plugin\.dll/i))
				continue;
			//WScript.Echo("path: " + MSVSOutputDir + files[i]);
			CopyFile(MSVSOutputDir + files[i], PluginsDir);
		}
	}
}

if(!Portable) {
	CopyFile(MSVSOutputDir + "StarDict.api", InstallDir);
}
{
	var LibSigcDir = MSVSDir + "libsigc++\\";
	var files = FindFiles(LibSigcDir, /sigc-.*\.dll/i);
	var cnt = 0;
	for(var i=0; i<files.length; ++i) {
		var is_debug_dll = (files[i].match(/.*-d-.*/i) != null);
		var is_debug_build = (VSConfig.toLowerCase() == "debug");
		if(is_debug_dll != is_debug_build)
			continue;
		CopyFile(LibSigcDir + files[i], InstallDir);
		++cnt;
	}
	if(cnt == 0) {
		WScript.Echo("libsigc++ dll is not found in " + LibSigcDir);
		WScript.Quit(1);
	}
}

{ // Gtk runtime
	var files = FindFiles("redist", /^gtk2-runtime-.*\.exe$/i);
	if(files.length == 0) {
		WScript.Echo("Unable to find gtk2-runtime.");
		WScript.Quit(1);
	}
	if(files.length > 1) {
		WScript.Echo("Multiple gtk2-runtime's found.");
		WScript.Quit(1);
	}
	var gtk2_runtime = "redist\\" + files[0];
	var GtkInstallDir = InstallDir + "Gtk";
	CreateFolder(GtkInstallDir);
	var cmd = "\"" + gtk2_runtime + "\" /S /sideeffects=no /dllpath=bin /translations=yes /compatdlls=yes "
		+ "/D=" + GtkInstallDir + "";
	var status = shell.Run(cmd, 1, true);
	if(status != 0) {
		WScript.Echo("gtk2-runtime failed.");
		WScript.Quit(status);
	}
}

if(!Portable) {

	{ // installer
		var cmd = "\"" + MakeNSISExe + "\" /O" + NSISLog + " stardict-installer.nsi";
		var status = shell.Run(cmd, 1, true);
		if(status != 0) {
			WScript.Echo("Building StarDict installer failed.");
			WScript.Quit(status);
		}
	}

	WScript.Echo("Done.");
	WScript.Quit(0);
}

{ // StarDict launcher
	var cmd = "\"" + MakeNSISExe + "\" /O" + NSISLog + " src\\win32\\nsis\\StarDictPortable.nsi";
	var status = shell.Run(cmd, 1, true);
	if(status != 0) {
		WScript.Echo("Building StarDict launcher failed.");
		WScript.Quit(status);
	}
	CopyFile(BaseDir + "src\\win32\\nsis\\StarDictPortable.exe", InstallRootDir + "StarDictPortable\\");
}

{ // StarDict Editor launcher
	var cmd = "\"" + MakeNSISExe + "\" /O" + NSISLog + " src\\win32\\nsis\\StarDictEditorPortable.nsi";
	var status = shell.Run(cmd, 1, true);
	if(status != 0) {
		WScript.Echo("Building StarDict Editor launcher failed.");
		WScript.Quit(status);
	}
	CopyFile(BaseDir + "src\\win32\\nsis\\StarDictEditorPortable.exe", InstallRootDir + "StarDictPortable\\");
}

{ // stardict-dirs.cfg
	var contents =
			"[general]\n"
		+	"user_config_dir=..\\\\..\\\\Data\\\\settings\\\\stardict\n"
		+	"user_cache_dir=..\\\\..\\\\Data\\\\cache\n"
		+	"log_dir=..\\\\..\\\\Data\\\\log\n"
	;
	var CfgDir = InstallRootDir + "StarDictPortable\\App\\DefaultData\\settings\\"
	CreateFolder(CfgDir);
	var filePath = CfgDir + "stardict-dirs.cfg";
	var file = fso.CreateTextFile(filePath, true, false);
	file.Write(contents);
	file.Close();
}

{ // StarDictPortableSettings.ini
	var contents =
			"[Language]\n"
		+	"STARDICTLANG=\n"
	;
	var CfgDir = InstallRootDir + "StarDictPortable\\App\\DefaultData\\settings\\"
	CreateFolder(CfgDir);
	var filePath = CfgDir + "StarDictPortableSettings.ini";
	var file = fso.CreateTextFile(filePath, true, false);
	file.Write(contents);
	file.Close();
}

{ // gtkrc
	var contents =
			"gtk-theme-name = \"MS-Windows\"\n"
	;
	var CfgDir = InstallRootDir + "StarDictPortable\\App\\DefaultData\\settings\\"
	CreateFolder(CfgDir);
	var filePath = CfgDir + "gtkrc";
	var file = fso.CreateTextFile(filePath, true, false);
	file.Write(contents);
	file.Close();
}

/* StarDict-loader sets $HOME enviroment variable to "StarDictPortable\\App":
Set $HOME so that the GTK+ settings get stored in the right place 
GTK will store settings in "StarDictPortable\\App\GTK" subfolder. */
CreateFolder(InstallRootDir + "StarDictPortable\\App\\GTK");

{ // AppInfo
	var appInfoDir = InstallRootDir + "StarDictPortable\\App\\AppInfo\\";
	CreateFolder(appInfoDir);
	CopyFile(BaseDir + "src\\win32\\PortableApps.com\\appinfo.ini", appInfoDir + "appinfo.ini");
	CopyFile(BaseDir + "pixmaps\\stardict_16.png", appInfoDir + "appicon_16.png");
	CopyFile(BaseDir + "pixmaps\\stardict_32.png", appInfoDir + "appicon_32.png");
	CopyFile(BaseDir + "pixmaps\\stardict_128.png", appInfoDir + "appicon_128.png");
	CopyFile(BaseDir + "pixmaps\\stardict_16.png", appInfoDir + "appicon1_16.png");
	CopyFile(BaseDir + "pixmaps\\stardict_32.png", appInfoDir + "appicon1_32.png");
	CopyFile(BaseDir + "pixmaps\\stardict_128.png", appInfoDir + "appicon1_128.png");
	CopyFile(BaseDir + "pixmaps\\stardict-editor_16.png", appInfoDir + "appicon2_16.png");
	CopyFile(BaseDir + "pixmaps\\stardict-editor_32.png", appInfoDir + "appicon2_32.png");
	CopyFile(BaseDir + "pixmaps\\stardict-editor_128.png", appInfoDir + "appicon2_128.png");
	CopyFile(BaseDir + "pixmaps\\stardict.ico", appInfoDir + "appicon.ico");
	CopyFile(BaseDir + "pixmaps\\stardict.ico", appInfoDir + "appicon1.ico");
	CopyFile(BaseDir + "pixmaps\\stardict-editor.ico", appInfoDir + "appicon2.ico");
}

{ // Other
	var sourceDir = InstallRootDir + "StarDictPortable\\Other\\Source\\";
	CreateFolder(sourceDir);
	CopyFile(BaseDir + "src\\win32\\PortableApps.com\\other-source-readme.txt", sourceDir + "Readme.txt");
}

{ // root application directory
	var appDir = InstallRootDir + "StarDictPortable\\";
	CreateFolder(appDir);
	CopyFile(BaseDir + "src\\win32\\PortableApps.com\\help.html", appDir);
}

{ // package the portable application
	var cmd = "\"" + PortableInstallerExe + "\" \"" + InstallRootDir + "StarDictPortable\"";
	var status = shell.Run(cmd, 1, true);
	if(status != 0) {
		WScript.Echo("Packaging StarDict portable failed.");
		WScript.Quit(status);
	}
	var oFolder = fso.GetFolder(InstallRootDir);
	var oFiles = new Enumerator(oFolder.Files);
	var cnt = 0;
	for (; !oFiles.atEnd(); oFiles.moveNext())
	{
		var oFile = oFiles.item();
		if(oFile.name.match(/\.paf\.exe$/i)) {
			++cnt;
			CopyFile(oFile.Path, BaseDir);
		}
	}
	if(cnt == 0) {
		WScript.Echo("StarDict portable package is not found.");
		WScript.Quit(1);
	}
}

WScript.Echo("Done.");