How to reorganize the folder structure of my units in Delphi?
Asked Answered
P

5

16

I started a prototype with Delphi with all source files under one single directory.
Now, I want to reorganize everything with folders.

I found two way to do this.

1/ First is to open each unit in Delphi IDE and Save as the new folder. Then delete the old unit file.

2/ Under windows explorer, reorganize everything, then open the project in Delphi IDE, remove reference to old (unknown) units, and then add each unit folder by folder.

As I have 100+ units, it is really painful, whatever method I use (1 or 2).
How are you doing such reorganization ? Did I miss a feature ?

Puffin answered 27/9, 2012 at 8:58 Comment(9)
Fastest would be, I guess, to move your files and edit each .dpr manually when the IDE is closed.Ladd
I agree with @sertac - this is the sort of activity best done outside the IDE. Once you have reorganised your units drop into a command window and enter dir /s /b *.pas >units.txt from the root of your source directory. This will give you a file units.txt containing the new path to your unit. Use this to edit your DPR files.Rockefeller
@KeithMiller don't forget that the .dproj files also need to be adjusted which can be a royal pain. @uweRaabe has a much better solution.Guardafui
Yes, - Uwe's solution is the easiest and best way.Maculation
@Sertac Akyuz> Hacking the *.dpr and .dproj files should never be the best solution ... even if it is the easiest way to do ! I would really like to avoid such hack!Puffin
@Jeroen - What's there to edit in the .dproj file? - Trident - It's your call... Having read all the comments and answers I still stand by comment, manual editing seems to be the fastest.Ladd
@SertacAkyuz the .dproj, like the .dpr contains paths to the files.Guardafui
@Jeroen - Looks like msbuild might need those entries, otherwise they're possibly redundant, AFAICT... In any case, there is not much editing involved, just delete 'dccreference' entries and then a 'save all' in the IDE regenerates them.Ladd
@SertacAkyuz I don't understand the exact procedure you intend when you say "just delete 'dccreference' entries". In which file you delete such dccreference' entries? Do you mean opening the .droj file with an external editor and then delete 'dccreference' entries from it? thanksMedicament
S
21

I didn't check for XE, but in XE2 this is quite easy to do inside the IDE:

  • in case of multiple projects have all projects in one project group
  • in the project manager for each unit do
    • rename the unit by pressing F2 or use the context menu
    • while renaming place the subfolder in front of the unit name (e.g test\Unit1.pas)

The IDE will move the unit (removing the old one) and promote this change to all projects in the project group.

Sheela answered 27/9, 2012 at 10:14 Comment(9)
This is the correct way to do it. The IDE will do all the work for you.Maculation
This works for me in DelphiXE. But I must do it one unit at a time ... this is a real pain ;) I was hoping to be able to drap & drop my units in the new folder, using the Project Manager window.Puffin
I will approve this answer as this works for me and this is using the IDE. Other developers might want to hack directly the project files ... and I can understand :)Puffin
Someone might check if this will also keep the version control integration happy. I don't use Git, but in Mercurial this is not a problem. Such things can be a real PITA with subversion though.Sheela
I'm using bazaar outside Delphi IDE, this is not a issue as bzr find find easily that files were moved (or renamed).Puffin
@TridenT, fine - as I said Mercurial isn't affected either, but I know that subversion has/had issues with that and I wonder if the version control integration for svn can handle this better.Sheela
@UweRaabe looks like it doesn't work anymore at delphi Alexandria. Any suggestions?Ostiary
@Evgeny, unfortunately yes: quality.embarcadero.com/browse/RSP-32413Sheela
@UweRaabe I found the solution, see my unswer belowOstiary
D
4

Do you really need all 100+ units to be named in the project? Are not some of them simply common functions which could be placed in a folder on the search path? Delete all the less frequently needed units from the project and place them in folders to suit your logic. The IDE will find them, and you can also right-click open them in the IDE when you need to, without their being named in the project with a fixed path.

Demobilize answered 27/9, 2012 at 9:22 Comment(3)
I strive for projects to be self-contained, that makes it way easier to transfer development to a different machine/user, and run automated builds.Guardafui
I would prefer not hacking the *.dpr and *.dproj. I'm using Continuous Integration with Unit test and Test coverage statistics, such tools parse the *.dpr file to gather units !Puffin
Just add the relative search paths to your project. That way the paths go wherever your project goes.Greenwood
L
4

The simplest way is:

  1. Copy project files to folders where you want them to be;
  2. Edit project *.dpr file to remove all in ..;
  3. Edit project options and add paths to project folders to search path option (not global IDE default project options, but your project's options).
Laxative answered 27/9, 2012 at 10:8 Comment(0)
A
2

Only 2nd option. Move files to new directories using TotalCommander, Windows Explorer etc. I think your source layout should look like:

myutils
libs
    crypto
    database
    network

and then add those directories:

myutils;libs\crypto;libs\database;libs\network

to your Unit Search Path

Alcoholicity answered 27/9, 2012 at 9:20 Comment(1)
See my comments to frogb, I prefer not using the search path for my owned units.Puffin
O
0

I found the solution for Delphi Alexandria. Just open file (doubleclick at project tree) and then from File menu select Save as... and place file into new location. That is, file saved into new location and project updated. Then just remove old file with your favorite file manager.

Ostiary answered 17/8, 2023 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.