Created
July 26, 2023 02:27
-
-
Save howmanysmall/c258eb71f478cce533cad0f92567bbfa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--!optimize 2 | |
--!strict | |
local fs = require("@lune/fs") | |
local process = require("@lune/process") | |
local task = require("@lune/task") | |
local FileInstance = {} | |
FileInstance.__index = FileInstance | |
local function Constructor(FilePath: string, FileName: string?) | |
local NewFilePath = if FileName then `{FilePath}/{FileName}` else FilePath | |
return setmetatable({ | |
Children = nil :: {Class}?; | |
FilePath = NewFilePath; | |
IsDirectory = fs.isDir(NewFilePath); | |
}, FileInstance) | |
end | |
function FileInstance.Mark(FilePath: string, FileName: string?): FileInstance | |
local self: any = Constructor(FilePath, FileName) | |
return self | |
end | |
function FileInstance.GetFullName(self: Class) | |
local FilePath = self.FilePath | |
if string.sub(FilePath, 1, 2) == "./" then | |
FilePath = string.sub(FilePath, 3) | |
end | |
return process.cwd .. string.gsub(FilePath, "/", "\\") | |
end | |
function FileInstance.GetChildren(self: Class): {Class} | |
if self.Children then | |
return self.Children | |
end | |
if self.IsDirectory then | |
local Children = table.clone(fs.readDir(self.FilePath)) :: any | |
for Index, Child in Children do | |
Children[Index] = FileInstance.Mark(self.FilePath, Child) | |
end | |
self.Children = Children :: {Class} | |
return Children :: {Class} | |
else | |
local Children: {Class} = {} | |
self.Children = Children | |
return Children | |
end | |
end | |
function FileInstance.GetDescendants(self: Class): {Class} | |
local Descendants = self:GetChildren() | |
local TotalDescendants = #Descendants | |
local Length = 0 | |
if TotalDescendants > 0 then | |
repeat | |
Length += 1 | |
local GrandChildren = Descendants[Length]:GetChildren() | |
for Index, GrandChild in GrandChildren do | |
Descendants[TotalDescendants + Index] = GrandChild | |
end | |
TotalDescendants += #GrandChildren | |
until Length == TotalDescendants | |
end | |
return Descendants | |
end | |
function FileInstance.Read(self: Class) | |
return if self.IsDirectory then nil else fs.readFile(self.FilePath) | |
end | |
function FileInstance.Write(self: Class, Source: string) | |
if not self.IsDirectory then | |
fs.writeFile(self.FilePath, Source) | |
end | |
end | |
function FileInstance.__tostring(self: Class) | |
return self:GetFullName() | |
end | |
type Class = typeof(Constructor(".")) | |
type FileInstance = { | |
FilePath: string, | |
IsDirectory: boolean, | |
GetChildren: (self: FileInstance) -> {FileInstance}, | |
GetDescendants: (self: FileInstance) -> {FileInstance}, | |
GetFullName: (self: FileInstance) -> string, | |
Read: (self: FileInstance) -> string?, | |
Write: (self: FileInstance, Source: string) -> (), | |
} | |
local TransformFunctions = {} | |
local function GetLuauFilesInDirectory(Directory: FileInstance): {FileInstance} | |
local LuauFiles = {} | |
local Length = 0 | |
for _, Descendant in Directory:GetDescendants() do | |
if string.sub(Descendant.FilePath, -4) == ".lua" then | |
Length += 1 | |
LuauFiles[Length] = Descendant | |
end | |
end | |
return LuauFiles | |
end | |
-- this function is stupid 💔 | |
function TransformFunctions.TransformDirectory(Directory: FileInstance) | |
local TotalTransformed = 0 | |
for _, LuauFile in GetLuauFilesInDirectory(Directory) do | |
local FileSource = LuauFile:Read() | |
if FileSource then | |
local WasChanged = false | |
local Lines = string.split(FileSource, "\n") | |
for Index, Line in Lines do | |
local InsideTable = false | |
if Index ~= 1 then | |
for NewIndex = Index - 1, 1, -1 do | |
local PreviousLine = Lines[NewIndex] | |
if string.match(PreviousLine, "{") then | |
InsideTable = true | |
break | |
end | |
end | |
end | |
if InsideTable then | |
continue | |
end | |
InsideTable = string.match(Line, "}") ~= nil | |
local TableDefinition, FunctionName = string.match(Line, "([%w_]+)%.([%w_]+)%s*=%s*function%(") | |
if TableDefinition and FunctionName then | |
if not WasChanged then | |
local New, Replacements = | |
string.gsub(Line, "([%w_]+)%.([%w_]+)%s*=%s*function%(", "function %1.%2(") | |
Lines[Index] = New | |
WasChanged = Replacements > 0 | |
else | |
Lines[Index] = string.gsub(Line, "([%w_]+)%.([%w_]+)%s*=%s*function%(", "function %1.%2(") | |
end | |
elseif string.match(Line, "([%w_]+)%s*=%s*function%(") then | |
if not WasChanged then | |
local New, Replacements = string.gsub(Line, "([%w_]+)%s*=%s*function%(", "function %1(") | |
Lines[Index] = New | |
WasChanged = Replacements > 0 | |
else | |
Lines[Index] = string.gsub(Line, "([%w_]+)%s*=%s*function%(", "function %1(") | |
end | |
end | |
end | |
if WasChanged then | |
LuauFile:Write(table.concat(Lines, "\n")) | |
-- task.defer(function() | |
-- local SpawnResult = process.spawn("stylua2Roact", {LuauFile:GetFullName()}) | |
-- if not SpawnResult.ok then | |
-- warn(SpawnResult.code, "-", SpawnResult.stderr) | |
-- end | |
-- end) | |
TotalTransformed += 1 | |
end | |
end | |
end | |
return TotalTransformed | |
end | |
function TransformFunctions.TransformPath(Path: string) | |
return TransformFunctions.TransformDirectory(FileInstance.Mark(Path)) | |
end | |
local START_TIME = os.clock() | |
local total = TransformFunctions.TransformPath("./out") | |
local FINISH_TIME = os.clock() - START_TIME | |
print(string.format("Took %d ms to transform %* files", FINISH_TIME * 1000, total)) | |
return table.freeze(TransformFunctions) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment