views:

99

answers:

2

Hi all,

I’m working on a c# project, lately we decided to restructure the directories structure of the source code. I’m doing the restructure and wanted to ask you some questions about what is the best way to conduct the change. The restructure is basically changing the current directories of all the projects to a more structured and logical order.

The current structure is (this is a scheme):

  • Main Dir:

    • Project
    • Project
    • Dir
      • Project
      • Project
      • Project
    • Dir
      • Project
      • Project
      • Project
    • Project Dir + solution file
    • Project Dir + solution file
    • Dir
      • Solution file.
      • Project
      • Project
    • Project Dir Etc..

The Target scheme is: TFS Project: Main - Source - Dir - Solution file. - Project - Project - Project - Project

     - Dir
        - Dir
            - Project
            - Project
            - Project
        - Dir
            - Solution file.
      - Dir

        - Project
        - Project

The relative location between the projects is changed and also the relative locations in the solutions.

The question is how to make change in the best manner: - How to Fix all the references of each project? - How to fix all the solutions? - Is there any tool available for that?

A: 

I am not aware of any tools that exist to automatically do this, but I do know of two options that you have, option 1 is to use VS to do the refactor (longer) or option 2 is to use something like powershell (or your favorite scripting language) to update the sln and csproj references.

If you are using option 1:

  1. Create your new directories using VS, and manually move the files into them
  2. Move your SLN files around by hand (the references inside the files are all relative) the sub folder references will remain intact.

If using option 2:

  1. Move your non csproj/sln files to their target locations, make sure you understand the relative changes in your paths.
  2. Create a script which can handle XML files (for the proj files, you will need regular text parsing for the SLN files), and open the files. Retarget each "item" node with a path in it (will either be ..\, , or file.etx) to the recalculated target location.

Unfortunately it will be pretty manual, and pretty error prone. I recommend option #1, and have just gotten done doing it on a project with 100+ csproj and 30 sln files. There were about 4000 source files, which I moved by hand and then used find replace in vs studio (with the project unloaded) to update.

Good luck.

GrayWizardx
+1  A: 

If using option 2:

Move your non csproj/sln files to their target locations, make sure you understand the relative changes in your paths.

A couple of additions:

  1. I'd have the script perform the required file operations as well. Of course, make sure to back up your original solution first.
  2. .sln files are not XML. They are text files with a pretty straightforward syntax, though. So, either you'll have to write your own parser, or consider using Visual Studio's DTE automation object to manipulate the contents of the solution file as shown in this thread: http://stackoverflow.com/questions/1891035/easy-way-to-add-multiple-existing-csproj-to-a-visual-studio-solution. The sample is written in PowerShell but the approach would be the same in, say, VBScript.
DmytroL
Good point @DmytroL I had meant the *proj files were XML, the SLN files are VS's super special take on INI files.
GrayWizardx
+1 for recommending the DTE, it sucks but it can make some of this stuff much easier if you do it regularly.
GrayWizardx