Skip to content

Commit

Permalink
Only read jsonDocs if it has been updated
Browse files Browse the repository at this point in the history
* Use hs.settings to store EmmyLua's last doc generation time
* Only read jsonDocs if file modification time is newer than last gen
  • Loading branch information
xaviervalarino committed Sep 15, 2022
1 parent 4b8f6ab commit 848284a
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions Source/EmmyLua.spoon/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ local options = {
},
}

M.last_generated = {
time = hs.settings.get("EmmyLua Last Generated") or {},
updated = false,
}

M.spoonPath = hs.spoons.scriptPath()

function M.comment(str, commentStr)
Expand Down Expand Up @@ -173,38 +178,47 @@ end

function M.create(jsonDocs, prefix)
local mtime = hs.fs.attributes(jsonDocs, "modification")
prefix = prefix or ""
local data = hs.json.read(jsonDocs)
for _, module in ipairs(data) do
if module.type ~= "Module" then
error("Expected a module, but found type=" .. module.type)
end
module.prefix = prefix
module.name = prefix .. module.name
local fname = options.annotations .. "/" .. module.name .. ".lua"
local fmtime = hs.fs.attributes(fname, "modification")
if fmtime == nil or mtime > fmtime then
-- print("creating " .. fname)
local gen_prefix = prefix or "hs"
local gen_time = M.last_generated.time[gen_prefix]
local m_prefix = prefix:gsub("%..*$", ".")
m_prefix = m_prefix == "hs" and "" or m_prefix
if gen_time == nil or mtime > gen_time then
local data = hs.json.read(jsonDocs)
for _, module in ipairs(data) do
module.prefix = m_prefix
module.name = m_prefix .. module.name
if module.type ~= "Module" then
error("Expected a module, but found type=" .. module.type)
end
local fname = options.annotations .. "/" .. module.name .. ".lua"
print("creating " .. fname)
local fd = io.open(fname, "w+")
io.output(fd)
M.processModule(module)
io.close(fd)
end
M.last_generated.time[gen_prefix] = os.time()

This comment has been minimized.

Copy link
@muescha

muescha Sep 20, 2022

You should not save the current OS time - better the excact time of the source file.

For example: When you have a file from 01. Sept and generate it today 20 September. And then checkout a file with timestamp of 15. September. Then it would not generated

This comment has been minimized.

Copy link
@xaviervalarino

xaviervalarino Sep 22, 2022

Author Owner

Thanks! It's a really good point, especially if checking out different branches or commits of the Spoon.

I've been using your PR of EmmyLua spoon in my my HS config. I found that I preferred the way you manage the timestamp on the settings table and thought it made more sense the way to wrap the create method in a new method (createWhenChanged), instead of refactoring it so that both would be accessible on the module API.

This comment has been minimized.

Copy link
@muescha

muescha Sep 22, 2022

One goal was also "not touch existing code" so a PR review would be more clear and pass more easy an review when I not refactor existing code and just wrap the checking logic around the existing methods.

M.last_generated.updated = true
end
end

function M:init()
hs.fs.mkdir(options.annotations)
-- Load hammerspoon docs
M.create(hs.docstrings_json_file)
M.create(hs.docstrings_json_file, "hs")

-- Load Spoons
for _, spoon in ipairs(hs.spoons.list()) do
local doc = hs.configdir .. "/Spoons/" .. spoon.name .. ".spoon/docs.json"
local prefix = "spoon." .. spoon.name
if hs.fs.attributes(doc, "modification") then
M.create(doc, "spoon.")
M.create(doc, prefix)
end
end

if M.last_generated.updated then
hs.settings.set("EmmyLua Last Generated", M.last_generated.time)
end
end

return M

0 comments on commit 848284a

Please sign in to comment.