File size: 2,813 Bytes
7e9dc27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
--[[------------------------------------------------------

  # Directory helper

  This is a helper to access/glob directory trees. It requires
  'lfs' (lua filesystem).

--]]------------------------------------------------------
local lub = require 'lub'
local lfs = require 'lfs'
local match,        create,           resume,           yield,           huge =
      string.match, coroutine.create, coroutine.resume, coroutine.yield, math.huge

local lib = lub.class('lk.Dir', {
  sep = '/',
  ignore_pattern = '^[.]'
})

-- ## Dependencies
--
-- * lfs

-- # Class functions

-- Create a new directory helper pointing at `path`.
function lib.new(path)
  local self = {path = path}
  return setmetatable(self, lib)
end

local function glob_list(base, pattern, max_depth)
  for file in lfs.dir(base) do
    if not match(file, lib.ignore_pattern) then
      local fullpath = base..lib.sep..file
      local attrs = lfs.attributes(fullpath)
      if attrs and attrs.mode == 'file' then
        if not pattern or match(fullpath, pattern) then
          yield(fullpath)
        end
      elseif attrs and attrs.mode == 'directory' and max_depth > 0 then
        glob_list(fullpath, pattern, max_depth - 1)
      end
    end
  end
end

-- # Methods

-- Return an iterator to recursively find files matching `pattern` in the
-- directory. The pattern syntax is the same as string.match. Recursivity can
-- be altered by setting `max_depth` argument. 0 = do not enter sub-directories.
-- Default value for max_depth is math.huge.
--
--   -- Find paths ending in ".lua".
--   for path in lub.Dir('lub'):glob '%.lua$' do
--     print(path)
--   end
--   --> lub/Dir.lua
--   --> lub/Doc.lua
--   --> ...
function lib:glob(pattern, max_depth)
  local max_depth = max_depth or huge
  local co = create(glob_list)
  return function()
    local ok, value = resume(co, self.path, pattern, max_depth)
    if ok then
      return value
    else
      return nil
    end
  end
end

local function list_files(self)
  local base = self.path
  for file in lfs.dir(base) do
    if not match(file, self.ignore_pattern) then
      yield(base..self.sep..file)
    end
	end
end

-- Return an iterator over the paths in the directory. The returned values are
-- paths, not just filenames.
--
--   for file in lk.Dir('lib'):list() do
--     print(file)
--   end
--   --> lib/lk
--   --> lib/lk.lua
function lib:list()
  local co = create(list_files)
  return function()
    local ok, value = resume(co, self)
    if ok then
      return value
    else
      return nil
    end
  end
end

-- Return true if there is at least one child in the directory that matches
-- `pattern`.
function lib:contains(pattern)
  for file in lfs.dir(self.path) do
    if match(file, pattern) then
      return true
    end
	end
  return false
end

return lib