6

每隔几周,我就会遇到这个问题:在 Delphi 项目中对使用单元执行 IDE 操作时,它会破坏.dpr文件。

发生的情况是它重建了uses列表,但位置错误。

我想知道要避免什么使用模式,所以我不会再遇到这个错误。

我在许多 Delphi 版本中都出现过这个错误。我知道它至少存在于 Delphi XE2(今天又发生了)、XE、2007、2006 和 7 中。

损坏的片段通常是这样的结构:

ususes
  Forms,
  ..
  LastUnitInUses in 'LastUnitInUses.pas';

R *.RES}

并且应该通过删除一个us并添加一个来纠正{$

uses
  Forms,
  ..
  LastUnitInUses in 'LastUnitInUses.pas';

{R *.RES}

出错的示例文件:

program SysUtilsFormatTests;
{

  Delphi DUnit Test Project
  -------------------------
  This project contains the DUnit test framework and the GUI/Console test runners.
  Add "CONSOLE_TESTRUNNER" to the conditional defines entry in the project options
  to use the console test runner.  Otherwise the GUI test runner will be used by
  default.

}

{$IFDEF CONSOLE_TESTRUNNER}
{$APPTYPE CONSOLE}
{$ENDIF}

ususes
  Forms,
  TestFramework,
  GUITestRunner,
  TextTestRunner,
  SysUtilsFormatUnit in 'SysUtilsFormatUnit.pas';

R *.RES}

begin
  Application.Initialize;
  if IsConsole then
    with TextTestRunner.RunRegisteredTests do
      Free
  else
    GUITestRunner.RunRegisteredTests;
end.

更正.dpr文件的示例:

program SysUtilsFormatTests;
{

  Delphi DUnit Test Project
  -------------------------
  This project contains the DUnit test framework and the GUI/Console test runners.
  Add "CONSOLE_TESTRUNNER" to the conditional defines entry in the project options
  to use the console test runner.  Otherwise the GUI test runner will be used by
  default.

}

{$IFDEF CONSOLE_TESTRUNNER}
{$APPTYPE CONSOLE}
{$ENDIF}

uses
  Forms,
  TestFramework,
  GUITestRunner,
  TextTestRunner,
  SysUtilsFormatUnit in 'SysUtilsFormatUnit.pas';

{$R *.RES}

begin
  Application.Initialize;
  if IsConsole then
    with TextTestRunner.RunRegisteredTests do
      Free
  else
    GUITestRunner.RunRegisteredTests;
end.
4

1 回答 1

4

我知道唯一可行的方法是让 IDE 管理 .dpr 文件。

  • 不要添加评论。
  • 不要使用 $IFDEF 之类的条件语句。
  • 不要修改 .dpr 文件中的代码。

如果您执行任何这些操作,预计 IDE 会反击。

就我个人而言,我会做所有这些并在提交时进行反击。我使用我的 VCS 来防御虚假的 IDE 更改。这并不理想,但它是最好的选择。

于 2013-05-27T14:38:51.637 回答