From 6da2b175e20ed3c03b0dfcfc9046de1e0e5d4444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Sat, 23 Nov 2019 00:05:30 +0800 Subject: =?UTF-8?q?=E6=AD=A3=E8=B7=AF=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/async/scanfiles.lua | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 script/async/scanfiles.lua (limited to 'script/async/scanfiles.lua') diff --git a/script/async/scanfiles.lua b/script/async/scanfiles.lua new file mode 100644 index 00000000..f5249c8e --- /dev/null +++ b/script/async/scanfiles.lua @@ -0,0 +1,55 @@ +local args = ... + +require 'utility' +local fs = require 'bee.filesystem' +local glob = require 'glob' + +local function scan(mode, root, pattern, options) + OUT:push('log', 'Scanning:', root:string()) + OUT:push('log', 'Scan pattern:', table.dump(pattern)) + OUT:push('log', 'Scan options:', table.dump(options)) + local session = glob.gitignore(pattern, options) + + session:setInterface('type', function (path) + local fullpath = root / path + if not fs.exists(fullpath) then + return nil + end + if fs.is_directory(fullpath) then + return 'directory' + else + return 'file' + end + return nil + end) + session:setInterface('list', function (path) + local fullpath = root / path + if not fs.exists(fullpath) then + return nil + end + local list = {} + for child in fullpath:list_directory() do + list[#list+1] = child:string() + end + return list + end) + + session:scan(function (path) + local ok, msg = IN:pop() + if ok and msg == 'stop' then + OUT:push 'stop' + return + end + OUT:push(mode, fs.absolute(root / path):string()) + end) +end + +for _, data in ipairs(args) do + local root = fs.path(data.root) + local suc, err = xpcall(scan, debug.traceback, data.mode, root, data.pattern, data.options) + if not suc then + ERR:push(err) + end +end + +OUT:push 'ok' -- cgit v1.2.3