diff --git a/Makefile b/Makefile index e6c7ce52a4..298c24249f 100644 --- a/Makefile +++ b/Makefile @@ -22,11 +22,15 @@ cat := node tools/cat.js cat_deps := tools/cat.js jshint_deps := .jshintrc node_modules/jshint/package.json -sources := \ +groups := 0 1 2 3 4 5 6 7 8 9 10 11 12 13 + +sources0 := \ src/General/Config.coffee \ - src/General/Globals.coffee \ + src/General/Globals.coffee +sources1 := \ src/General/$$.coffee \ - src/General/CrossOrigin.coffee \ + src/General/CrossOrigin.coffee +sources2 := \ src/classes/Callbacks.coffee \ src/classes/Board.coffee \ src/classes/Thread.coffee \ @@ -39,31 +43,44 @@ sources := \ src/classes/SimpleDict.coffee \ src/classes/ShimSet.coffee \ src/classes/Connection.coffee \ - src/classes/Fetcher.coffee \ + src/classes/Fetcher.coffee +sources3 := \ src/General/Polyfill.coffee \ src/General/Header.coffee \ src/General/Index.coffee \ src/General/Build.coffee \ src/General/Get.coffee \ src/General/UI.coffee \ - src/General/BuildTest.coffee \ - $(sort $(wildcard src/Filtering/*.coffee)) \ - $(sort $(wildcard src/Quotelinks/*.coffee)) \ + src/General/BuildTest.coffee +sources4 := \ + $(sort $(wildcard src/Filtering/*.coffee)) +sources5 := \ + $(sort $(wildcard src/Quotelinks/*.coffee)) +sources6 := \ src/Posting/QR.coffee \ src/Posting/Captcha.coffee \ $(sort $(wildcard src/Posting/Captcha.*.coffee)) \ src/Posting/PassLink.coffee \ src/Posting/PostSuccessful.coffee \ - $(sort $(wildcard src/Posting/QR.*.coffee)) \ - $(sort $(wildcard src/Images/*.coffee)) \ - $(sort $(wildcard src/Linkification/*.coffee)) \ - $(sort $(wildcard src/Menu/*.coffee)) \ - $(sort $(wildcard src/Monitoring/*.coffee)) \ - $(sort $(wildcard src/Archive/*.coffee)) \ - $(sort $(wildcard src/Miscellaneous/*.coffee)) \ + $(sort $(wildcard src/Posting/QR.*.coffee)) +sources7 := \ + $(sort $(wildcard src/Images/*.coffee)) +sources8 := \ + $(sort $(wildcard src/Linkification/*.coffee)) +sources9 := \ + $(sort $(wildcard src/Menu/*.coffee)) +sources10 := \ + $(sort $(wildcard src/Monitoring/*.coffee)) +sources11 := \ + $(sort $(wildcard src/Archive/*.coffee)) +sources12 := \ + $(sort $(wildcard src/Miscellaneous/*.coffee)) +sources13 := \ src/General/Settings.coffee \ src/General/Main.coffee +sources := $(foreach i,$(groups),$(sources$(i))) + imports := \ node_modules/font-awesome/package.json \ $(wildcard src/Linkification/icons/*.png) \ @@ -91,11 +108,13 @@ testbds := $(foreach f,$(subst .crx,.crx.zip,$(bds)),test$(f)) jshint := $(foreach f,script-crx eventPage script-userscript,.events/jshint.$(f)) +jshint_parts := $(foreach t,crx userscript,$(foreach i,$(parts),.events/jshint.script$(i)-$(t))) .events/jshint.eventPage + default : install all : bds install -.events tmp testbuilds builds : +.events tmp tmp/parts testbuilds builds : $(MKDIR) .events/npm : npm-shrinkwrap.json | .events @@ -108,14 +127,23 @@ node_modules/%/package.json : .events/npm .tests_enabled : echo false> .tests_enabled -tmp/script.coffee : $(sources) $(cat_deps) | tmp - $(cat) $(sources) $@ +define rules_group + +tmp/parts/script$1.coffee : $$(sources$1) $(cat_deps) | tmp/parts + $(cat) $$(sources$1) $$@ -tmp/script-%.coffee : tmp/script.coffee $(imports) $(template_deps) - $(template) $< $@ type=$* +tmp/parts/script$1-%.coffee : tmp/parts/script$1.coffee $(imports) $(template_deps) + $(template) $$< $$@ type=$$* + +tmp/parts/script$1-%.js : tmp/parts/script$1-%.coffee $(coffee_deps) + $(coffee) $$< + +endef -tmp/script-%.js : tmp/script-%.coffee $(coffee_deps) - $(coffee) $< +$(foreach i,$(groups),$(eval $(call rules_group,$(i)))) + +tmp/script-%.js : $(foreach i,$(groups),tmp/parts/script$(i)-%.js) tools/cat-coffee.js + node tools/cat-coffee.js $(foreach i,$(groups),tmp/parts/script$(i)-$*.js) $@ tmp/eventPage.js : src/General/eventPage.coffee $(coffee_deps) | tmp $(coffee) -o tmp src/General/eventPage.coffee @@ -169,23 +197,30 @@ builds/% : testbuilds/% $(jshint) | builds test.html : README.md template.jst tools/markdown.js node_modules/marked/package.json node_modules/lodash/package.json node tools/markdown.js +tmp/parts/.jshintrc : src/meta/jshint.json $(template_deps) | tmp/parts + $(template) $< $@ stage=parts + .jshintrc : src/meta/jshint.json $(template_deps) - $(template) $< .jshintrc + $(template) $< $@ stage=full + +.events/jshint.% : tmp/%.js .jshintrc node_modules/jshint/package.json | .events + $(BIN)jshint $< + echo -> $@ -.events/jshint.% : tmp/%.js $(jshint_deps) | .events +.events/jshint_parts.% : tmp/parts/%.js tmp/parts/.jshintrc node_modules/jshint/package.json | .events $(BIN)jshint $< echo -> $@ install.json : echo {}> $@ -.events/install : $(testbds) $(jshint) install.json tools/install.js | .events +.events/install : $(testbds) $(jshint_parts) install.json tools/install.js | .events node tools/install.js echo -> $@ .SECONDARY : -.PHONY: default all clean cleanall testbds bds jshint install +.PHONY: default all clean cleanall testbds bds jshint jshint_parts install clean : $(RMDIR) tmp testbuilds .events @@ -200,4 +235,6 @@ bds : $(bds) jshint : $(jshint) +jshint_parts : $(jshint_parts) + install : .events/install diff --git a/package.json b/package.json index f1d8500628..356c491119 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,15 @@ "GM_openInTab", "GM_xmlhttpRequest" ], + "globals": [ + "$$", + "c", + "Conf", + "d", + "doc", + "E", + "g" + ], "min": { "chrome": "33", "firefox": "26", diff --git a/src/meta/jshint.json b/src/meta/jshint.json index 29b6334b2f..4dc7507774 100644 --- a/src/meta/jshint.json +++ b/src/meta/jshint.json @@ -19,7 +19,17 @@ "cloneInto": true, "unsafeWindow": true, "chrome": true<%= - readJSON('package.json').meta.grants.map(x => `,\n "${x}": true`).join('') -%> + meta.grants.map(x => `,\n "${x}": true` +).join('') +%><% if (stage === 'parts') { %> + meta.globals.map(x => `,\n "${x}": true` +<% } %><% if (stage === 'parts') { %> + fs.readdirSync('src') + .map(x => fs.readdirSync(`src/${x}`)) + .reduce((x,y) => x.concat(y)) + .filter(x => /^[$A-Z]\w*\.coffee/.test(x)) + .map(x => x.split('.')[0]) + .map(x => `,\n "${x}": true` +<% } %> } } diff --git a/tools/cat-coffee.js b/tools/cat-coffee.js new file mode 100644 index 0000000000..d7932ba787 --- /dev/null +++ b/tools/cat-coffee.js @@ -0,0 +1,56 @@ +var fs = require('fs'); + +var inputFiles = process.argv.slice(2, -1); + +var allVars = []; +var allHelperNames = []; +var allHelperValues = {}; +var allBodies = []; + +for (var file of inputFiles) { + var inputText = fs.readFileSync(file, 'utf8').replace(/\r\n/g, '\n'); + + var parts = inputText.match(/^\(function\(\) {\n var ([\w$]+(?:, [\w$]+)*)((?:,\n [\w$]+ = .*)*);\n\n([^]*)\n\n}\)\.call\(this\);\n$/); + if (!parts) throw new Error(`${file}: unexpected format`); + + var vars = parts[1].split(', '); + for (var v of vars) { + if (allVars.indexOf(v) >= 0) { + throw new Error(`${file}: reused variable name ${v}`); + } + if (allHelperNames.indexOf(v) >= 0) { + throw new Error(`${file}: variable clashes with helper ${v}`); + } + allVars.push(v); + } + + var helpers = parts[2].split(',\n ').slice(1); + for (var h of helpers) { + var hparts = h.match(/^([\w$]+) = (.*)$/); + var hn = hparts[1]; + var hv = hparts[2]; + if (allVars.indexOf(hn) >= 0) { + throw new Error(`${file}: helper clashes with variable ${v}`); + } + if (allHelperNames.indexOf(hn) >= 0) { + if (allHelperValues[hn] !== hv) { + throw new Error(`${file}: redefined helper ${hn}`); + } + } else { + allHelperNames.push(hn); + allHelperValues[hn] = hv; + } + } + + var body = parts[3]; + allBodies.push(body); +} + +var varText = allVars.sort().join(', '); +var helperText = allHelperNames.map(hn => `,\n ${hn} = ${allHelperValues[hn]}`).join(''); +var bodyText = allBodies.join('\n\n'); + +var outputText = `(function() {\n var ${varText}${helperText};\n\n${bodyText}\n\n}).call(this);\n`; + +var outputName = process.argv[process.argv.length - 1]; +fs.writeFileSync(outputName, outputText);