added -d, splitsp, sortedpairs, toplevel sort and disabled cycle checker

alfons-task
Nathan DECHER 4 years ago
parent c455f2c873
commit aec9df6861
  1. 36
      moonbuild.moon
  2. 19
      util.moon

@ -8,7 +8,7 @@ import truncate_traceback, rewrite_traceback from require 'moonscript.errors'
import trim from require 'moonscript.util' import trim from require 'moonscript.util'
util=require 'util' util=require 'util'
import exists, mtime, run, min, max, first, flatten, match, patsubst from util import exists, mtime, run, min, max, first, flatten, match, patsubst, sortedpairs from util
import insert, concat from table import insert, concat from table
@ -16,12 +16,15 @@ parser=argparse 'moonbuild'
parser\argument('targets', "Targets to run")\args '*' parser\argument('targets', "Targets to run")\args '*'
parser\flag '-a --noskip', "Always run targets" parser\flag '-a --noskip', "Always run targets"
parser\flag '-l --list', "List available targets" parser\flag '-l --list', "List available targets"
parser\flag '-d --deps', "List targets and their dependancies"
args=parser\parse! args=parser\parse!
-- util functions -- util functions
loadwithscope= (file, scope) -> loadwithscope= (file, scope) ->
fn=loadfile file fn, err=loadfile file
dumped=string.dump fn error err or "failed to load code" unless fn
dumped, err=string.dump fn
error err or "failed to dump function" unless dumped
load dumped, file, 'b', scope load dumped, file, 'b', scope
pcall= (fn, ...) -> pcall= (fn, ...) ->
rewrite=(err) -> rewrite=(err) ->
@ -48,7 +51,6 @@ class Command
class BuildObject class BuildObject
all={} all={}
skip={} skip={}
cycle={}
@find: (name) => @find: (name) =>
target=all[name] target=all[name]
@ -57,9 +59,11 @@ class BuildObject
return tgt if match name, glob return tgt if match name, glob
nil nil
@list: =>
{target, {dep, @find dep for dep in *target.deps} for name, target in pairs all}
@build: (name) => @build: (name) =>
target=@find name or error "No such target: #{name}" target=(@find name) or error "No such target: #{name}"
target\build name target\build name
__tostring: => __tostring: =>
@ -72,8 +76,6 @@ class BuildObject
build: (name) => build: (name) =>
return if skip[name] return if skip[name]
error "Can't build #{name}: cyclic depenancies found" if cycle[name]
cycle[name]=true
if @name!=name if @name!=name
@@build patsubst name, @name, dep for dep in *@deps @@build patsubst name, @name, dep for dep in *@deps
else else
@ -153,6 +155,7 @@ setmetatable buildscope,
file=first {'Build.moon', 'Buildfile.moon', 'Build', 'Buildfile'}, exists file=first {'Build.moon', 'Buildfile.moon', 'Build', 'Buildfile'}, exists
error "No Build.moon or Buildfile found" unless file error "No Build.moon or Buildfile found" unless file
buildfn=loadwithscope file, buildscope buildfn=loadwithscope file, buildscope
error "Failed to load build function" unless buildfn
ok, err=pcall buildfn ok, err=pcall buildfn
unless ok unless ok
if err if err
@ -166,6 +169,25 @@ if args.list
io.write "\t#{concat targets, ', '}\n" io.write "\t#{concat targets, ', '}\n"
os.exit 0 os.exit 0
if args.deps
io.write "Targets:\n"
for target, deps in sortedpairs BuildObject\list!, (a, b) -> a.name<b.name
io.write "\t#{target.name} "
if #target.ins==0
if #target.outs==0
io.write "[no in/out]"
else
io.write "[spontaneous generation]"
else
if #target.outs==0
io.write "[consumer]"
else
io.write "(#{concat target.ins, ', '} -> #{concat target.outs, ', '})"
io.write "\n"
for name, dep in sortedpairs deps
io.write "\t\t#{name} (#{dep.name})\n"
os.exit 0
if #args.targets==0 if #args.targets==0
BuildObject\build defaulttarget BuildObject\build defaulttarget
for target in *args.targets for target in *args.targets

@ -1,6 +1,6 @@
import attributes, dir from require 'lfs' import attributes, dir from require 'lfs'
import insert, concat from table import insert, concat, sort from table
unpack or=table.unpack unpack or=table.unpack
GLOB_PATT='^([^%%]*)%%([^%%]*)$' GLOB_PATT='^([^%%]*)%%([^%%]*)$'
@ -65,7 +65,8 @@ escapecmdpart= (p) ->
'"'..p\gsub('\\', '\\\\')\gsub('"', '\\"')..'"' '"'..p\gsub('\\', '\\\\')\gsub('"', '\\"')..'"'
escapecmd= (c, args={}) -> escapecmd= (c, args={}) ->
c=escapecmdpart c c=escapecmdpart c
c..=' '..escapecmdpart a for a in *args for a in *flatten args
c..=' '..escapecmdpart a if a
c c
run= (c, args, params={}) -> run= (c, args, params={}) ->
escaped=escapecmd c, args escaped=escapecmd c, args
@ -120,6 +121,9 @@ patsubst= (str, pattern, replacement) ->
return reprefix..(str\sub #prefix+1, -#suffix-1)..resuffix return reprefix..(str\sub #prefix+1, -#suffix-1)..resuffix
str str
splitsp= (str) ->
[elem for elem in str\gmatch '%S+']
-- glob match -- glob match
match= (str, glob) -> match= (str, glob) ->
prefix, suffix=glob\match GLOB_PATT prefix, suffix=glob\match GLOB_PATT
@ -137,14 +141,22 @@ isglob= (glob) ->
env= (key, def) -> env= (key, def) ->
(os.getenv key) or def (os.getenv key) or def
sortedpairs= (table, cmp) ->
keys = [k for k in pairs table]
sort keys, cmp
coroutine.wrap ->
for key in *keys
coroutine.yield key, table[key]
{ {
-- table functions -- table functions
:min, :max :min, :max
:foreach :foreach
:first :first
:insert, :unpack, :concat :insert, :unpack, :concat, :sort
:exclude :exclude
:flatten :flatten
:sortedpairs
-- file functions -- file functions
:wildcard :wildcard
@ -156,4 +168,5 @@ env= (key, def) ->
-- string functions -- string functions
:patsubst, :match, :isglob :patsubst, :match, :isglob
:env :env
:splitsp
} }

Loading…
Cancel
Save