From e98c5becba67d642b5fd5c6e4a206d79749e8953 Mon Sep 17 00:00:00 2001 From: s-ol Date: Thu, 22 Nov 2018 22:07:24 +1100 Subject: bundle binary files as URLs --- Tupfile | 2 +- Tuprules.lua | 10 ++ Tuprules.tup | 1 + build/bundle_fileder.moon | 155 +++++++++++++++++++++ build/bundle_module.moon | 51 +++++++ build/render_fileder.moon | 47 +++++++ build/util.moon | 16 +++ bundle.moon | 51 ------- bundle_fileder.moon | 126 ----------------- mmm/Tupdefault | 3 +- mmm/mmmfs/browser.moon | 4 +- mmm/mmmfs/fileder.moon | 1 + render.moon | 54 ------- root/Tupdefault.lua | 14 +- .../mmmfs/gallery/actual_image/image$png.png | Bin 0 -> 678429 bytes .../gallery/actual_image/preview: image$png.png | Bin 0 -> 31880 bytes .../vision-training-kit/description: text$plain | 1 - .../vision_training_kit/description: text$plain | 1 + root/games/vision_training_kit/icon: image$png.png | Bin 0 -> 16412 bytes 19 files changed, 291 insertions(+), 246 deletions(-) create mode 100644 build/bundle_fileder.moon create mode 100644 build/bundle_module.moon create mode 100644 build/render_fileder.moon create mode 100644 build/util.moon delete mode 100644 bundle.moon delete mode 100644 bundle_fileder.moon delete mode 100644 render.moon create mode 100644 root/articles/mmmfs/gallery/actual_image/image$png.png create mode 100644 root/articles/mmmfs/gallery/actual_image/preview: image$png.png delete mode 100644 root/games/vision-training-kit/description: text$plain create mode 100644 root/games/vision_training_kit/description: text$plain create mode 100755 root/games/vision_training_kit/icon: image$png.png diff --git a/Tupfile b/Tupfile index 30ea402..c955a33 100644 --- a/Tupfile +++ b/Tupfile @@ -12,4 +12,4 @@ include_rules : scss/main.scss |> !sassc |> root/main.css # bundle for client loading -: mmm/.bundle.lua | |> ^ WRAP %d^ moon bundle.moon %o --wrap %f |> root/mmm.bundle.lua +: mmm/.bundle.lua | |> ^ WRAP %d^ moon &(build)/bundle_module.moon %o --wrap %f |> root/mmm.bundle.lua diff --git a/Tuprules.lua b/Tuprules.lua index 77b1ed8..cd711a5 100644 --- a/Tuprules.lua +++ b/Tuprules.lua @@ -1,3 +1,13 @@ tup.creategitignore() root = tup.nodevariable '.' +build = tup.nodevariable 'build' + +function lua_path() + local LUA_PATH = {} + LUA_PATH += root .. '/?.lua' + LUA_PATH += root .. '/?.server.lua' + LUA_PATH += root .. '/?/init.lua' + LUA_PATH += root .. '/?/init.server.lua' + return 'LUA_PATH="' .. table.concat(LUA_PATH, ';') .. '"' +end diff --git a/Tuprules.tup b/Tuprules.tup index 3c408e7..eec602c 100644 --- a/Tuprules.tup +++ b/Tuprules.tup @@ -1,3 +1,4 @@ .gitignore &root = . +&build = build diff --git a/build/bundle_fileder.moon b/build/bundle_fileder.moon new file mode 100644 index 0000000..c9f8c35 --- /dev/null +++ b/build/bundle_fileder.moon @@ -0,0 +1,155 @@ +require 'mmm' +import get_path from require 'build.util' +import Fileder, Key from require 'mmm.mmmfs.fileder' +import opairs from require 'mmm.ordered' +import to_lua from require 'moonscript.base' + +-- usage: +-- moon bundle_fileder.moon ... -- ... +{ path_to_root, dirname } = arg + +assert path_to_root, "please specify the relative root path" +assert dirname, "please specify the fileder dirname" +path = get_path path_to_root + +facets = {} +children_bundles = {} + +do + addto = facets + for file in *arg[3,] + continue if file == 'Tupdefault.lua' + + if file == '--' + addto = children_bundles + continue + table.insert addto, file + +-- load a fs file as a fileder facet +load_facet = (filename) -> + key = (filename\match '(.*)%.%w+') or filename + key = Key key\gsub '%$', '/' + key.filename = filename + + file = assert (io.open filename, 'r'), "couldn't open facet file '#{filename}'" + value = file\read '*all' + file\close! + + key, value + +-- escape a string for lua aparser +escape = (str) -> string.format '%q', tostring str + +-- dump a fileder subtree as Lua source +dump_fileder = do + _dump = (fileder, root=false) -> + code = "Fileder {" + + for key, val in pairs fileder.facets + if key.original + key = "fromcache(#{escape key}, #{escape key.original})" + else + key = escape key + code ..= "\n[#{key}] = #{escape val}," + + for child in *fileder.children + code ..= "\n#{_dump child}," + + code ..= "\n}" + code + + + (fileder) -> " +local mmmfs = require 'mmm.mmmfs' +local Key, Fileder = mmmfs.Key, mmmfs.Fileder +local function fromcache(str, orig) + local key = Key(str) + key.original = Key(orig) + return key +end + +return #{_dump fileder, true} + " + +renders = { + { + inp: '^text/moonscript %-> (.*)' + out: 'text/lua -> %1' + render: (val, fileder, old, new) -> + lua, err = to_lua val + + if not lua + error "Error compiling #{old}: #{err}" + + "-- this moonscript facet has been transpiled to +-- '#{new}' for execution in the browser. +-- refer to the original facet as the source. +#{lua}" + }, + { + inp: '^image/' + out: 'URL -> %0' + render: (val, fileder, old, new) -> "#{fileder.path}/#{old.filename}", "[binary removed]" + }, + { + inp: '^video/' + out: 'URL -> %0' + render: (val, fileder, old, new) -> "#{fileder.path}/#{old.filename}", "[binary removed]" + }, +} + +with io.open '$bundle.lua', 'w' + \write dump_fileder with fileder = Fileder 'name: alpha': dirname + \mount path, true + + order = nil + children = {} + + for facet in *facets + if facet == '$order' + order = [line for line in io.lines facet] + continue + key, value = load_facet facet + .facets[key] = value + + extra_facets = {} + for key, value in pairs .facets + for { :inp, :out, :render } in *renders + continue unless key.type\match inp + + built_key = Key key.name, key.type\gsub inp, out + built_key.original = key + + -- dont overwrite existing keys + continue if \has built_key + + rendered, replace = render value, fileder, key, built_key + + extra_facets[built_key] = rendered + .facets[key] = replace if replace + + break + + for k,v in pairs extra_facets + .facets[k] = v + + for child in *children_bundles + -- @BUG: child bundles are malformed due to Tup bug ($ symbol) + child = child\gsub '/%.lua$', '/$bundle.lua' + + dirname = assert child\match '^([%w-_%.]+)/%$bundle%.lua$' + children[dirname] = dofile child + + if order + -- order from order file + for i, name in pairs order + child = assert children[name], "child in $order but not fs: #{name}" + table.insert .children, child + children[name] = nil + + -- sort remainder alphabeticalally + for name, child in opairs children + table.insert .children, child + warn "child #{name} not in $order!" if order + + \close! diff --git a/build/bundle_module.moon b/build/bundle_module.moon new file mode 100644 index 0000000..1c6b5f6 --- /dev/null +++ b/build/bundle_module.moon @@ -0,0 +1,51 @@ +output_name = assert arg[1], "please specify the output directory" + +escape = (str) -> string.format '%q', str + +readfile = (name) -> + file = io.open name, 'r' + with file\read '*all' + file\close + +with io.open output_name, 'w' + -- final wrap mode + if arg[2] == '--wrap' + assert #arg == 3, "too many arguments for 'wrap' mode" + bundle = arg[3] + + bundle = dofile bundle + \write "local p = package.preload\n" + for { :module, :file, :source } in *bundle + module = escape module + \write "if not p[#{module}] then p[#{module}] = load(#{escape source}, #{escape file}) end\n" + + -- iterative bundling mode + else + \write "return {\n" + + this = assert arg[2] + addmod = (module, file, source) -> + modname = "#{this}.#{module}" + modname = this if module == 'init' + \write " +{ + module = #{escape modname}, + file = #{escape "#{this}/#{file}"}, + source = #{escape source}, +}," + + for file in *arg[3,] + if dirname = file\match '^([%w-_]+)/%.bundle%.lua$' + bundle = dofile file + for { :module, :file, :source } in *bundle + addmod module, file, source + else + module = file\gsub '%.lua$', '' + continue if module\match '%.server' + module = module\gsub '%.client$', '' + module = module\gsub '%.init$', '' + addmod module, file, readfile file + + \write "}" + + \close! diff --git a/build/render_fileder.moon b/build/render_fileder.moon new file mode 100644 index 0000000..d76d162 --- /dev/null +++ b/build/render_fileder.moon @@ -0,0 +1,47 @@ +require 'mmm' +import render from require 'mmm.mmmfs' +import get_path from require 'build.util' + +-- usage: +-- moon render.moon +{ path_to_root } = arg + +assert path_to_root, "please specify the relative root path" +path = get_path path_to_root + +root = dofile '$bundle.lua' +assert root, "couldn't load $bundle.lua" +root\mount path, true + +content, rehydrate = render root, path +assert content, "no content" + +with io.open 'index.html', 'w' + \write " + + + + MMM: lunar low-gravity scripting playground + + + + + #{content} + + + + + + + + + #{rehydrate} + +" + \close! diff --git a/build/util.moon b/build/util.moon new file mode 100644 index 0000000..b9c9613 --- /dev/null +++ b/build/util.moon @@ -0,0 +1,16 @@ +require 'lfs' + +get_path = (root) -> + cwd = lfs.currentdir! + path = '' + + while root\find '^%.%./' + root = root\match '^%.%./(.*)' + cwd, trimmed = cwd\match '(.*)(/[^/]+)$' + path = trimmed .. path + + path + +{ + :get_path +} diff --git a/bundle.moon b/bundle.moon deleted file mode 100644 index 1c6b5f6..0000000 --- a/bundle.moon +++ /dev/null @@ -1,51 +0,0 @@ -output_name = assert arg[1], "please specify the output directory" - -escape = (str) -> string.format '%q', str - -readfile = (name) -> - file = io.open name, 'r' - with file\read '*all' - file\close - -with io.open output_name, 'w' - -- final wrap mode - if arg[2] == '--wrap' - assert #arg == 3, "too many arguments for 'wrap' mode" - bundle = arg[3] - - bundle = dofile bundle - \write "local p = package.preload\n" - for { :module, :file, :source } in *bundle - module = escape module - \write "if not p[#{module}] then p[#{module}] = load(#{escape source}, #{escape file}) end\n" - - -- iterative bundling mode - else - \write "return {\n" - - this = assert arg[2] - addmod = (module, file, source) -> - modname = "#{this}.#{module}" - modname = this if module == 'init' - \write " -{ - module = #{escape modname}, - file = #{escape "#{this}/#{file}"}, - source = #{escape source}, -}," - - for file in *arg[3,] - if dirname = file\match '^([%w-_]+)/%.bundle%.lua$' - bundle = dofile file - for { :module, :file, :source } in *bundle - addmod module, file, source - else - module = file\gsub '%.lua$', '' - continue if module\match '%.server' - module = module\gsub '%.client$', '' - module = module\gsub '%.init$', '' - addmod module, file, readfile file - - \write "}" - - \close! diff --git a/bundle_fileder.moon b/bundle_fileder.moon deleted file mode 100644 index 7bccf53..0000000 --- a/bundle_fileder.moon +++ /dev/null @@ -1,126 +0,0 @@ -require 'mmm' -import Fileder, Key from require 'mmm.mmmfs.fileder' -import opairs from require 'mmm.ordered' -import to_lua from require 'moonscript.base' -require 'lfs' - --- usage: --- moon bundle_fileder.moon ... -- ... -{ dirname } = arg - -assert dirname, "please specify the fileder dirname" - -facets = {} -children_bundles = {} - -do - addto = facets - for file in *arg[2,] - continue if file == 'Tupdefault.lua' - - if file == '--' - addto = children_bundles - continue - table.insert addto, file - --- load a fs file as a fileder facet -load_facet = (filename) -> - key = (filename\match '(.*)%.%w+') or filename - key = Key key\gsub '%$', '/' - - file = io.open filename, 'r' - value = file\read '*all' - file\close! - - key, value - --- escape a string for lua aparser -escape = (str) -> string.format '%q', tostring str - --- compile a moonscript facet to lua -compile = (old, new, val) -> - lua, err = to_lua val - - if not lua - error "Error compiling #{old}: #{err}" - - "-- this facet has been transpiled from '#{old}' --- to '#{new}' for execution in the browser. --- refer to the original facet as the source. -#{lua}" - --- dump a fileder subtree as Lua source -dump_fileder = do - _dump = (fileder, root=false) -> - code = "Fileder {" - - for key, val in pairs fileder.facets - if key.original - key = "fromcache(#{escape key}, #{escape key.original})" - else - key = escape key - code ..= "\n[#{key}] = #{escape val}," - - for child in *fileder.children - code ..= "\n#{_dump child}," - - code ..= "\n}" - code - - - (fileder) -> " -local mmmfs = require 'mmm.mmmfs' -local Key, Fileder = mmmfs.Key, mmmfs.Fileder -local function fromcache(str, orig) - local key = Key(str) - key.original = Key(orig) - return key -end - -return #{_dump fileder, true} - " - -with io.open '$bundle.lua', 'w' - \write dump_fileder with Fileder 'name: alpha': dirname - order = nil - children = {} - - for facet in *facets - if facet == '$order' - order = [line for line in io.lines facet] - continue - key, value = load_facet facet - .facets[key] = value - - extra_facets = {} - for key, value in pairs .facets - continue unless key.type\match '^text/moonscript %->' - built_key = Key key.name, key.type\gsub '^text/moonscript %-> (.*)', 'text/lua -> %1' - built_key.original = key - - continue if \has built_key - extra_facets[built_key] = compile key, built_key, value - - for k,v in pairs extra_facets - .facets[k] = v - - for child in *children_bundles - -- @BUG: child bundles are malformed due to Tup bug ($ symbol) - child = child\gsub '/%.lua$', '/$bundle.lua' - - dirname = assert child\match '^([%w-_%.]+)/%$bundle%.lua$' - children[dirname] = dofile child - - if order - -- order from order file - for i, name in pairs order - child = assert children[name], "child in $order but not fs: #{name}" - table.insert .children, child - children[name] = nil - - -- sort remainder alphabeticalally - for name, child in opairs children - table.insert .children, child - warn "child #{name} not in $order!" if order - - \close! diff --git a/mmm/Tupdefault b/mmm/Tupdefault index b827c36..3a0783f 100644 --- a/mmm/Tupdefault +++ b/mmm/Tupdefault @@ -1,4 +1,5 @@ include_rules + : foreach *.moon |> ^ MOON %f^ moonc -p > %o %f |> %B.lua -: |> ^ BNDL %d^ moon &(root)/bundle.moon %o %d % |> .bundle.lua ../ +: |> ^ BNDL %d^ moon &(build)/bundle_module.moon %o %d % |> .bundle.lua ../ diff --git a/mmm/mmmfs/browser.moon b/mmm/mmmfs/browser.moon index f654074..5038a3a 100644 --- a/mmm/mmmfs/browser.moon +++ b/mmm/mmmfs/browser.moon @@ -98,7 +98,7 @@ class Browser current = @facet\get! current = current and current.name with select :onchange, disabled: not fileder - has_main = fileder\find current.name, '.*' + has_main = fileder and fileder\find current.name, '.*' \append option '(main)', value: '', disabled: not has_main, selected: current == '' if fileder for i, value in ipairs fileder\get_facets! @@ -150,7 +150,7 @@ class Browser -- (re)set to match when @facet changes @inspect_prop = @facet\map (prop) -> active = @active\get! - key = active\find prop + key = active and active\find prop key = key.original if key and key.original key diff --git a/mmm/mmmfs/fileder.moon b/mmm/mmmfs/fileder.moon index 559e08b..114ec75 100644 --- a/mmm/mmmfs/fileder.moon +++ b/mmm/mmmfs/fileder.moon @@ -20,6 +20,7 @@ class Key @name = opts.name @type = opts.type @original = opts.original + @filename = opts.filename else error "wrong argument type: #{type opts}, #{type second}" diff --git a/render.moon b/render.moon deleted file mode 100644 index 4998452..0000000 --- a/render.moon +++ /dev/null @@ -1,54 +0,0 @@ -require 'mmm' -import render from require 'mmm.mmmfs' - --- usage: --- moon render.moon -{ root } = arg - -assert root, "please specify the relative root path as an argument" - -require 'lfs' -cwd = lfs.currentdir! -path = '' - -while root\find '^%.%./' - root = root\match '^%.%./(.*)' - cwd, trimmed = cwd\match '(.*)(/[^/]+)$' - path = trimmed .. path - -root = dofile '$bundle.lua' -assert root, "couldn't load $bundle.lua" -root\mount path, true - -content, rehydrate = render root, path -assert content, "no content" - -with io.open 'index.html', 'w' - \write " - - - - MMM: lunar low-gravity scripting playground - - - - - #{content} - - - - - - - - - #{rehydrate} - -" - \close! diff --git a/root/Tupdefault.lua b/root/Tupdefault.lua index 5cd45a9..039edf0 100644 --- a/root/Tupdefault.lua +++ b/root/Tupdefault.lua @@ -1,12 +1,6 @@ -LUA_PATH = {} -LUA_PATH += root .. '/?.lua' -LUA_PATH += root .. '/?.server.lua' -LUA_PATH += root .. '/?/init.lua' -LUA_PATH += root .. '/?/init.server.lua' -LUA_PATH = 'LUA_PATH="' .. table.concat(LUA_PATH, ';') .. '"' - -bundle = LUA_PATH .. ' moon ' .. root .. '/bundle_fileder.moon' -render = LUA_PATH .. ' moon ' .. root .. '/render.moon' +local LUA_PATH = lua_path() +bundle = LUA_PATH .. ' moon ' .. build .. '/bundle_fileder.moon' +render = LUA_PATH .. ' moon ' .. build .. '/render_fileder.moon' -- @TODO: whish there was a better glob for this? facets = tup.glob '*$*' @@ -20,7 +14,7 @@ facets += root .. '/' tup.rule( facets, - '^ BNDL %d^ ' .. bundle .. ' %d ' .. inputs .. ' -- %', + '^ BNDL %d^ ' .. bundle .. ' ' .. root .. ' %d ' .. inputs .. ' -- %', { '$bundle.lua', '../' } ) diff --git a/root/articles/mmmfs/gallery/actual_image/image$png.png b/root/articles/mmmfs/gallery/actual_image/image$png.png new file mode 100644 index 0000000..b499413 Binary files /dev/null and b/root/articles/mmmfs/gallery/actual_image/image$png.png differ diff --git a/root/articles/mmmfs/gallery/actual_image/preview: image$png.png b/root/articles/mmmfs/gallery/actual_image/preview: image$png.png new file mode 100644 index 0000000..f9dbfad Binary files /dev/null and b/root/articles/mmmfs/gallery/actual_image/preview: image$png.png differ diff --git a/root/games/vision-training-kit/description: text$plain b/root/games/vision-training-kit/description: text$plain deleted file mode 100644 index 0511e77..0000000 --- a/root/games/vision-training-kit/description: text$plain +++ /dev/null @@ -1 +0,0 @@ -a puzzle game based on a famicase cartridge design. diff --git a/root/games/vision_training_kit/description: text$plain b/root/games/vision_training_kit/description: text$plain new file mode 100644 index 0000000..0511e77 --- /dev/null +++ b/root/games/vision_training_kit/description: text$plain @@ -0,0 +1 @@ +a puzzle game based on a famicase cartridge design. diff --git a/root/games/vision_training_kit/icon: image$png.png b/root/games/vision_training_kit/icon: image$png.png new file mode 100755 index 0000000..ef73861 Binary files /dev/null and b/root/games/vision_training_kit/icon: image$png.png differ -- cgit v1.2.3