aboutsummaryrefslogtreecommitdiff
path: root/files.moon
blob: 1a9c50a6bb757687a49aa1e2629117f2f0eb9f42 (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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
-- Some file utilities for searching for files recursively and using package.nomsupath
lpeg = require 'lpeg'
re = require 're'
Files = {}
assert package.nomsupath, "No package.nomsupath was found"

run_cmd = (cmd)->
    f = io.popen(cmd)
    lines = [line for line in f\lines!]
    return nil unless f\close!
    return lines
        

_SPOOFED_FILES = {}
_FILE_CACHE = setmetatable {}, __index:_SPOOFED_FILES
_BROWSE_CACHE = {}

-- Create a fake file and put it in the cache
Files.spoof = (filename, contents)->
    print("SPOOFING #{filename}")
    _SPOOFED_FILES[filename] = contents
    return contents

-- Read a file's contents (searching first locally, then in the nomsupath)
Files.read = (filename)->
    if file_contents = _FILE_CACHE[filename]
        return file_contents
    if filename == 'stdin'
        return Files.spoof('stdin', io.read('*a'))
    file = io.open(filename)
    return nil unless file
    contents = file\read("*a")
    file\close!
    _FILE_CACHE[filename] = contents
    return contents

-- `walk` returns an iterator over all files matching a path.
{:match, :gsub} = string

-- TODO: improve sanitization
sanitize = (path)->
    path = gsub(path,"\\","\\\\")
    path = gsub(path,"`","")
    path = gsub(path,'"','\\"')
    path = gsub(path,"$","")
    return path

Files.exists = (path)->
    return true if _SPOOFED_FILES[path]
    return true if path == 'stdin'
    return true if run_cmd("ls #{sanitize(path)}")
    for nomsupath in package.nomsupath\gmatch("[^;]+")
        return true if run_cmd("ls #{nomsupath}/#{sanitize(path)}")
    return false

browse = (path)->
    unless _BROWSE_CACHE[path]
        local files
        _BROWSE_CACHE[path] = if _SPOOFED_FILES[path]
            {path}
        else run_cmd('find -L "'..path..'" -not -path "*/\\.*" -type f') or false
    return _BROWSE_CACHE[path]

ok, lfs = pcall(require, "lfs")
if ok
    raw_file_exists = (filename)->
        mode = lfs.attributes(filename, 'mode')
        return if mode == 'file' or mode == 'directory' or mode == 'link' then true else false
    Files.exists = (path)->
        return true if _SPOOFED_FILES[path]
        return true if path == 'stdin' or raw_file_exists(path)
        for nomsupath in package.nomsupath\gmatch("[^;]+")
            return true if raw_file_exists(nomsupath.."/"..path)
        return false

    export browse
    browse = (filename)->
        unless _BROWSE_CACHE[filename]
            _BROWSE_CACHE[filename] = if _SPOOFED_FILES[filename] or filename == 'stdin'
                {filename}
            else
                file_type, err = lfs.attributes(filename, 'mode')
                switch file_type
                    when "file", "char device"
                        {filename}
                    when "directory", "link"
                        files = {}
                        for subfile in lfs.dir(filename)
                            continue if subfile == "." or subfile == ".."
                            for f in *(browse(filename.."/"..subfile) or {})
                                files[#files+1] = f
                        files
                    else false
        return _BROWSE_CACHE[filename]
else
    unless run_cmd('find . -maxdepth 0')
        url = if jit
            'https://github.com/spacewander/luafilesystem'
        else 'https://github.com/keplerproject/luafilesystem'
        error "Could not find 'luafilesystem' module and couldn't run system command `find` (this might happen on Windows). Please install `luafilesystem` (which can be found at: #{url} or `luarocks install luafilesystem`)", 0

Files.walk = (path, flush_cache=false)->
    if flush_cache
        export _BROWSE_CACHE
        _BROWSE_CACHE = {}
    local files
    if path == 'stdin'
        files = {path}
    else
        for nomsupath in package.nomsupath\gmatch("[^;]+")
            if files = browse(nomsupath.."/"..path) then break
    iter = (files, i)->
        return unless files
        i += 1
        if f = files[i]
            return i, f
    return iter, files, 0

line_counter = re.compile([[
    lines <- {| line (%nl line)* |}
    line <- {} (!%nl .)*
]], nl:lpeg.P("\r")^-1 * lpeg.P("\n"))

-- LINE_STARTS is a mapping from strings to a table that maps line number to character positions
_LINE_STARTS = {}
Files.get_line_starts = (str)->
    if type(str) != 'string'
        str = tostring(str)
    if starts = _LINE_STARTS[str]
        return starts
    line_starts = line_counter\match(str)
    _LINE_STARTS[str] = line_starts
    return line_starts

log = {}
Files.get_line_number = (str, pos)->
    line_starts = Files.get_line_starts(str)
    -- Binary search for line number of position
    lo, hi = 1, #line_starts
    while lo <= hi
        mid = math.floor((lo+hi)/2)
        if line_starts[mid] > pos
            hi = mid-1
        else lo = mid+1
    return hi

Files.get_line = (str, line_no)->
    line_starts = Files.get_line_starts(str)
    start = line_starts[line_no]
    return unless start
    stop = line_starts[line_no+1]
    return unless stop
    return str\sub(start, stop - 2)

get_lines = re.compile([[
    lines <- {| line (%nl line)* |}
    line <- {[^%nl]*}
]], nl:lpeg.P("\r")^-1 * lpeg.P("\n"))

Files.get_lines = (str)-> get_lines\match(str)

return Files