Last active
June 28, 2017 14:14
-
-
Save iainbeeston/88afce9d99d1938314c9e80372118016 to your computer and use it in GitHub Desktop.
Recursively find and print out all subclasses
This file contains hidden or 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
#!/usr/bin/env ruby | |
# recursively find classes | |
starting_file, working_directory = *ARGV | |
throw 'Please specify the file that contains the base class and the source directory' unless starting_file && working_directory | |
def files_that_subclass(file_path, dir) | |
contents = File.read(file_path) | |
# expects to find classes defined as "class foo..." | |
class_name = contents[/^class ([a-zA-Z0-9]+)\b/, 1] | |
# expects to find subclasses defined as "extends foo" | |
subclass_regexp = 'extends ' + class_name + '\b' | |
subclass_file_paths = `grep --files-with-matches --recursive --exclude-dir=".*" "#{subclass_regexp}" src`.split | |
[File.absolute_path(file_path)] + subclass_file_paths.flat_map {|p| files_that_subclass(p, dir)} | |
end | |
puts files_that_subclass(starting_file, working_directory).sort |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment