Last active
December 20, 2015 10:49
-
-
Save GigsD4X/6118640 to your computer and use it in GitHub Desktop.
Prints instances of the given case-insensitive pattern in any scripts within a given object. Usage: findPatternInChildScripts( `Parent`, `pattern` )
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
function _trimString( str ) | |
-- Returns string `str` without leading/trailing whitespace | |
return ( str:gsub( "^%s*(.-)%s*$", "%1" ) ); | |
end; | |
function _countStringOccurrences( haystack, needle ) | |
-- Returns the amount of occurrences of a pattern in a string | |
local count = 0; | |
for _ in haystack:gmatch( needle ) do | |
count = count + 1; | |
end; | |
return count; | |
end; | |
function _splitString( str, delimiter ) | |
-- Returns a table of string `str` split by pattern `delimiter` | |
local parts = {}; | |
local pattern = ( "([^%s]+)" ):format( delimiter ); | |
str:gsub( pattern, function ( part ) | |
table.insert( parts, part ); | |
end ); | |
return parts; | |
end; | |
function findPatternInChildScripts( Parent, pattern ) | |
-- Prints any instances of the given pattern in the scripts within `Parent`, recursively | |
-- Go through every child of the given parent | |
for _, Child in pairs( Parent:GetChildren() ) do | |
local Output = {}; | |
-- Add the header (the path to the object) | |
table.insert( Output, "\n-- in " .. Child:GetFullName() ); | |
-- Check if this child is a script and if it is, add it to the list | |
if Child:IsA( "Script" ) or Child:IsA( "LocalScript" ) then | |
-- Keep a list of all the lines | |
local lines = { Child.Source }; | |
if _countStringOccurrences( Child.Source, "\n" ) > 0 then | |
lines = _splitString( Child.Source, "\n" ); | |
end; | |
for line_index, line in pairs( lines ) do | |
local case_insensitive_pattern = pattern:gsub( "(%a)", function ( letter ) | |
return "[" .. letter:upper() .. letter:lower() .. "]"; | |
end ); | |
if line:find( case_insensitive_pattern ) then | |
table.insert( Output, "@" .. line_index .. ": " .. _trimString( line ) ); | |
end; | |
end; | |
end; | |
-- If there's anything other than the header in the output stack, print it | |
if #Output > 1 then | |
for _, message in pairs( Output ) do | |
print( message ); | |
end; | |
end; | |
-- Look for any scripts in this child | |
findPatternInChildScripts( Child, pattern ); | |
end; | |
end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment