Skip to content

Instantly share code, notes, and snippets.

@iainbeeston
Last active June 28, 2017 14:14
Show Gist options
  • Save iainbeeston/88afce9d99d1938314c9e80372118016 to your computer and use it in GitHub Desktop.
Save iainbeeston/88afce9d99d1938314c9e80372118016 to your computer and use it in GitHub Desktop.
Recursively find and print out all subclasses
#!/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