-
Notifications
You must be signed in to change notification settings - Fork 0
/
howl.lua
6952 lines (5907 loc) · 171 KB
/
howl.lua
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
The MIT License (MIT)
Copyright (c) 2015-2016 SquidDev
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]--
local loading = {}
local oldRequire, preload, loaded = require, {}, { startup = loading }
local function require(name)
local result = loaded[name]
if result ~= nil then
if result == loading then
error("loop or previous error loading module '" .. name .. "'", 2)
end
return result
end
loaded[name] = loading
local contents = preload[name]
if contents then
result = contents(name)
elseif oldRequire then
result = oldRequire(name)
else
error("cannot load '" .. name .. "'", 2)
end
if result == nil then result = true end
loaded[name] = result
return result
end
preload["howl.tasks.Task"] = function(...)
--- The main task class
-- @classmod howl.tasks.Task
local assert = require "howl.lib.assert"
local class = require "howl.class"
local colored = require "howl.lib.colored"
local mixin = require "howl.class.mixin"
local os = require "howl.platform".os
local utils = require "howl.lib.utils"
local insert = table.insert
--- Convert a pattern
local function parsePattern(from, to)
local fromParsed = utils.parsePattern(from, true)
local toParsed = utils.parsePattern(to)
local newType = fromParsed.Type
assert(newType == toParsed.Type, "Both from and to must be the same type " .. newType .. " and " .. fromParsed.Type)
return { Type = newType, From = fromParsed.Text, To = toParsed.Text }
end
local Task = class("howl.tasks.Task")
:include(mixin.configurable)
:include(mixin.optionGroup)
:addOptions { "description" }
--- Create a task
-- @tparam string name The name of the task
-- @tparam table dependencies A list of tasks this task requires
-- @tparam function action The action to run
-- @treturn Task The created task
function Task:initialize(name, dependencies, action)
assert.argType(name, "string", "Task", 1)
-- Check calling with no dependencies
if type(dependencies) == "function" then
action = dependencies
dependencies = {}
end
self.options = {}
self.name = name -- The name of the function
self.action = action -- The action to call
self.dependencies = {} -- Task dependencies
self.maps = {} -- Reads and produces list
self.produces = {} -- Files this task produces
if dependencies then self:depends(dependencies) end
end
function Task.static:addDependency(class, name)
local function apply(self, ...)
if select('#', ...) == 1 and type(...) == "table" and (#(...) > 0 or next(...) == nil) then
local first = ...
for i = 1, #first do
insert(self.dependencies, class(self, first[i]))
end
else
insert(self.dependencies, class(self, ...))
end
return self
end
self[name] = apply
self[name:gsub("^%l", string.upper)] = apply
return self
end
function Task:setup(context, runner) end
--- Sets a file this task produces
-- @tparam string|table file The path of the file
-- @treturn Task The current object (allows chaining)
function Task:Produces(file)
if type(file) == "table" then
local produces = self.produces
for _, file in ipairs(file) do
table.insert(produces, file)
end
else
table.insert(self.produces, file)
end
return self
end
--- Sets a file mapping
-- @tparam string from The file to map form
-- @tparam string to The file to map to
-- @treturn Task The current object (allows chaining)
function Task:Maps(from, to)
table.insert(self.maps, parsePattern(from, to))
return self
end
--- Set the action for this task
-- @tparam function action The action to run
-- @treturn Task The current object (allows chaining)
function Task:Action(action)
self.action = action
return self
end
--- Run the action with no bells or whistles
function Task:runAction(context, ...)
if self.action then
return self.action(self, context, ...)
else
return true
end
end
--- Execute the task
-- @tparam Context.Context context The task context
-- @param ... The arguments to pass to task
-- @tparam boolean Success
function Task:Run(context, ...)
local shouldRun = false
if #self.dependencies == 0 then
shouldRun = true
else
for _, depends in ipairs(self.dependencies) do
if depends:resolve(context.env, context) then
shouldRun = true
end
end
end
if not shouldRun then return false end
for _, file in ipairs(self.produces) do
context.filesProduced[file] = true
end
-- Technically we don't need to specify an action
local args = { ... }
local description = ""
-- Get a list of arguments
if #args > 0 then
local newArgs = {}
for _, arg in ipairs(args) do
table.insert(newArgs, tostring(arg))
end
description = " (" .. table.concat(newArgs, ", ") .. ")"
end
context.env.logger:info("Running %s", self.name .. description)
local oldTime = os.clock()
local s, err = true, nil
if context.Traceback then
xpcall(function() self:runAction(context.env, unpack(args)) end, function(msg)
for i = 5, 15 do
local _, err = pcall(function() error("", i) end)
if msg:match("Howlfile") then break end
msg = msg .. "\n " .. err
end
err = msg
s = false
end)
else
s, err = pcall(self.runAction, self, context.env, ...)
end
if s then
context.env.logger:success("%s finished", self.name)
else
context.env.logger:error("%s: %s", self.name, err or "no message")
error("Error running tasks", 0)
end
if context.ShowTime then
print(" ", "Took " .. os.clock() - oldTime .. "s")
end
return true
end
return Task
end
preload["howl.tasks.Runner"] = function(...)
--- Handles tasks and dependencies
-- @classmod howl.tasks.Runner
local class = require "howl.class"
local colored = require "howl.lib.colored"
local Context = require "howl.tasks.Context"
local mixin = require "howl.class.mixin"
local os = require "howl.platform".os
local Task = require "howl.tasks.Task"
--- Handles a collection of tasks and running them
-- @type Runner
local Runner = class("howl.tasks.Runner"):include(mixin.sealed)
--- Create a @{Runner} object
-- @tparam env env The current environment
-- @treturn Runner The created runner object
function Runner:initialize(env)
self.tasks = {}
self.default = nil
self.env = env
end
function Runner:setup()
for _, task in pairs(self.tasks) do
task:setup(self.env, self)
end
if self.env.logger.hasError then return false end
for _, task in pairs(self.tasks) do
for _, dependency in ipairs(task.dependencies) do
dependency:setup(self.env, self)
end
end
if self.env.logger.hasError then return false end
return true
end
--- Create a task
-- @tparam string name The name of the task to create
-- @treturn function A builder for tasks
function Runner:Task(name)
return function(dependencies, action) return self:addTask(name, dependencies, action) end
end
--- Add a task to the collection
-- @tparam string name The name of the task to add
-- @tparam table dependencies A list of tasks this task requires
-- @tparam function action The action to run
-- @treturn Task The created task
function Runner:addTask(name, dependencies, action)
return self:injectTask(Task(name, dependencies, action))
end
--- Add a Task object to the collection
-- @tparam Task task The task to insert
-- @tparam string name The name of the task (optional)
-- @treturn Task The current task
function Runner:injectTask(task, name)
self.tasks[name or task.name] = task
return task
end
--- Set the default task
-- @tparam ?|string|function task The task to run or the name of the task
-- @treturn Runner The current object for chaining
function Runner:Default(task)
local defaultTask
if task == nil then
self.default = nil
elseif type(task) == "string" then
self.default = self.tasks[task]
if not self.default then
error("Cannot find task " .. task)
end
else
self.default = Task("<default>", {}, task)
end
return self
end
--- Run a task, and all its dependencies
-- @tparam string name Name of the task to run
-- @treturn Runner The current object for chaining
function Runner:Run(name)
return self:RunMany({ name })
end
--- Run a task, and all its dependencies
-- @tparam table names Names of the tasks to run
-- @return The result of the last task
function Runner:RunMany(names)
local oldTime = os.clock()
local value = true
local context = Context(self)
if #names == 0 then
context:Start()
else
for _, name in ipairs(names) do
value = context:Start(name)
end
end
if context.ShowTime then
colored.printColor("orange", "Took " .. os.clock() - oldTime .. "s in total")
end
return value
end
return Runner
end
preload["howl.tasks.OptionTask"] = function(...)
--- A Task that can store options
-- @classmod howl.tasks.OptionTask
local assert = require "howl.lib.assert"
local mixin = require "howl.class.mixin"
local rawset = rawset
local Task = require "howl.tasks.Task"
local OptionTask = Task:subclass("howl.tasks.OptionTask")
:include(mixin.configurable)
function OptionTask:initialize(name, dependencies, keys, action)
Task.initialize(self, name, dependencies, action)
self.options = {}
self.optionKeys = {}
for _, key in ipairs(keys or {}) do
self:addOption(key)
end
end
function OptionTask:addOption(key)
local options = self.options
local func = function(self, value)
if value == nil then value = true end
options[key] = value
return self
end
self[key:gsub("^%l", string.upper)] = func
self[key] = func
self.optionKeys[key] = true
end
function OptionTask:configure(item)
assert.argType(item, "table", "configure", 1)
for k, v in pairs(item) do
if self.optionKeys[k] then
self.options[k] = v
else
-- TODO: Configure filtering
-- error("Unknown option " .. tostring(k), 2)
end
end
end
return OptionTask
end
preload["howl.tasks.Dependency"] = function(...)
--- An abstract class dependency
-- @classmod howl.tasks.Dependency
local class = require "howl.class"
local Dependency = class("howl.tasks.Dependency")
--- Create a new dependency
function Dependency:initialize(task)
if self.class == Dependency then
error("Cannot create instance of abstract class " .. tostring(Dependency), 2)
end
self.task = task
end
--- Setup the dependency, checking if it cannot be resolved
function Dependency:setup(context, runner)
error("setup has not been overridden in " .. self.class, 2)
end
--- Execute the dependency
-- @treturn boolean If the task was run
function Dependency:resolve(context, runner)
error("resolve has not been overridden in " .. self.class, 2)
end
return Dependency
end
preload["howl.tasks.Context"] = function(...)
--- Manages the running of tasks
-- @classmod howl.tasks.Context
local class = require "howl.class"
local fs = require "howl.platform".fs
local mixin = require "howl.class.mixin"
local platform = require "howl.platform"
--- Holds task contexts
local Context = class("howl.tasks.Context"):include(mixin.sealed)
--- Create a new task context
-- @tparam Runner.Runner runner The task runner to run tasks from
-- @treturn Context The resulting context
function Context:initialize(runner)
self.ran = {} -- List of task already run
self.filesProduced = {}
self.tasks = runner.tasks
self.default = runner.default
self.Traceback = runner.Traceback
self.ShowTime = runner.ShowTime
self.env = runner.env
self:BuildCache()
end
function Context:DoRequire(path, quite)
if self.filesProduced[path] then return true end
-- Check for normal files
local task = self.producesCache[path]
if task then
self.filesProduced[path] = true
return self:Run(task)
end
-- Check for file mapping
task = self.normalMapsCache[path]
local from, name
local to = path
if task then
self.filesProduced[path] = true
-- Convert task.Pattern.From to path
-- (which should be task.Pattern.To)
name = task.Name
from = task.Pattern.From
end
for match, data in pairs(self.patternMapsCache) do
if path:match(match) then
self.filesProduced[path] = true
-- Run task, replacing match with the replacement pattern
name = data.Name
from = path:gsub(match, data.Pattern.From)
break
end
end
if name then
local canCreate = self:DoRequire(from, true)
if not canCreate then
if not quite then
self.env.logger:error("Cannot find '" .. from .. "'")
end
return false
end
return self:Run(name, from, to)
end
if fs.exists(fs.combine(self.env.root, path)) then
self.filesProduced[path] = true
return true
end
if not quite then
self.env.logger:error("Cannot find a task matching '" .. path .. "'")
end
return false
end
local function arrayEquals(x, y)
local len = #x
if #x ~= #y then return false end
for i = 1, len do
if x[i] ~= y[i] then return false end
end
return true
end
--- Run a task
-- @tparam string|Task.Task name The name of the task or a Task object
-- @param ... The arguments to pass to it
-- @treturn boolean Success in running the task?
function Context:Run(name, ...)
local task = name
if type(name) == "string" then
task = self.tasks[name]
if not task then
error("Cannot find a task called '" .. name .. "'")
return false
end
elseif not task or not task.Run then
error("Cannot call task " .. tostring(task) .. " as it has no 'Run' method")
return false
end
-- Search if this task has been run with the given arguments
local args = { ... }
local ran = self.ran[task]
if not ran then
ran = { args }
self.ran[task] = ran
else
for i = 1, #ran do
if arrayEquals(args, ran[i]) then return false end
end
ran[#ran + 1] = args
end
-- Sleep before every task just in case
platform.refreshYield()
return task:Run(self, ...)
end
Context.run = Context.Run
--- Start the task process
-- @tparam string name The name of the task (Optional)
-- @treturn boolean Success in running the task?
function Context:Start(name)
local task
if name then
task = self.tasks[name]
else
task = self.default
name = "<default>"
end
if not task then
self.env.logger:error("Cannot find a task called '" .. name .. "'")
return false
end
return self:Run(task)
end
--- Build a cache of tasks
-- This is used to speed up finding file based tasks
-- @treturn Context The current context
function Context:BuildCache()
local producesCache = {}
local patternMapsCache = {}
local normalMapsCache = {}
self.producesCache = producesCache
self.patternMapsCache = patternMapsCache
self.normalMapsCache = normalMapsCache
for name, task in pairs(self.tasks) do
local produces = task.produces
if produces then
for _, file in ipairs(produces) do
local existing = producesCache[file]
if existing then
error(string.format("Both '%s' and '%s' produces '%s'", existing, name, file))
end
producesCache[file] = name
end
end
local maps = task.maps
if maps then
for _, pattern in ipairs(maps) do
-- We store two separate caches for each of them
local toMap = (pattern.Type == "Pattern" and patternMapsCache or normalMapsCache)
local match = pattern.To
local existing = toMap[match]
if existing then
error(string.format("Both '%s' and '%s' match '%s'", existing, name, match))
end
toMap[match] = { Name = name, Pattern = pattern }
end
end
end
return self
end
return Context
end
preload["howl.scratchpad"] = function(...)
local utils = require "howl.lib.utils"
local dump = require "howl.lib.dump".dump
local printColor = require "howl.lib.colored".printColor
local parsePattern = utils.parsePattern
local createLookup = utils.createLookup
local tasks = {
{
name = "input",
provides = createLookup { "foo.un.lua" },
},
{
name = "output",
requires = createLookup { "foo.min.lua" },
},
{
name = "minify",
maps = {
{
from = parsePattern("wild:*.lua", true),
to = parsePattern("wild:*.min.lua")
}
},
},
{
name = "licence",
maps = {
{
from = parsePattern("wild:*.un.lua", true),
to = parsePattern("wild:*.lua")
}
},
},
}
for k, v in pairs(tasks) do
tasks[v.name] = v
if not v.maps then v.maps = {} end
v.mapper = #v.maps > 0
if not v.provides then v.provides = {} end
if not v.requires then v.requires = {} end
end
local function matching(name)
local out = {}
for _, task in ipairs(tasks) do
if task.provides[name] then
out[#out + 1] = { task = task.name }
end
for _, mapping in ipairs(task.maps) do
if mapping.to.Type == "Text" then
if mapping.to.Text == name then
out[#out + 1] = {
task = task.name,
mapping.from.Text,
name
}
end
else
if name:find(mapping.to.Text) then
out[#out + 1] = {
task = task.name,
name:gsub(mapping.to.Text, mapping.from.Text),
name
}
end
end
end
end
return out
end
local function resolveTasks(...)
local out = {}
local queue = {}
local depCache = {}
local function addDep(dependency, depth)
local hash = dependency.task .. "|"..table.concat(dependency, "|")
local existing = depCache[hash]
if existing then
existing.depth = math.min(existing.depth, depth)
return existing
else
dependency.depth = depth
dependency.needed = {}
dependency.solutions = {}
dependency.name = dependency.task .. ": " .. table.concat(dependency, " \26 ")
depCache[hash] = dependency
queue[#queue + 1] = dependency
return dependency
end
end
local function addSolution(solution, dependency)
local solution = addDep(solution, dependency.depth + 1)
solution.needed[#solution.needed + 1] = dependency
return solution
end
for i = 1, select('#', ...) do
addDep({ task = select(i, ...)}, 1)
end
while #queue > 0 do
local dependency = table.remove(queue, 1)
local task = tasks[dependency.task]
print("Task '" .. dependency.name)
if #dependency.needed > 0 then
print(" Needed for")
for i = 1, #dependency.needed do
printColor("lightGrey", " " .. dependency.needed[i].name)
end
end
if dependency.depth > 4 then
printColor("red", " Too deep")
elseif #dependency.solutions > 0 or (#task.requires == 0 and not task.mapper) then
printColor("green", " Endpoint")
out[#out + 1] = dependency
for i = 1, #dependency.needed do
local needed = dependency.needed[i]
needed.solutions[#needed.solutions + 1] = dependency
-- This should only happen once everything has happened
if #needed.solutions == 1 then
queue[#queue + 1] = needed
end
end
else
for i = 1, #task.requires do
local requirement = task.requires[i]
print(" Depends on '" .. requirement .. "'")
local matching = matching(requirement)
for i = 1, #matching do
local solution = addSolution(matching[i], dependency)
printColor("yellow", " Maybe: " .. solution.name)
end
end
if task.mapper then
local requirement = dependency[1]
print(" Depends on '" .. requirement .. "'")
local matching = matching(requirement)
for i = 1, #matching do
local solution = addSolution(matching[i], dependency)
printColor("yellow", " Maybe: " .. solution.name)
end
end
end
end
return out
end
-- print(dump(tasks))
-- print("Resolved", dump(matching("foo.min.lua")))
local resolved = resolveTasks("output")
for i = 1, #resolved do
print(resolved[i].name)
end
end
preload["howl.platform.oc"] = function(...)
--- OpenComputers's platform table
-- @module howl.platform.oc
local filesystem = require("filesystem")
local term = require("term")
local component = require("component")
local hasInternet = pcall(function() return component.internet end)
local internet = require("internet")
local gpu = component.gpu
local function read(filename)
local size = getSize(filename)
local fh = filesystem.open(filename)
local contents = fh:read(size)
fh:close()
return contents
end
--readDir and writeDir copied semi-verbatim from CC platform (with a slight modification)
local function readDir(directory)
local offset = #directory + 2
local stack, n = { directory }, 1
local files = {}
while n > 0 do
local top = stack[n]
n = n - 1
if fs.isDir(top) then
for _, file in ipairs(filesystem.list(top)) do
n = n + 1
stack[n] = filesystem.combine(top, file)
end
else
files[top:sub(offset)] = read(top)
end
end
return files
end
local function writeDir(dir, files)
for file, contents in pairs(files) do
write(filesystem.combine(dir, file), contents)
end
end
local function write(filename,contents)
local fh = filesystem.open(filename,"w")
local ok, err = fh:write(contents)
if not ok then io.stderr:write(err) end
fh:close()
end
local function assertExists(file,name,level)
if not filesystem.exists(file) then
error("Cannot find "..name.." (looking for "..file..")",level or 1)
end
end
local function getSize(file)
local fh = filesystem.open(file)
local size = fh:seek("end")
fh:close()
return size
end
local function request(url,post,headers)
if not hasInternet then error("No internet card found",0) end
local resp = ""
for chunk in internet.request(url,post,headers) do
resp = resp..chunk
end
return resp
end
local function notImplemented(name)
return function() error(name.." has not been implemented for OpenComputers!",2) end
end
return {
os = {
clock = os.clock,
time = os.time,
getEnv = os.getEnv,
},
fs = {
-- Path manipulation
combine = filesystem.concat,
normalise = filesystem.canonical,
getDir = filesystem.path,
getName = filesystem.name,
currentDir = shell.getWorkingDirectory,
currentProgram = function() return process.info().command end,
-- File access
read = read,
write = write,
readDir = readDir,
writeDir = writeDir,
getSize = getSize,
-- Type checking
assertExists = assertExists,
exists = filesystem.exists,
isDir = filesystem.isDir,
-- Other
list = filesystem.list,
makeDir = filesystem.makeDir,
delete = filesystem.delete,
move = filesystem.move,
copy = filesystem.copy,
},
term = {
setColor = gpu.setForeground,
resetColor = function() gpu.setForeground(colors.white) end,
print = print,
write = io.write,
},
http = {
request = request,
},
log = function() return end,
refreshYield = function() os.sleep(0) end,
}
end
preload["howl.platform.native"] = function(...)
--- Platform implementation for vanilla Lua
-- @module howl.platform.native
local escapeBegin = string.char(27) .. '['
local colorMappings = {
white = 97,
orange = 33,
magenta = 95,
lightBlue = 94,
yellow = 93,
lime = 92,
pink = 95, -- No pink
gray = 90, grey = 90,
lightGray = 37, lightGrey = 37,
cyan = 96,
purple = 35, -- Dark magenta
blue = 36,
brown = 31,
green = 32,
red = 91,
black = 30,
}
local function notImplemented(name)
return function() error(name .. " is not implemented", 2) end
end
local path = require('pl.path')
local dir = require('pl.dir')
local file = require('pl.file')
return {
fs = {
combine = path.join,
normalise = path.normpath,
getDir = path.dirname,
getName = path.basename,
currentDir = function() return path.currentdir end,
read = file.read,
write = file.write,
readDir = notImplemented("fs.readDir"),
writeDir = notImplemented("fs.writeDir"),
getSize = function(n)
local file = io:open(n,"r")
local size = file:seek("end")
file:close()
return size
end,
assertExists = function(file)
if not path.exists(file) then
error("File does not exist")
end
end,
exists = path.exists,