Skip to content

Commit 08592e1

Browse files
committed
Added WSH script to build rules file on Windows without python.
1 parent 0f824a8 commit 08592e1

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ To build the Chrome version go to the git repository root and run:
8585

8686
After building the extension the xpi files (for Firefox) and crx files (for Chrome) get created in the pkg directory. You can open those files within your browser to install the browser extension.
8787

88+
To construct ruleset file in Windows use `./utils/merge-rulesets.js`
89+
8890
Ruleset Tests
8991
-------------
9092

utils/merge-rulesets.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Merge all the .xml rulesets into a single "default.rulesets" file -- this
2+
// prevents inodes from wasting disk space, but more importantly, works around
3+
// the fact that zip does not perform well on a pile of small files.
4+
5+
// it is a translation of merge-rulesets.py (without linting, backups, git support, unicode normalisation and --fast option), but it doesn't require Python (only WSH)
6+
7+
8+
var fs = new ActiveXObject("Scripting.FileSystemObject"),
9+
shell = new ActiveXObject("WScript.Shell"),
10+
args = WScript.Arguments,
11+
scriptFullPath = WScript.ScriptFullName,
12+
sourceDir = args[0],
13+
targetFile = args[1];
14+
15+
var msg="";
16+
17+
//console or gui?
18+
var enginefile=WScript.FullName;
19+
var stdout=null;
20+
if(enginefile.toLowerCase().indexOf("cscript.exe")>-1){
21+
stdout=WScript.StdOut;
22+
}
23+
function getDir(a) {
24+
return a.substring(0, a.lastIndexOf("\\") || a.lastIndexOf("/"))
25+
}
26+
27+
//checking input
28+
29+
var scriptPath = getDir(WScript.ScriptFullName.toString());
30+
if (!sourceDir) {
31+
sourceDir = sourceDir || (getDir(scriptPath) + "\\src\\chrome\\content\\rules");
32+
}
33+
var defaultFileName="default.rulesets";
34+
if (!targetFile) {
35+
targetFile = sourceDir + "\\" + defaultFileName;
36+
}
37+
38+
//shell.Popup(sourceDir, 0, "HTTPS Everywhere Rules Merger",64);
39+
//shell.Popup(targetFile, 0, "HTTPS Everywhere Rules Merger",64);
40+
41+
42+
if(stdout)
43+
stdout.WriteLine("Merging rulesets... and removing whitespaces and comments...");
44+
var infldr = fs.GetFolder(sourceDir);
45+
var res,fileI;
46+
47+
targetFile=fs.CreateTextFile(targetFile);
48+
49+
function clean_up(rulefile){
50+
//Remove extra whitespace and comments from a rulesets
51+
rulefile = rulefile.replace(/<!--[.\n\r\u2028]*?-->/ig,'');
52+
rulefile = rulefile.replace(/\s+/ig,' ');
53+
//rulefile = rulefile.replace(/\s*(to=|from=)/ig," $1");
54+
rulefile = rulefile.replace(/>\s*</, "><");
55+
//rulefile = rulefile.replace(/<\/ruleset>\s*/ig, "</ruleset>\n");
56+
rulefile = rulefile.replace(/\s*(\/>|<ruleset)/, "$1");
57+
return rulefile;
58+
}
59+
60+
var commitId=shell.Environment("Process")("GIT_COMMIT_ID");//lol, it won't work - there is no such variable
61+
if(commitId)
62+
targetFile.Write('<rulesetlibrary gitcommitid="'+commitId+'">');
63+
else
64+
targetFile.Write('<rulesetlibrary>');
65+
66+
var fileNameRx=/\.xml$/i;
67+
for(fileI = new Enumerator(infldr.Files); !fileI.atEnd(); fileI.moveNext()){
68+
if (file = fileI.item()) {
69+
var name = file.Name;
70+
if(!fileNameRx.test(name))continue;
71+
if(stdout)stdout.WriteLine("Processing file "+name+"...");
72+
var currentFileStream=file.OpenAsTextStream(1);
73+
var ruleset = currentFileStream.ReadAll();
74+
currentFileStream.Close();
75+
delete currentFileStream;
76+
ruleset = ruleset.replace("<ruleset", '<ruleset f="'+name+'"');
77+
targetFile.Write(clean_up(ruleset));
78+
}
79+
}
80+
targetFile.Write("</rulesetlibrary>\n");
81+
targetFile.Close();
82+
delete targetFile;
83+
84+
var currentStageMsg="Merging has been FINISHED!";
85+
86+
if(stdout){
87+
stdout.WriteLine(currentStageMsg);
88+
}else{
89+
msg+="\n"+currentStageMsg;
90+
shell.Popup(msg, 1, "HTTPS Everywhere Rules Merger", 64);
91+
}
92+
93+
94+
/*function normalize(f){
95+
//OSX and Linux filesystems encode composite characters differently in filenames.
96+
//We should normalize to NFC: http://unicode.org/reports/tr15/.
97+
f = unicodedata.normalize('NFC', unicode(f, 'utf-8')).encode('utf-8')
98+
return f
99+
}*/

0 commit comments

Comments
 (0)