Search code examples
delphiidewizardopentools

Delphi OpenTools API - Editing the project requires clause


I have written an OpenTools wizard for creating a skeleton for a custom project type. It does work, and the project and units are properly created. But, how do I edit the .dpk or .dpk file's requires clause?

A call to ModuleServices.CreateModule(MyIOTAProjectCreatorInterface) gives me the .dproj file only.


Solution

  • In my VCL Component Installer (since XE, this is part of the Delphi IDE), I do it this way:

    procedure TCompInstallWizard.AddReferenceFiles(InstallProject: IOTAProject;
      const FileNames: array of string);
    var
      ReferenceFile: string;
    begin
      WriteDebugMessage('AddReferenceFiles');
      for ReferenceFile in FileNames do
        if not ContainsFile(InstallProject, ReferenceFile) then
          InstallProject.AddFile(ReferenceFile, False);
    end;
    

    with the help of function IOTAProject.AddFile(FileName, IsUnitOrForm). Note that I call it like this:

    if FPersonality = ppCppBuilder then
      AddReferenceFiles(InstallProject,
        ['rtl.bpi', 'designide.bpi', 'vcl.bpi', 'vclactnband.bpi',
         'vclx.bpi', 'xmlrtl.bpi'])
    else
      AddReferenceFiles(InstallProject,
        ['rtl.dcp', 'designide.dcp', 'vcl.dcp', 'vclactnband.dcp',
         'vclx.dcp', 'xmlrtl.dcp']);
    

    Note that the docs say:

    { Call this function to add an arbitrary file to the project.  NOTE: some
      files have special meaning to different projects.  For example: adding
      VCL60.DCP will cause a new entry in a package project's "requires" list
      while it will be a raw file to any other project type.  Set IsUnitOrForm
      to true for files that are considered items that the project would
      process directly or indirectly (ie. .pas, .cpp, .rc, etc..) or can be
      opened in the code editor. For all others, including binary files
      (.res, .bpi, .dcp, etc..) set this to False. }
    procedure AddFile(const AFileName: string; IsUnitOrForm: Boolean);
    

    This means that if you add a 'bla.dcp' it will automatically land in the requires section, and if you add a 'bla.pas' file, it will land in the contains section. It took me a while to find out.