delphitoolsapi

How to get the main source file name from an IOTAProject?


Since at least D2007 a project file can have a main source file with differing base name. The DevExpress demos make use of this: E.g. there is a single dpr file UnboundListDemo.dpr which serves as the main source for both UnboundListDemoD11.dproj and UnboundListDemoD12.dproj.

Now if I have a Project: IOTAProject then Project.FileName returns the dproj file name. I couldn't find an "official" way to get the dpr's file name. Is there any? One can get it from parsing the dproj file (see here) but I'd prefer a ToolsAPI method.


Edit: I came up with this code based on Jon's answer:

function IsProjectSource(const FileName: string): Boolean;
begin
  Result := IsDpr(FileName) or IsBpr(FileName) or IsPackage(FileName);
end;

function GxOtaGetProjectFileName2(Project: IOTAProject; NormalizeBdsProj: Boolean = False): string;
var
  i: Integer;
  Module: IOTAModule;
  Editor: IOTAEditor;
begin
  Result := '';
  if Assigned(Project) then begin
    Result := Project.FileName;
    if NormalizeBdsProj and IsBdsprojOrDproj(Result) then begin
      Module := Project as IOTAModule;
      for i := 0 to Module.ModuleFileCount - 1 do
      begin
        Editor := Module.ModuleFileEditors[i];
        if IsProjectSource(Editor.FileName) then begin
          Result := Editor.FileName;
          Exit;
        end;
      end;
    end;
  end;
end;

where the Is... routines are from GX_GenericUtils.


Edit 2: How to create one of these situations:

  1. Create new VCL application.
  2. Save as MyProject.dproj.
  3. Close project in IDE.
  4. In Windows explorer, rename MyProject.dproj to MyProjectD11.dproj.
  5. From now on be sure to open MyProjectD11.dproj, not MyProject.dpr!

Solution

  • If you iterate the editors on the IOTAProject instance, you'll probably find the dpr.

    var
      Module: IOTAModule;
      Project: IOTAProject;
      Editor: IOTAEditor;
    begin
      // Set Project Here....
      Module := Project as IOTAModule;
      for I := 0 to Module.ModuleFileCount - 1 do
      begin
        Editor := Module.ModuleFileEditors[I];
    
      end;
    end;