mirror of
https://github.com/natnat-mc/moonbuild
synced 2026-06-11 13:39:39 +02:00
added spec for stringutil, tableutil and the pure part of fsutil
This commit is contained in:
+21
-2
@@ -16,6 +16,8 @@ makecached = (fn) ->
|
||||
cache = {}
|
||||
|
||||
get = (val) ->
|
||||
if cache == FROZEN
|
||||
return fn val
|
||||
cached = cache[val]
|
||||
if cached!=FROZEN and cached!=nil
|
||||
return unpack cached
|
||||
@@ -24,10 +26,27 @@ makecached = (fn) ->
|
||||
cache[val] = ret
|
||||
unpack ret
|
||||
|
||||
setmetatable { :get, :invalidate, :freeze, :clear },
|
||||
enable = ->
|
||||
cache = {} if cache==FROZEN
|
||||
|
||||
disable = ->
|
||||
cache = FROZEN
|
||||
|
||||
setmetatable { :get, :invalidate, :freeze, :clear, :enable, :disable },
|
||||
__call: (val) => get val
|
||||
|
||||
{
|
||||
cached = {
|
||||
attributes: makecached attributes
|
||||
dir: makecached (file) -> [k for k in dir file]
|
||||
}
|
||||
|
||||
enable = ->
|
||||
fn\enable! for _, fn in cached
|
||||
|
||||
disable = ->
|
||||
fn\disable! for _, fn in cached
|
||||
|
||||
clear = ->
|
||||
fn\clear! for _, fn in cached
|
||||
|
||||
setmetatable { :enable, :disable, :clear }, __index: cached
|
||||
|
||||
+12
-12
@@ -1,4 +1,4 @@
|
||||
import dir, attributes from require 'moonbuild.fscache'
|
||||
import dir, attributes, clear, enable, disable from require 'moonbuild.fscache'
|
||||
|
||||
import gmatch, match, gsub, sub from string
|
||||
import insert, remove, concat from table
|
||||
@@ -6,18 +6,19 @@ import insert, remove, concat from table
|
||||
normalizepath = (file) ->
|
||||
parts = [part for part in gmatch file, '[^/]+']
|
||||
absolute = (sub file, 1, 1)=='/'
|
||||
for i=1, #parts
|
||||
i = 1
|
||||
while i<=#parts
|
||||
if parts[i]=='.'
|
||||
remove parts, i
|
||||
i -= 1
|
||||
continue
|
||||
if parts[i]=='..' and i!=1
|
||||
if parts[i]=='..' and i!=1 and parts[i-1]!='..'
|
||||
remove parts, i
|
||||
remove parts, i-1
|
||||
i -= 2
|
||||
i -= 1
|
||||
continue
|
||||
i += 1
|
||||
if #parts==0
|
||||
'.'
|
||||
absolute and '/' or '.'
|
||||
else
|
||||
(absolute and '/' or '') .. concat parts, '/'
|
||||
|
||||
@@ -40,7 +41,8 @@ mtime = (f) ->
|
||||
a and a.modification
|
||||
|
||||
matchglob = (str, glob) ->
|
||||
patt = '^'..(gsub (gsub glob, '%*%*', '.*'), '%*', '[^/]*')..'$'
|
||||
glob = gsub glob, '[%[%]%%+.?-]', => '%'..@
|
||||
patt = '^'..(gsub glob, '%*%*?', => @=='**' and '.*' or '[^/]*')..'$'
|
||||
rst = if (type str)=='table'
|
||||
results, i = {}, 1
|
||||
for s in *str
|
||||
@@ -111,14 +113,12 @@ invalidatecache = (file) ->
|
||||
dir.invalidate parentdir file
|
||||
attributes.invalidate file
|
||||
|
||||
clearcache = ->
|
||||
dir.clear!
|
||||
attributes.clear!
|
||||
|
||||
{
|
||||
:wildcard
|
||||
:exists, :isdir
|
||||
:mtime
|
||||
:normalizepath, :parentdir
|
||||
:freezecache, :invalidatecache, :clearcache
|
||||
:matchglob
|
||||
:freezecache, :invalidatecache
|
||||
clearcache: clear, enablecache: enable, disablecache: disable
|
||||
}
|
||||
|
||||
@@ -5,13 +5,25 @@ GLOB_PATT='^([^%%]*)%%([^%%]*)$'
|
||||
|
||||
patsubst = (str, pattern, replacement) ->
|
||||
return [patsubst s, pattern, replacement for s in *str] if (type str)=='table'
|
||||
prefix, suffix = match pattern, GLOB_PATT
|
||||
return str unless prefix
|
||||
reprefix, resuffix = match replacement, GLOB_PATT
|
||||
return replacement unless reprefix
|
||||
|
||||
if (sub str, 1, #prefix)==prefix and (sub str, -#suffix)==suffix
|
||||
return reprefix..(sub str, #prefix+1, -#suffix-1)..resuffix
|
||||
if str==pattern
|
||||
return replacement
|
||||
|
||||
prefix, suffix = match pattern, GLOB_PATT
|
||||
if not (prefix or suffix)
|
||||
return str
|
||||
|
||||
reprefix, resuffix = match replacement, GLOB_PATT
|
||||
if not (reprefix or resuffix)
|
||||
if (#prefix==0 or (sub str, 1, #prefix)==prefix) and (#suffix==0 or (sub str, -#suffix)==suffix)
|
||||
return replacement
|
||||
else
|
||||
return str
|
||||
|
||||
if #prefix==0 or (sub str, 1, #prefix)==prefix
|
||||
str = reprefix..(sub str, #prefix+1)
|
||||
if #suffix==0 or (sub str, -#suffix)==suffix
|
||||
str = (sub str, 1, -#suffix-1)..resuffix
|
||||
str
|
||||
|
||||
splitsp = (str) ->
|
||||
|
||||
@@ -48,7 +48,10 @@ flatten = (tab) ->
|
||||
out = {}
|
||||
for e in *tab
|
||||
if (type e)=='table'
|
||||
insert out, v for v in *flatten e
|
||||
if e[1] == nil and (next e)!=nil
|
||||
insert out, e
|
||||
else
|
||||
insert out, v for v in *flatten e
|
||||
else
|
||||
insert out, e
|
||||
out
|
||||
|
||||
Reference in New Issue
Block a user