Changeset 39


Ignore:
Timestamp:
05/01/2011 11:13:36 PM (2 years ago)
Author:
reyalP
Message:

add simple dependency system to rlib

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lua/chdku.lua

    r38 r39  
    3939end 
    4040 
    41 --[[ 
     41-- TODO this should be split out into it's own module(s) 
     42--[[ 
     43simple library system for building remote commands 
    4244chunks of source code to be used remotely 
    4345can be used with chdku.exec 
    4446TODO some of these are duplicated with local code, but we don't yet have an easy way of sharing them 
    4547TODO would be good to minify 
    46 TODO handle order and dependencies 
    4748TODO passing compiled chunks might be better but our lua configurations are different 
    4849]] 
    49 chdku.rlib={ 
     50local rlibs = { 
     51        libs={}, 
     52} 
     53 
     54--[[ 
     55register{ 
     56        name='libname' 
     57        depend={'lib1','lib2'...}, -- already registered rlibs this one requires (cyclic deps not allowed) 
     58        code='', -- main lib code. 
     59} 
     60]] 
     61function rlibs:register(t) 
     62        if type(t.depend) == 'nil' then 
     63                t.depend = {} 
     64        elseif type(t.depend) ~= 'table' then 
     65                error('expected dependency table') 
     66        end 
     67        if type(t.code) ~= 'string' then 
     68                error('expected code string') 
     69        end 
     70        if type(t.name) ~= 'string' then 
     71                error('expected name string') 
     72        end 
     73        for i,v in ipairs(t.depend) do 
     74                if not self.libs[v] then 
     75                        errf('%s missing dep %s\n',t.name,v) 
     76                end 
     77        end 
     78        self.libs[t.name] = t 
     79end 
     80 
     81--[[ 
     82register an array of libs 
     83]] 
     84function rlibs:register_array(t) 
     85        for i,v in ipairs(t) do 
     86                self:register(v) 
     87        end 
     88end 
     89 
     90--[[ 
     91add deps for a single lib 
     92]] 
     93function rlibs:build_single(build,name) 
     94        local lib = self.libs[name] 
     95        if not lib then 
     96                errf('unknown lib %s\n',tostring(name)) 
     97        end 
     98        -- already added 
     99        if build.map[name] then 
     100                return 
     101        end 
     102        for i,dname in ipairs(lib.depend) do 
     103                self:build_single(build,dname) 
     104        end 
     105        build.map[name]=lib 
     106        table.insert(build.list,lib) 
     107end 
     108--[[ 
     109return a list of rlibs in dependency order 
     110]] 
     111function rlibs:build_list(libnames) 
     112        local build={ 
     113                list={}, 
     114                map={}, 
     115        } 
     116        for i,name in ipairs(libnames) do 
     117                self:build_single(build,name) 
     118        end 
     119        return build.list 
     120end 
     121--[[ 
     122return a string containing all the required rlib code 
     123]] 
     124function rlibs:build(names) 
     125        local liblist = self:build_list(names) 
     126        -- TODO would be good to keep a map of line numbers here somehow 
     127        -- or possibly exec should keep the code around ? 
     128        local code="" 
     129        for i,lib in ipairs(liblist) do 
     130                code = code .. lib.code .. '\n' 
     131        end 
     132        return code 
     133end 
     134 
     135rlibs:register_array{ 
    50136--[[ 
    51137mostly duplicated from util.serialize 
    52138global defaults can be changed from code 
    53139]] 
    54         serialize=[[ 
     140{ 
     141        name='serialize', 
     142        code=[[ 
    55143serialize_r = function(v,opts,seen,depth) 
    56144        local vt = type(v) 
     
    119207        return serialize_r(v,opts) 
    120208end 
    121  
    122209]], 
     210}, 
    123211-- override default table serialization for messages 
    124         serialize_msgs=[[ 
     212{ 
     213        name='serialize_msgs', 
     214        depend={'serialize'}, 
     215        code=[[ 
    125216        usb_msg_table_to_string=serialize 
    126217]], 
     218}, 
    127219--[[ 
    128220        status[,err]=dir_iter(path,func,opts) 
     
    132224func is called with a nil filename after listing is complete 
    133225]] 
    134         dir_iter=[[ 
     226{ 
     227        name='dir_iter', 
     228        code=[[ 
    135229function dir_iter(path,func,opts) 
    136230        if not opts then 
     
    151245end 
    152246]], 
     247}, 
    153248--[[ 
    154249function to batch stuff in groups of messages 
     
    161256b:flush() sends any remaining items 
    162257]] 
    163         msg_batch=[[ 
     258{ 
     259        name='msg_batcher', 
     260        depend={'serialize_msgs'}, 
     261        code=[[ 
    164262function msg_batcher(opts_in) 
    165263        local t = { 
     
    195293end 
    196294]], 
     295}, 
    197296--[[ 
    198297retrieve a directory listing of names, batched in messages 
    199298]] 
    200         ls_simple=[[ 
     299{ 
     300        name='ls_simple', 
     301        depend={'msg_batcher'}, 
     302        code=[[ 
    201303function ls_simple(path) 
    202304        local b=msg_batcher() 
     
    213315end 
    214316]], 
     317}, 
    215318--[[ 
    216319TODO rework this to a general iterate over directory function 
     
    242345TODO handle case if 'path' is a file 
    243346]] 
    244         ls=[[ 
     347{ 
     348        name='ls', 
     349        depend={'serialize_msgs'}, 
     350        code=[[ 
    245351function ls(path,opts_in) 
    246352        local opts={ 
     
    293399end 
    294400]], 
    295 } 
    296  
     401}, 
     402} 
     403 
     404chdku.rlibs = rlibs 
    297405--[[ 
    298406opts may be a table, or a string containing lua code for a table 
     
    310418                { 
    311419                        wait=true, 
    312                         libs={'serialize','serialize_msgs','ls'}, 
     420                        libs={'ls'}, 
    313421                        msgs=function(msg) 
    314422                                if msg.subtype ~= 'table' then 
     
    368476        local opts = extend_table({},opts_in) 
    369477        if opts.libs then 
    370                 local libcode='' 
    371                 for k,v in ipairs(opts.libs) do 
    372                         if chdku.rlib[v] then 
    373                                 libcode = libcode .. chdku.rlib[v]; 
    374                         else 
    375                                 return false,'unknown rlib'..v 
    376                         end 
    377                 end 
    378                 code = libcode .. code 
     478                code = chdku.rlibs:build(opts.libs) .. code 
    379479        end 
    380480        local status,err=chdk.execlua(code) 
Note: See TracChangeset for help on using the changeset viewer.