summaryrefslogtreecommitdiff
path: root/script/src/async/scanfiles.lua
blob: f5249c8ebc887ff51e77b917b572a2888b54fe5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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'