First past at implementing init.lua support

This commit is contained in:
Lucien Greathouse
2017-12-01 01:28:23 -08:00
parent 2681972976
commit c3d6dc0e2c

View File

@@ -1,5 +1,27 @@
local Reconciler = {} local Reconciler = {}
local function isInit(item, itemFileName)
if item.type == "dir" then
return
end
return not not itemFileName:find("^init%.")
end
local function findInit(item)
if item.type ~= "dir" then
return nil, nil
end
for childFileName, childItem in pairs(item.children) do
if isInit(childItem, childFileName) then
return childItem, childFileName
end
end
return nil, nil
end
local function itemToName(item, fileName) local function itemToName(item, fileName)
if item and item.type == "dir" then if item and item.type == "dir" then
return fileName, "Folder" return fileName, "Folder"
@@ -30,11 +52,19 @@ end
function Reconciler._reifyShallow(item, fileName) function Reconciler._reifyShallow(item, fileName)
if item.type == "dir" then if item.type == "dir" then
-- TODO: handle init local initItem, initFileName = findInit(item)
local rbx = Instance.new("Folder")
rbx.Name = fileName
return rbx if initItem then
local rbx = Reconciler._reify(initItem, initFileName)
rbx.Name = fileName
return rbx
else
local rbx = Instance.new("Folder")
rbx.Name = fileName
return rbx
end
elseif item.type == "file" then elseif item.type == "file" then
local objectName, className = itemToName(item, fileName) local objectName, className = itemToName(item, fileName)
@@ -49,22 +79,24 @@ function Reconciler._reifyShallow(item, fileName)
end end
end end
function Reconciler._reify(item, fileName) function Reconciler._reify(item, fileName, parent)
local rbx = Reconciler._reifyShallow(item, fileName) local rbx = Reconciler._reifyShallow(item, fileName)
if item.type == "dir" then if item.type == "dir" then
for childName, child in pairs(item.children) do for childFileName, childItem in pairs(item.children) do
local childRbx = Reconciler._reify(child, childName) if not isInit(childItem, childFileName) then
childRbx.Parent = rbx local childRbx = Reconciler._reify(childItem, childFileName)
childRbx.Parent = rbx
-- TODO: handle init end
end end
end end
rbx.Parent = parent
return rbx return rbx
end end
function Reconciler.reconcile(rbx, item, fileName) function Reconciler.reconcile(rbx, item, fileName, parent)
-- Item was deleted! -- Item was deleted!
if not item then if not item then
if rbx then if rbx then
@@ -74,26 +106,38 @@ function Reconciler.reconcile(rbx, item, fileName)
return return
end end
-- Item was created!
if not rbx then
return Reconciler._reify(item, fileName)
end
if item.type == "dir" then if item.type == "dir" then
-- TODO: handle init -- Folder was created!
if not rbx then
return Reconciler._reify(item, fileName, parent)
end
if rbx.ClassName ~= "Folder" then local initItem, initFileName = findInit(item)
return Reconciler._reify(item, fileName)
if initItem then
local _, initClassName = itemToName(initItem, initFileName)
if rbx.ClassName == initClassName then
setValues(rbx, initItem, initFileName)
else
return Reconciler._reify(item, fileName, parent)
end
else
if rbx.ClassName ~= "Folder" then
return Reconciler._reify(item, fileName, parent)
end
end end
local visitedChildren = {} local visitedChildren = {}
for childFileName, childItem in pairs(item.children) do for childFileName, childItem in pairs(item.children) do
local childName = itemToName(childItem, childFileName) if not isInit(childItem, childFileName) then
local childName = itemToName(childItem, childFileName)
visitedChildren[childName] = true visitedChildren[childName] = true
Reconciler.reconcile(rbx:FindFirstChild(childName), childItem, childFileName) Reconciler.reconcile(rbx:FindFirstChild(childName), childItem, childFileName, rbx)
end
end end
for _, childRbx in ipairs(rbx:GetChildren()) do for _, childRbx in ipairs(rbx:GetChildren()) do
@@ -105,10 +149,18 @@ function Reconciler.reconcile(rbx, item, fileName)
return rbx return rbx
elseif item.type == "file" then elseif item.type == "file" then
if isInit(item, fileName) then
-- TODO: usurp parent
end
if not rbx then
return Reconciler._reify(item, fileName, parent)
end
local _, className = itemToName(item, fileName) local _, className = itemToName(item, fileName)
if rbx.ClassName ~= className then if rbx.ClassName ~= className then
return Reconciler._reify(item, fileName) return Reconciler._reify(item, fileName, parent)
end end
setValues(rbx, item, fileName) setValues(rbx, item, fileName)
@@ -139,7 +191,7 @@ function Reconciler.reconcileRoute(route, item)
local name = itemToName(item, fileName) local name = itemToName(item, fileName)
local rbx = location:FindFirstChild(name) local rbx = location:FindFirstChild(name)
local newRbx = Reconciler.reconcile(rbx, item, fileName) local newRbx = Reconciler.reconcile(rbx, item, fileName, location)
if newRbx ~= rbx then if newRbx ~= rbx then
if rbx then if rbx then