/src/refac/**init**.py#main
is the entrypoint for the refac
script.
The three commands file
, symbol
, and import
are implemented in /src/refac/move_file.py
, /src/refac/move_symbol.py
, and /src/refac/move_import.py
, respectively.
Each of these files has a similar layout. A move_*()
function that consists of three parts:
- Validation of the inputs.
- Moving the file/symbol/import.
- Updating the import statements for the other files in the codebase.
- Updating any string references to the moved file/symbol/import.
graph LR;
valid[Validate]-->move[Move file/symbol/import];
move[Move file/symbol/import]-->import[Update imports];
import[Update imports]-->string[Update strings];
The logic for #1 + #2 is bespoke to each move function.
-
Validation is fairly straightforward, and is mostly just checking that the source and destination paths are valid.
-
move_file
is simpler thanmove_symbol
. It effectively just runsmv
command, but it will try and merge files or directories if the destination already exists. Themove_symbol
is a bit more complicated. It finds the symbol(s) to move, and finds all associated symbols needed to move as well. (An example of an associated symbol:def f(): pass; def g(): return f
--f
is an associated symbol for the functiong
). It removes the symbol(s) from the source file withRemoveSymbolsVisitor
and adds them to the new file withAddSymbolsVisitor
. It will also add back any symbols to the old module if they were still being used by other symbols in the old module.move_import
is very similar tomove_symbol
and may be merged in the future
The logic for #3 + #4 is shared across the move_*
family.
-
We execute the
ReplaceImportsCodemod
to update all the import statements in the codebase. This is the meat of the refac codemod. As a performance improvement, we try to only run it on files that may have been affected bygit grep
-ing for relevant words. -
Finally, we
git grep
for the old and new paths and usesed
to replace any string references to the moved file/symbol/import.