1

I'd like to specify an environment variable for use in the source path (library path) at a project level.

We often have a couple of versions checked out of our SVN repository at the same time in different directories, and I'd like to specify the repository root for a project in relative terms at a project level. I could then use that path in a project's source path and I wouldn't have to include indecipherable dot dot slashes (..\) in paths.

For example, say I have checked out trunk to c:\projects\trunk. Then underneath there I have a project in <repositoryroot>\Foo\Bar\ under trunk which uses the Delphi Spring framework under <repositoryroot>\components\external\Spring4d. I end up with a whole bunch of directories in the search path with ..\..\External\Spring4D\Source at the beginning. For example ..\..\External\Spring4D\Source\Base\Collections. I would like to be able to be able to use ${Spring4D} instead, producing ${Spring4D}\Base\Collections\, which is much less wordy and it means that if you move a project or component you can change one value and it updates all paths.

I know that you can do this on a Delphi level by specifying paths in Delphi's environment variables, but I would like to achieve the same thing on a project level or repository level.

Does anyone have any ideas on how to achieve this? Are there any settings or even add-ins that would allow this sort of functionality?

Nat
  • 5,414
  • 26
  • 38
  • 1
    I have my global libraries in their own repos, so I can specify them as ``svn:externals`` in the repo properties, e.g. ``https://svn.domain.com/repos/Spring4D/trunk/ Foo/Bar/Spring4D`` (properties in this example for the ``trunk`` folder!). On ``Checkout``/``Update`` this puts the trunk of the Spring4D repo to the subfolder Foo/Bar/Spring4D of the current repo. So all you have to do is to add ``Spring4D`` to the project search path. – Delphi Coder Mar 09 '20 at 11:11
  • There are [predefined variables like $(ProjectDir)](https://stackoverflow.com/questions/5533718/build-event-macros-in-delphi-xe#5533901), but I've never tried the project specific ones in the library paths... or relative paths in general. – GolezTrol Mar 09 '20 at 12:05
  • A folder above the folder of the current project is just a folder like any other folder. Any solution to this question must therefore be valid for arbitrary folders, too. – Uwe Raabe Mar 09 '20 at 16:58
  • @GolezTrol $(ProjectDir) is the directory that all the paths are relative to anyway, so sadly it doesn't gain us anything. – Nat Mar 09 '20 at 21:58

2 Answers2

3

You can manually edit your project file (.dproj) and add a variable there:

<PropertyGroup>
  <MyVariableName>MyVariableContent</MyVariableName>
</PropertyGroup>

Later on, you can refer to the content of that variable:

<DCC_UnitSearchPath>C:\Components;$(MyVariableName)</DCC_UnitSearchPath>

You can also define a new environment variable (SystemPropertiesAdvanced.exe -> Environment variables -> Add) and then refer to that variable using the same syntax, e.g.:

<DCC_UnitSearchPath>C:\Components;$(PATH)</DCC_UnitSearchPath>

(Note that it is a very bad idea to use PATH here, it's only an example of a variable which will exist in your environment.)

pepak
  • 712
  • 4
  • 13
  • That does help a little bit except that it's fragile... Something might reset the project file (I'm sure Delphi itself might do that!) and the developer wouldn't know why. Also, there is no nice user interface to add the variables. Would you know if it is possible to modify the project file in arbitrary places with a plug-in? – Nat Mar 27 '20 at 21:20
  • Never noticed any fragility myself. The added variables have remained in my project file unchanged for years. – pepak Mar 28 '20 at 04:29
  • OK. I suppose my remaining reluctance is the requirement for the developer to hand edit the file for new projects. – Nat Mar 29 '20 at 01:46
1

You could also employ some cmd script magic to to create environment variables that point to those subdirectories and at the end call the IDE, so these environment variables are available in the IDE in the same way as global environment variables would be (see pepak's answer for that).

Pointers:

  • %0 is the name of the current cmd file
  • use tilde for file name parts
dummzeuch
  • 10,975
  • 4
  • 51
  • 158
  • Hmmm... That has some merit. The only problem I can see with that is that it depends on external conditions for the compilation to work in the IDE... But that does give me an idea... A plugin could modify the environment variables that Delphi sees. I wonder if Delphi evaluates the environment block each time, or if it loads it once at startup. I might write a plugin to test that and report back. – Nat Mar 27 '20 at 21:26
  • The environment variables have to be visible to the build engine. This is accomplished by a file named _environment.proj_ located in _%APPDATA%\Embarcadero\BDS\19.0_ (for Tokyo), which AFAIK is refreshed with each IDE start. – Uwe Raabe Mar 28 '20 at 08:50
  • @UweRaabe That's very useful information! I'll have a play with it today. Thanks! – Nat Mar 29 '20 at 01:44