From 297a19924a3ea29ed6a7d628130edd0d58524321 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Sat, 16 Apr 2016 06:38:51 -0700 Subject: [PATCH] Support arbitrary mixture of JS and Coffeescript sections. --- Makefile | 85 +++++++++++++++++++++++++++++----------------- tools/declare.js | 2 +- tools/globalize.js | 8 +++-- 3 files changed, 60 insertions(+), 35 deletions(-) diff --git a/Makefile b/Makefile index 5ab308b7d..6e8492036 100644 --- a/Makefile +++ b/Makefile @@ -26,19 +26,28 @@ cat_deps := tools/cat.js capitalized = $(filter-out a,$(foreach x,$1,$(subst a $(x),,$(sort a $(x))))) parts := \ - Config platform classes \ + globals Config platform classes \ $(sort $(call capitalized, \ $(subst src/,,$(wildcard src/*)) \ )) \ Main -intermediate := LICENSE src/meta/fbegin.js tmp/declaration.js tmp/globals.js $(foreach p,$(parts),tmp/$(p).js) src/meta/fend.js +lang = $(if $(filter globals,$1),js,coffee) # remove extension when sorting so X.coffee comes before X.Y.coffee -sources = \ - $(subst !,.coffee,$(sort $(subst .coffee,!, \ - $(wildcard src/$1/*.coffee src/main/$1.coffee)))) +sources_lang = \ + $(subst !,.$2,$(sort $(subst .$2,!, \ + $(wildcard src/$1/*.$2 src/main/$1.$2)))) +sources = $(call sources_lang,$1,$(call lang,$1)) + +imports = \ + $(filter-out %.coffee %.js,$(wildcard src/$1/*.* src/$1/*/*.* src/$1/*/*/*.*)) \ + .tests_enabled \ + $(imports_$1) + +imports_globals := \ + version.json imports_Config := \ src/Archive/archives.json \ src/css/custom.css @@ -57,6 +66,15 @@ imports_font_awesome := \ imports_style := \ $(wildcard src/css/linkIcons/*.png) +intermediate = \ + LICENSE \ + src/meta/fbegin.js \ + tmp/declaration.js \ + $(foreach p, \ + $(subst platform,platform_$1,$(parts)) \ + ,tmp/$(p).js) \ + src/meta/fend.js + crx_contents := script.js eventPage.js icon16.png icon48.png icon128.png manifest.json release := \ @@ -71,7 +89,7 @@ script := $(foreach f,$(filter-out %.crx %.zip,$(release)),test$(f)) $(foreach t crx := $(foreach f,$(filter %.crx %.zip,$(release)),test$(f)) -jshint := $(foreach f,globals $(subst platform,platform_crx platform_userscript,$(parts)),.events/jshint.$(f)) +jshint := $(foreach f,$(subst platform,platform_crx platform_userscript,$(parts)),.events/jshint.$(f)) default : script jshint install @@ -103,34 +121,39 @@ tmp/style.css : src/css/style.css $(imports_style) $(template_deps) | tmp tmp/declaration.js : .events/declare -tmp/globals.js : src/main/globals.js version.json $(template_deps) | tmp - $(template) $< $@ - -define rules_part - +define concatenate tmp/$1.jst : $$(call sources,$1) $(cat_deps) | tmp - $(cat) $$(call sources,$1) $$@ - -tmp/$1.coffee : tmp/$1.jst $$(filter-out %.coffee,$$(wildcard src/$1/*.* src/$1/*/*.* src/$1/*/*/*.*)) $$(imports_$1) .tests_enabled $(template_deps) - $(template) $$< $$@ - -tmp/$1.js : tmp/$1.coffee $(coffee_deps) tools/globalize.js - $(coffee) $$< - node tools/globalize.js $$@ $$(call sources,$1) - + $(cat) $$(subst $$$$,$$(ESC_DOLLAR),$$(call sources,$1)) $$@ endef -$(foreach i,$(filter-out platform,$(parts)),$(eval $(call rules_part,$(i)))) +$(foreach p, \ + $(parts), \ + $(eval $(call concatenate,$(p))) \ +) -tmp/platform.jst : $(call sources,platform) $(cat_deps) | tmp - $(cat) $(subst $$,$(ESC_DOLLAR),$(call sources,platform)) $@ +define interpolate +tmp/$1.$$(call lang,$1) : tmp/$1.jst $$(call imports,$1) $(template_deps) + $(template) $$< $$@ +endef -tmp/platform_%.coffee : tmp/platform.jst $(template_deps) +$(foreach p, \ + $(filter-out platform,$(parts)), \ + $(eval $(call interpolate,$(p))) \ +) + +tmp/platform_%.coffee : tmp/platform.jst $(call imports,platform) $(template_deps) $(template) $< $@ type=$* -tmp/platform_%.js : tmp/platform_%.coffee $(coffee_deps) - $(coffee) $< - node tools/globalize.js $@ $(subst $$,$(ESC_DOLLAR),$(call sources,platform)) +define compile +tmp/$1.js : tmp/$1.coffee $(coffee_deps) tools/globalize.js + $(coffee) $$< + node tools/globalize.js $1 +endef + +$(foreach p, \ + $(filter-out globals platform,$(parts)) platform_crx platform_userscript, \ + $(eval $(call compile,$(p))) \ +) tmp/eventPage.js : src/main/eventPage.coffee $(coffee_deps) | tmp $(coffee) -o tmp src/main/eventPage.coffee @@ -140,8 +163,8 @@ define rules_channel testbuilds/crx$1 : $$(MKDIR) -testbuilds/crx$1/script.js : src/meta/botproc.js $(subst platform,platform_crx,$(intermediate)) $(cat_deps) | testbuilds/crx$1 - $(cat) src/meta/botproc.js $(subst platform,platform_crx,$(intermediate)) $$@ +testbuilds/crx$1/script.js : src/meta/botproc.js $(call intermediate,crx) $(cat_deps) | testbuilds/crx$1 + $(cat) src/meta/botproc.js $(call intermediate,crx) $$@ testbuilds/crx$1/eventPage.js : tmp/eventPage.js | testbuilds/crx$1 $$(CP) @@ -166,8 +189,8 @@ testbuilds/$(name)$1.crx : testbuilds/$(name)$1.crx.zip package.json tools/sign. testbuilds/$(name)$1.meta.js : src/meta/metadata.js src/meta/icon48.png version.json $(template_deps) | testbuilds $(template) $$< $$@ type=userscript channel=$1 -testbuilds/$(name)$1.user.js : src/meta/botproc.js testbuilds/$(name)$1.meta.js $(subst platform,platform_userscript,$(intermediate)) $(cat_deps) - $(cat) src/meta/botproc.js testbuilds/$(name)$1.meta.js $(subst platform,platform_userscript,$(intermediate)) $$@ +testbuilds/$(name)$1.user.js : src/meta/botproc.js testbuilds/$(name)$1.meta.js $(call intermediate,userscript) $(cat_deps) + $(cat) src/meta/botproc.js testbuilds/$(name)$1.meta.js $(call intermediate,userscript) $$@ endef diff --git a/tools/declare.js b/tools/declare.js index a91fd3e7c..d08aa86b3 100644 --- a/tools/declare.js +++ b/tools/declare.js @@ -3,7 +3,7 @@ var fs = require('fs'); var names = []; for (var d of fs.readdirSync('src')) { for (var f of fs.readdirSync(`src/${d}`)) { - var m = f.match(/^([$A-Z][$\w]*)\.coffee$/); + var m = f.match(/^([$A-Z][$\w]*)\.(?:coffee|js)$/); if (m) names.push(m[1]); } } diff --git a/tools/globalize.js b/tools/globalize.js index 001783a3c..1f747e2cf 100644 --- a/tools/globalize.js +++ b/tools/globalize.js @@ -1,14 +1,16 @@ var fs = require('fs'); -var filename = process.argv[2]; -var sources = process.argv.slice(3); +var basename = process.argv[2].split('_')[0]; // e.g. template_crx -> template +var filename = `tmp/${process.argv[2]}.js`; + +// If it's not a collection of files in src/, it's a single file in src/main/ +var sources = (fs.readdirSync('src').indexOf(basename) >= 0) ? fs.readdirSync(`src/${basename}`) : [`${basename}.coffee`]; // Extract variables to be made global from source file list // e.g. ImageExpand from src/Images/ImageExpand.coffee // but not QR.post or eventPage var names = []; for (var f of sources) { - f = f.match(/[^/]*$/)[0]; var m = f.match(/^([$A-Z][$\w]*)\.coffee$/); if (m) names.push(m[1]); }