diff --git a/moonscript/cmd/watcher.lua b/moonscript/cmd/watcher.lua new file mode 100644 index 0000000..52be1d5 --- /dev/null +++ b/moonscript/cmd/watcher.lua @@ -0,0 +1,149 @@ +local remove_dupes +remove_dupes = function(list, key_fn) + local seen = { } + return (function() + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #list do + local _continue_0 = false + repeat + local item = list[_index_0] + local key + if key_fn then + key = key_fn(item) + else + key = item + end + if seen[key] then + _continue_0 = true + break + end + seen[key] = true + local _value_0 = item + _accum_0[_len_0] = _value_0 + _len_0 = _len_0 + 1 + _continue_0 = true + until true + if not _continue_0 then + break + end + end + return _accum_0 + end)() +end +local Watcher +do + local _class_0 + local _base_0 = { } + _base_0.__index = _base_0 + _class_0 = setmetatable({ + __init = function(self, file_list) + self.file_list = file_list + end, + __base = _base_0, + __name = "Watcher" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({}, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + Watcher = _class_0 +end +local InotifyWacher +do + local _class_0 + local _parent_0 = Watcher + local _base_0 = { + get_dirs = function(self) + local parse_dir + parse_dir = require("moonscript.cmd.moonc").parse_dir + local dirs + do + local _accum_0 = { } + local _len_0 = 1 + local _list_0 = self.file_list + for _index_0 = 1, #_list_0 do + local _des_0 = _list_0[_index_0] + local file_path + file_path = _des_0[1] + local dir = parse_dir(file_path) + if dir == "" then + dir = "./" + end + local _value_0 = dir + _accum_0[_len_0] = _value_0 + _len_0 = _len_0 + 1 + end + dirs = _accum_0 + end + return remove_dupes(dirs) + end + } + _base_0.__index = _base_0 + setmetatable(_base_0, _parent_0.__base) + _class_0 = setmetatable({ + __init = function(self, ...) + return _class_0.__parent.__init(self, ...) + end, + __base = _base_0, + __name = "InotifyWacher", + __parent = _parent_0 + }, { + __index = function(cls, name) + local val = rawget(_base_0, name) + if val == nil then + local parent = rawget(cls, "__parent") + if parent then + return parent[name] + end + else + return val + end + end, + __call = function(cls, ...) + local _self_0 = setmetatable({}, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + local self = _class_0 + self.available = function(self) + return pcall(function() + return require("inotify") + end) + end + if _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end + InotifyWacher = _class_0 +end +local SleepWatcher +do + local _class_0 + local _base_0 = { } + _base_0.__index = _base_0 + _class_0 = setmetatable({ + __init = function() end, + __base = _base_0, + __name = "SleepWatcher" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({}, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + SleepWatcher = _class_0 +end +return { + Watcher = Watcher, + SleepWatcher = SleepWatcher, + InotifyWacher = InotifyWacher +} diff --git a/moonscript/cmd/watcher.moon b/moonscript/cmd/watcher.moon new file mode 100644 index 0000000..afb5dd7 --- /dev/null +++ b/moonscript/cmd/watcher.moon @@ -0,0 +1,29 @@ +remove_dupes = (list, key_fn) -> + seen = {} + return for item in *list + key = if key_fn then key_fn item else item + continue if seen[key] + seen[key] = true + item + +-- files is a list of tuples, {source, target} +class Watcher + new: (@file_list) => + + +class InotifyWacher extends Watcher + @available: => + pcall -> require "inotify" + + get_dirs: => + import parse_dir from require "moonscript.cmd.moonc" + dirs = for {file_path} in *@file_list + dir = parse_dir file_path + dir = "./" if dir == "" + dir + + remove_dupes dirs + +class SleepWatcher + +{:Watcher, :SleepWatcher, :InotifyWacher} diff --git a/spec/cmd_spec.moon b/spec/cmd_spec.moon index 5e7b0dc..2ab5199 100644 --- a/spec/cmd_spec.moon +++ b/spec/cmd_spec.moon @@ -9,7 +9,7 @@ describe "moonc", -> dev_loaded = with_dev -> moonc = require "moonscript.cmd.moonc" - same = (fn, a, b)-> + same = (fn, a, b) -> assert.same b, fn a it "should normalize dir", -> @@ -18,7 +18,6 @@ describe "moonc", -> same moonc.normalize_dir, "", "/" -- wrong same moonc.normalize_dir, "hello", "hello/" - it "should parse dir", -> same moonc.parse_dir, "/hello/world/file", "/hello/world/" same moonc.parse_dir, "/hello/world/", "/hello/world/" @@ -63,6 +62,21 @@ describe "moonc", -> moonc.compile_file_text "print'hello'", fname: "test.moon" } + describe "watcher", -> + describe "inotify watcher", -> + it "gets dirs", -> + import InotifyWacher from require "moonscript.cmd.watcher" + watcher = InotifyWacher { + {"hello.moon", "hello.lua"} + {"cool/no.moon", "cool/no.lua"} + } + + assert.same { + "./" + "cool/" + }, watcher\get_dirs! + + describe "stubbed lfs", -> local dirs