Update, unify, encapsulate, and fix various code tools in Rails

Part of merging development from https://github.com/bf4/code_metrics
    and https://github.com/rails/rails/pull/11148
    back into Rails

Next steps, if given go ahead, is to create an internal
code_tools gem, either in the tools directory, or inside railties
that

- is not part of the Rails release cycle
- is used internally by Rails
- does not depend on Rails

See https://github.com/bf4/rails/tree/code_tools_next/tools/code_tools for what this could look like

I would also move into it the code and functionality of
rake stats and rake notes.