Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a unit to the uses clauses of all units in a project?

I am finding that certain kinds of code cleanups and refactorings are made VERY difficult by the difficulty of adding a unit to the uses-clauses of a large project.

I want to add a unit to the interface-uses-clause of all delphi .pas units in a single project, and that means manually doing that in over 500 places. Every time I refactor a giant unit and split it from one unit into two, I can probably search and replace using something like "notepad++" to change "MyOldUnit," to "MyOldUnit,MyNewUnit," but sometimes, that's just not safe to do. It also misses the cases where "MyOldUnit" is the last thing in the uses clause ("uses MyOldUnit;").

Anyways, search and replacing in files is dangerous business. If no such tool exists, I am contemplating writing one, using the Castalia delphi parser. I have checked GExperts, Castalia, ModelMakerCodeExplorer and none of them have a way to batch-insert units into all uses clauses in a project. I'm hoping a tool to do this exists.

Secondly, in many cases, I'm moving a function from one unit where it doesn't belong to another, but this means I need to add that unit to 30% of the project's units, but not the other 70% where it's already added. That means a parser approach is required, not a regex approach.

like image 887
Warren P Avatar asked Oct 11 '12 15:10

Warren P


1 Answers

Because we all write code we will certainly use in other projects. If you move interface parts from one unit to another you will break your projects. Same with Old and New Units.

But you can refactor without breaking your projects. Just mark the parts (unit, class, method, procedure) as deprecated. Your Code is working, but you will be warned by the compiler.

Here an example of moving a procedure from one unit to another:

unit Foo;

interface

procedure FooProc; deprecated; // new location in unit FooNew

implementation

uses
  FooNew;

procedure FooProc;
begin
  FooNew.FooProc;
end;

end.
like image 123
Sir Rufo Avatar answered Oct 12 '22 05:10

Sir Rufo