diff --git a/plugin/src/Reconciler.lua b/plugin/src/Reconciler.lua index 03373e65..1a478ec8 100644 --- a/plugin/src/Reconciler.lua +++ b/plugin/src/Reconciler.lua @@ -1,5 +1,27 @@ 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) if item and item.type == "dir" then return fileName, "Folder" @@ -30,11 +52,19 @@ end function Reconciler._reifyShallow(item, fileName) if item.type == "dir" then - -- TODO: handle init - local rbx = Instance.new("Folder") - rbx.Name = fileName + local initItem, initFileName = findInit(item) - 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 local objectName, className = itemToName(item, fileName) @@ -49,22 +79,24 @@ function Reconciler._reifyShallow(item, fileName) end end -function Reconciler._reify(item, fileName) +function Reconciler._reify(item, fileName, parent) local rbx = Reconciler._reifyShallow(item, fileName) if item.type == "dir" then - for childName, child in pairs(item.children) do - local childRbx = Reconciler._reify(child, childName) - childRbx.Parent = rbx - - -- TODO: handle init + for childFileName, childItem in pairs(item.children) do + if not isInit(childItem, childFileName) then + local childRbx = Reconciler._reify(childItem, childFileName) + childRbx.Parent = rbx + end end end + rbx.Parent = parent + return rbx end -function Reconciler.reconcile(rbx, item, fileName) +function Reconciler.reconcile(rbx, item, fileName, parent) -- Item was deleted! if not item then if rbx then @@ -74,26 +106,38 @@ function Reconciler.reconcile(rbx, item, fileName) return end - -- Item was created! - if not rbx then - return Reconciler._reify(item, fileName) - end - 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 - return Reconciler._reify(item, fileName) + local initItem, initFileName = findInit(item) + + 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 local visitedChildren = {} 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 for _, childRbx in ipairs(rbx:GetChildren()) do @@ -105,10 +149,18 @@ function Reconciler.reconcile(rbx, item, fileName) return rbx 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) if rbx.ClassName ~= className then - return Reconciler._reify(item, fileName) + return Reconciler._reify(item, fileName, parent) end setValues(rbx, item, fileName) @@ -139,7 +191,7 @@ function Reconciler.reconcileRoute(route, item) local name = itemToName(item, fileName) 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 rbx then