summaryrefslogtreecommitdiff
path: root/script/fs-utility.lua
blob: 8d2bf319f1d5bd9055ed9e3179dedd1ac20edd78 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
local fs           = require 'bee.filesystem'
local platform     = require 'bee.platform'

local type         = type
local ioOpen       = io.open
local pcall        = pcall
local pairs        = pairs
local setmetatable = setmetatable
local next         = next
local ipairs       = ipairs
local tostring     = tostring
local tableSort    = table.sort

_ENV = nil

---@class fs-utility
local m = {}
--- 读取文件
---@param path string|fs.path
function m.loadFile(path, keepBom)
    if type(path) ~= 'string' then
        ---@diagnostic disable-next-line: undefined-field
        path = path:string()
    end
    local f, e = ioOpen(path, 'rb')
    if not f then
        return nil, e
    end
    local text = f:read 'a'
    f:close()
    if not keepBom then
        if text:sub(1, 3) == '\xEF\xBB\xBF' then
            return text:sub(4)
        end
        if text:sub(1, 2) == '\xFF\xFE'
        or text:sub(1, 2) == '\xFE\xFF' then
            return text:sub(3)
        end
    end
    return text
end

--- 写入文件
---@param path any
---@param content string
function m.saveFile(path, content)
    if type(path) ~= 'string' then
        ---@diagnostic disable-next-line: undefined-field
        path = path:string()
    end
    local f, e = ioOpen(path, "wb")

    if f then
        f:write(content)
        f:close()
        return true
    else
        return false, e
    end
end

function m.relative(path, base)
    local sPath = fs.absolute(path):string()
    local sBase = fs.absolute(base):string()
    --TODO 先只支持最简单的情况
    if  sPath:sub(1, #sBase) == sBase
    and sPath:sub(#sBase + 1, #sBase + 1):match '^[/\\]' then
        return fs.path(sPath:sub(#sBase + 1):gsub('^[/\\]+', ''))
    end
    return nil
end

local function buildOption(option)
    option     = option     or {}
    option.add = option.add or {}
    option.del = option.del or {}
    option.mod = option.mod or {}
    option.err = option.err or {}
    return option
end

local function split(str, sep)
    local t = {}
    local current = 1
    while current <= #str do
        local s, e = str:find(sep, current)
        if not s then
            t[#t+1] = str:sub(current)
            break
        end
        if s > 1 then
            t[#t+1] = str:sub(current, s - 1)
        end
        current = e + 1
    end
    return t
end

---@class dummyfs
---@operator div(string|fs.path|dummyfs): dummyfs
---@field files table
local dfs = {}
dfs.__index = dfs
dfs.type = 'dummy'
dfs.path = ''

---@return dummyfs
function m.dummyFS(t)
    return setmetatable({
        files = t or {},
    }, dfs)
end

function dfs:__tostring()
    return 'dummy:' .. tostring(self.path)
end

function dfs:__div(filename)
    if type(filename) ~= 'string' then
        filename = filename:string()
    end
    local new = m.dummyFS(self.files)
    if self.path:sub(-1):match '[^/\\]' then
        new.path = self.path .. '\\' .. filename
    else
        new.path = self.path .. filename
    end
    return new
end

---@package
function dfs:_open(index)
    local paths = split(self.path, '[/\\]')
    local current = self.files
    if not index then
        index = #paths
    elseif index < 0 then
        index = #paths + index + 1
    end
    for i = 1, index do
        local path = paths[i]
        if current[path] then
            current = current[path]
        else
            return nil
        end
    end
    return current
end

---@package
function dfs:_filename()
    return self.path:match '[^/\\]+$'
end

function dfs:parent_path()
    local new = m.dummyFS(self.files)
    if self.path:find('[/\\]') then
        new.path = self.path:gsub('[/\\]+[^/\\]*$', '')
    else
        new.path = ''
    end
    return new
end

function dfs:filename()
    local new = m.dummyFS(self.files)
    new.path = self:_filename()
    return new
end

function dfs:string()
    return self.path
end

---@return fun(): dummyfs?
function dfs:listDirectory()
    local dir = self:_open()
    if type(dir) ~= 'table' then
        return function () end
    end
    local keys = {}
    for k in pairs(dir) do
        keys[#keys+1] = k
    end
    tableSort(keys)
    local i = 0
    return function ()
        i = i + 1
        local k = keys[i]
        if not k then
            return nil
        end
        return self / k
    end
end

function dfs:isDirectory()
    local target = self:_open()
    if type(target) == 'table' then
        return true
    end
    return false
end

function dfs:remove()
    local dir = self:_open(-2)
    local filename = self:_filename()
    if not filename then
        return
    end
    dir[filename] = nil
end

function dfs:exists()
    local target = self:_open()
    return target ~= nil
end

function dfs:createDirectories(path)
    if not path then
        return false
    end
    if type(path) ~= 'string' then
        path = path:string()
    end
    local paths = split(path, '[/\\]+')
    local current = self.files
    for i = 1, #paths do
        local sub = paths[i]
        if current[sub] then
            if type(current[sub]) ~= 'table' then
                return false
            end
        else
            current[sub] = {}
        end
        current = current[sub]
    end
    return true
end

function dfs:saveFile(path, text)
    if not path then
        return false, 'no path'
    end
    if type(path) ~= 'string' then
        path = path:string()
    end
    local temp = m.dummyFS(self.files)
    temp.path = path
    local dir = temp:_open(-2)
    if not dir then
        return false, '无法打开:' .. path
    end
    local filename = temp:_filename()
    if not filename then
        return false, '无法打开:' .. path
    end
    if type(dir[filename]) == 'table' then
        return false, '无法打开:' .. path
    end
    dir[filename] = text
    return true
end

---@param path   string|fs.path|dummyfs
---@param option table
---@return fs.path|dummyfs?
local function fsAbsolute(path, option)
    if type(path) == 'string' then
        local suc, res = pcall(fs.path, path)
        if not suc then
            option.err[#option.err+1] = res
            return nil
        end
        path = res
    elseif type(path) == 'table' then
        return path
    end
    local suc, res = pcall(fs.absolute, path)
    if not suc then
        option.err[#option.err+1] = res
        return nil
    end
    return res
end

local function fsIsDirectory(path, option)
    if not path then
        return false
    end
    if path.type == 'dummy' then
        return path:isDirectory()
    end
    ---@cast path -dummyfs
    local status = fs.symlink_status(path):type()
    return status == 'directory'
end

---@param path fs.path|dummyfs|nil
---@param option table
---@return fun(): fs.path|dummyfs|nil
local function fsPairs(path, option)
    if not path then
        return function () end
    end
    if path.type == 'dummy' then
        return path:listDirectory()
    end
    local suc, res = pcall(fs.pairs, path)
    if not suc then
        option.err[#option.err+1] = res
        return function () end
    end
    return res
end

local function fsRemove(path, option)
    if not path then
        return false
    end
    if path.type == 'dummy' then
        return path:remove()
    end
    local suc, res = pcall(fs.remove, path)
    if not suc then
        option.err[#option.err+1] = res
    end
    option.del[#option.del+1] = path:string()
end

local function fsExists(path, option)
    if not path then
        return false
    end
    if path.type == 'dummy' then
        return path:exists()
    end
    local suc, res = pcall(fs.exists, path)
    if not suc then
        option.err[#option.err+1] = res
        return false
    end
    return res
end

local function fsSave(path, text, option)
    if not path then
        return false
    end
    if path.type == 'dummy' then
        ---@cast path -fs.path
        local dir = path:_open(-2)
        if not dir then
            option.err[#option.err+1] = '无法打开:' .. path:string()
            return false
        end
        local filename = path:_filename()
        if not filename then
            option.err[#option.err+1] = '无法打开:' .. path:string()
            return false
        end
        if type(dir[filename]) == 'table' then
            option.err[#option.err+1] = '无法打开:' .. path:string()
            return false
        end
        dir[filename] = text
    else
        local suc, err = m.saveFile(path, text)
        if suc then
            return true
        end
        option.err[#option.err+1] = err
        return false
    end
end

local function fsLoad(path, option)
    if not path then
        return nil
    end
    if path.type == 'dummy' then
        local text = path:_open()
        if type(text) == 'string' then
            return text
        else
            option.err[#option.err+1] = '无法打开:' .. path:string()
            return nil
        end
    else
        ---@cast path -dummyfs
        local text, err = m.loadFile(path)
        if text then
            return text
        else
            option.err[#option.err+1] = err
            return nil
        end
    end
end

local function fsCopy(source, target, option)
    if not source or not target then
        return
    end
    if source.type == 'dummy' then
        local sourceText = source:_open()
        if not sourceText then
            option.err[#option.err+1] = '无法打开:' .. source:string()
            return false
        end
        return fsSave(target, sourceText, option)
    else
        ---@cast source -dummyfs
        if target.type == 'dummy' then
            local sourceText, err = m.loadFile(source)
            if not sourceText then
                option.err[#option.err+1] = err
                return false
            end
            return fsSave(target, sourceText, option)
        else
            local suc, res = pcall(fs.copy_file, source, target, fs.copy_options.overwrite_existing)
            if not suc then
                option.err[#option.err+1] = res
                return false
            end
        end
    end
    return true
end

---@param path dummyfs|fs.path
---@param option table
local function fsCreateDirectories(path, option)
    if not path then
        return
    end
    if path.type == 'dummy' then
        return path:createDirectories()
    end
    local suc, res = pcall(fs.create_directories, path)
    if not suc then
        option.err[#option.err+1] = res
        return false
    end
    return true
end

local function fileRemove(path, option)
    if not path then
        return
    end
    if option.onRemove and option.onRemove(path) == false then
        return
    end
    if fsIsDirectory(path, option) then
        for child in fsPairs(path, option) do
            fileRemove(child, option)
        end
    end
    if fsRemove(path, option) then
        option.del[#option.del+1] = path:string()
    end
end

---@param source fs.path|dummyfs?
---@param target fs.path|dummyfs?
---@param option table
local function fileCopy(source, target, option)
    if not source or not target then
        return
    end
    local isDir1   = fsIsDirectory(source, option)
    local isDir2   = fsIsDirectory(target, option)
    local isExists = fsExists(target, option)
    if isDir1 then
        if isDir2 or fsCreateDirectories(target, option) then
            for filePath in fsPairs(source, option) do
                local name = filePath:filename():string()
                fileCopy(filePath, target / name, option)
            end
        end
    else
        if isExists and not isDir2 then
            local buf1 = fsLoad(source, option)
            local buf2 = fsLoad(target, option)
            if buf1 and buf2 then
                if buf1 ~= buf2 then
                    if fsCopy(source, target, option) then
                        option.mod[#option.mod+1] = target:string()
                    end
                end
            end
        else
            if fsCopy(source, target, option) then
                option.add[#option.add+1] = target:string()
            end
        end
    end
end

---@param source fs.path|dummyfs?
---@param target fs.path|dummyfs?
---@param option table
local function fileSync(source, target, option)
    if not source or not target then
        return
    end
    local isDir1   = fsIsDirectory(source, option)
    local isDir2   = fsIsDirectory(target, option)
    local isExists = fsExists(target, option)
    if isDir1 then
        if isDir2 then
            local fileList = m.fileList()
            if type(target) == 'table' then
                ---@cast target dummyfs
                for filePath in target:listDirectory() do
                    fileList[filePath] = true
                end
            else
                ---@cast target fs.path
                for filePath in fs.pairs(target) do
                    fileList[filePath] = true
                end
            end
            for filePath in fsPairs(source, option) do
                local name = filePath:filename():string()
                local targetPath = target / name
                fileSync(filePath, targetPath, option)
                fileList[targetPath] = nil
            end
            for path in pairs(fileList) do
                fileRemove(path, option)
            end
        else
            if isExists then
                fileRemove(target, option)
            end
            if fsCreateDirectories(target, option) then
                for filePath in fsPairs(source, option) do
                    local name = filePath:filename():string()
                    fileCopy(filePath, target / name, option)
                end
            end
        end
    else
        if isDir2 then
            fileRemove(target, option)
        end
        if isExists then
            local buf1 = fsLoad(source, option)
            local buf2 = fsLoad(target, option)
            if buf1 and buf2 then
                if buf1 ~= buf2 then
                    if fsCopy(source, target, option) then
                        option.mod[#option.mod+1] = target:string()
                    end
                end
            end
        else
            if fsCopy(source, target, option) then
                option.add[#option.add+1] = target:string()
            end
        end
    end
end

--- 文件列表
function m.fileList(option)
    option = option or buildOption(option)
    local os = platform.OS
    local keyMap = {}
    local fileList = {}
    local function computeKey(path)
        path = fsAbsolute(path, option)
        if not path then
            return nil
        end
        return path:string()
    end
    return setmetatable({}, {
        __index = function (_, path)
            local key = computeKey(path)
            return fileList[key]
        end,
        __newindex = function (_, path, value)
            local key = computeKey(path)
            if not key then
                return
            end
            if value == nil then
                keyMap[key] = nil
            else
                keyMap[key] = path
                fileList[key] = value
            end
        end,
        __pairs = function ()
            local key, path
            return function ()
                key, path = next(keyMap, key)
                return path, fileList[key]
            end
        end,
    })
end

--- 删除文件(夹)
function m.fileRemove(path, option)
    option = buildOption(option)
    path = fsAbsolute(path, option)

    fileRemove(path, option)

    return option
end

--- 复制文件(夹)
---@param source string|fs.path|dummyfs
---@param target string|fs.path|dummyfs
---@return table
function m.fileCopy(source, target, option)
    option = buildOption(option)
    local fsSource = fsAbsolute(source, option)
    local fsTarget = fsAbsolute(target, option)

    fileCopy(fsSource, fsTarget, option)

    return option
end

--- 同步文件(夹)
---@param source string|fs.path|dummyfs
---@param target string|fs.path|dummyfs
---@return table
function m.fileSync(source, target, option)
    option = buildOption(option)
    local fsSource = fsAbsolute(source, option)
    local fsTarget = fsAbsolute(target, option)

    fileSync(fsSource, fsTarget, option)

    return option
end

---@param dir fs.path
---@param callback fun(fullPath: fs.path)
function m.scanDirectory(dir, callback)
    for fullpath in fs.pairs(dir) do
        local status = fs.symlink_status(fullpath):type()
        if status == 'directory' then
            m.scanDirectory(fullpath, callback)
        elseif status == 'regular' then
            callback(fullpath)
        end
    end
end

function m.listDirectory(dir)
    if dir.type == 'dummy' then
        return dir:listDirectory()
    else
        return fs.pairs(dir)
    end
end

return m